Это очень странная проблема с динамическим синтаксисом c import. По умолчанию веб-пакет назначает [id]. js имени чанка. Я могу изменить имя следующим образом:
import React from "react";
import "../css/Gallery.css";
const getBundle = () => {
import(/*webpackChunkName:"lodash"*/ "lodash").then(_ =>
console.log("imported", _)
);
};
export default () => (
<div>
<h1>Galery</h1>
<button onClick={getBundle}>download bundle</button>
</div>
);
, но если я пытаюсь изменить имя через webpack.config, браузер не загружает css.
вот конфигурация веб-пакета
module.exports = {
name: "client",
entry: {
main: [
"react-hot-loader/patch",
// "regenerator-runtime/runtime",
"webpack-hot-middleware/client?reload=true",
"./src/main.js"
]
},
mode: "development",
output: {
filename: "[name]-bundle.js",
chunkLoadTimeout: 30000,
chunkFilename: "[name]-chunk.js",
path: path.resolve(__dirname, "../dist"),
publicPath: "/"
},
optimization: {
minimize: true,
minimizer: [new TerserPlugin()],
splitChunks: {
chunks: "all",
cacheGroups: {
vendor: {
name: "vendor",
chunks: "initial",
minChunks: 2
}
}
}
},
devServer: {
contentBase: "dist",
overlay: true,
hot: true,
stats: {
colors: true
}
},
module: {
rules: [
{
test: /\.js$/,
use: [{ loader: "babel-loader" }],
exclude: /node_modules/
},
{
test: /\.css$/,
use: [{ loader: "style-loader" }, { loader: "css-loader" }]
},
{
test: /\.html$/,
use: [
{
loader: "html-loader"
}
]
},
{
test: /\.md$/,
use: [{ loader: "html-loader" }, { loader: "markdown-loader" }]
},
{
test: /\.(jpg|jpeg|png|gif)$/,
use: [
{
loader: "file-loader",
options: { name: "images/[name].[ext]" }
}
]
}
]
},
plugins: [
// new BundleAnalyzerPlugin({ generateStatsFile: true }),
new webpack.HotModuleReplacementPlugin(),
new CleanWebpackPlugin(),
new webpack.DefinePlugin({
"process.env.NODE_ENV": JSON.stringify(
process.env.NODE_ENV || "development"
)
}),
new webpack.SourceMapDevToolPlugin()
]
};