|
1 | | -"use strict"; |
| 1 | +'use strict'; |
2 | 2 |
|
3 | | -const HtmlWebpackPlugin = require("html-webpack-plugin"); |
4 | | -const resolverPath = require("../utils/resolverPath"); |
| 3 | +const HtmlWebpackPlugin = require('html-webpack-plugin'); |
| 4 | +const resolverPath = require('../utils/resolverPath'); |
5 | 5 |
|
6 | | -const APP_PATH = resolverPath("src/dev"); |
| 6 | +const APP_PATH = resolverPath('src/dev'); |
| 7 | +const BUILD_PATH = resolverPath('build'); |
7 | 8 |
|
8 | | -const config = { |
9 | | - entry: APP_PATH, |
10 | | - mode: "development", |
11 | | - resolve: { |
12 | | - modules: ["node_modules", "src/lib"], |
13 | | - extensions: [".ts", ".tsx", ".js", ".json"] |
14 | | - }, |
15 | | - module: { |
16 | | - rules: [ |
17 | | - { |
18 | | - test: /\.(ts|js)x?$/, |
19 | | - loader: "babel-loader", |
20 | | - exclude: /node_modules/, |
21 | | - options: { |
22 | | - presets: [require.resolve("babel-preset-react-app")] |
23 | | - } |
24 | | - }, |
25 | | - { |
26 | | - test: /\.(gif|png|jpe?g|svg)$/i, |
27 | | - use: [ |
28 | | - "file-loader", |
29 | | - { |
30 | | - loader: "image-webpack-loader" |
| 9 | +const configWebpack = ({ mode }) => { |
| 10 | + const isEnvProduction = mode === 'production'; |
| 11 | + |
| 12 | + return { |
| 13 | + entry: APP_PATH, |
| 14 | + output: { |
| 15 | + path: BUILD_PATH, |
| 16 | + pathinfo: !isEnvProduction, |
| 17 | + filename: isEnvProduction |
| 18 | + ? 'static/js/[name].[contenthash:8].js' |
| 19 | + : 'bundle.js', |
| 20 | + chunkFilename: isEnvProduction |
| 21 | + ? 'static/js/[name].[contenthash:8].chunk.js' |
| 22 | + : 'static/js/[name].chunk.js', |
| 23 | + globalObject: 'this' |
| 24 | + }, |
| 25 | + mode, |
| 26 | + bail: isEnvProduction, |
| 27 | + resolve: { |
| 28 | + modules: [ |
| 29 | + 'node_modules', |
| 30 | + resolverPath('node_modules'), |
| 31 | + resolverPath('src') |
| 32 | + ], |
| 33 | + extensions: ['.ts', '.tsx', '.js', '.json'] |
| 34 | + }, |
| 35 | + module: { |
| 36 | + rules: [ |
| 37 | + { |
| 38 | + test: /\.(ts|js)x?$/, |
| 39 | + loader: require.resolve('babel-loader'), |
| 40 | + exclude: /node_modules/, |
| 41 | + options: { |
| 42 | + babelrc: false, |
| 43 | + configFile: false, |
| 44 | + compact: isEnvProduction, |
| 45 | + sourceMaps: false, |
| 46 | + inputSourceMap: false, |
| 47 | + presets: [require.resolve('babel-preset-react-app')] |
31 | 48 | } |
32 | | - ] |
33 | | - }, |
34 | | - { |
35 | | - test: /\.scss$/, |
36 | | - use: ["style-loader", "css-loader", "sass-loader"] |
37 | | - }, |
38 | | - { |
39 | | - test: /\.css$/, |
40 | | - use: ["style-loader", "css-loader"] |
41 | | - } |
| 49 | + }, |
| 50 | + { |
| 51 | + test: /\.(gif|png|jpe?g|svg)$/i, |
| 52 | + use: ['file-loader', { loader: 'image-webpack-loader' }] |
| 53 | + }, |
| 54 | + { |
| 55 | + test: /\.scss$/, |
| 56 | + use: ['style-loader', 'css-loader', 'sass-loader'] |
| 57 | + }, |
| 58 | + { |
| 59 | + test: /\.css$/, |
| 60 | + use: ['style-loader', 'css-loader'] |
| 61 | + } |
| 62 | + ] |
| 63 | + }, |
| 64 | + optimization: { minimize: isEnvProduction }, |
| 65 | + node: { |
| 66 | + module: 'empty', |
| 67 | + dgram: 'empty', |
| 68 | + dns: 'mock', |
| 69 | + fs: 'empty', |
| 70 | + http2: 'empty', |
| 71 | + net: 'empty', |
| 72 | + tls: 'empty', |
| 73 | + child_process: 'empty' |
| 74 | + }, |
| 75 | + plugins: [ |
| 76 | + new HtmlWebpackPlugin( |
| 77 | + Object.assign( |
| 78 | + {}, |
| 79 | + { inject: true, template: `${APP_PATH}/index.html` }, |
| 80 | + isEnvProduction |
| 81 | + ? { |
| 82 | + minify: { |
| 83 | + removeComments: true, |
| 84 | + collapseWhitespace: true, |
| 85 | + removeRedundantAttributes: true, |
| 86 | + useShortDoctype: true, |
| 87 | + removeEmptyAttributes: true, |
| 88 | + removeStyleLinkTypeAttributes: true, |
| 89 | + keepClosingSlash: true, |
| 90 | + minifyJS: true, |
| 91 | + minifyCSS: true, |
| 92 | + minifyURLs: true |
| 93 | + } |
| 94 | + } |
| 95 | + : undefined |
| 96 | + ) |
| 97 | + ) |
42 | 98 | ] |
43 | | - }, |
44 | | - |
45 | | - plugins: [ |
46 | | - new HtmlWebpackPlugin({ |
47 | | - inject: true, |
48 | | - template: `${APP_PATH}/index.html` |
49 | | - }) |
50 | | - ], |
51 | | - |
52 | | - optimization: { minimize: false } |
| 99 | + }; |
53 | 100 | }; |
54 | 101 |
|
55 | | -module.exports = config; |
| 102 | +module.exports = configWebpack; |
0 commit comments