I've been tryng how to create separate chunks for components in webpack.
My entry webpack see like this:
entry: [
'./src/components/home/hero/hero.js',
'./src/components/home/hero/hero.css',
'./src/components/layout/navbar.js',
'./src/components/layout/css/navbar.css',
isEnvDevelopment &&
require.resolve('react-dev-utils/webpackHotDevClient'),
// Finally, this is your app's code:
paths.appIndexJs,
// We include the app code last so that if there is a runtime error during
// initialization, it doesn't blow up the WebpackDevServer client, and
// changing JS code would still trigger a refresh.
].filter(Boolean),
//...
splitChunks: {
chunks: "all",
minSize: 0
},
//...
and when i build, another build.js is created name vendor...etc and it's ok, but another css chunck is not created.
I've been watched that do this:
entry: {
a: "./src/a.js",
b: "./src/b.js"
},
output: {
filename: "[name].[chunkhash].bundle.js",
path: __dirname + "/dist"
},
optimization: {
splitChunks: {
chunks: "all",
cacheGroups: {
utilities: {
test: /[\\/]src[\\/]utilities[\\/]/,
minSize: 0
}
}
}
}
would work but my entry is a map and i can't add like array. I've trying change the map for array but the webpack need the previous configuration to build the project.
How i can implemet this with my entry configuration? or exists other way to do this?
Thaks you for your support.
EDIT:
Finally i believe that i can separate the map like this:
entry: {
hero: './src/components/home/hero/hero.js',
navbar: './src/components/layout/navbar.js'
}
[
isEnvDevelopment &&
require.resolve('react-dev-utils/webpackHotDevClient'),
paths.appIndexJs
],
output:{
//..
}
//..
But now with entry, i still don't get separate css and js for this components.
Solution
Finally. In this stackoverflow link show how to separate css and the code is this:
optimization: {
//..
splitChunks: {
cacheGroups: {
hero: {
name: 'hero',
test: /hero\.css$/,
chunks: 'all',
enforce: true
},
navbar: {
name: 'navbar',
test: /navbar\.css$/,
chunks: 'all',
enforce: true
}
}
},
//...
plugins: [
//...
new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: 'chunk-[id].css',
})
//...
]
and create the css (are the chunk names). But how do I replace the css that I created with the css that react create?