Skip to content

Commit f3ebe2e

Browse files
committed
support default imports, side effect imports and namespace imports
1 parent c2bd3db commit f3ebe2e

File tree

2 files changed

+110
-3
lines changed

2 files changed

+110
-3
lines changed

repo-scripts/size-analysis/bundle-analysis.ts

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ enum Mode {
7474
Local = 'local'
7575
}
7676

77+
enum SpecialImport {
78+
Default = 'default import',
79+
Sizeeffect = 'side effect import',
80+
Namespace = 'namespace import'
81+
}
82+
7783
export async function run({
7884
input,
7985
bundler,
@@ -392,15 +398,23 @@ async function analyzeBundleWithBundler(
392398

393399
function createEntryFileContent(bundleDefinition: BundleDefinition): string {
394400
const contentArray = [];
401+
// cache used symbols. Used to avoid symbol collision when multiple modules export symbols with the same name.
402+
const symbolsCache = new Set<string>();
395403
for (const dep of bundleDefinition.dependencies) {
396404
for (const imp of dep.imports) {
397405
if (typeof imp === 'string') {
398-
contentArray.push(`export {${imp}} from '${dep.packageName}';`);
406+
contentArray.push(
407+
...createImportExport(imp, dep.packageName, symbolsCache)
408+
);
399409
} else {
400-
// Import object
410+
// submodule imports
401411
for (const subImp of imp.imports) {
402412
contentArray.push(
403-
`export {${subImp}} from '${dep.packageName}/${imp.path}';`
413+
...createImportExport(
414+
subImp,
415+
`${dep.packageName}/${imp.path}`,
416+
symbolsCache
417+
)
404418
);
405419
}
406420
}
@@ -410,6 +424,58 @@ function createEntryFileContent(bundleDefinition: BundleDefinition): string {
410424
return contentArray.join('\n');
411425
}
412426

427+
function createImportExport(
428+
symbol: string,
429+
modulePath: string,
430+
symbolsCache: Set<string>
431+
): string[] {
432+
const contentArray = [];
433+
434+
switch (symbol) {
435+
case SpecialImport.Default:
436+
contentArray.push(
437+
`import * as ${createSymbolName('default_import', symbolsCache)}`
438+
);
439+
break;
440+
case SpecialImport.Namespace:
441+
contentArray.push(
442+
`import * as ${createSymbolName('namespace', symbolsCache)}`
443+
);
444+
break;
445+
case SpecialImport.Sizeeffect:
446+
contentArray.push(`import '${modulePath}';`);
447+
break;
448+
default:
449+
// named imports
450+
const nameToUse = createSymbolName(symbol, symbolsCache);
451+
452+
if (nameToUse !== symbol) {
453+
contentArray.push(
454+
`export {${symbol} as ${nameToUse}} from '${modulePath}';`
455+
);
456+
} else {
457+
contentArray.push(`export {${symbol}} from '${modulePath}';`);
458+
}
459+
}
460+
461+
return contentArray;
462+
}
463+
464+
/**
465+
* In case a symbol with the same name is already imported from another module, we need to give this symbol another name
466+
* using "originalname as anothername" syntax, otherwise it returns the original symbol name.
467+
*/
468+
function createSymbolName(symbol: string, symbolsCache: Set<string>): string {
469+
let nameToUse = symbol;
470+
const max = 100;
471+
while (symbolsCache.has(nameToUse)) {
472+
nameToUse = `${symbol}_${Math.floor(Math.random() * max)}`;
473+
}
474+
475+
symbolsCache.add(nameToUse);
476+
return nameToUse;
477+
}
478+
413479
interface BundleAnalysis {
414480
name: string; // the bundle name defined in the bundle definition
415481
results: BundleAnalysisResult[];

repo-scripts/size-analysis/bundle-definition-examples/bundle-definition-1.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[
22
{
33
"name": "test",
4+
"desecription": "",
45
"dependencies": [
56
{
67
"packageName": "@firebase/app",
@@ -19,5 +20,45 @@
1920
]
2021
}
2122
]
23+
},
24+
{
25+
"name": "test2",
26+
"description": "default imports and side effect imports",
27+
"dependencies": [
28+
{
29+
"packageName": "@firebase/app",
30+
"versionOrTag": "latest",
31+
"imports": [
32+
"default import"
33+
]
34+
},
35+
{
36+
"packageName": "@firebase/firestore",
37+
"versionOrTag": "latest",
38+
"imports": [
39+
"side effect import"
40+
]
41+
}
42+
]
43+
},
44+
{
45+
"name": "test3",
46+
"description": "symbols with the same name",
47+
"dependencies": [
48+
{
49+
"packageName": "@firebase/firestore",
50+
"versionOrTag": "exp",
51+
"imports": [
52+
"ref"
53+
]
54+
},
55+
{
56+
"packageName": "@firebase/storage",
57+
"versionOrTag": "exp",
58+
"imports": [
59+
"ref"
60+
]
61+
}
62+
]
2263
}
2364
]

0 commit comments

Comments
 (0)