Skip to content

Commit 51569ce

Browse files
committed
feat(mobile): add prod build step to concatenate scripts
For projects that use the --mobile flag, this will cause all third-party polyfills and the system-loader script to be concatenated into a single file that can be loaded via an async script tag. This is necessary for App Shell to work properly, so that rendering won't be blocked on synchronous script loading. Note: this change still loads app and library files individually. Follow-up work should be done to further improve loading performance.
1 parent cb1270f commit 51569ce

File tree

4 files changed

+68
-13
lines changed

4 files changed

+68
-13
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
System.import('system-config.js').then(function () {
2+
System.import('main');
3+
}).catch(console.error.bind(console));

addon/ng2/blueprints/ng2/files/__path__/index.html

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,31 @@
3333
<body>
3434
<<%= htmlComponentName %>-app>Loading...</<%= htmlComponentName %>-app>
3535

36-
{{#each scripts.polyfills}}<script src="{{.}}"></script>{{/each}}
36+
<% if (isMobile) { %>
37+
38+
{{#if environment.production}}
39+
<script src="/app-concat.js" async></script>
40+
{{else}}
41+
{{#each scripts.polyfills}}<script src="{{.}}"></script>{{/each}}
42+
<script>
43+
System.import('system-config.js').then(function () {
44+
System.import('main');
45+
}).catch(console.error.bind(console));
46+
</script>
47+
{{/if}}
48+
49+
<% } else { %>
50+
51+
{{#each scripts.polyfills}}<script src="{{.}}"></script>{{/each}}
52+
<script>
53+
System.import('system-config.js').then(function () {
54+
System.import('main');
55+
}).catch(console.error.bind(console));
56+
</script>
57+
58+
<% } %>
59+
60+
3761

38-
<script>
39-
System.import('system-config.js').then(function () {
40-
System.import('main');
41-
}).catch(console.error.bind(console));
42-
</script>
4362
</body>
4463
</html>

lib/broccoli/angular2-app.js

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const Project = require('ember-cli/lib/models/project');
1313
const HandlebarReplace = require('./broccoli-handlebars');
1414
const config = require('../../addon/ng2/models/config');
1515
const loadEnvironment = require('./environment');
16+
const concat = require('broccoli-concat');
17+
const uglify = require('broccoli-uglify-js');
1618

1719
class Angular2App extends BroccoliPlugin {
1820
constructor(project, inputNode, options) {
@@ -30,6 +32,13 @@ class Angular2App extends BroccoliPlugin {
3032
this._sourceDir = options.sourceDir
3133
|| (this.ngConfig.defaults && this.ngConfig.defaults.sourceDir)
3234
|| 'src';
35+
this._options.polyfills = this._options.polyfills || [
36+
'vendor/es6-shim/es6-shim.js',
37+
'vendor/reflect-metadata/Reflect.js',
38+
'vendor/systemjs/dist/system.src.js',
39+
'vendor/zone.js/dist/zone.js'
40+
];
41+
3342
this._destDir = options.destDir || '';
3443

3544
// By default, add all spec files to the tsCompiler.
@@ -285,12 +294,7 @@ class Angular2App extends BroccoliPlugin {
285294
config: this.ngConfig,
286295
environment: loadEnvironment(this.project),
287296
scripts: {
288-
polyfills: this._options.polyfills || [
289-
'vendor/es6-shim/es6-shim.js',
290-
'vendor/reflect-metadata/Reflect.js',
291-
'vendor/systemjs/dist/system.src.js',
292-
'vendor/zone.js/dist/zone.js'
293-
]
297+
polyfills: this._options.polyfills
294298
},
295299
mobile: mobile
296300
}, {
@@ -411,7 +415,35 @@ class Angular2App extends BroccoliPlugin {
411415
include: ['**/*.js']
412416
});
413417

414-
return BroccoliMergeTrees([nonJsTree, scriptTree, new BundlePlugin([jsTree])], { overwrite: true });
418+
var bundleTree = new BundlePlugin([jsTree]);
419+
420+
if (this.ngConfig.apps[0].mobile) {
421+
bundleTree = concat(BroccoliMergeTrees([jsTree, scriptTree, bundleTree], {
422+
overwrite: true
423+
}), {
424+
headerFiles: this._options.polyfills.concat([
425+
'system-config.js',
426+
'main.js',
427+
'app/index.js'
428+
]),
429+
inputFiles: [
430+
'system-import.js'
431+
],
432+
header: ';(function() {',
433+
footer: '}());',
434+
sourceMapConfig: { enabled: true },
435+
allowNone: false,
436+
outputFile: '/app-concat.js'
437+
});
438+
439+
bundleTree = uglify(bundleTree, {
440+
mangle: false
441+
});
442+
}
443+
444+
445+
446+
return BroccoliMergeTrees([nonJsTree, scriptTree, bundleTree], { overwrite: true });
415447
}
416448
}
417449

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"broccoli-funnel": "^1.0.1",
4848
"broccoli-merge-trees": "^1.1.1",
4949
"broccoli-source": "^1.1.0",
50+
"broccoli-uglify-js": "^0.1.3",
5051
"broccoli-writer": "^0.1.1",
5152
"chalk": "^1.1.3",
5253
"ember-cli": "2.5.0",

0 commit comments

Comments
 (0)