Skip to content

Commit

Permalink
Assign chunk IDs before creating outputBundle chunks (#2483)
Browse files Browse the repository at this point in the history
* add failing test for #2461

* update test so that it actually fails

* assign chunk IDs before creating outputBundle chunks

* deconflict chunk names
  • Loading branch information
Rich-Harris authored and lukastaegert committed Oct 2, 2018
1 parent 8a8721e commit ad21dd8
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 10 deletions.
18 changes: 11 additions & 7 deletions src/rollup/index.ts
Expand Up @@ -282,11 +282,9 @@ export default function rollup(
}

// name all chunks
const usedIds: Record<string, true> = {};
for (let i = 0; i < chunks.length; i++) {
const chunk = chunks[i];
const imports = chunk.getImportIds();
const exports = chunk.getExportNames();
const modules = chunk.renderedModules;

if (chunk === singleChunk) {
singleChunk.id = basename(
Expand All @@ -306,15 +304,21 @@ export default function rollup(
pattern = outputOptions.chunkFileNames || '[name]-[hash].js';
patternName = 'output.chunkFileNames';
}
chunk.generateId(pattern, patternName, addons, outputOptions, outputBundle);
chunk.generateId(pattern, patternName, addons, outputOptions, usedIds);
usedIds[chunk.id] = true;
}
}

// assign to outputBundle
for (let i = 0; i < chunks.length; i++) {
const chunk = chunks[i];

outputBundle[chunk.id] = {
fileName: chunk.id,
isEntry: chunk.entryModule !== undefined,
imports,
exports,
modules,
imports: chunk.getImportIds(),
exports: chunk.getExportNames(),
modules: chunk.renderedModules,
code: undefined,
map: undefined
};
Expand Down
64 changes: 61 additions & 3 deletions test/hooks/index.js
Expand Up @@ -829,13 +829,13 @@ module.exports = input;
plugins: [
loader({ input: `alert('hello')` }),
{
name: name,
name,
buildStart() {
this.cache.set('asdf', 'asdf');
}
},
{
name: name,
name,
buildStart() {
this.cache.set('asdf', 'asdf');
}
Expand All @@ -845,7 +845,7 @@ module.exports = input;
.catch(err => {
assert.equal(err.code, 'PLUGIN_ERROR');
assert.equal(err.pluginCode, 'DUPLICATE_PLUGIN_NAME');
assert.equal(err.message.includes(name), true)
assert.equal(err.message.includes(name), true);
});
});
});
Expand Down Expand Up @@ -1073,4 +1073,62 @@ module.exports = input;
assert.equal(renderErrorCount, 1, 'renderError count');
});
});

it('assigns chunk IDs before creating outputBundle chunks', () => {
const chunks = [];
return rollup
.rollup({
input: 'input',
experimentalCodeSplitting: true,
plugins: [
loader({
input: `export default [import('a'), import('b')];`,
a: `import d from 'd'; import c from 'c'; export default () => c();`,
b: `import c from 'c'; export default () => c();`,
c: `export default () => console.log('c');`,
d: `export default {};`
}),
{
renderChunk(code, chunk, options) {
chunks.push({
fileName: chunk.fileName,
imports: chunk.imports,
modules: Object.keys(chunk.modules)
});
}
}
]
})
.then(bundle =>
bundle.generate({
entryFileNames: '[name].js',
chunkFileNames: '[name].js',
format: 'esm'
})
)
.then(() => {
assert.deepEqual(chunks, [
{
fileName: 'input.js',
imports: [],
modules: ['input']
},
{
fileName: 'a.js',
imports: ['chunk.js'],
modules: ['d', 'a']
},
{
fileName: 'chunk.js',
imports: [],
modules: ['c']
},
{
fileName: 'b.js',
imports: ['chunk.js'],
modules: ['b']
}
]);
});
});
});

0 comments on commit ad21dd8

Please sign in to comment.