|
1 | 1 | const path = require('path') |
2 | 2 | const packageJson = require('./package.json') |
| 3 | +const webpackMerge = require('webpack-merge') |
3 | 4 | const UglifyJsPlugin = require('uglifyjs-webpack-plugin') |
| 5 | +const Visualizer = require('webpack-visualizer-plugin') |
4 | 6 |
|
5 | | -module.exports = function (env = {}) { |
6 | | - const config = { |
7 | | - mode: env.production ? 'production' : 'development', |
8 | | - devtool: 'source-map', |
9 | | - |
10 | | - entry: { |
11 | | - 'zaf_sdk': [ |
12 | | - 'native-promise-only', |
13 | | - './lib/index.js' |
14 | | - ], |
15 | | - 'zaf_sdk.min': [ |
16 | | - 'native-promise-only', |
17 | | - './lib/index.js' |
18 | | - ] |
19 | | - }, |
| 7 | +const commonConfig = { |
| 8 | + devtool: 'source-map', |
20 | 9 |
|
21 | | - module: { |
22 | | - rules: [] |
23 | | - }, |
| 10 | + entry: { |
| 11 | + 'zaf_sdk': [ |
| 12 | + 'native-promise-only', |
| 13 | + './lib/index.js' |
| 14 | + ], |
| 15 | + 'zaf_sdk.min': [ |
| 16 | + 'native-promise-only', |
| 17 | + './lib/index.js' |
| 18 | + ] |
| 19 | + }, |
24 | 20 |
|
25 | | - plugins: [], |
| 21 | + output: { |
| 22 | + library: 'ZAFClient', |
| 23 | + filename: '[name].js', |
| 24 | + path: path.resolve(__dirname, 'build') |
| 25 | + }, |
26 | 26 |
|
27 | | - output: { |
28 | | - library: 'ZAFClient', |
29 | | - filename: '[name].js', |
30 | | - path: path.resolve(__dirname, 'build') |
31 | | - }, |
| 27 | + externals: { |
| 28 | + version: `"${packageJson.version}"` |
| 29 | + }, |
32 | 30 |
|
33 | | - externals: { |
34 | | - version: `"${packageJson.version}"` |
35 | | - }, |
36 | | - |
37 | | - // Note: devServer does not serve from build/, but from cache. It also doesn't respect mode |
38 | | - // so outputed files are very different from server/build/build:dev |
39 | | - devServer: { |
40 | | - contentBase: path.join(__dirname, 'build'), |
41 | | - compress: true, |
42 | | - port: 9001 |
43 | | - } |
| 31 | + // Note: devServer does not serve from build/, but from cache. It also doesn't respect mode |
| 32 | + // so outputed files are very different from server/build/build:dev |
| 33 | + devServer: { |
| 34 | + contentBase: path.join(__dirname, 'build'), |
| 35 | + compress: true, |
| 36 | + port: 9001 |
44 | 37 | } |
| 38 | +} |
45 | 39 |
|
46 | | - // For everything execpt tests we add optimization and babel |
47 | | - if (!env.test) { |
48 | | - config.optimization = { |
49 | | - minimize: true, |
50 | | - minimizer: [new UglifyJsPlugin({ |
| 40 | +// For everything execpt tests we add optimization and babel |
| 41 | +const nonTestConfig = { |
| 42 | + optimization: { |
| 43 | + minimize: true, |
| 44 | + minimizer: [ |
| 45 | + new UglifyJsPlugin({ |
51 | 46 | include: /\.min\.js$/, |
52 | 47 | sourceMap: true |
53 | | - })] |
54 | | - } |
| 48 | + }) |
| 49 | + ] |
| 50 | + }, |
| 51 | + |
| 52 | + module: { |
| 53 | + rules: [ |
| 54 | + { |
| 55 | + test: /\.js$/, |
| 56 | + use: { loader: 'babel-loader', options: { plugins: [], presets: ['babel-preset-env'] } } |
| 57 | + } |
| 58 | + ] |
| 59 | + } |
| 60 | +} |
55 | 61 |
|
56 | | - config.module.rules.push({ |
57 | | - test: /\.js$/, |
58 | | - use: { loader: 'babel-loader', options: { plugins: [], presets: ['babel-preset-env'] } } |
| 62 | +const statsConfig = { |
| 63 | + plugins: [ |
| 64 | + new Visualizer({ |
| 65 | + filename: './statistics.html' |
59 | 66 | }) |
| 67 | + ] |
| 68 | +} |
| 69 | + |
| 70 | +module.exports = function (env = {}) { |
| 71 | + let config = webpackMerge(commonConfig, { |
| 72 | + mode: env.production ? 'production' : 'development' |
| 73 | + }) |
| 74 | + |
| 75 | + if (!env.test) { |
| 76 | + config = webpackMerge(config, nonTestConfig) |
60 | 77 | } |
61 | 78 |
|
62 | 79 | if (env.stats) { |
63 | | - const Visualizer = require('webpack-visualizer-plugin') |
64 | | - config.plugins.push(new Visualizer({ |
65 | | - filename: './statistics.html' |
66 | | - })) |
| 80 | + config = webpackMerge(config, statsConfig) |
67 | 81 | } |
68 | 82 |
|
69 | 83 | return config |
|
0 commit comments