webpack.prod.js
const merge = require("webpack-merge");
const UglifyJSPlugin = require("uglifyjs-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CleanWebpackPlugin = require("clean-webpack-plugin");
const common = require("./webpack.common.js");
module.exports = merge(common, {
devtool: "source-map",
plugins: [
new CleanWebpackPlugin(["dist"]),
new MiniCssExtractPlugin({
filename: "[name]/[name].css",
chunkFilename: "[name].css"
}),
new UglifyJSPlugin({
sourceMap: true
})
]
});
webpack.common.js
/**
* Created by MBJ20180827S1 on 2018/10/19.
*/
const webpack = require("webpack");
const path = require("path");
const glob = require("glob");
const HtmlWebpackPlugin = require("html-webpack-plugin"); // html
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const HtmlWebpackInlineSourcePlugin = require("html-webpack-inline-source-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
function buildEntriesAndHTML() {
// entery
const result = glob.sync("views/**/*.js");
const config = {
hash: true,
inject: true
};
const entries = {};
const htmls = [];
result.forEach(item => {
const one = path.parse(item);
const outputfile = one.dir.split("/").slice(-1)[0];
entries[outputfile] = "./" + item;
htmls.push(
new HtmlWebpackPlugin({
...config,
template: "./" + one.dir + "/index.html",
filename: "./" + outputfile + "/index.html", // html
title:outputfile+"",
chunks: [outputfile]
})
);
});
if (htmls.length > 0) {
htmls.push(new HtmlWebpackInlineSourcePlugin());
}
return {
entries,
htmls
};
}
const final = buildEntriesAndHTML();
module.exports = {
entry: final.entries,
output: { //
path: __dirname + "/dist",
publicPath: "https://test-dsp.rayjump.com/test/dsp_model_test/m2/",
// publicPath: "/",
filename: "[name]/[name].js" //
},
devtool: "inline-source-map",
watch: true,
devServer: {
contentBase: path.join(__dirname, "./views"),
inline:true,
host: "localhost",
port: "8090",
overlay: true,
open: true,
hot: true,
watchOptions: {
poll: true,
}
},
module: {
rules: [
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
{loader: "postcss-loader", options: {plugins: [require("autoprefixer")("last 100 versions")]}}
]
},
{
test: /\.less$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader?importLoaders=1",
{loader: "postcss-loader", options: {plugins: [require("autoprefixer")("last 100 versions")]}},
"less-loader"
]
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
/*use:[
{
loader: "url-loader",
options: {
limit: 1024 * 10,
name: "image/[name].[ext]",
fallback: "file-loader"
}
},
{
loader: "image-webpack-loader",//
options: {
bypassOnDebug: true,
}
}
]*/
use: [
{
loader: "file-loader",
options: {
name: "image/[name].[hash:7].[ext]",
}
},
{
loader: "image-webpack-loader",//
options: {
bypassOnDebug: true,
}
}
]
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac|swf|mov)(\?.*)?$/,
use: [
{
loader: "file-loader",
options: {
name: "media/[name].[hash:7].[ext]",
}
},
]
},
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.html$/,
use: [
{
loader: "html-loader",
options: {minimize: true} //
}
]
}
]
},
plugins: [
new OptimizeCSSAssetsPlugin({
assetNameRegExp: /\.css\.*(?!.*map)/g, // /\.css$/g
cssProcessor: require("cssnano"),
cssProcessorOptions: {
discardComments: {removeAll: true},
// cssnano z-index
safe: true,
// cssnano autoprefixer
// autoprefixer
// autoprefixer
// postcssautoprefixer
autoprefixer: false
},
canPrint: true
}),
...final.htmls
],
resolve: {
extensions: [".js", ".json", ".jsx", ".css"]
},
}