Skip to content

Commit

Permalink
Fix output sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Sep 6, 2018
1 parent 18cba9d commit de60640
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
Expand Up @@ -345,16 +345,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
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 @@ -663,4 +663,32 @@ 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'
]);
});
});
});

0 comments on commit de60640

Please sign in to comment.