Skip to content

Commit ea58ef8

Browse files
committed
chore(build): move the js.prod build over to broccoli
1 parent bf79337 commit ea58ef8

3 files changed

Lines changed: 117 additions & 6 deletions

File tree

Brocfile-js_prod.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
var Funnel = require('broccoli-funnel');
2+
var flatten = require('broccoli-flatten');
3+
var mergeTrees = require('broccoli-merge-trees');
4+
var stew = require('broccoli-stew');
5+
var TraceurCompiler = require('./tools/broccoli/traceur');
6+
var replace = require('broccoli-replace');
7+
var htmlReplace = require('./tools/broccoli/html-replace');
8+
var path = require('path');
9+
10+
var modulesTree = new Funnel('modules', {include: ['**/**'], destDir: '/'});
11+
12+
// Use Traceur to transpile original sources to ES6
13+
var es6ProdTree = new TraceurCompiler(modulesTree, '.es6', {
14+
sourceMaps: true,
15+
annotations: true, // parse annotations
16+
types: true, // parse types
17+
script: false, // parse as a module
18+
memberVariables: true, // parse class fields
19+
modules: 'instantiate',
20+
typeAssertionModule: 'rtts_assert/rtts_assert',
21+
typeAssertions: false,
22+
outputLanguage: 'es6'
23+
});
24+
// Munge the filenames since we use an '.es6' extension
25+
es6ProdTree = stew.rename(es6ProdTree, function(relativePath) {
26+
return relativePath.replace(/\.(js|es6)\.map$/, '.map').replace(/\.js$/, '.es6');
27+
});
28+
29+
// Call Traceur again to lower the ES6 build tree to ES5
30+
var es5ProdTree = new TraceurCompiler(es6ProdTree, '.js', {modules: 'instantiate', sourceMaps: true});
31+
es5ProdTree = stew.rename(es5ProdTree, '.es6.map', '.js.map');
32+
33+
// Now we add a few more files to the es6 tree that Traceur should not see
34+
['angular2', 'benchmarks', 'benchmarks_external', 'benchpress', 'examples', 'rtts_assert'].forEach(
35+
function(destDir) {
36+
var extras = new Funnel('tools/build', {files: ['es5build.js'], destDir: destDir});
37+
es6ProdTree = mergeTrees([es6ProdTree, extras]);
38+
});
39+
40+
var vendorScriptsTree = flatten(new Funnel('.', {
41+
files: [
42+
'node_modules/es6-module-loader/dist/es6-module-loader-sans-promises.src.js',
43+
'node_modules/zone.js/zone.js',
44+
'node_modules/zone.js/long-stack-trace-zone.js',
45+
'node_modules/systemjs/dist/system.src.js',
46+
'node_modules/systemjs/lib/extension-register.js',
47+
'node_modules/systemjs/lib/extension-cjs.js',
48+
'node_modules/rx/dist/rx.all.js',
49+
'tools/build/snippets/runtime_paths.js',
50+
path.relative(__dirname, TraceurCompiler.RUNTIME_PATH)
51+
]
52+
}));
53+
var vendorScripts_benchmark =
54+
new Funnel('tools/build/snippets', {files: ['url_params_to_form.js'], destDir: '/'});
55+
var vendorScripts_benchmarks_external =
56+
new Funnel('node_modules/angular', {files: ['angular.js'], destDir: '/'});
57+
58+
var servingTrees = [];
59+
function copyVendorScriptsTo(destDir) {
60+
servingTrees.push(new Funnel(vendorScriptsTree, {srcDir: '/', destDir: destDir}));
61+
if (destDir.indexOf('benchmarks') > -1) {
62+
servingTrees.push(new Funnel(vendorScripts_benchmark, {srcDir: '/', destDir: destDir}));
63+
}
64+
if (destDir.indexOf('benchmarks_external') > -1) {
65+
servingTrees.push(
66+
new Funnel(vendorScripts_benchmarks_external, {srcDir: '/', destDir: destDir}));
67+
}
68+
}
69+
70+
function writeScriptsForPath(relativePath, result) {
71+
copyVendorScriptsTo(path.dirname(relativePath));
72+
return result.replace('@@FILENAME_NO_EXT', relativePath.replace(/\.\w+$/, ''));
73+
}
74+
75+
var htmlTree = new Funnel(modulesTree, {include: ['*/src/**/*.html'], destDir: '/'});
76+
htmlTree = replace(htmlTree, {
77+
files: ['examples*/**'],
78+
patterns: [{match: /\$SCRIPTS\$/, replacement: htmlReplace('SCRIPTS')}],
79+
replaceWithPath: writeScriptsForPath
80+
});
81+
htmlTree = replace(htmlTree, {
82+
files: ['benchmarks/**'],
83+
patterns: [{match: /\$SCRIPTS\$/, replacement: htmlReplace('SCRIPTS_benchmarks')}],
84+
replaceWithPath: writeScriptsForPath
85+
});
86+
htmlTree = replace(htmlTree, {
87+
files: ['benchmarks_external/**'],
88+
patterns: [{match: /\$SCRIPTS\$/, replacement: htmlReplace('SCRIPTS_benchmarks_external')}],
89+
replaceWithPath: writeScriptsForPath
90+
});
91+
// TODO(broccoli): are these needed here, if not loaded by a script tag??
92+
['benchmarks/src', 'benchmarks_external/src', 'examples/src/benchpress'].forEach(
93+
copyVendorScriptsTo);
94+
95+
var scripts = mergeTrees(servingTrees, {overwrite: true});
96+
var css = new Funnel(modulesTree, {include: ["**/*.css"]});
97+
var polymerFiles = new Funnel('.', {
98+
files: [
99+
'bower_components/polymer/lib/polymer.html',
100+
'tools/build/snippets/url_params_to_form.js'
101+
]
102+
});
103+
var polymer = stew.mv(flatten(polymerFiles), 'benchmarks_external/src/tree/polymer');
104+
htmlTree = mergeTrees([htmlTree, scripts, polymer, css]);
105+
106+
es5ProdTree = mergeTrees([es5ProdTree, htmlTree]);
107+
108+
module.exports = mergeTrees([stew.mv(es6ProdTree, 'js/prod/es6'), stew.mv(es5ProdTree, 'js/prod/es5')]);

