Skip to content

docs: update splitChunks info #630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -710,11 +710,16 @@ const MiniCssExtractPlugin = require('mini-css-extract-plugin');
function recursiveIssuer(m) {
if (m.issuer) {
return recursiveIssuer(m.issuer);
} else if (m.name) {
return m.name;
} else {
return false;
}

const chunks = m.getChunks();
// For webpack@4 chunks = m._chunks

for (const chunk of chunks) {
return chunk.name;
}

return false;
}

module.exports = {
Expand All @@ -726,14 +731,14 @@ module.exports = {
splitChunks: {
cacheGroups: {
fooStyles: {
name: 'foo',
name: 'styles_foo',
test: (m, c, entry = 'foo') =>
m.constructor.name === 'CssModule' && recursiveIssuer(m) === entry,
chunks: 'all',
enforce: true,
},
barStyles: {
name: 'bar',
name: 'styles_bar',
test: (m, c, entry = 'bar') =>
m.constructor.name === 'CssModule' && recursiveIssuer(m) === entry,
chunks: 'all',
Expand Down
3 changes: 3 additions & 0 deletions test/cases/split-chunks-recursiveIssuer/a.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.class_a {
background: red;
}
3 changes: 3 additions & 0 deletions test/cases/split-chunks-recursiveIssuer/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './a.css';

import(/* webpackChunkName: "comp1" */ './components/comp1');
3 changes: 3 additions & 0 deletions test/cases/split-chunks-recursiveIssuer/b.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.class_b {
background: red;
}
3 changes: 3 additions & 0 deletions test/cases/split-chunks-recursiveIssuer/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './b.css';

import(/* webpackChunkName: "comp2" */ './components/comp2');
3 changes: 3 additions & 0 deletions test/cases/split-chunks-recursiveIssuer/components/comp1.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background-color: yellow;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './comp1.css';
3 changes: 3 additions & 0 deletions test/cases/split-chunks-recursiveIssuer/components/comp2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background-color: green;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './comp2.css';
8 changes: 8 additions & 0 deletions test/cases/split-chunks-recursiveIssuer/expected/styles_a.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.class_a {
background: red;
}

body {
background-color: yellow;
}

8 changes: 8 additions & 0 deletions test/cases/split-chunks-recursiveIssuer/expected/styles_b.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.class_b {
background: red;
}

body {
background-color: green;
}

54 changes: 54 additions & 0 deletions test/cases/split-chunks-recursiveIssuer/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { version as webpackVersion } from 'webpack';

import Self from '../../../src';

function recursiveIssuer(m) {
if (m.issuer) {
return recursiveIssuer(m.issuer);
}

// eslint-disable-next-line no-underscore-dangle
const chunks = webpackVersion === '4' ? m._chunks : m.getChunks();

for (const chunk of chunks) {
return chunk.name;
}

return false;
}

module.exports = {
entry: {
a: './a.js',
b: './b.js',
},
module: {
rules: [
{
test: /\.css$/,
use: [Self.loader, 'css-loader'],
},
],
},
optimization: {
splitChunks: {
cacheGroups: {
aStyles: {
name: 'styles_a',
test: (m, c, entry = 'a') =>
m.constructor.name === 'CssModule' && recursiveIssuer(m) === entry,
chunks: 'all',
enforce: true,
},
bStyles: {
name: 'styles_b',
test: (m, c, entry = 'b') =>
m.constructor.name === 'CssModule' && recursiveIssuer(m) === entry,
chunks: 'all',
enforce: true,
},
},
},
},
plugins: [new Self()],
};