|
1 | 1 | const { resolve } = require('path')
|
2 | 2 | const HtmlWebpackPlugin = require('html-webpack-plugin')
|
| 3 | +const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer') |
3 | 4 | const { VueLoaderPlugin } = require('vue-loader')
|
4 | 5 | const webpack = require('webpack')
|
5 | 6 |
|
6 | 7 | const outputPath = resolve(__dirname, 'playground_dist')
|
7 | 8 |
|
8 | 9 | /** @type {import('webpack').ConfigurationFactory} */
|
9 |
| -const config = (env = {}) => ({ |
10 |
| - mode: env.prod ? 'production' : 'development', |
11 |
| - devtool: env.prod ? 'source-map' : 'inline-source-map', |
| 10 | +const config = (env = {}) => { |
| 11 | + const extraPlugins = env.prod ? [new BundleAnalyzerPlugin()] : [] |
12 | 12 |
|
13 |
| - devServer: { |
14 |
| - contentBase: outputPath, |
15 |
| - historyApiFallback: true, |
16 |
| - hot: true, |
17 |
| - stats: 'minimal', |
18 |
| - }, |
| 13 | + return { |
| 14 | + mode: env.prod ? 'production' : 'development', |
| 15 | + devtool: env.prod ? 'source-map' : 'inline-source-map', |
19 | 16 |
|
20 |
| - output: { |
21 |
| - path: outputPath, |
22 |
| - publicPath: '/', |
23 |
| - filename: 'bundle.js', |
24 |
| - }, |
| 17 | + devServer: { |
| 18 | + contentBase: outputPath, |
| 19 | + historyApiFallback: true, |
| 20 | + hot: true, |
| 21 | + stats: 'minimal', |
| 22 | + }, |
25 | 23 |
|
26 |
| - entry: [resolve(__dirname, 'playground/main.ts')], |
27 |
| - module: { |
28 |
| - rules: [ |
29 |
| - { |
30 |
| - test: /\.ts$/, |
31 |
| - use: 'ts-loader', |
32 |
| - }, |
33 |
| - { |
34 |
| - test: /\.vue$/, |
35 |
| - use: 'vue-loader', |
36 |
| - }, |
37 |
| - ], |
38 |
| - }, |
39 |
| - resolve: { |
40 |
| - alias: { |
41 |
| - // this isn't technically needed, since the default `vue` entry for bundlers |
42 |
| - // is a simple `export * from '@vue/runtime-dom`. However having this |
43 |
| - // extra re-export somehow causes webpack to always invalidate the module |
44 |
| - // on the first HMR update and causes the page to reload. |
45 |
| - vue: '@vue/runtime-dom', |
| 24 | + output: { |
| 25 | + path: outputPath, |
| 26 | + publicPath: '/', |
| 27 | + filename: 'bundle.js', |
46 | 28 | },
|
47 |
| - // Add `.ts` and `.tsx` as a resolvable extension. |
48 |
| - extensions: ['.ts', 'd.ts', '.tsx', '.js', '.vue'], |
49 |
| - }, |
50 |
| - plugins: [ |
51 |
| - new VueLoaderPlugin(), |
52 |
| - new HtmlWebpackPlugin({ |
53 |
| - template: resolve(__dirname, 'playground/index.html'), |
54 |
| - }), |
55 |
| - new webpack.DefinePlugin({ |
56 |
| - __DEV__: JSON.stringify(!env.prod), |
57 |
| - __BROWSER__: 'true', |
58 |
| - 'process.env': { |
59 |
| - NODE_ENV: JSON.stringify(process.env.NODE_ENV), |
| 29 | + |
| 30 | + entry: [resolve(__dirname, 'playground/main.ts')], |
| 31 | + module: { |
| 32 | + rules: [ |
| 33 | + { |
| 34 | + test: /\.ts$/, |
| 35 | + use: 'ts-loader', |
| 36 | + }, |
| 37 | + { |
| 38 | + test: /\.vue$/, |
| 39 | + use: 'vue-loader', |
| 40 | + }, |
| 41 | + ], |
| 42 | + }, |
| 43 | + resolve: { |
| 44 | + alias: { |
| 45 | + // this isn't technically needed, since the default `vue` entry for bundlers |
| 46 | + // is a simple `export * from '@vue/runtime-dom`. However having this |
| 47 | + // extra re-export somehow causes webpack to always invalidate the module |
| 48 | + // on the first HMR update and causes the page to reload. |
| 49 | + vue: '@vue/runtime-dom', |
60 | 50 | },
|
61 |
| - }), |
62 |
| - ], |
63 |
| -}) |
| 51 | + // Add `.ts` and `.tsx` as a resolvable extension. |
| 52 | + extensions: ['.ts', 'd.ts', '.tsx', '.js', '.vue'], |
| 53 | + }, |
| 54 | + plugins: [ |
| 55 | + new VueLoaderPlugin(), |
| 56 | + new HtmlWebpackPlugin({ |
| 57 | + template: resolve(__dirname, 'playground/index.html'), |
| 58 | + }), |
| 59 | + new webpack.DefinePlugin({ |
| 60 | + __DEV__: JSON.stringify(!env.prod), |
| 61 | + __BROWSER__: 'true', |
| 62 | + 'process.env': { |
| 63 | + NODE_ENV: JSON.stringify(process.env.NODE_ENV), |
| 64 | + }, |
| 65 | + }), |
| 66 | + ...extraPlugins, |
| 67 | + ], |
| 68 | + } |
| 69 | +} |
64 | 70 |
|
65 | 71 | module.exports = config
|
0 commit comments