I have a Webpack setting to build different bundles and put all the CSS used by the bundle in a CSS file.
The problem is that Webpack is putting all the CSS of all the bundles together.
Mi project is configured in the following way.
webpack.config.js
module.exports = {
entry: {
'bundle-a': './src/bundle-a.ts',
'bundle-b': './src/bundle-b.ts'
},
...
plugins: [
new MiniCssExtractPlugin(),
],
module: {
rules: [
{
test: /\.ts(x?)$/,
include: path.resolve(__dirname, 'src'),
use: ['ts-loader']
},
{
test: /\.css$/,
include: path.resolve(__dirname, 'src'),
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
modules: 'local'
}
}
]
}
]
}
};
src/bundle-a.ts
import style from './bundle-a.css';
...
src/bundle-a.css
.class-bundle-a {
...
}
src/bundle-b.js
import style from './bundle-b.css';
...
src/bundle-b.css
.class-bundle-b {
...
}
When I build the project I get the following CSS files.
dist/bundle-a.css
.class-bundle-a {
...
}
.class-bundle-b {
...
}
dist/bundle-b.css
.class-bundle-a {
...
}
.class-bundle-b {
...
}
I expect that the dist/bundle-a.css
only have the CSS imported in the src/bundle-a.ts
, but also I am getting the CSS imported by the src/bundle-b.ts
, and the same pass with the src/bundle-b.css
. The same happens if I use style-loader.
I want to know if this is the expected behavior or I have some error in my code.
You can add an additional rule. It's a bit redundant but works for me: