forked from angular/angularfire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ts
194 lines (178 loc) · 7.92 KB
/
build.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { spawn } from 'child_process';
import { copy, readFile, writeFile } from 'fs-extra';
import { prettySize } from 'pretty-size';
import { sync as gzipSync } from 'gzip-size';
import { dirname, join } from 'path';
import { keys as tsKeys } from 'ts-transformer-keys';
import firebase from 'firebase/app';
// TODO infer these from the package.json
const MODULES = [
'core', 'analytics', 'auth', 'auth-guard', 'database',
'firestore', 'functions', 'remote-config',
'storage', 'messaging', 'performance'
];
const LAZY_MODULES = ['analytics', 'auth', 'functions', 'messaging', 'remote-config'];
const UMD_NAMES = MODULES.map(m => m === 'core' ? 'angular-fire' : `angular-fire-${m}`);
const ENTRY_NAMES = MODULES.map(m => m === 'core' ? '@angular/fire' : `@angular/fire/${m}`);
function proxyPolyfillCompat() {
const defaultObject = {
analytics: tsKeys<firebase.analytics.Analytics>(),
auth: tsKeys<firebase.auth.Auth>(),
functions: tsKeys<firebase.functions.Functions>(),
messaging: tsKeys<firebase.messaging.Messaging>(),
performance: tsKeys<firebase.performance.Performance>(),
'remote-config': tsKeys<firebase.remoteConfig.RemoteConfig>(),
};
return Promise.all(Object.keys(defaultObject).map(module =>
writeFile(`./src/${module}/base.ts`, `export const proxyPolyfillCompat = {
${defaultObject[module].map(it => ` ${it}: null,`).join('\n')}
};\n`)
));
}
const src = (...args: string[]) => join(process.cwd(), 'src', ...args);
const dest = (...args: string[]) => join(process.cwd(), 'dist', 'packages-dist', ...args);
const rootPackage = import(join(process.cwd(), 'package.json'));
async function replacePackageCoreVersion() {
const root = await rootPackage;
const replace = require('replace-in-file');
return replace({
files: dest('**', '*.js'),
from: 'ANGULARFIRE2_VERSION',
to: root.version
});
}
async function replacePackageJsonVersions() {
const path = dest('package.json');
const root = await rootPackage;
const pkg = await import(path);
Object.keys(pkg.peerDependencies).forEach(peer => {
pkg.peerDependencies[peer] = root.dependencies[peer];
});
pkg.version = root.version;
return writeFile(path, JSON.stringify(pkg, null, 2));
}
async function replaceSchematicVersions() {
const root = await rootPackage;
const path = dest('schematics', 'versions.json');
const dependencies = await import(path);
Object.keys(dependencies.default).forEach(name => {
dependencies.default[name].version = root.dependencies[name] || root.devDependencies[name];
});
Object.keys(dependencies.firebaseFunctions).forEach(name => {
dependencies.firebaseFunctions[name].version = root.dependencies[name] || root.devDependencies[name];
});
return writeFile(path, JSON.stringify(dependencies, null, 2));
}
function spawnPromise(command: string, args: string[]) {
return new Promise(resolve => spawn(command, args, { stdio: 'inherit' }).on('close', resolve));
}
async function compileSchematics() {
await spawnPromise(`npx`, ['tsc', '-p', src('schematics', 'tsconfig.json')]);
return Promise.all([
copy(src('core', 'builders.json'), dest('builders.json')),
copy(src('core', 'collection.json'), dest('collection.json')),
copy(src('schematics', 'deploy', 'schema.json'), dest('schematics', 'deploy', 'schema.json')),
replaceSchematicVersions()
]);
}
async function measure(module: string) {
const path = dest('bundles', `${module}.umd.min.js`);
const file = await readFile(path);
const gzip = prettySize(gzipSync(file), true);
const size = prettySize(file.byteLength, true);
return { size, gzip };
}
async function fixImportForLazyModules() {
await Promise.all(LAZY_MODULES.map(async module => {
const packageJson = JSON.parse((await readFile(dest(module, 'package.json'))).toString());
const entries = Array.from(new Set(Object.values(packageJson).filter(v => typeof v === 'string' && v.endsWith('.js')))) as string[];
// TODO don't hardcode esm2015 here, perhaps we should scan all the entry directories
// e.g, if ng-packagr starts building other non-flattened entries we'll lose the dynamic import
entries.push(`../esm2015/${module}/public_api.js`); // the import isn't pulled into the ESM public_api
await Promise.all(entries.map(async path => {
const source = (await readFile(dest(module, path))).toString();
let newSource: string;
if (path.endsWith('.umd.js')) {
// in the UMD for lazy modules replace the dyanamic import
newSource = source.replace(`import('firebase/${module}')`, 'rxjs.of(undefined)');
} else {
// in everything else get rid of the global side-effect import
newSource = source.replace(new RegExp(`^import 'firebase/${module}'.+$`, 'gm'), '');
}
await writeFile(dest(module, path), newSource);
}));
}));
}
async function buildLibrary() {
await proxyPolyfillCompat();
await spawnPromise('npx', ['ng', 'build']);
await Promise.all([
copy(join(process.cwd(), '.npmignore'), dest('.npmignore')),
copy(join(process.cwd(), 'README.md'), dest('README.md')),
copy(join(process.cwd(), 'docs'), dest('docs')),
compileSchematics(),
replacePackageJsonVersions(),
replacePackageCoreVersion(),
fixImportForLazyModules(),
]);
}
function measureLibrary() {
return Promise.all(UMD_NAMES.map(measure));
}
async function buildDocs() {
// INVESTIGATE json to stdout rather than FS?
await Promise.all(MODULES.map(module => spawnPromise('npx', ['typedoc', `./src/${module}`, '--json', `./dist/typedocs/${module}.json`])));
const entries = await Promise.all(MODULES.map(async (module) => {
const buffer = await readFile(`./dist/typedocs/${module}.json`);
const typedoc = JSON.parse(buffer.toString());
// TODO infer the entryPoint from the package.json
const entryPoint = typedoc.children.find((c: any) => c.name === '"public_api"');
const allChildren = [].concat(...typedoc.children.map(child =>
// TODO chop out the working directory and filename
child.children ? child.children.map(c => ({ ...c, path: dirname(child.originalName.split(process.cwd())[1]) })) : []
));
return entryPoint.children
.filter(c => c.name[0] !== 'ɵ' && c.name[0] !== '_' /* private */)
.map(child => ({ ...allChildren.find(c => child.target === c.id) }))
.reduce((acc, child) => ({ ...acc, [encodeURIComponent(child.name)]: child }), {});
}));
const root = await rootPackage;
const pipes = ['MonoTypeOperatorFunction', 'OperatorFunction', 'AuthPipe', 'UnaryFunction'];
const tocType = child => {
const decorators: string[] = child.decorators && child.decorators.map(d => d.name) || [];
if (decorators.includes('NgModule')) {
return 'NgModule';
} else if (child.kindString === 'Type alias') {
return 'Type alias';
} else if (child.kindString === 'Variable' && child.defaultValue && child.defaultValue.startsWith('new InjectionToken')) {
return 'InjectionToken';
} else if (child.type) {
return pipes.includes(child.type.name) ? 'Pipe' : child.type.name;
} else if (child.signatures && child.signatures[0] && child.signatures[0].type && pipes.includes(child.signatures[0].type.name)) {
return 'Pipe';
} else {
return child.kindString;
}
};
const tableOfContents = entries.reduce((acc, entry, index) =>
({
...acc, [MODULES[index]]: {
name: ENTRY_NAMES[index],
exports: Object.keys(entry).reduce((acc, key) => ({ ...acc, [key]: tocType(entry[key]) }), {})
}
}),
{}
);
const afdoc = entries.reduce((acc, entry, index) => ({ ...acc, [MODULES[index]]: entry }), { table_of_contents: tableOfContents });
return writeFile(`./api-${root.version}.json`, JSON.stringify(afdoc, null, 2));
}
Promise.all([
buildDocs(),
buildLibrary()
]).then(measureLibrary).then(stats =>
console.log(`
Package Size Gzipped
------------------------------------
${stats.map((s, i) => [MODULES[i].padEnd(16), s.size.padEnd(8), s.gzip].join('')).join('\n')}`
)
);