Skip to content
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

Fix for webpack v5: find dynamic chunks in stats.chunks #105

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
133 changes: 133 additions & 0 deletions __fixtures__/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,139 @@ export const stats = {
publicPath: '/static/'
}

export const statsV5 = {
publicPath: 'auto',
assetsByChunkName: {
main: ['main.js'],
routeA: ['routeA.js'],
routeB: ['routeB.js']
},
chunks: [
{
files: ['main.js'],
id: 'main'
},
{
files: ['routeA.js'],
id: 'routeA'
},
{
files: ['routeB.js'],
id: 'routeB'
},
{
files: ['vendors-node_modules_react-dom_index_js.js'],
id: 'vendors-node_modules_react-dom_index_js'
}
],
modules: [
{
id: 138,
name: './src/entry.js',
chunks: ['main']
},
{
id: 3,
name: './src/routeA.js',
chunks: ['routeA']
},
{
id: 642,
name: './src/routeB.js',
chunks: ['routeB']
},
{
id: 294,
name: './node_modules/react/index.js',
chunks: ['vendors-node_modules_react-dom_index_js']
},
{
id: 935,
name: './node_modules/react-dom/index.js',
chunks: ['vendors-node_modules_react-dom_index_js']
},
{
id: 408,
name: './node_modules/react/cjs/react.production.min.js',
chunks: ['vendors-node_modules_react-dom_index_js']
},
{
id: 448,
name: './node_modules/react-dom/cjs/react-dom.production.min.js',
chunks: ['vendors-node_modules_react-dom_index_js']
},
{
id: 418,
name: './node_modules/object-assign/index.js',
chunks: ['vendors-node_modules_react-dom_index_js']
},
{
id: 840,
name: './node_modules/scheduler/index.js',
chunks: ['vendors-node_modules_react-dom_index_js']
},
{
id: 53,
name: './node_modules/scheduler/cjs/scheduler.production.min.js',
chunks: ['vendors-node_modules_react-dom_index_js']
},
{
id: '',
name: 'webpack/runtime/ensure chunk',
chunks: ['main']
},
{
id: '',
name: 'webpack/runtime/get javascript chunk filename',
chunks: ['main']
},
{
id: '',
name: 'webpack/runtime/global',
chunks: ['main']
},
{
id: '',
name: 'webpack/runtime/hasOwnProperty shorthand',
chunks: ['main']
},
{
id: '',
name: 'webpack/runtime/jsonp chunk loading',
chunks: ['main']
},
{
id: '',
name: 'webpack/runtime/load script',
chunks: ['main']
},
{
id: '',
name: 'webpack/runtime/make namespace object',
chunks: ['main']
},
{
id: '',
name: 'webpack/runtime/publicPath',
chunks: ['main']
}
],
namedChunkGroups: {
main: {
name: 'main',
chunks: ['main']
},
routeA: {
name: 'routeA',
chunks: ['vendors-node_modules_react-dom_index_js', 'routeA']
},
routeB: {
name: 'routeB',
chunks: ['vendors-node_modules_react-dom_index_js', 'routeB']
}
}
}

export const babelFilePaths = [
'./src/Components/Example.js',
'./src/Components/Foo.js',
Expand Down
30 changes: 30 additions & 0 deletions __tests__/__snapshots__/flushChunks.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,36 @@ Object {
}
`;

exports[`flushChunks() called as pure function webpack v5 - chunkNames 1`] = `
Object {
"Css": [Function],
"CssHash": [Function],
"Js": [Function],
"Styles": [Function],
"css": Object {
"toString": [Function],
},
"cssHash": Object {
"toString": [Function],
},
"cssHashRaw": Object {},
"js": Object {
"toString": [Function],
},
"outputPath": undefined,
"publicPath": "auto",
"scripts": Array [
"vendors-node_modules_react-dom_index_js.js",
"routeA.js",
"main.js",
],
"styles": Object {
"toString": [Function],
},
"stylesheets": Array [],
}
`;

exports[`flushChunks() called as pure function webpack: uses default entries when no named chunks provided via opts.before/after 1`] = `
Object {
"Css": [Function],
Expand Down
9 changes: 9 additions & 0 deletions __tests__/flushChunks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {

import {
stats,
statsV5,
rootDir,
babelFilePaths,
webpackModuleIds,
Expand Down Expand Up @@ -77,6 +78,14 @@ describe('flushChunks() called as pure function', () => {

expect(files).toMatchSnapshot()
})

it('webpack v5 - chunkNames', () => {
const files = flushChunks(statsV5, true, {
chunkNames: ['routeA']
}) /*? */

expect(files).toMatchSnapshot()
})
})

describe('flushFiles() called as a pure function', () => {
Expand Down
12 changes: 4 additions & 8 deletions src/flushChunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,10 @@ const filesFromChunks = (
): Files => {
const chunksByID = findChunkById(stats)

const entryToFiles = entry => {
if (typeof entry === 'number') {
return chunksByID[entry]
}
return (
stats.assetsByChunkName[entry] || stats.assetsByChunkName[`${entry}-`]
)
}
const entryToFiles = entry =>
chunksByID[entry] ||
stats.assetsByChunkName[entry] ||
stats.assetsByChunkName[`${entry}-`]

const chunksWithAssets = chunksToResolve({
chunkNames,
Expand Down