你可以使用MiniCssExtractPlugin來進行CSS的樹搖。如果你使用的是scss或sass,你也可以添加這些loader。
// webpack.config.js (production) const path = require('path') const MiniCssExtractPlugin = require('mini-css-extract-plugin') module.exports = { mode: 'production', entry: path.resolve('src/index.js'), output: { filename: '[name].js', path: path.resolve('dist'), }, module: { rules: [ { test: /\.jsx?$/, loader: 'babel-loader', exclude: /node_modules/, }, { test: /\.css$/, exclude: /node_modules/, use: [ // 將css提取到文件中 MiniCssExtractPlugin.loader, // 處理你的應(yīng)用程序中的`.css`文件的導入 'css-loader', ], }, ], }, plugins: [ // 將所有的css提取到一個單獨的文件中 new MiniCssExtractPlugin({ filename: '[name].css', }), ], }