概述
const webpack = require('webpack');
const path = require('path');
/**
* Plugin that simplifies creation of HTML files to serve your bundles
* npm i --save-dev html-webpack-plugin
* @type {HtmlWebpackPlugin}
*/
const HtmlWebpackPlugin = require('html-webpack-plugin');
/**
* A webpack plugin to remove/clean your build folder(s) before building
* npm i clean-webpack-plugin --save-dev
* @type {CleanWebpackPlugin}
*/
const CleanWebpackPlugin = require('clean-webpack-plugin');
/**
* 提取css到单独文件
* npm install --save-dev mini-css-extract-plugin
*/
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
/**
* css 压缩 会清除css中注释
* npm install --save-dev optimize-css-assets-webpack-plugin
*/
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
/**
* js 压缩
* npm install uglifyjs-webpack-plugin --save-dev
*/
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
module.exports = {
mode: 'production',
/**
* entry 输入文件
*/
entry: {
/**
* common 自定义公共文件
* common: ['./commons/a.js', "./commons/b.js"],
*/
common: [],
/**
* app 主文件
*/
app: './main.js',
},
/**
* 打包输出
*/
output: {
/**
* publicPath
* 生成如下链接
* <script type="text/javascript" src="/cx/tpl/Support/search/dist/build.js">
*/
publicPath: '/cx/tpl/Support/search/dist/',
/**
* [name] 指向entry 的key 也就是 app
*/
filename: '[name].js',
path: path.resolve(__dirname, 'dist/'),
},
plugins: [
/**
* 打包HTML
*/
new HtmlWebpackPlugin({
title: 'Output Management',
/**
* template 指定要打包的原文件
*/
template: './online.html'
}),
/**
* 由 webpack 生成的文件或目录才能被 CleanWebpackPlugin 删除
* 下面配置的是 打包前 需要被删除的目录
*/
new CleanWebpackPlugin(['dist', 'build']),
new MiniCssExtractPlugin({
filename: "[name].css",
chunkFilename: "[id].css"
}),
// new webpack.optimize.DedupePlugin(),
/**
* 提取SourceMap到独立文件
*/
new webpack.SourceMapDevToolPlugin({
filename: '[name].js.map',
// exclude: ['vendor.js']
})
],
/**
* devtool 调试工具,用来追踪被打包前源文件的错误
* 关闭可减少打包文件的大小
*/
// devtool: 'inline-source-map',
module: {
rules: [
{
/**
* https://www.npmjs.com/package/babel-loader
* npm install -D babel-loader @babel/core @babel/preset-env webpack
*/
test: /.m?js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/react']
}
}
},
{
test: /.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
// you can specify a publicPath here
// by default it use publicPath in webpackOptions.output
publicPath: '../'
}
},
"css-loader"
]
}
]
},
optimization: {
/**
* 分包
*/
splitChunks: {
chunks: 'all',
minSize: 30000,
maxSize: 0,
},
minimizer: [
new UglifyJsPlugin({
cache: true,
parallel: true,
/**
* sourceMap 和 devtool: 'inline-source-map', 冲突
*/
sourceMap: false, // set to true if you want JS source maps,
/**
* extractComments 导出备注
*/
extractComments: 'all'
}),
new OptimizeCSSAssetsPlugin({})
]
}
};
最后
以上就是俊秀豌豆为你收集整理的webpack4完全配置(js分组打包分模块打包压缩提取备注和css压缩打包)的全部内容,希望文章能够帮你解决webpack4完全配置(js分组打包分模块打包压缩提取备注和css压缩打包)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复