似乎你不是唯一一个困惑的人,关于如何做到这一点的52 条评论。html-webpack-plugin 中的 publicPath问题类似并有所帮助。然而,最大的帮助是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
避免在eg中指定嵌套路径public/assets/js
,而是设置root directory:public
并设置filename
key:的嵌套assets/js/[name].js
。
publicPath
然后,您可以在 main中指定module.output
用于子域或 CDN等的 a。
最终的完整配置对我有用:
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,
}),
]
};