|
1 | | -import path from "path"; |
2 | | -import webpack from "webpack"; |
3 | | -import HtmlWebpackPlugin from "html-webpack-plugin"; |
4 | | -import ForkTsCheckerWebpackPlugin from "fork-ts-checker-webpack-plugin"; |
5 | | -import ESLintPlugin from "eslint-webpack-plugin"; |
| 1 | +import * as path from 'path'; |
| 2 | +import HtmlWebpackPlugin from 'html-webpack-plugin'; |
| 3 | +import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin'; |
| 4 | +import ESLintPlugin from 'eslint-webpack-plugin'; |
| 5 | +import { Configuration, HotModuleReplacementPlugin } from 'webpack'; |
6 | 6 |
|
7 | | -interface WebpackConfiguration extends webpack.Configuration { |
8 | | - devServer: { |
9 | | - contentBase: string, |
10 | | - historyApiFallback: boolean, |
11 | | - port: number, |
12 | | - open: boolean, |
13 | | - hot: boolean, |
14 | | - } |
15 | | -} |
| 7 | +const isProd = process.env.NODE_ENV === "production"; |
16 | 8 |
|
17 | | -const config: WebpackConfiguration = { |
18 | | - mode: "development", |
| 9 | +const config: Configuration = { |
| 10 | + mode: isProd ? 'production' : 'development', |
19 | 11 | output: { |
20 | | - publicPath: "/", |
| 12 | + path: path.resolve(__dirname, 'build'), |
| 13 | + filename: "[name].[contenthash].js", |
21 | 14 | }, |
22 | | - entry: "./src/index.tsx", |
| 15 | + entry: './src/index.tsx', |
23 | 16 | module: { |
24 | 17 | rules: [ |
25 | 18 | { |
26 | 19 | test: /\.(ts|js)x?$/i, |
27 | 20 | exclude: /node_modules/, |
28 | 21 | use: { |
29 | | - loader: "babel-loader", |
| 22 | + loader: 'babel-loader', |
30 | 23 | options: { |
31 | 24 | presets: [ |
32 | | - "@babel/preset-env", |
33 | | - "@babel/preset-react", |
34 | | - "@babel/preset-typescript", |
| 25 | + '@babel/preset-env', |
| 26 | + '@babel/preset-react', |
| 27 | + '@babel/preset-typescript', |
35 | 28 | ], |
36 | 29 | }, |
37 | 30 | }, |
38 | 31 | }, |
| 32 | + { |
| 33 | + test: /\.css$/i, |
| 34 | + use: ['style-loader', 'css-loader'], |
| 35 | + }, |
| 36 | + { |
| 37 | + test: /\.(?:ico|gif|png|jpg|jpeg)$/i, |
| 38 | + type: 'asset/resource', |
| 39 | + }, |
| 40 | + { |
| 41 | + test: /\.(woff(2)?|eot|ttf|otf|svg|)$/, |
| 42 | + type: 'asset/inline', |
| 43 | + }, |
39 | 44 | ], |
40 | 45 | }, |
41 | 46 | resolve: { |
42 | | - extensions: [".tsx", ".ts", ".js"], |
| 47 | + extensions: ['.tsx', '.ts', '.js'], |
43 | 48 | }, |
44 | 49 | plugins: [ |
45 | 50 | new HtmlWebpackPlugin({ |
46 | | - template: "src/index.html", |
| 51 | + template: 'src/index.html', |
47 | 52 | }), |
48 | | - new webpack.HotModuleReplacementPlugin(), |
| 53 | + new HotModuleReplacementPlugin(), |
49 | 54 | new ForkTsCheckerWebpackPlugin({ |
50 | 55 | async: false, |
51 | 56 | }), |
52 | 57 | new ESLintPlugin({ |
53 | | - extensions: ["js", "jsx", "ts", "tsx"], |
| 58 | + extensions: ['js', 'jsx', 'ts', 'tsx'], |
54 | 59 | }), |
55 | 60 | ], |
56 | | - devtool: "inline-source-map", |
| 61 | + devtool: 'inline-source-map', |
57 | 62 | devServer: { |
58 | | - contentBase: path.join(__dirname, "build"), |
| 63 | + contentBase: path.join(__dirname, 'build'), |
59 | 64 | historyApiFallback: true, |
60 | 65 | port: 4000, |
61 | 66 | open: true, |
62 | 67 | hot: true, |
| 68 | + compress: true, |
| 69 | + watchContentBase: true, |
63 | 70 | }, |
64 | 71 | }; |
65 | 72 |
|
| 73 | +if (isProd) { |
| 74 | + delete config.devtool; |
| 75 | + delete config.devServer; |
| 76 | +} |
| 77 | + |
66 | 78 | export default config; |
0 commit comments