Authored by ccbikai(👎🏻🍜)

webpack 参数优化

'use strict';
const path = require('path');
const _ = require('lodash');
const webpack = require('webpack');
const StyleLintPlugin = require('stylelint-webpack-plugin');
let baseConfig = require('./webpack.base.config.js');
baseConfig = baseConfig('dev');
baseConfig.devtool = '#inline-source-map';
baseConfig.output.publicPath = 'http://localhost:5001/';
baseConfig.module.rules.push({
enforce: 'pre',
test: /(\.js|\.vue)$/,
exclude: /node_modules/,
loader: 'eslint-loader',
options: {
cache: true,
formatter: require('eslint/lib/formatters/codeframe')
}
});
baseConfig.devServer = {
host: '0.0.0.0',
port: 5001,
contentBase: [path.join(__dirname, './bundle/'), path.join(__dirname, '../')],
publicPath: baseConfig.output.publicPath,
hot: true,
inline: true,
compress: true,
stats: {
colors: true,
children: false,
chunks: false,
assetsSort: 'size',
_.mergeWith(baseConfig, {
devtool: '#inline-source-map',
output: {
publicPath: 'http://localhost:5001/'
},
module: {
rules: [{
enforce: 'pre',
test: /(\.js|\.vue)$/,
exclude: /node_modules/,
loader: 'eslint-loader',
options: {
cache: true,
formatter: require('eslint/lib/formatters/codeframe')
}
}]
},
headers: {
'Access-Control-Allow-Origin': '*'
devServer: {
host: '0.0.0.0',
port: 5001,
contentBase: [path.join(__dirname, './bundle/'), path.join(__dirname, '../')],
publicPath: baseConfig.output.publicPath,
hot: true,
inline: true,
compress: true,
stats: {
colors: true,
children: false,
chunks: false,
assetsSort: 'size',
},
headers: {
'Access-Control-Allow-Origin': '*'
}
},
plugins: [
new StyleLintPlugin({
files: ['public/scss/**/*.css', 'public/vue/**/*.vue'],
syntax: 'scss'
}),
new webpack.HotModuleReplacementPlugin()
]
}, function customizer(objValue, srcValue) {
if (_.isArray(objValue)) {
return objValue.concat(srcValue);
}
};
baseConfig.plugins.push(
new StyleLintPlugin({
files: ['public/scss/**/*.css', 'public/vue/**/*.vue'],
syntax: 'scss'
}),
new webpack.HotModuleReplacementPlugin()
);
});
delete baseConfig.extends;
... ...
... ... @@ -3,6 +3,7 @@
const os = require('os');
const path = require('path');
const shelljs = require('shelljs');
const _ = require('lodash');
const webpack = require('webpack');
const HappyPack = require('happypack');
const UglifyJsParallelPlugin = require('webpack-uglify-parallel');
... ... @@ -21,32 +22,41 @@ shelljs.cp('-R', path.join(__dirname, '../img/'), distDir);
shelljs.cp('-R', path.join(__dirname, '../font/'), distDir);
baseConfig = baseConfig('pro');
baseConfig.output.path = distDir;
baseConfig.plugins.push(
new ExtractTextPlugin('[name].css'),
new HappyPack({
id: 'js',
threadPool: happyThreadPool,
loaders: ['babel-loader'],
}),
new HappyPack({
id: 'hbs',
threadPool: happyThreadPool,
loaders: [baseConfig.extends.hbsLoader]
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new UglifyJsParallelPlugin({
workers: os.cpus().length,
compress: {
warnings: false
},
comments: false
})
);
_.mergeWith(baseConfig, {
output: {
path: distDir
},
plugins: [
new ExtractTextPlugin('[name].css'),
new HappyPack({
id: 'js',
threadPool: happyThreadPool,
loaders: ['babel-loader'],
}),
new HappyPack({
id: 'hbs',
threadPool: happyThreadPool,
loaders: [baseConfig.extends.hbsLoader]
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new UglifyJsParallelPlugin({
workers: os.cpus().length,
compress: {
warnings: false
},
comments: false
})
]
}, function customizer(objValue, srcValue) {
if (_.isArray(objValue)) {
return objValue.concat(srcValue);
}
});
delete baseConfig.extends;
... ...