Skip to content

Commit a84e5bc

Browse files
committed
Enhance webpack config
1 parent 1d177ed commit a84e5bc

File tree

4 files changed

+66
-48
lines changed

4 files changed

+66
-48
lines changed

gulpfile.babel.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import jade from 'gulp-jade';
55
import rename from 'gulp-rename';
66
import zip from 'gulp-zip';
77
import webpack from 'webpack';
8+
import devConfig from './webpack/dev.config';
9+
import prodConfig from './webpack/prod.config';
810

911
/*
1012
* common tasks
@@ -25,8 +27,7 @@ gulp.task('replace-webpack-code', () => {
2527
*/
2628

2729
gulp.task('webpack:dev', (callback) => {
28-
let myConfig = Object.create(require('./webpack/dev.config'));
29-
webpack(myConfig, (err, stats) => {
30+
webpack(devConfig, (err, stats) => {
3031
if (err) {
3132
throw new gutil.PluginError('webpack:dev', err);
3233
}
@@ -55,8 +56,7 @@ gulp.task('copy:dev', () => {
5556
*/
5657

5758
gulp.task('webpack:build:extension', (callback) => {
58-
let myConfig = Object.create(require('./webpack/prod.config'));
59-
webpack(myConfig, (err, stats) => {
59+
webpack(prodConfig, (err, stats) => {
6060
if (err) {
6161
throw new gutil.PluginError('webpack:build', err);
6262
}

webpack/base.config.js

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,56 @@ import webpack from 'webpack';
33

44
const extpath = path.join(__dirname, '../src/browser/extension/');
55

6-
export default {
7-
entry: {
8-
background: [ extpath + 'background/index' ],
9-
options: [ extpath + 'options/index' ],
10-
window: [ extpath + 'window/index' ],
11-
devpanel: [ extpath + 'devpanel/index' ],
12-
devtools: [ extpath + 'devtools/index' ],
13-
content: [ extpath + 'inject/contentScript' ],
14-
page: [ extpath + 'inject/pageScript' ],
15-
inject: [ extpath + 'inject/index' ]
6+
const baseConfig = (params) => ({
7+
entry: params.input || {
8+
background: [ `${extpath}background/index` ],
9+
options: [ `${extpath}options/index` ],
10+
window: [ `${extpath}window/index` ],
11+
devpanel: [ `${extpath}devpanel/index` ],
12+
devtools: [ `${extpath}devtools/index` ],
13+
content: [ `${extpath}inject/contentScript` ],
14+
page: [ `${extpath}inject/pageScript` ],
15+
inject: [ `${extpath}inject/index` ],
16+
...params.inputExtra
1617
},
1718
output: {
1819
filename: '[name].bundle.js',
19-
chunkFilename: '[id].chunk.js'
20+
chunkFilename: '[id].chunk.js',
21+
...params.output
2022
},
23+
plugins: [
24+
new webpack.DefinePlugin(params.globals),
25+
...(params.plugins ? params.plugins :
26+
[
27+
new webpack.optimize.DedupePlugin(),
28+
new webpack.optimize.UglifyJsPlugin({
29+
comments: false,
30+
compressor: {
31+
warnings: false
32+
}
33+
})
34+
])
35+
],
2136
resolve: {
37+
alias: {
38+
app: path.join(__dirname, '../src/app'),
39+
tmp: path.join(__dirname, '../build/tmp')
40+
},
2241
extensions: ['', '.js']
2342
},
2443
module: {
25-
loaders: [{
26-
test: /\.js$/,
27-
loaders: ['babel'],
28-
exclude: /node_modules/
29-
}, {
30-
test: /\.css?$/,
31-
loaders: ['style', 'raw']
32-
}]
44+
loaders: [
45+
...(params.loaders ? params.loaders : [{
46+
test: /\.js$/,
47+
loaders: ['babel'],
48+
exclude: /node_modules/
49+
}]),
50+
{
51+
test: /\.css?$/,
52+
loaders: ['style', 'raw']
53+
}
54+
]
3355
}
34-
};
56+
});
57+
58+
export default baseConfig;

webpack/dev.config.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import path from 'path';
22
import webpack from 'webpack';
3-
import config from './base.config';
3+
import baseConfig from './base.config';
44

5-
config.output.path = path.join(__dirname, '../dev/js');
6-
config.plugins = [
7-
new webpack.DefinePlugin({
5+
let config = baseConfig({
6+
output: { path: path.join(__dirname, '../dev/js') },
7+
globals: {
88
'process.env': {
99
NODE_ENV: '"development"'
1010
}
11-
}),
12-
new webpack.optimize.OccurenceOrderPlugin(),
13-
new webpack.NoErrorsPlugin()
14-
];
11+
},
12+
plugins: [
13+
new webpack.optimize.OccurenceOrderPlugin(),
14+
new webpack.NoErrorsPlugin()
15+
]
16+
});
17+
1518
config.watch = true;
1619

17-
export default config;
20+
export default config;

webpack/prod.config.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
import path from 'path';
22
import webpack from 'webpack';
3-
import config from './base.config';
3+
import baseConfig from './base.config';
44

5-
config.output.path = path.join(__dirname, '../build/extension/js');
6-
config.plugins = [
7-
new webpack.DefinePlugin({
5+
export default baseConfig({
6+
output: { path: path.join(__dirname, '../build/extension/js') },
7+
globals: {
88
'process.env': {
99
NODE_ENV: '"production"'
1010
}
11-
}),
12-
new webpack.optimize.DedupePlugin(),
13-
new webpack.optimize.UglifyJsPlugin({
14-
comments: false,
15-
compressor: {
16-
warnings: false
17-
}
18-
})
19-
];
20-
21-
export default config;
11+
}
12+
});

0 commit comments

Comments
 (0)