Skip to content

Commit 6bb0796

Browse files
committed
rollup seems to be working
1 parent d974b64 commit 6bb0796

File tree

11 files changed

+210
-435
lines changed

11 files changed

+210
-435
lines changed

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,13 @@
137137
"typescript": "3.8.3",
138138
"watch": "1.0.2",
139139
"webpack": "4.43.0",
140-
"yargs": "15.3.1"
140+
"yargs": "15.3.1",
141+
"@rollup/plugin-commonjs": "13.0.0",
142+
"@rollup/plugin-node-resolve": "8.1.0"
141143
},
142144
"husky": {
143145
"hooks": {
144146
"pre-commit": "node tools/gitHooks/precommit.js"
145147
}
146148
}
147-
}
149+
}

packages-exp/dummy-exp/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
},
3939
"license": "Apache-2.0",
4040
"devDependencies": {
41+
"@rollup/plugin-alias": "3.1.1",
4142
"rollup": "1.32.1",
4243
"rollup-plugin-json": "4.0.0",
4344
"rollup-plugin-replace": "2.2.0",

packages-exp/dummy-exp/src/bar.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
* limitations under the License.
1616
*/
1717
import { version } from '../../../packages/firebase/package.json';
18+
import { LogLevel } from '@firebase/logger';
1819
export const SDK_VERSION = version;
19-
export function bar(): string {
20+
export function bar(): LogLevel {
2021
return bar1();
2122
}
2223

23-
export function bar1(): string {
24-
return 'bar1';
24+
export function bar1(): LogLevel {
25+
return LogLevel.DEBUG;
2526
}
2627

2728
export function bar2(): string {
28-
return bar1() + bar();
29+
return bar1().toLocaleString() + bar();
2930
}

packages-exp/dummy-exp/src/far.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ export function far1(version: string): string {
2424
console.log(version);
2525
return version;
2626
}
27+
export function far3(version: string): string {
28+
console.log(version);
29+
return version;
30+
}

packages-exp/dummy-exp/src/foo.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17+
export const fooo: string = 'foooo';
18+
export const f2: string = 'foo2';
19+
export const f1: string = 'foo1';
1720

1821
export function foo(): string {
1922
return foo1();
2023
}
2124

2225
export function foo1(): string {
23-
return 'foo';
26+
return f1;
2427
}
2528
export function foo2(): string {
26-
return 'foo2';
29+
return f1 + f2;
2730
}
2831
export class Apple {}
2932

@@ -35,5 +38,3 @@ export enum BUG {
3538
ERROR = 4,
3639
SILENT = 5
3740
}
38-
export const fooo: string = 'foooo';
39-
export const f1: string = 'f1';

packages-exp/dummy-exp/src/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
*/
1717

1818
export * from './bar';
19-
export { foo1, fooo, Apple, BUG } from './foo';
20-
export { foo2 as foo3 } from './foo';
21-
import { SDK_VERSION } from './bar';
22-
import { far } from './far';
19+
20+
export * from './foo';
21+
// import { SDK_VERSION } from './bar';
22+
// import { far } from './far';
23+
export { far1, far, far3 } from './far';
2324
export const VAR = 'variable';
2425
export let var2: string;
2526
export let var3 = 'var3';
@@ -31,7 +32,7 @@ export function boo(): LogLevel {
3132
return LogLevel.DEBUG;
3233
}
3334

34-
export declare enum LogLevel1 {
35+
export enum LogLevel1 {
3536
DEBUG = 0,
3637
VERBOSE = 1,
3738
INFO = 2,
@@ -55,6 +56,6 @@ export type LogLevel2 =
5556
| 'info'
5657
| 'verbose';
5758

58-
far(SDK_VERSION);
59+
// far(SDK_VERSION);
5960

6061
export { LogLevel } from '@firebase/logger';

scripts/exp/modular-export-binary-size-analysis/analysis-helper.ts

Lines changed: 80 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ import * as ts from 'typescript';
2424
import { TYPINGS } from './analysis';
2525
import { request } from 'express';
2626

27+
import resolve from 'rollup-plugin-node-resolve';
28+
import commonjs from 'rollup-plugin-commonjs';
29+
2730
/** Contains a list of members by type. */
2831
export type MemberList = {
2932
classes: string[];
@@ -46,12 +49,12 @@ export type ExportData = { dependencies: MemberList; sizeInBytes: number };
4649
export async function extractDependencies(
4750
exportName: string,
4851
jsBundle: string,
49-
allModuleLocations: string[]
52+
map: Map<string, string>
5053
): Promise<MemberList> {
5154
const { dependencies } = await extractDependenciesAndSize(
5255
exportName,
5356
jsBundle,
54-
allModuleLocations
57+
map
5558
);
5659
return dependencies;
5760
}
@@ -63,27 +66,34 @@ export async function extractDependencies(
6366
export async function extractDependenciesAndSize(
6467
exportName: string,
6568
jsBundle: string,
66-
allModuleLocations: string[]
69+
map: Map<string, string>
6770
): Promise<ExportData> {
6871
const input = tmp.fileSync().name + '.js';
6972
const output = tmp.fileSync().name + '.js';
7073

7174
const beforeContent = `export { ${exportName} } from '${path.resolve(
7275
jsBundle
7376
)}';`;
77+
//console.log(beforeContent);
7478
fs.writeFileSync(input, beforeContent);
7579

7680
// Run Rollup on the JavaScript above to produce a tree-shaken build
7781
const bundle = await rollup.rollup({
7882
input,
79-
external: id => id.startsWith('@firebase-exp/')
83+
plugins: [
84+
resolve({
85+
mainFields: ['esm2017', 'module', 'main']
86+
}),
87+
commonjs()
88+
]
8089
});
8190
await bundle.write({ file: output, format: 'es' });
8291

83-
const dependencies = extractDeclarations(allModuleLocations, output);
92+
const dependencies = extractDeclarations(output, map);
8493

8594
// Extract size of minified build
8695
const afterContent = fs.readFileSync(output, 'utf-8');
96+
//console.log(afterContent);
8797
const { code } = terser.minify(afterContent, {
8898
output: {
8999
comments: false
@@ -102,8 +112,8 @@ export async function extractDependenciesAndSize(
102112
* compiler API.
103113
*/
104114
export function extractDeclarations(
105-
allModulesLocation: string[],
106-
jsFile: string
115+
jsFile: string,
116+
map?: Map<string, string>
107117
): MemberList {
108118
const program = ts.createProgram([jsFile], { allowJs: true });
109119
const checker = program.getTypeChecker();
@@ -113,7 +123,7 @@ export function extractDeclarations(
113123
throw new Error('Failed to parse file: ' + jsFile);
114124
}
115125

116-
const declarations: MemberList = {
126+
let declarations: MemberList = {
117127
functions: [],
118128
classes: [],
119129
variables: [],
@@ -150,10 +160,7 @@ export function extractDeclarations(
150160
const symbol = checker.getSymbolAtLocation(node.moduleSpecifier);
151161
const reExportFullPath = symbol.valueDeclaration.getSourceFile()
152162
.fileName;
153-
const reExportsMember = extractDeclarations(
154-
allModulesLocation,
155-
reExportFullPath
156-
);
163+
const reExportsMember = extractDeclarations(reExportFullPath);
157164

158165
// named exports
159166
if (node.exportClause && ts.isNamedExports(node.exportClause)) {
@@ -184,6 +191,12 @@ export function extractDeclarations(
184191
}
185192
}
186193
});
194+
195+
if (map) {
196+
declarations = mapSymbolToType(map, declarations);
197+
declarations = dedup(declarations);
198+
}
199+
187200
// Sort to ensure stable output
188201
declarations.functions.sort();
189202
declarations.classes.sort();
@@ -193,6 +206,61 @@ export function extractDeclarations(
193206
return declarations;
194207
}
195208

209+
function dedup(memberList: MemberList): MemberList {
210+
const classesSet: Set<string> = new Set(memberList.classes);
211+
memberList.classes = Array.from(classesSet);
212+
213+
const functionsSet: Set<string> = new Set(memberList.functions);
214+
memberList.functions = Array.from(functionsSet);
215+
216+
const enumsSet: Set<string> = new Set(memberList.enums);
217+
memberList.enums = Array.from(enumsSet);
218+
219+
const variablesSet: Set<string> = new Set(memberList.variables);
220+
memberList.variables = Array.from(variablesSet);
221+
222+
return memberList;
223+
}
224+
function mapSymbolToType(
225+
map: Map<string, string>,
226+
memberList: MemberList
227+
): MemberList {
228+
const newMemberList: MemberList = {
229+
functions: [],
230+
classes: [],
231+
variables: [],
232+
enums: []
233+
};
234+
memberList.classes.forEach(element => {
235+
if (map.has(element)) {
236+
newMemberList[map.get(element)].push(element);
237+
} else {
238+
newMemberList.classes.push(element);
239+
}
240+
});
241+
memberList.variables.forEach(element => {
242+
if (map.has(element)) {
243+
newMemberList[map.get(element)].push(element);
244+
} else {
245+
newMemberList.variables.push(element);
246+
}
247+
});
248+
memberList.enums.forEach(element => {
249+
if (map.has(element)) {
250+
newMemberList[map.get(element)].push(element);
251+
} else {
252+
newMemberList.enums.push(element);
253+
}
254+
});
255+
memberList.functions.forEach(element => {
256+
if (map.has(element)) {
257+
newMemberList[map.get(element)].push(element);
258+
} else {
259+
newMemberList.functions.push(element);
260+
}
261+
});
262+
return newMemberList;
263+
}
196264
function isReExported(symbol: string, reExportedSymbols: string[]): boolean {
197265
return reExportedSymbols.includes(symbol);
198266
}

0 commit comments

Comments
 (0)