|
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: ['node_modules', 'src'], |
| 29 | + extensions: ['.ts', '.tsx', '.js', '.json'] |
| 30 | + }, |
| 31 | + module: { |
| 32 | + rules: [ |
| 33 | + { |
| 34 | + test: /\.(ts|js)x?$/, |
| 35 | + loader: require.resolve('babel-loader'), |
| 36 | + exclude: /node_modules/, |
| 37 | + options: { |
| 38 | + babelrc: false, |
| 39 | + configFile: false, |
| 40 | + compact: isEnvProduction, |
| 41 | + sourceMaps: false, |
| 42 | + inputSourceMap: false, |
| 43 | + presets: [require.resolve('babel-preset-react-app')] |
31 | 44 | }
|
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 |
| - } |
| 45 | + }, |
| 46 | + { |
| 47 | + test: /\.(gif|png|jpe?g|svg)$/i, |
| 48 | + use: ['file-loader', { loader: 'image-webpack-loader' }] |
| 49 | + }, |
| 50 | + { |
| 51 | + test: /\.scss$/, |
| 52 | + use: ['style-loader', 'css-loader', 'sass-loader'] |
| 53 | + }, |
| 54 | + { |
| 55 | + test: /\.css$/, |
| 56 | + use: ['style-loader', 'css-loader'] |
| 57 | + } |
| 58 | + ] |
| 59 | + }, |
| 60 | + optimization: { minimize: isEnvProduction }, |
| 61 | + node: { |
| 62 | + module: 'empty', |
| 63 | + dgram: 'empty', |
| 64 | + dns: 'mock', |
| 65 | + fs: 'empty', |
| 66 | + http2: 'empty', |
| 67 | + net: 'empty', |
| 68 | + tls: 'empty', |
| 69 | + child_process: 'empty' |
| 70 | + }, |
| 71 | + plugins: [ |
| 72 | + new HtmlWebpackPlugin( |
| 73 | + Object.assign( |
| 74 | + {}, |
| 75 | + { inject: true, template: `${APP_PATH}/index.html` }, |
| 76 | + isEnvProduction |
| 77 | + ? { |
| 78 | + minify: { |
| 79 | + removeComments: true, |
| 80 | + collapseWhitespace: true, |
| 81 | + removeRedundantAttributes: true, |
| 82 | + useShortDoctype: true, |
| 83 | + removeEmptyAttributes: true, |
| 84 | + removeStyleLinkTypeAttributes: true, |
| 85 | + keepClosingSlash: true, |
| 86 | + minifyJS: true, |
| 87 | + minifyCSS: true, |
| 88 | + minifyURLs: true |
| 89 | + } |
| 90 | + } |
| 91 | + : undefined |
| 92 | + ) |
| 93 | + ) |
42 | 94 | ]
|
43 |
| - }, |
44 |
| - |
45 |
| - plugins: [ |
46 |
| - new HtmlWebpackPlugin({ |
47 |
| - inject: true, |
48 |
| - template: `${APP_PATH}/index.html` |
49 |
| - }) |
50 |
| - ], |
51 |
| - |
52 |
| - optimization: { minimize: false } |
| 95 | + }; |
53 | 96 | };
|
54 | 97 |
|
55 |
| -module.exports = config; |
| 98 | +module.exports = configWebpack; |
0 commit comments