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

Assign chunk IDs before creating outputBundle chunks #2483

Merged
merged 5 commits into from Oct 2, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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']
}
]);
});
});
});