-
Notifications
You must be signed in to change notification settings - Fork 12k
/
angular2-app.js
472 lines (409 loc) · 13.3 KB
/
angular2-app.js
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
'use strict';
const path = require('path');
const fs = require('fs');
const BroccoliPlugin = require('broccoli-writer');
const BroccoliTypescript = require('./broccoli-typescript');
const BundlePlugin = require('./angular-broccoli-bundle');
const BroccoliFunnel = require('broccoli-funnel');
const BroccoliMergeTrees = require('broccoli-merge-trees');
const BroccoliSource = require('broccoli-source');
const UnwatchedDir = BroccoliSource.UnwatchedDir;
const Project = require('ember-cli/lib/models/project');
const HandlebarReplace = require('./broccoli-handlebars');
const config = require('../../addon/ng2/models/config');
const loadEnvironment = require('./environment');
const concat = require('broccoli-concat');
const uglify = require('broccoli-uglify-js');
class Angular2App extends BroccoliPlugin {
constructor(project, inputNode, options) {
super();
this.ngConfig = config.CliConfig.fromProject();
if (!options) {
options = inputNode;
inputNode = null;
}
options = options || {};
this._options = options;
this._sourceDir = options.sourceDir
|| (this.ngConfig.defaults && this.ngConfig.defaults.sourceDir)
|| 'src';
this._options.polyfills = this._options.polyfills || [
'vendor/es6-shim/es6-shim.js',
'vendor/reflect-metadata/Reflect.js',
'vendor/systemjs/dist/system.src.js',
'vendor/zone.js/dist/zone.js'
];
this._destDir = options.destDir || '';
// By default, add all spec files to the tsCompiler.
this._tsCompiler = options.tsCompiler || {
additionalFiles: ['**/*.spec.ts']
};
this._initProject();
this._notifyAddonIncluded();
this._inputNode = inputNode || this._buildInputTree();
this._tree = this._buildTree();
}
/**
* For backward compatibility.
* @public
* @method toTree
* @return {BroccoliPlugin} A broccoli plugin.
*/
toTree() {
// eslint-disable-next-line no-console
console.warn('Angular2App is now a broccoli plugin. Calling toTree() is deprecated.');
return this;
}
/**
* @override
*/
read(readTree) {
return this._tree.read(readTree);
}
/**
* @override
*/
cleanup() {
return this._tree.cleanup();
}
_buildInputTree() {
const inputTreeArray = [
new BroccoliFunnel(this._sourceDir, { destDir: this._sourceDir }),
new BroccoliFunnel('typings', { destDir: 'typings' }),
this._getConfigTree()
];
if (fs.existsSync('public')) {
inputTreeArray.push(new BroccoliFunnel('public', { destDir: 'public' }));
}
if (fs.existsSync('icons')) {
inputTreeArray.push(new BroccoliFunnel('icons', { destDir: 'icons' }));
}
return new BroccoliMergeTrees(inputTreeArray, { overwrite: true });
}
/**
* Create and return the app build system tree that:
* - Get the `assets` tree
* - Get the TS tree
* - Get the TS src tree
* - Get the index.html tree
* - Get the NPM modules tree
* - Apply/remove stuff based on the environment (dev|prod)
* - Return the app trees to be extended
*
* @private
* @method _buildTree
* @return {BroccoliFunnel} The app trees that can be used to extend the build.
*/
_buildTree() {
var assetTree = this._getAssetsTree();
var tsTree = this._getTsTree();
var indexTree = this._getIndexTree();
var vendorNpmTree = this._getVendorNpmTree();
var buildTrees = [assetTree, tsTree, indexTree, vendorNpmTree];
// Add available and supported CSS plugins.
for (const suffix of ['sass', 'less', 'stylus', 'compass']) {
const plugin = require(`./angular-broccoli-${suffix}`);
const tree = plugin.makeBroccoliTree(this._inputNode, this._options[`${suffix}Compiler`]);
if (!tree) {
continue;
}
buildTrees.push(new BroccoliFunnel(tree, {
include: ['**/*'],
getDestinationPath: (n) => {
if (n.startsWith(this._sourceDir)) {
return n.substr(this._sourceDir.length);
}
return n;
}
}));
}
// Add the public folder in.
buildTrees.push(new BroccoliFunnel(this._inputNode, {
allowEmpty: true,
srcDir: 'public',
name: 'PublicFolderFunnel'
}));
var merged = new BroccoliMergeTrees(buildTrees, { overwrite: true });
if (this.ngConfig.apps[0].mobile) {
let AppShellPlugin = require('angular2-broccoli-prerender').AppShellPlugin;
merged = new BroccoliMergeTrees([merged, new AppShellPlugin(merged, 'index.html', 'main-app-shell')], {
overwrite: true
});
}
if (loadEnvironment(this.project).production) {
merged = this._getBundleTree(merged);
}
return new BroccoliFunnel(merged, {
destDir: this._destDir,
overwrite: true
});
}
/**
* @private
* @method _initProject
* @param {Object} options
*/
_initProject() {
this.project = Project.closestSync(process.cwd());
// project root dir env used on angular-cli side for including packages from project
process.env.PROJECT_ROOT = process.env.PROJECT_ROOT || this.project.root;
}
/**
* @private
* @method _notifyAddonIncluded
*/
_notifyAddonIncluded() {
this.initializeAddons();
this.project.addons = this.project.addons.filter(function (addon) {
addon.app = this;
if (!addon.isEnabled || addon.isEnabled()) {
if (addon.included) {
addon.included(this);
}
return addon;
}
}, this);
}
/**
* Loads and initializes addons for this project.
* Calls initializeAddons on the Project.
*
* @private
* @method initializeAddons
*/
initializeAddons() {
this.project.initializeAddons();
}
/**
* Returns the content for a specific type (section) for index.html.
*
* Currently supported types:
* - 'head'
* //- 'config-module'
* //- 'app'
* //- 'head-footer'
* //- 'test-header-footer'
* //- 'body-footer'
* //- 'test-body-footer'
*
* Addons can also implement this method and could also define additional
* types (eg. 'some-addon-section').
*
* @private
* @method _contentFor
* @param {RegExP} match Regular expression to match against
* @param {String} type Type of content
* @return {String} The content.
*/
_contentFor(match, type) {
var content = [];
/*switch (type) {
case 'head': this._contentForHead(content, config); break;
case 'config-module': this._contentForConfigModule(content, config); break;
case 'app-boot': this._contentForAppBoot(content, config); break;
}*/
content = this.project.addons.reduce(function (content, addon) {
var addonContent = addon.contentFor ? addon.contentFor(type) : null;
if (addonContent) {
return content.concat(addonContent);
}
return content;
}, content);
return content.join('\n');
}
/**
* Returns the tree for app/index.html.
*
* @private
* @method _getIndexTree
* @return {Tree} Tree for app/index.html.
*/
_getIndexTree() {
var files = [
'index.html'
];
var mobile;
let indexTree = new BroccoliFunnel(this._inputNode, {
include: files.map(name => path.join(this._sourceDir, name)),
getDestinationPath: (n) => {
if (n.startsWith(this._sourceDir)) {
return n.substr(this._sourceDir.length);
}
return n;
}
});
if (this.ngConfig.apps[0].mobile) {
mobile = {
icons: [
{ rel: 'apple-touch-icon', href: '/icons/apple-touch-icon.png' },
{ rel: 'apple-touch-icon', sizes: '57x57', href: '/icons/apple-touch-icon-57x57.png' },
{ rel: 'apple-touch-icon', sizes: '60x60', href: '/icons/apple-touch-icon-60x60.png' },
{ rel: 'apple-touch-icon', sizes: '72x72', href: '/icons/apple-touch-icon-72x72.png' },
{ rel: 'apple-touch-icon', sizes: '76x76', href: '/icons/apple-touch-icon-76x76.png' },
{ rel: 'apple-touch-icon', sizes: '114x114', href: '/icons/apple-touch-icon-114x114.png' },
{ rel: 'apple-touch-icon', sizes: '120x120', href: '/icons/apple-touch-icon-120x120.png' },
{ rel: 'apple-touch-icon', sizes: '144x144', href: '/icons/apple-touch-icon-144x144.png' },
{ rel: 'apple-touch-icon', sizes: '152x152', href: '/icons/apple-touch-icon-152x152.png' },
{ rel: 'apple-touch-icon', sizes: '180x180', href: '/icons/apple-touch-icon-180x180.png' },
{ rel: 'apple-touch-startup-image', href: '/icons/apple-touch-icon-180x180.png' }
]
}
}
return new HandlebarReplace(indexTree, {
config: this.ngConfig,
environment: loadEnvironment(this.project),
scripts: {
polyfills: this._options.polyfills
},
mobile: mobile
}, {
helpers: {
'content-for': (name) => {
// TODO: remove content-for.
// eslint-disable-next-line no-console
console.warn('"{{content-for}}" has been deprecated and will be removed before RC.');
return this._contentFor(null, name);
}
}
});
}
/**
* Returns the TS tree.
*
* @private
* @method _getTsTree
* @return {Tree} Tree for TypeScript files.
*/
_getTsTree() {
var tsConfigPath = path.join(this._sourceDir, 'tsconfig.json');
var tsTree = new BroccoliTypescript(this._inputNode, tsConfigPath, this._tsCompiler);
var tsTreeExcludes = ['*.d.ts', 'tsconfig.json'];
var excludeSpecFiles = '**/*.spec.*';
if (loadEnvironment(this.project).production) {
tsTreeExcludes.push(excludeSpecFiles);
}
tsTree = new BroccoliFunnel(tsTree, {
srcDir: this._sourceDir,
exclude: tsTreeExcludes
});
return tsTree;
}
/**
* Returns the `vendorNpm` tree by merging the CLI dependencies plus the ones
* passed by the user.
*
* @private
* @method _getVendorNpmTree
* @return {Tree} The NPM tree.
*/
_getVendorNpmTree() {
var vendorNpmFiles = [
];
if (this.ngConfig.apps[0].mobile) {
vendorNpmFiles.push('@angular/service-worker/dist/worker.js')
}
if (this._options.vendorNpmFiles) {
vendorNpmFiles = vendorNpmFiles.concat(this._options.vendorNpmFiles);
}
return new BroccoliFunnel(new UnwatchedDir('node_modules'), {
include: vendorNpmFiles,
destDir: 'vendor',
name: 'vendor'
});
}
/**
* Returns the `assets` tree.
*
* @private
* @method _getAssetsTree
* @return {Tree} The assets tree.
*/
_getAssetsTree() {
return new BroccoliFunnel(this._inputNode, {
srcDir: this._sourceDir,
exclude: [
'**/*.ts',
'**/*.scss',
'**/*.sass',
'**/*.less',
'**/*.styl',
'**/tsconfig.json'
],
allowEmpty: true
});
}
/**
* Returns the config files tree.
*
* @private
* @method _getConfigTree
* @return {Tree} The config files tree.
*/
_getConfigTree() {
const isProduction = loadEnvironment(this.project).production;
var envConfigFile = isProduction ? 'environment.prod.ts' : 'environment.dev.ts';
return new BroccoliFunnel('config', {
include: [envConfigFile],
destDir: `${this._sourceDir}/app`,
getDestinationPath: () => 'environment.ts'
});
}
_getBundleTree(preBundleTree){
var vendorTree = this._getVendorNpmTree();
var assetsTree = this._getAssetsTree();
var scriptTree = new BroccoliFunnel(preBundleTree, {
include: this._options.polyfills
});
var nonJsTree = new BroccoliFunnel(preBundleTree, {
exclude: ['**/*.js', '**/*.js.map']
});
var jsTree = new BroccoliFunnel(preBundleTree, {
include: ['**/*.js', '**/*.js.map']
});
var bundleTree = new BundlePlugin([jsTree]);
if (this.ngConfig.apps[0].mobile) {
bundleTree = concat(BroccoliMergeTrees([vendorTree, jsTree, scriptTree, bundleTree], {
overwrite: true
}), {
headerFiles: this._options.polyfills.concat([
'system-config.js',
'main.js',
'app/index.js'
]),
inputFiles: [
'system-import.js'
],
header: ';(function() {',
footer: '}());',
sourceMapConfig: { enabled: true },
allowNone: false,
outputFile: '/app-concat.js'
});
bundleTree = uglify(bundleTree, {
mangle: false
});
// Required here since the package isn't installed for non-mobile apps.
var ServiceWorkerPlugin = require('@angular/service-worker').ServiceWorkerPlugin;
// worker.js is needed so it can be copied to dist
var workerJsTree = new BroccoliFunnel(jsTree, {
include: ['vendor/@angular/service-worker/dist/worker.js']
});
/**
* ServiceWorkerPlugin will automatically pre-fetch and cache every file
* in the tree it receives, so it should only receive the app bundle,
* and non-JS static files from the app. The plugin also needs to have
* the worker.js file available so it can copy it to dist.
**/
var swTree = new ServiceWorkerPlugin(BroccoliMergeTrees([
bundleTree,
assetsTree,
workerJsTree
]));
bundleTree = BroccoliMergeTrees([bundleTree, swTree], {
overwrite: true
});
}
return BroccoliMergeTrees([nonJsTree, scriptTree, bundleTree], { overwrite: true });
}
}
module.exports = Angular2App;