使用process.argv 获取命令行使用的参数
var isprod = false;
for (var i in process.argv) {
if (process.argv[i] === "-p" || process.argv[i] === "--production") {
isprod = true;
break;
}
}
url-loader 路径不正确,可通过 publicPath进行配置
output: {
path: path.resolve(HTML_DIST_PATH, "assets"),
publicPath: '/dist/assets/',
filename: "[name].[hash:6].js",
chunkFilename: "[id].chunk.js",
}
html-webpack-plugin minify: true 报错,请改成
new HtmlWebpackPlugin({
title: '',
template: currentpath,
filename: currentpath.replace("\\html\\", "\\dist\\"),
minify: {
"removeAttributeQuotes": true,
"removeComments": true,
"removeEmptyAttributes": true,
}
inject: 'body'
})
ExtractTextPlugin.extract(["css-loader", "postcss-loader"])
完整的配置文件
var fs = require('fs');
var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var CommonsChunkPlugin = require("webpack/lib/optimize/CommonsChunkPlugin");
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var CleanWebpackPlugin = require('clean-webpack-plugin');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var BomPlugin = require('webpack-utf8-bom');
var autoprefixer = require('autoprefixer')
var cssparams = JSON.stringify({ discardComments: { removeAll: false } });
var htmlMinifyOptions = require('./htmlminify.config.js');
var ROOT_PATH = path.resolve(__dirname);
var HTML_ROOT_PATH = path.resolve(__dirname, "html");
var HTML_SRC_PATH = path.resolve(ROOT_PATH, 'dev');
var HTML_DIST_PATH = path.resolve(ROOT_PATH, 'dist');
var isprod = false;
for (var i in process.argv) {
if (process.argv[i] === "-p" || process.argv[i] === "--production") {
isprod = true;
break;
}
}
var config = {
entry: {
index: path.resolve(HTML_SRC_PATH, 'index.js'),
vendors: ['jquery'],
},
output: {
path: path.resolve(HTML_DIST_PATH, "assets"),
publicPath: '/dist/assets/',
filename: "[name].[hash:6].js",
chunkFilename: "[id].chunk.js",
},
plugins: [
new ExtractTextPlugin("styles/[name].[contenthash:6].css", { allChunks: false }),
new CommonsChunkPlugin('vendors', 'vendors.[hash:6].js'),
new CleanWebpackPlugin(['dist', 'build'], {
root: ROOT_PATH,
verbose: true,
dry: false,
}),
],
module: {
unknownContextRegExp: /$^/,
unknownContextCritical: false,
exprContextRegExp: /$^/,
exprContextCritical: false,
wrappedContextRegExp: /$^/,
wrappedContextCritical: false,
loaders: [
{ test: require.resolve('jquery'), loader: 'expose?$!expose?jQuery' },
{
test: /\.css$/,
loader: ExtractTextPlugin.extract(["css-loader", "postcss-loader"])
},
{ test: /\.(woff|woff2|eot|ttf|svg)(\?[a-z0-9]+)?$/, loader: 'url-loader?limit=1000&name=styles/fonts/[name].[hash:6].[ext]' },
{ test: /\.(png|jpg|gif)(\?[a-z0-9]+)?$/, loader: 'url-loader?limit=1000&name=styles/images/[name].[hash:8].[ext]' },
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel',
query: {
presets: ['es2015']
}
},
{ test: /\.tpl/, loader: 'art-template-loader' },
]
},
postcss: [autoprefixer()],
resolve: {
alias: {
"datepicker": "jquery-ui/ui/widgets/datepicker",
}
}
};
var htmlfiles = fs.readdirSync(HTML_ROOT_PATH);
htmlfiles.forEach(function (item) {
var currentpath = path.join(HTML_ROOT_PATH, item);
var extname = path.extname(currentpath);
if (fs.statSync(currentpath).isFile()) {
config.plugins.push(new HtmlWebpackPlugin({
title: '',
template: currentpath,
filename: currentpath.replace("\\html\\", "\\dist\\"),
minify: isprod ? htmlMinifyOptions : false,
inject: 'body'
}))
}
});
module.exports = config;