我正在使用 Webpack ^4.26.0。在用于 CSS 之前,我已经在 Webpack 3 中使用了“extract-text-webpack-plugin”。但是我读到该插件在 Webpack 4 上不再工作了。“extract-text-webpack-plugin”建议使用“mini-css-extract-plugin”-plugin。
我已经通过命令安装了插件:
npm install --save-dev mini-css-extract-plugin
并需要 webpackconfig 中的插件,并将其添加到我的规则和插件中:
// webpack.config.js
const webpack = require("webpack");
const path = require("path");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const config = {
context: path.resolve(__dirname),
entry: "./index.js",
devServer: {
contentBase: "./dist"
},
output: {
path: path.resolve(__dirname, "dist"),
filename: "bundle.js"
},
resolve: {
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: [".ts", ".tsx", ".js", ".json"]
},
module: {
rules: [
{
test: /\.js$/,
include: path.resolve(__dirname),
use: [
{
loader: "babel-loader",
options: {
presets: [["@babel/env", { modules: false }], "@babel/react"]
}
}
]
},
{
test: /\.tsx?$/,
loader: "awesome-typescript-loader"
},
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, "css-loader"]
}
],
noParse: [/aws-sdk/]
},
plugins: [
new webpack.DefinePlugin({
"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
"process.env.STATIC_PORT": JSON.stringify(process.env.STATIC_PORT),
VERSION: JSON.stringify(require("./package.json").version)
}),
new MiniCssExtractPlugin({
filename: 'bundle.css'
}),
new CopyWebpackPlugin([{ from: "./cb_icons", to: "cb_icons" }])
],
node: { fs: "empty" },
externals: [{ "./cptable": "var cptable" }, { "./jszip": "jszip" }]
};
module.exports = config;
它安装在我的 node_modules 中:
组件/searchkit/node_modules/mini-css-extract-plugin