|
1 |
| -// TS -> ES2015 -> ES5 -> UMD |
2 |
| - |
3 |
| -// tsc -> tsc -p -> rollup |
4 |
| - |
5 | 1 | const { rollup } = require('rollup');
|
6 | 2 | const { spawn } = require('child_process');
|
7 | 3 | const { Observable } = require('rxjs');
|
8 |
| -const { copy } = require('fs-extra'); |
| 4 | +const { copy, rename, readdirSync } = require('fs-extra'); |
9 | 5 |
|
10 | 6 | function spawnObservable(command, args) {
|
11 | 7 | return Observable.create(observer => {
|
12 | 8 | const cmd = spawn(command, args);
|
13 |
| - observer.next(''); |
| 9 | + observer.next(''); // hack to kick things off, not every command will have a stdout |
14 | 10 | cmd.stdout.on('data', (data) => { observer.next(data.toString('utf8')); });
|
15 | 11 | cmd.stderr.on('data', (data) => { observer.error(data.toString('utf8')); });
|
16 | 12 | cmd.on('close', (data) => { observer.complete(); });
|
17 | 13 | });
|
18 | 14 | }
|
19 | 15 |
|
20 |
| -function createUmd(module) { |
21 |
| - return rollup({ |
22 |
| - entry: `${process.cwd()}/dist/packages/${module}/index.js` |
23 |
| - }) |
| 16 | +function createUmd(name) { |
| 17 | + // core module is angularfire2 the rest are angularfire2.feature |
| 18 | + const moduleName = name === 'core' ? 'angularfire2' : `angularfire2.${name}`; |
| 19 | + // core is at the root and the rest are in their own folders |
| 20 | + const entry = name === 'core' ? `${process.cwd()}/dist/packages-dist/index.js` : |
| 21 | + `${process.cwd()}/dist/packages-dist/${name}/index.js`; |
| 22 | + return rollup({ entry }) |
24 | 23 | .then(bundle => {
|
25 | 24 | const result = bundle.generate({
|
26 | 25 | format: 'umd',
|
27 |
| - moduleName: 'angularfire2' |
| 26 | + moduleName |
28 | 27 | });
|
29 | 28 | return bundle.write({
|
30 | 29 | format: 'umd',
|
31 |
| - dest: `${process.cwd()}/dist/bundles/${module}.umd.js`, |
32 |
| - moduleName: 'angularfire2' |
| 30 | + dest: `${process.cwd()}/dist/bundles/${name}.umd.js`, |
| 31 | + moduleName |
33 | 32 | });
|
34 | 33 | });
|
35 | 34 | }
|
36 | 35 |
|
37 |
| -function getSrcPackageFile(module) { |
38 |
| - return `${process.cwd()}/src/${module}/package.json`; |
| 36 | +function getSrcPackageFile(moduleName) { |
| 37 | + const PATHS = { |
| 38 | + core: `${process.cwd()}/src/core/package.json`, |
| 39 | + auth: `${process.cwd()}/src/auth/package.json`, |
| 40 | + database: `${process.cwd()}/src/database/package.json` |
| 41 | + }; |
| 42 | + return PATHS[moduleName]; |
| 43 | +} |
| 44 | + |
| 45 | +function getDestPackageFile(moduleName) { |
| 46 | + const PATHS = { |
| 47 | + core: `${process.cwd()}/dist/packages-dist/package.json`, |
| 48 | + auth: `${process.cwd()}/dist/packages-dist/auth/package.json`, |
| 49 | + database: `${process.cwd()}/dist/packages-dist/database/package.json` |
| 50 | + }; |
| 51 | + return PATHS[moduleName]; |
39 | 52 | }
|
40 | 53 |
|
41 |
| -function getDestPackageFile(module) { |
42 |
| - return `${process.cwd()}/dist/packages/${module}/package.json`; |
| 54 | +function copyPackage(moduleName) { |
| 55 | + return copy(getSrcPackageFile(moduleName), getDestPackageFile(moduleName)); |
43 | 56 | }
|
44 | 57 |
|
| 58 | +// constants for running typescript commands |
| 59 | +const TSC = 'node_modules/.bin/tsc'; |
| 60 | +const NGC = 'node_modules/.bin/ngc'; |
| 61 | +const TSC_ARGS = (name, config = 'build') => [`-p`, `${process.cwd()}/src/${name}/tsconfig-${config}.json`]; |
| 62 | + |
45 | 63 | function buildModule(name) {
|
46 |
| - const module$ = spawnObservable('node_modules/.bin/tsc', [`-p`, `${process.cwd()}/src/${name}/tsconfig-build.json`]); |
47 |
| - // Run tsc -> copy files -> run rollup |
48 |
| - return module$ |
49 |
| - .mergeMap(() => { |
50 |
| - return Observable.from(copy(getSrcPackageFile(name), getDestPackageFile(name))) |
51 |
| - }) |
52 |
| - .mergeMap(() => { |
53 |
| - return Observable.from(createUmd(name)) |
54 |
| - }); |
| 64 | + // Run tsc on module (TS -> ES2015) |
| 65 | + return spawnObservable(TSC, TSC_ARGS(name)) |
| 66 | + // Copy package.json to dist/pacakges/modulename/package.json |
| 67 | + // Run tsc but for ES5 target and ES2015 imports, exports to dist/packages-dist |
| 68 | + // (this is for rollup) |
| 69 | + .concatMap(() => spawnObservable(TSC, TSC_ARGS(name, 'esm'))) |
| 70 | + .concatMap(() => Observable.from(copyPackage(name))) |
| 71 | + .debounceTime(2000) |
| 72 | + // Use rollup to build UMD bundle to dist/bundles` |
| 73 | + .concatMap(() => Observable.from(createUmd(name))); |
55 | 74 | }
|
56 | 75 |
|
57 | 76 | buildModule('core').subscribe(
|
|
0 commit comments