forked from SnakeByteDevelopment/angulartics2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ts
256 lines (231 loc) · 7.53 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* tslint:disable:import-blacklist */
// build based on
// https://github.com/angular/angularfire2/blob/master/tools/build.js
import { spawn } from 'child_process';
import * as copyfiles from 'copy';
import { copy } from 'fs-extra';
import { rollup } from 'rollup';
import * as filesize from 'rollup-plugin-filesize';
import * as sourcemaps from 'rollup-plugin-sourcemaps';
import { Observable } from 'rxjs';
const copyAll: ((s: string, s1: string) => any) = Observable.bindCallback(
copyfiles,
);
const core = ['core', 'uiroutermodule', 'routerlessmodule'];
// Rollup globals
const MODULE_NAMES = {
core: 'ngx-analytics',
uiroutermodule: 'ngx-analytics.uiroutermodule',
routerlessmodule: 'ngx-analytics.routerlessmodule',
adobeanalytics: 'ngx-analytics.adobeanalytics',
appinsights: 'ngx-analytics.appinsights',
baidu: 'ngx-analytics.baidu',
facebook: 'ngx-analytics.facebook',
ga: 'ngx-analytics.ga',
'ga-enhanced-ecom': 'ngx-analytics.ga-enhanced-ecom',
gtm: 'ngx-analytics.gtm',
hubspot: 'ngx-analytics.hubspot',
kissmetrics: 'ngx-analytics.kissmetrics',
mixpanel: 'ngx-analytics.mixpanel',
piwik: 'ngx-analytics.piwik',
segment: 'ngx-analytics.segment',
intercom: 'ngx-analytics.intercom',
woopra: 'ngx-analytics.woopra',
clicky: 'ngx-analytics.clicky',
amplitude: 'ngx-analytics.amplitude',
};
const GLOBALS = {
'tslib': 'tslib',
'@angular/core': 'ng.core',
'@angular/common': 'ng.common',
'@angular/forms': 'ng.forms',
'@angular/http': 'ng.http',
'@angular/router': 'ng.router',
'@angular/platform-browser': 'ng.platformBrowser',
'@angular/platform-server': 'ng.platformServer',
'@angular/platform-browser-dynamic': 'ng.platformBrowserDynamic',
'@uirouter/core': '@uirouter/core',
'rxjs/Observable': 'Rx',
'rxjs/Subject': 'Rx',
'rxjs/Observer': 'Rx',
'rxjs/Subscription': 'Rx',
'rxjs/ReplaySubject': 'Rx',
'rxjs/BehaviorSubject': 'Rx',
'rxjs/operators/filter': 'Rx.operators',
'rxjs/operators/map': 'Rx.operators',
'rxjs/observable/merge': 'Rx.Observable',
'rxjs/observable/of': 'Rx.Observable',
'ngx-analytics': MODULE_NAMES['core'],
};
function createEntry(name): string {
if (name === 'core') {
return `${process.cwd()}/dist/es5/index.js`;
}
return `${process.cwd()}/dist/${name}/es5/index.js`;
}
// Constants for running typescript commands
const NGC = './node_modules/.bin/ngc';
const TSC_ARGS = (type: string, name: string, config= 'build') => {
if (!type || type === 'routerlessmodule' || type === 'uiroutermodule') {
return ['-p', `${process.cwd()}/src/lib/${name}/tsconfig-${config}.json`];
}
return ['-p', `${process.cwd()}/src/lib/${type}/${name}/tsconfig-${config}.json`];
};
/**
* Create an Observable of a spawned child process.
*/
function spawnObservable(command: string, args: string[]) {
return Observable.create(observer => {
const cmd = spawn(command, args);
observer.next(''); // hack to kick things off, not every command will have a stdout
cmd.stdout.on('data', (data) => { observer.next(data.toString()); });
cmd.stderr.on('data', (data) => { observer.error(data.toString()); });
cmd.on('close', (data) => { observer.complete(); });
});
}
function generateBundle(input, file, name, format): Promise<any> {
const plugins = [
sourcemaps(),
filesize(),
];
return rollup({
input,
external: Object.keys(GLOBALS),
onwarn(warning) {
if (warning.code === 'THIS_IS_UNDEFINED') {
return;
}
if (warning.code === 'UNUSED_EXTERNAL_IMPORT') {
return;
}
console.log(warning.message);
},
file,
plugins,
}).then(bundle => {
return bundle.write({
file,
name,
globals: GLOBALS,
format,
sourcemap: true,
});
});
}
function createUmd(name: string) {
const moduleName = MODULE_NAMES[name];
const entry = createEntry(name);
const file = `${process.cwd()}/dist/packages-dist/bundles/${name}.umd.js`;
return generateBundle(entry, file, moduleName, 'umd');
}
function createEs(name: string, target: string) {
const moduleName = MODULE_NAMES[name];
const entry = createEntry(name);
let output = `${process.cwd()}/dist/packages-dist/${name}/${name}.${target}.js`;
if (name === 'core') {
output = `${process.cwd()}/dist/packages-dist/${name}.${target}.js`;
}
return generateBundle(
entry,
output,
name,
'es',
);
}
function buildModule(name: string, type: string) {
const es2015$ = spawnObservable(NGC, TSC_ARGS(type, name));
const esm$ = spawnObservable(NGC, TSC_ARGS(type, name, 'esm'));
return Observable.forkJoin(es2015$, esm$);
}
async function buildModulesProviders() {
const providers = Object.keys(MODULE_NAMES).filter((n) => !core.includes(n));
for (const name of providers) {
await buildModule(name, 'providers').toPromise();
}
}
function buildUmds() {
return Promise.all(Object.keys(MODULE_NAMES).map(async (name) => {
await createUmd(name);
await createEs(name, 'es2015');
await createEs(name, 'es5');
}));
}
async function copyFilesCore() {
await copyAll(
`${process.cwd()}/dist/es2015/**/*.d.ts`,
`${process.cwd()}/dist/packages-dist`,
).toPromise();
await copy(
`${process.cwd()}/README.md`,
`${process.cwd()}/dist/packages-dist/README.md`,
);
await copy(
`${process.cwd()}/src/lib/core/package.json`,
`${process.cwd()}/dist/packages-dist/package.json`,
);
await copy(
`${process.cwd()}/dist/es2015/index.metadata.json`,
`${process.cwd()}/dist/packages-dist/index.metadata.json`,
);
copyAll(
`${process.cwd()}/dist/routerlessmodule/es2015/**/*.d.ts`,
`${process.cwd()}/dist/packages-dist/routerlessmodule`,
);
await copy(
`${process.cwd()}/dist/routerlessmodule/es2015/index.metadata.json`,
`${process.cwd()}/dist/packages-dist/routerlessmodule/index.metadata.json`,
);
await copy(
`${process.cwd()}/src/lib/routerlessmodule/package.json`,
`${process.cwd()}/dist/packages-dist/routerlessmodule/package.json`,
);
await copyAll(
`${process.cwd()}/dist/uiroutermodule/es2015/**/*.d.ts`,
`${process.cwd()}/dist/packages-dist/uiroutermodule`,
).toPromise();
await copy(
`${process.cwd()}/dist/uiroutermodule/es2015/index.metadata.json`,
`${process.cwd()}/dist/packages-dist/uiroutermodule/index.metadata.json`,
);
await copy(
`${process.cwd()}/src/lib/uiroutermodule/package.json`,
`${process.cwd()}/dist/packages-dist/uiroutermodule/package.json`,
);
}
function copyFilesProviders() {
const providers = Object.keys(MODULE_NAMES).filter((n) => !core.includes(n));
return Promise.all(providers.map(async (name) => {
await copyAll(
`${process.cwd()}/dist/${name}/es2015/**/*.d.ts`,
`${process.cwd()}/dist/packages-dist/${name}`,
).toPromise();
await copy(
`${process.cwd()}/src/lib/providers/${name}/package.json`,
`${process.cwd()}/dist/packages-dist/${name}/package.json`,
);
await copy(
`${process.cwd()}/dist/${name}/es2015/index.metadata.json`,
`${process.cwd()}/dist/packages-dist/${name}/index.metadata.json`,
);
}));
}
async function buildLibrary() {
try {
await buildModule('core', '').toPromise();
await buildModule('routerlessmodule', 'routerlessmodule').toPromise();
await buildModule('uiroutermodule', 'uiroutermodule').toPromise();
await buildModulesProviders();
await copyFilesCore();
await copyFilesProviders();
await buildUmds();
} catch (e) {
console.error(e);
process.exit(1);
}
}
buildLibrary()
.then(() => console.log('success'))
.catch((e) => {
console.error(e);
process.exit(1);
});