Skip to content

Commit 55631cc

Browse files
committed
Fixing a bug where entrypoints.json contained bad names in production
mode
1 parent cf0192f commit 55631cc

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

lib/webpack/entry-files-manifest-plugin.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ function EntryFilesManifestPlugin(manifestFilename, entryNamesToSkip) {
2121
* @param {Entrypoint} entryPoint
2222
* @returns {object}
2323
*/
24-
function extractFiles(entryPoint) {
24+
function extractChunkIds(entryPoint) {
2525
const files = {
26-
jsFiles: [],
27-
cssFiles: [],
26+
jsChunkIds: [],
27+
cssChunkIds: [],
2828
};
2929

3030
for (let chunk of entryPoint.chunks) {
3131
for (let filename of chunk.files) {
3232
if (/\.js$/.test(filename)) {
3333
// use the chunk id because we do not want the versioned filename
34-
files.jsFiles.push(`${chunk.id}.js`);
34+
files.jsChunkIds.push(chunk.id);
3535
} else if (/\.css$/.test(filename)) {
36-
files.cssFiles.push(`${chunk.id}.css`);
36+
files.cssChunkIds.push(chunk.id);
3737
} else {
3838
logger.debug(`Unable to determine file type for entry ${filename}. This is possibly a bug, but will not impact your setup unless you use the entrypoints.json file. To be super awesome, please report this as an issue.`);
3939
}
@@ -43,6 +43,16 @@ function extractFiles(entryPoint) {
4343
return files;
4444
}
4545

46+
function getChunkNameFromId(allChunks, chunkId) {
47+
return allChunks.find(chunk => {
48+
return chunk.id === chunkId;
49+
}).name;
50+
}
51+
52+
function convertChunkNamesToFilenames(chunkNames, extension) {
53+
return chunkNames.map(chunkName => `${chunkName}.${extension}`);
54+
}
55+
4656
EntryFilesManifestPlugin.prototype.apply = function(compiler) {
4757
const done = (stats) => {
4858
const entrypoints = {};
@@ -51,7 +61,14 @@ EntryFilesManifestPlugin.prototype.apply = function(compiler) {
5161
return;
5262
}
5363

54-
const { cssFiles, jsFiles } = extractFiles(entry);
64+
const { cssChunkIds, jsChunkIds } = extractChunkIds(entry);
65+
66+
// look up the original chunk name by id
67+
const cssChunkNames = cssChunkIds.map(chunkId => getChunkNameFromId(stats.compilation.chunks, chunkId));
68+
const jsChunkNames = jsChunkIds.map(chunkId => getChunkNameFromId(stats.compilation.chunks, chunkId));
69+
const cssFiles = convertChunkNamesToFilenames(cssChunkNames, 'css');
70+
const jsFiles = convertChunkNamesToFilenames(jsChunkNames, 'js');
71+
5572
entrypoints[entryName] = {
5673
js: jsFiles,
5774
css: cssFiles,

test/functional.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,36 @@ module.exports = {
13021302
done();
13031303
});
13041304
});
1305+
1306+
it('Use splitChunks in production mode', (done) => {
1307+
const config = createWebpackConfig('web/build', 'production');
1308+
config.addEntry('main', ['./css/roboto_font.css', './js/no_require', 'vue']);
1309+
config.addEntry('other', ['./css/roboto_font.css', 'vue']);
1310+
config.setPublicPath('/build');
1311+
config.splitEntryChunks();
1312+
config.configureSplitChunks((splitChunks) => {
1313+
splitChunks.minSize = 0;
1314+
});
1315+
1316+
testSetup.runWebpack(config, (webpackAssert) => {
1317+
// in production, we hash the chunk names to avoid exposing any extra details
1318+
webpackAssert.assertOutputJsonFileMatches('entrypoints.json', {
1319+
main: {
1320+
js: ['vendors~cc515e6e.js', 'default~cc515e6e.js', 'main.js'],
1321+
css: ['default~cc515e6e.css']
1322+
},
1323+
other: {
1324+
js: ['vendors~cc515e6e.js', 'default~cc515e6e.js', 'other.js'],
1325+
css: ['default~cc515e6e.css']
1326+
},
1327+
});
1328+
1329+
// make split chunks are correct in manifest
1330+
webpackAssert.assertManifestKeyExists('build/vendors~cc515e6e.js');
1331+
1332+
done();
1333+
});
1334+
});
13051335
});
13061336
});
13071337
});

0 commit comments

Comments
 (0)