Skip to content

Commit

Permalink
fix: node build for es6
Browse files Browse the repository at this point in the history
  • Loading branch information
hubcarl committed Nov 22, 2018
1 parent 9a0a772 commit 6acfaf8
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 33 deletions.
2 changes: 1 addition & 1 deletion lib/core/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ module.exports = {

createPostCssLoader(loaderOptions) {
const loader = 'postcss-loader';
let options = this.merge(this.webpackConfig.devtool ? { sourceMap: true } : {}, loaderOptions);
let options = this.merge(!this.ssr && this.webpackConfig.devtool ? { sourceMap: true } : {}, loaderOptions);
const postcssFile = path.join(this.baseDir, 'postcss.config.js');
if (!fs.existsSync(postcssFile)) {
const config = { path: path.resolve(__dirname, '../../config/') };
Expand Down
128 changes: 96 additions & 32 deletions lib/core/optimize.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
module.exports = class WebpackOptimize {
constructor(ctx) {
this.ctx = ctx;
Expand Down Expand Up @@ -60,51 +61,113 @@ module.exports = class WebpackOptimize {
};
}

normalizeMinimizer() {
normalizeChunks() {
const { runtimeChunk, splitChunks } = this.optimization;
if (runtimeChunk) {
delete this.optimization.runtimeChunk;
}
if (splitChunks) {
delete this.optimization.splitChunks;
}
}

normalizeMinimizer(minimizer) {
if (!this.ctx.prod && this.optimization.minimizer) {
delete this.optimization.minimizer;
}
// compatible old config
if (minimizer) {
this.optimization.minimizer = [].concat(minimizer);
}
return this.optimization;
}

getMinimizerOptions() {
if (this.ctx.prod && !this.optimization.minimizer) {
const uglifyJs = this.ctx.getConfigPlugin('uglifyJs');
if (uglifyJs) {
const args = this.ctx.utils.isObject(uglifyJs) ? uglifyJs.args || uglifyJs : {};
const options = this.ctx.merge({
cache: true,
parallel: 4,
sourceMap: !!this.ctx.devtool,
uglifyOptions: {
ie8: false,
warnings: false,
compress: {
dead_code: true,
drop_console: true,
drop_debugger: true
},
output: {
comments: false
}
}
}, args);
this.optimization.minimizer = [ new UglifyJsPlugin(options) ];
if (uglifyJs === true) {
return {};
}
if (this.ctx.utils.isObject(uglifyJs)) {
return uglifyJs.args || uglifyJs;
}
}
return this.optimization;
return null;
}

getOptimization() {
const { runtimeChunk, splitChunks } = this.optimization;
if (runtimeChunk) {
delete this.optimization.runtimeChunk;
createUglifyJsMinimizer() {
const options = this.getMinimizerOptions();
if (options) {
delete options.terserOptions;
return this.createUglifyJsPlguin(options);
}
if (splitChunks) {
delete this.optimization.splitChunks;
return null;
}

createTerserMinimizer() {
const options = this.getMinimizerOptions();
if (options) {
delete options.uglifyOptions;
return this.createTerserPlugin(options);
}
return this.normalizeMinimizer();
return null;
}

createUglifyJsPlguin(options) {
const opt = this.ctx.merge({
cache: true,
parallel: 2,
sourceMap: !!this.ctx.devtool,
uglifyOptions: {
ie8: false,
safari10: false,
warnings: false,
compress: {
dead_code: true,
drop_console: true,
drop_debugger: true
},
output: {
comments: false
}
}
}, options);
return new UglifyJsPlugin(opt);
}

createTerserPlugin(options) {
const opt = this.ctx.merge({
cache: true,
parallel: 2,
sourceMap: !!this.ctx.devtool,
terserOptions: {
ie8: false,
safari10: false,
warnings: false,
compress: {
dead_code: true,
drop_console: true,
drop_debugger: true
},
output: {
comments: false
}
}
}, options);
return new TerserPlugin(opt);
}

getOptimization() {
return this.normalizeMinimizer(this.createUglifyJsMinimizer());
}

getDLLOptimization() {
this.normalizeChunks();
return this.normalizeMinimizer(this.createUglifyJsMinimizer());
}

getWebOptimization() {
const optimization = this.normalizeMinimizer();
const minimizer = this.createUglifyJsMinimizer();
const optimization = this.normalizeMinimizer(minimizer);
return this.ctx.merge({
namedModules: true,
namedChunks: true,
Expand All @@ -126,6 +189,7 @@ module.exports = class WebpackOptimize {
}

getNodeOptimization() {
return this.getOptimization();
this.normalizeChunks();
return this.normalizeMinimizer(process.env.BABEL_ENV ? this.createTerserMinimizer() : this.createUglifyJsMinimizer());
}
};
4 changes: 4 additions & 0 deletions lib/target/dll.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class WebpackDllBuilder extends WebpackBaseBuilder {
this.addEntry(item.name, item.lib);
});
}

createOptimization() {
return this.webpackOptimize.getDLLOptimization();
}
}
WebpackDllBuilder.TYPE = 'client';
WebpackDllBuilder.TARGET = 'web';
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"service-worker-precache-webpack-plugin": "^1.0.0",
"shelljs": "^0.7.4",
"style-loader": "^0.18.2",
"terser-webpack-plugin": "^1.1.0",
"thread-loader": "^1.1.5",
"uglifyjs-webpack-plugin": "^2.0.1",
"url-loader": "^0.5.8",
Expand Down

0 comments on commit 6acfaf8

Please sign in to comment.