1. 安装所需库
yarn add webpack webpack-cli webpack-command webpack-dev-server --dev
yarn add ts-loader html-webpack-plugin clean-webpack-plugin friendly-errors-webpack-plugin extract-text-webpack-plugin webpack-bundle-analyzer babel-plugin-syntax-dynamic-import image-webpack-loader MiniCssExtractPlugin --dev
yarn add babel-loader babel-core @babel/preset-env @babel/core @babel/cli @babel/preset-react @babel/preset-typescript @babel/polyfill @babel/plugin-proposal-class-properties --dev
yarn add css-loader node-sass sass-loader file-loader url-loader json-loader --dev
2. 新建webpack.config.js
const path = require('path');
const htmlWebpackPlugin = require('html-webpack-plugin')
const {CleanWebpackPlugin} = require('clean-webpack-plugin')
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const CompressionPlugin = require('compression-webpack-plugin');//引入gzip压缩插件
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
module.exports = {
entry: './src/index.js',//入口地址
output: { //打包文件的输出位置
path: path.resolve(__dirname, 'dist'),
filename: 'main.[chunkhash:8].js',
chunkFilename: 'chunks/[name].[hash:8].js',
},
devServer: {
static: "./public",//本地服务器所加载的页面所在的目录
historyApiFallback: true,//不跳转
hot: true,//实时刷新
compress: true, //gzip压缩
},
module:{
rules:[
{
test: /\.(js|jsx|mjs)$/,
loader: "babel-loader",
exclude: /node_modules/,
},
{
test: /\.(ts|tsx)?$/,
loader: "ts-loader",
exclude: /node_modules/,
},
{
test:/\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: '../'
}
},
"css-loader"
]
},
{
test:/\.scss$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: '../'
}
},
"css-loader",
'sass-loader'
]
},
{
test: /\.(jpe?g|png|gif|svg)$/,
use:[
{
loader:'url-loader',
options: {
limit:8*1024,
name: '[name]_[hash:6].[ext]'//这里我们给图片名添加了6位的hash值
}
},
'image-webpack-loader'
],
},
{
test: /\.(mp4|mp3|webm|0gg|wav|flac|acc)$/,
loader:'url-loader',
options: {
limit:8*1024,
name: '[name]_[hash:6].[ext]'
}
},
{
test: /\.(wogg2?|eot|ttf|otf)$/,
loader:'url-loader',
options: {
limit:8*1024,
name: '[name]_[hash:6].[ext]'
}
},
{
test: /\.json$/,
use: ['json-loader'],
type: 'javascript/auto'
}
],
},
plugins:[
new htmlWebpackPlugin({
template:path.join(__dirname,'./public/index.html'),
filename: 'index.html'
}),
new CleanWebpackPlugin({}),
new BundleAnalyzerPlugin({ analyzerPort: 8919 }),
new UglifyJsPlugin({
uglifyOptions: {
warnings: false,
compress: {
drop_console: true,
drop_debugger: true,
pure_funcs: ['console.log']
}
}
}),
new MiniCssExtractPlugin({
filename: 'main.[chunkhash:8].css',
chunkFilename: 'chunks/[name].[hash:8].css',
}),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
})
],
mode:'production', //环境 production
resolve:{
extensions:['.js','.ts','.tsx','.jsx','.mjs']
},
optimization: {
splitChunks: {
chunks: 'async',
minSize: 20000,
minRemainingSize: 0,
minChunks: 1,
maxAsyncRequests: 30,
maxInitialRequests: 30,
enforceSizeThreshold: 50000,
cacheGroups: {
defaultVendors: {
test: /[\\/]node_modules[\\/]/,
priority: -10,
reuseExistingChunk: true,
},
default: {
minChunks: 2,
priority: -20,
reuseExistingChunk: true,
},
},
},
},
};
3. 新建.babelrc
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]
}
4. 修改tsconfig.json
"noEmit": false,
5. 打包
webpack
网友评论