Skip to content

Commit

Permalink
Fix rehydrate for leading commas
Browse files Browse the repository at this point in the history
  • Loading branch information
dmichon-msft committed Jul 9, 2020
1 parent 66d2a68 commit 2d9783c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion webpack/module-minifier-plugin/src/RehydrateAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export function rehydrateAsset(asset: IAssetInfo, moduleMap: IModuleMap, banner:
const useConcatAtStart: boolean = useConcat && minId > 8;
lastId = useConcat ? minId : 0;
// TODO: Just because we want to use concat elsewhere doesn't mean its optimal to use at the start
let separator: string = useConcatAtStart ? `Array(${minId}).concat([` : `[${enoughCommas.slice(0, minId)}`;
let separator: string = useConcatAtStart ? `Array(${minId}).concat([` : '[';
let concatInserted: boolean = useConcatAtStart;
for (const id of modules) {
const delta: number = id as number - lastId - 1;
Expand Down
43 changes: 43 additions & 0 deletions webpack/module-minifier-plugin/src/test/RehydrateAsset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ modules.set(2, {
extractedComments: [],
module: undefined!
});
for (let i: number = 14; i < 30; i++) {
if (i !== 25) {
modules.set(i, {
source: new RawSource('bozz'),
extractedComments: [],
module: undefined!
});
}
}
modules.set(25, {
source: new RawSource('bang'),
extractedComments: [],
Expand Down Expand Up @@ -74,6 +83,40 @@ describe('rehydrateAsset', () => {
}
});

it('uses a regular array for a couple missing leading elements', () => {
const asset: IAssetInfo = {
source: new RawSource(`<before>${CHUNK_MODULES_TOKEN}<after>`),
modules: [2],
extractedComments: [],
fileName: 'test',
chunk: undefined!
};

const result: string = rehydrateAsset(asset, modules, banner).source();
const expected: string = `/* fnord */\n<before>[,,buzz]<after>`;

if (result !== expected) {
throw new Error(`Expected ${expected} but received ${result}`);
}
});

it('uses a regular array for several missing leading elements', () => {
const asset: IAssetInfo = {
source: new RawSource(`<before>${CHUNK_MODULES_TOKEN}<after>`),
modules: [14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29],
extractedComments: [],
fileName: 'test',
chunk: undefined!
};

const result: string = rehydrateAsset(asset, modules, banner).source();
const expected: string = `/* fnord */\n<before>[,,,,,,,,,,,,,,bozz,bozz,bozz,bozz,bozz,bozz,bozz,bozz,bozz,bozz,bozz,bang,bozz,bozz,bozz,bozz]<after>`;

if (result !== expected) {
throw new Error(`Expected ${expected} but received ${result}`);
}
});

it('uses a concat array for a tight cluster of ids', () => {
const asset: IAssetInfo = {
source: new RawSource(`<before>${CHUNK_MODULES_TOKEN}<after>`),
Expand Down

0 comments on commit 2d9783c

Please sign in to comment.