From 325339ef8b8a0d408a9108ee42a83cfe9744b21b Mon Sep 17 00:00:00 2001 From: rayoungpark Date: Wed, 21 Sep 2022 18:18:02 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20webpack=20+=20typescript=20?= =?UTF-8?q?=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tsconfig.json | 28 +++++++++++++++++----------- webpack.config.js | 46 +++++++++++++++++++++++++++++++++++++--------- 2 files changed, 54 insertions(+), 20 deletions(-) diff --git a/tsconfig.json b/tsconfig.json index cf64d4e..5d2e01a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,24 +1,30 @@ { - "include": ["src"], + "include": ["src", "index.ts"], "exclude": ["node_modules"], "compilerOptions": { - "target": "ES2017", - "lib": ["DOM"], - "module": "ES6" /* Specify what module code is generated. */, - "outDir": "build" /* Specify an output folder for all emitted files. */, + "target": "ES2016", + "lib": ["ES2020", "DOM"], + "module": "ESNext" /* Specify what module code is generated. */, + // "outDir": "build" /* Specify an output folder for all emitted files. */, "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */, "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */, "strict": true /* Enable all strict type-checking options. */, "skipLibCheck": true /* Skip type checking all .d.ts files. */, - "baseUrl": ".", + "moduleResolution": "Node", + "baseUrl": "./src", "paths": { - "@type/*": ["./src/types/*"], - "@components/*": ["./src/components/*"], - "@api/*": ["./src/api/*"], - "@util/*": ["./src/util/*"] + "@type/*": ["types/*"], + "@components/*": ["components/*"], + "@api/*": ["api/*"], + "@util/*": ["util/*"] } }, "ts-node": { - "esm": true + "compilerOptions": { + "module": "CommonJS", + "moduleResolution": "Node", + "target": "ES5", + "esModuleInterop": true + } } } diff --git a/webpack.config.js b/webpack.config.js index d01e8e9..8b9c09a 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,33 +1,61 @@ const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); -const MiniCssExtractPlugin = require('mini-css-extract-plugin'); +const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin'); +require('webpack-dev-server'); +// const MiniCssExtractPlugin = require('mini-css-extract-plugin'); +const isDevelopment = process.env.NODE_ENV !== 'production'; module.exports = { - entry: './build/index.js', + name: 'Coffee Shop', + mode: isDevelopment ? 'development' : 'production', + devtool: isDevelopment ? 'hidden-source-map' : 'eval', + entry: './index', output: { - filename: 'bundle.js', + filename: '[name].js', path: path.resolve(__dirname, 'dist'), clean: true, + publicPath: '/dist/', }, plugins: [ new HtmlWebpackPlugin({ template: './index.html' }), - new MiniCssExtractPlugin({ linkType: false, filename: '[name].css', chunkFilename: '[id].css' }), + new ForkTsCheckerWebpackPlugin({ + async: false, + }), ], resolve: { + extensions: ['.js', '.jsx', '.ts', '.tsx', '.json'], alias: { - '@type': path.resolve(__dirname, 'build/types'), - '@components': path.resolve(__dirname, 'build/components'), - '@api': path.resolve(__dirname, 'build/api'), - '@util': path.resolve(__dirname, 'build/util'), + '@type': path.resolve(__dirname, 'src', 'types'), + '@components': path.resolve(__dirname, 'src', 'components'), + '@api': path.resolve(__dirname, 'src', 'api'), + '@util': path.resolve(__dirname, 'src', 'util'), }, }, module: { rules: [ + { + test: /\.ts?$/, + loader: 'babel-loader', + options: { + presets: [ + ['@babel/preset-env', { targets: { browsers: ['last 2 chrome versions', 'IE 10'] }, debug: isDevelopment }], + '@babel/preset-typescript', + ], + }, + }, { test: /\.(sa|sc|c)ss$/i, exclude: /node_modules/, - use: [MiniCssExtractPlugin.loader, 'css-loader'], + use: ['style-loader', 'css-loader'], }, ], }, + devServer: { + historyApiFallback: true, + port: 5000, + devMiddleware: { + publicPath: '/dist/', + }, + static: { directory: path.resolve(__dirname) }, + }, };