webpack5-不负责任配置
文件目录
plain-text
webpack5
│ ├─src
│ │ ├─index.html
│ │ └─index.ts
│ ├─package-lock.json
│ ├─package.json
│ ├─postcss.config.js
│ ├─tsconfig.json
│ ├─webpack.common.js
│ ├─webpack.dev.js
│ └─webpack.prod.js
依赖安装
shell
# webpack 套装
npm i -D webpack webpack-cli html-webpack-plugin webpack-dev-server webpack-merge
# style 套装
npm i -D sass-loader node-sass style-loader css-loader postcss-loader autoprefixer mini-css-extract-plugin css-minimizer-webpack-plugin
# js 套装
npm i -D babel-loader @babel/core @babel/preset-env @babel/plugin-transform-runtime @babel/runtime
# 打包优化套装
npm i -D closure-webpack-plugin google-closure-compiler
# ts 套装
npm i -D typescript ts-loader
package.json
json
{
"name": "webpack5",
"version": "0.0.1",
"description": "webpack5 demo",
"main": "index.js",
"scripts": {
"dev": "webpack serve --config webpack.dev.js",
"build": "webpack --config webpack.prod.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.13.10",
"@babel/plugin-transform-runtime": "^7.13.10",
"@babel/preset-env": "^7.13.10",
"@babel/runtime": "^7.13.10",
"autoprefixer": "^10.2.5",
"babel-loader": "^8.2.2",
"closure-webpack-plugin": "^2.5.0",
"css-loader": "^5.1.3",
"css-minimizer-webpack-plugin": "^1.3.0",
"google-closure-compiler": "^20210302.0.0",
"html-webpack-plugin": "^5.3.1",
"mini-css-extract-plugin": "^1.3.9",
"node-sass": "^5.0.0",
"postcss-loader": "^5.2.0",
"sass-loader": "^11.0.1",
"style-loader": "^2.0.0",
"ts-loader": "^8.0.18",
"typescript": "^4.2.3",
"webpack": "^5.26.1",
"webpack-cli": "^4.5.0",
"webpack-dev-server": "^3.11.2",
"webpack-merge": "^5.7.3"
}
}
配置相关
webpack.common.js
js
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const isProductionMode = process.env.NODE_ENV === 'production';
const resolve = (filepath) => {
return path.resolve(__dirname, filepath);
};
module.exports = {
entry: './src/index.ts',
output: {
filename: '[name].[hash:5].js',
path: resolve('dist'),
clean: true
},
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.m?js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
[
'@babel/preset-env',
{
targets: 'defaults'
}
]
],
plugins: ['@babel/plugin-transform-runtime']
}
}
},
{
test: /\.scss$/,
use: [
isProductionMode
? MiniCssExtractPlugin.loader
: 'style-loader',
'css-loader',
'postcss-loader',
'sass-loader'
]
},
{
test: /\.css$/,
use: [
isProductionMode
? MiniCssExtractPlugin.loader
: 'style-loader',
'css-loader',
'postcss-loader'
]
},
// 资源,图片、字体等资源都可以用以下方式
{
test: /\.(png|jpeg|jpg|gif|svg)/i,
type: 'asset/resource'
}
]
},
plugins: [
new MiniCssExtractPlugin({
filename: isProductionMode
? '[name].[contenthash].css'
: '[name].css',
chunkFilename: isProductionMode
? '[id].[contenthash].css'
: '[id].css'
}),
new HtmlWebpackPlugin({
title: '测试webpack5',
template: resolve('./src/index.html')
})
],
optimization: {
splitChunks: {
chunks: 'all'
}
},
resolve: {
extensions: ['.tsx', '.ts', '.js']
}
};
webpack.dev.js
js
const CommonConfig = require('./webpack.common');
const { merge } = require('webpack-merge');
const webpack = require('webpack');
module.exports = merge(CommonConfig, {
mode: 'development',
devtool: 'inline-source-map',
devServer: {
contentBase: './dist',
hot: true
},
plugins: [
new webpack.HotModuleReplacementPlugin()
]
});
webpack.prod.js
js
const CommonConfig = require('./webpack.common');
const { merge } = require('webpack-merge');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const ClosurePlugin = require('closure-webpack-plugin');
module.exports = merge(CommonConfig, {
mode: 'production',
// devtool: 'source-map',
optimization: {
minimizer: [
new CssMinimizerPlugin(),
new ClosurePlugin({
mode: 'STANDARD'
})
]
}
});
postcss.config.js
js
module.exports = {
plugins: [
require('autoprefixer')
]
};