gulpfile.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,11 @@ gulp.task('build.dart', function(done) {
758758
gulp.task('broccoli.js.dev', function() {
759759
return broccoliBuild(require('./Brocfile-js_dev.js'), path.join('js', 'dev'));
760760
});
761+
762+
gulp.task('broccoli.js.prod', function() {
763+
return broccoliBuild(require('./Brocfile-js_prod.js'), path.join('js', 'prod'));
764+
});
765+
761766
gulp.task('build.js.dev', function(done) {
762767
runSequence(
763768
'broccoli.js.dev',
@@ -766,12 +771,7 @@ gulp.task('build.js.dev', function(done) {
766771
);
767772
});
768773

769-
gulp.task('build.js.prod', function(done) {
770-
runSequence(
771-
['build/transpile.js.prod', 'build/html.js.prod', 'build/copy.js.prod', 'build/multicopy.js.prod.es6'],
772-
done
773-
);
774-
});
774+
gulp.task('build.js.prod', ['broccoli.js.prod']);
775775

776776
gulp.task('broccoli.js.cjs', function() {
777777
return broccoliBuild(require('./Brocfile-js_cjs.js'), path.join('js', 'cjs'));

tools/broccoli/gulp/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function broccoliBuild(tree, outputRoot) {
3333
var builder = new broccoli.Builder(tree);
3434
return builder.build()
3535
.then(function (hash) {
36+
console.log('=== Stats for %s (total: %ds) ===', outputRoot, Math.round(hash.totalTime/1000000)/1000);
3637
printSlowTrees(hash.graph);
3738

3839
var dir = hash.directory;
@@ -49,6 +50,8 @@ function broccoliBuild(tree, outputRoot) {
4950
time('Build cleanup', function() {
5051
builder.cleanup();
5152
});
53+
54+
console.log('=== Done building %s ===', outputRoot);
5255
})
5356
.catch(function (err) {
5457
// Should show file and line/col if present

0 commit comments

Comments
 (0)