好像你的not the only one confused, 52 cmets 关于如何做到这一点。 publicPath in html-webpack-plugin 的问题是类似的并且有所帮助。然而,最大的帮助来自于 npm run eject 退出 create-react-app 并检查 webpack.config.js 文件。
TL;DR:您需要在插件构造函数中指定保存路径。
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
filename: "assets/css/[name].css",
}),
您可能需要重新考虑模块输出逻辑。
避免在 module.output.path 中指定嵌套路径,例如public/assets/js,而是设置 root 目录:public 并设置 filename 键的嵌套:assets/js/[name].js。
然后您可以在主module.output 中指定一个publicPath,用于subdomains or CDN's 等。
最终的完整配置对我有用:
const path = require('path');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const HtmlWebpackPlugin = require('html-webpack-plugin');
const publicPath = '/';
module.exports = {
entry: './_src/_js/index.js',
output: {
filename: 'assets/js/[name].js',
path: path.resolve(__dirname, 'dist'),
publicPath: publicPath,
},
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
'css-loader',
],
},
{
test: /\.(png|svg|jpg|gif)$/,
use: [
{
loader: 'file-loader',
options: {
name: 'assets/images/[name].[ext]',
},
},
]
},
{
test: /\.js$/,
exclude: /node_modules/,
use: ['babel-loader','eslint-loader']
},
],
},
plugins: [
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: "assets/css/[name].css",
}),
new HtmlWebpackPlugin({
inject: true,
}),
]
};