webpack 基本配置 (记录)
webpack-config.js
开发环境配置
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MinniCssExtractPlugin = require('mini-css-extract-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin')
const ProgressBarPlugin = require('progress-bar-webpack-plugin')
const CssUrlRelativePlugin = require('css-url-relative-plugin')
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin')
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin')
const TerserPlugin = require('terser-webpack-plugin')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
module.exports = {
entry: path.join(__dirname, '../src/index.js'),
output: {
path: path.join(__dirname, '../build'),
filename: 'js/app.js'
},
mode: 'development',
module: {
rules: [
{
test: /\.css$/,
use: [
MinniCssExtractPlugin.loader,
'css-loader',
]
},
{
test: /\.js$/,
exclude: /node_modules/,
use: [{
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}]
},
{
test: /\.(png|jpg|gif)/,
use: [{
loader: 'url-loader',
options: {
limit: 5000,
outputPath: 'images/',
}
}]
}
]
},
devServer: {
hot: true,
open: true,
},
plugins: [
new CleanWebpackPlugin(),
new MiniCssExtractPlugin({
filename: '[name].css'
}),
new CssUrlRelativePlugin(),
new ProgressBarPlugin(),
new CaseSensitivePathsPlugin(),
new HtmlWebpackPlugin({
template: path.join(__dirname, '../public/index.html'),
filename: './index.html',
hash: true,
minify: {
removeAttributeQuotes: true
}
}),
new CopyWebpackPlugin([
{
from: path.resolve(__dirname, '../public'),
to: path.resolve(__dirname, '../build')
}
]),
new ForkTsCheckerWebpackPlugin({
tsconfig: paths.appTsConfig,
async: false,
silent: true,
eslint: true
}),
process.env.NODE_ENV === 'production' && new OptimizeCssAssetsPlugin({
assetNameRegExp: /\.css$/g,
cssProcessorOptions: {
discardComments: {
removeAll: true
},
autoprefixer: false,
zindex: false
},
canPrint: true
})
],
resolve: {
modules: [path.join('node_modules')],
mainFields: ['style', 'main'],
extensions: ['.js', '.css', '.json'],
alias: {
components: './src/components/'
}
},
devtool: process.env.NODE_ENV === 'production' ? false : 'cheap-module-source-map',
optimization: process.env.NODE_ENV === 'production' ? {
minimize: true,
minimizer: [new TerserPlugin({
parallel: true,
sourceMap,
terserOptions: {
compress: {
comparisons: false
},
output: {
ascii_only: true
},
mangle: {
safari10: true
}
}
})]
} : {}
}
package.json
配置
{
"name": "code",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "webpack --config config/webpack-config-dev.js",
"prod": "webpack --config config/webpack-config-prod.js",
"start": "npm run dev & webpack-dev-server -w --config config/webpack-config-dev.js ",
"build": " npm run prod & webpack-dev-server --config config/webpack-config-prod.js",
"test": "echo \"Error: no test specified\" && exit 1"
}