Skip to content

Commit c37a5fb

Browse files
author
Olaf Kwant
committed
Use webpack-merge to split config clearly
1 parent 49bd018 commit c37a5fb

File tree

3 files changed

+84
-61
lines changed

3 files changed

+84
-61
lines changed

package-lock.json

Lines changed: 19 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"babel-preset-env": "^1.7.0",
3535
"native-promise-only": "^0.8.1",
3636
"webpack": "^4.25.1",
37-
"webpack-cli": "^3.1.0"
37+
"webpack-cli": "^3.1.0",
38+
"webpack-merge": "^4.1.4"
3839
},
3940
"devDependencies": {
4041
"chai": "^4.1.2",

webpack.config.js

Lines changed: 63 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,83 @@
11
const path = require('path')
22
const packageJson = require('./package.json')
3+
const webpackMerge = require('webpack-merge')
34
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
5+
const Visualizer = require('webpack-visualizer-plugin')
46

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',
209

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+
},
2420

25-
plugins: [],
21+
output: {
22+
library: 'ZAFClient',
23+
filename: '[name].js',
24+
path: path.resolve(__dirname, 'build')
25+
},
2626

27-
output: {
28-
library: 'ZAFClient',
29-
filename: '[name].js',
30-
path: path.resolve(__dirname, 'build')
31-
},
27+
externals: {
28+
version: `"${packageJson.version}"`
29+
},
3230

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
4437
}
38+
}
4539

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({
5146
include: /\.min\.js$/,
5247
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+
}
5561

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'
5966
})
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)
6077
}
6178

6279
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)
6781
}
6882

6983
return config

0 commit comments

Comments
 (0)