Skip to content

Commit

Permalink
Fix output sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Oct 3, 2018
1 parent d2a1283 commit d42cf0c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
25 changes: 20 additions & 5 deletions src/rollup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,16 +360,31 @@ export default function rollup(rawInputOptions: GenericConfigObject): Promise<Ro
}
}

enum SortingFileType {
ENTRY_CHUNK = 0,
SECONDARY_CHUNK = 1,
ASSET = 2
}

function getSortingFileType(file: OutputAsset | OutputChunk): SortingFileType {
if ((<OutputAsset>file).isAsset) {
return SortingFileType.ASSET;
}
if ((<OutputChunk>file).isEntry) {
return SortingFileType.ENTRY_CHUNK;
}
return SortingFileType.SECONDARY_CHUNK;
}

function createOutput(outputBundle: Record<string, OutputChunk | OutputAsset>): RollupOutput {
return {
output: Object.keys(outputBundle)
.map(fileName => outputBundle[fileName])
.sort((outputFileA, outputFileB) => {
// sort by entry chunks, shared chunks, then assets
if (isOutputAsset(outputFileA)) return isOutputAsset(outputFileB) ? 0 : 1;
if (isOutputAsset(outputFileB)) return -1;
if (outputFileA.isEntry) return outputFileB.isEntry ? 0 : -1;
outputFileB.isEntry ? 1 : 0;
const fileTypeA = getSortingFileType(outputFileA);
const fileTypeB = getSortingFileType(outputFileB);
if (fileTypeA === fileTypeB) return 0;
return fileTypeA < fileTypeB ? -1 : 1;
})
};
}
Expand Down
30 changes: 29 additions & 1 deletion test/misc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ describe('sanity checks', () => {
});
});

it.only('does not throw when using dynamic imports with the "file" option and "inlineDynamicImports"', () => {
it('does not throw when using dynamic imports with the "file" option and "inlineDynamicImports"', () => {
const warnings = [];

return rollup
Expand Down Expand Up @@ -664,6 +664,34 @@ describe('misc', () => {
});
});

it('sorts chunks in the output', () => {
const warnings = [];

return rollup
.rollup({
input: ['main1', 'main2'],
plugins: [
loader({
main1: 'import "dep";console.log("main1");',
main2: 'import "dep";console.log("main2");',
dep: 'console.log("dep");import("dyndep");',
dyndep: 'console.log("dyndep");'
})
],
onwarn: warning => warnings.push(warning)
})
.then(bundle => bundle.generate({ format: 'es' }))
.then(({ output }) => {
assert.equal(warnings.length, 0);
assert.deepEqual(output.map(({ fileName }) => fileName), [
'main1.js',
'main2.js',
'dyndep.js',
'chunk-7e6a340f.js'
]);
});
});

it('ignores falsy plugins', () => {
return rollup.rollup({
input: 'x',
Expand Down

0 comments on commit d42cf0c

Please sign in to comment.