为了提高性能,我需要将webpack配置中的css和js分离到它自己的文件中.例如style.css
和main.js
,或者我 Select 给它们起的任何名字.目前,我在我的主JS文件中导入了css,比如import 'style.css'
.
我怎样才能改变这一点?
Webpack配置.
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
const isProduction = process.env.NODE_ENV === 'production';
const stylesHandler = 'style-loader';
const config = {
entry: './src/js/index.js',
output: {
path: path.resolve(__dirname, './dist/js'),
filename: 'main.js',
clean: true, // remove unused bundled files
},
module: {
rules: [
{
test: /\.(js|jsx)$/i,
loader: 'babel-loader',
},
{
test: /\.css$/i,
use: [stylesHandler, 'css-loader', 'postcss-loader'],
},
{
test: /\.(eot|svg|ttf|woff|woff2|png|jpg|gif|otf)$/i,
type: 'asset',
},
],
},
};
module.exports = () => {
if (isProduction) {
config.mode = 'production';
} else {
config.mode = 'development';
}
return config;
};
Package.json(脚本)
"scripts": {
"build": "webpack --mode=production --node-env=production",
"build:dev": "webpack --mode=development",
"build:prod": "webpack --mode=production --node-env=production",
"watch": "webpack --watch"
}