X Tutup
Skip to content

Commit 896a045

Browse files
committed
refactor(build): Better encapsulate the broccoli builder.
1 parent caf8e27 commit 896a045

File tree

7 files changed

+376
-349
lines changed

7 files changed

+376
-349
lines changed

gulpfile.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var insert = require('gulp-insert');
3636
function missingDynamicBroccoli() {
3737
throw new Error('ERROR: build.broccoli.tools task should have been run before using broccoli');
3838
}
39-
var makeBroccoliTree = missingDynamicBroccoli, broccoliBuild = missingDynamicBroccoli;
39+
var getBroccoli = missingDynamicBroccoli;
4040

4141
// Note: when DART_SDK is not found, all gulp tasks ending with `.dart` will be skipped.
4242

@@ -295,7 +295,7 @@ gulp.task('build/clean.docs', clean(gulp, gulpPlugins, {
295295
// transpile
296296

297297
gulp.task('build/transpile.dart', ['build.broccoli.tools'], function() {
298-
return broccoliBuild(makeBroccoliTree('dart'), 'dart');
298+
return getBroccoli().forDartTree().buildOnce();
299299
});
300300

301301
// ------------
@@ -609,7 +609,7 @@ gulp.task('test.transpiler.unittest', function() {
609609
return gulp.src('tools/transpiler/unittest/**/*.js')
610610
.pipe(jasmine({
611611
includeStackTrace: true
612-
}))
612+
}));
613613
});
614614

615615
// -----------------
@@ -641,17 +641,19 @@ gulp.task('build.broccoli.tools', function() {
641641
.pipe(tsc({ target: 'ES5', module: 'commonjs' }));
642642
return tsResult.js.pipe(gulp.dest('dist/broccoli'))
643643
.on('end', function() {
644-
makeBroccoliTree = require('./dist/broccoli/make-broccoli-tree');
645-
broccoliBuild = require('./dist/broccoli/gulp');
644+
var BroccoliBuilder = require('./dist/broccoli/broccoli_builder').BroccoliBuilder;
645+
getBroccoli = function() {
646+
return BroccoliBuilder;
647+
};
646648
});
647649
});
648650

649651
gulp.task('broccoli.js.dev', ['build.broccoli.tools'], function() {
650-
return broccoliBuild(makeBroccoliTree('dev'), path.join('js', 'dev'));
652+
return getBroccoli().forDevTree().buildOnce();
651653
});
652654

653655
gulp.task('broccoli.js.prod', ['build.broccoli.tools'], function() {
654-
return broccoliBuild(makeBroccoliTree('prod'), path.join('js', 'prod'));
656+
return getBroccoli().forProdTree().buildOnce();
655657
});
656658

657659
gulp.task('build.js.dev', function(done) {
@@ -666,7 +668,7 @@ gulp.task('build.js.dev', function(done) {
666668
gulp.task('build.js.prod', ['broccoli.js.prod']);
667669

668670
gulp.task('broccoli.js.cjs', ['build.broccoli.tools'], function() {
669-
return broccoliBuild(makeBroccoliTree('cjs'), path.join('js', 'cjs'));
671+
return getBroccoli().forNodeTree().buildOnce();
670672
});
671673
gulp.task('build.js.cjs', function(done) {
672674
runSequence(

tools/broccoli/broccoli_builder.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
var broccoli = require('broccoli');
2+
var destCopy = require('./broccoli-dest-copy');
3+
var fse = require('fs-extra');
4+
var makeBrowserTree = require('./trees/browser_tree');
5+
var makeNodeTree = require('./trees/node_tree');
6+
var makeDartTree = require('./trees/dart_tree');
7+
var path = require('path');
8+
var printSlowTrees = require('broccoli-slow-trees');
9+
10+
/**
11+
* Helper for building with broccoli.
12+
*/
13+
export class BroccoliBuilder {
14+
private builder;
15+
private broccoliExecuted: {[s: string]: boolean} = {};
16+
17+
// Named constructors
18+
static forDevTree(): BroccoliBuilder {
19+
return new BroccoliBuilder(makeBrowserTree({name: 'dev', typeAssertions: true}),
20+
path.join('js', 'dev'));
21+
}
22+
static forNodeTree(): BroccoliBuilder {
23+
return new BroccoliBuilder(makeNodeTree(), path.join('js', 'cjs'));
24+
}
25+
static forProdTree(): BroccoliBuilder {
26+
return new BroccoliBuilder(makeBrowserTree({name: 'prod', typeAssertions: false}),
27+
path.join('js', 'prod'));
28+
}
29+
static forDartTree(): BroccoliBuilder { return new BroccoliBuilder(makeDartTree(), 'dart'); }
30+
31+
constructor(tree, private outputRoot: string) {
32+
if (this.broccoliExecuted[outputRoot]) {
33+
throw new Error('The broccoli task can be called only once for outputRoot ' + outputRoot);
34+
}
35+
this.broccoliExecuted[outputRoot] = true;
36+
37+
var distPath = path.join('dist', outputRoot);
38+
39+
// We do a clean build every time to avoid stale outputs.
40+
// Broccoli's cache folders allow it to remain incremental without reading this dir.
41+
fse.removeSync(distPath);
42+
43+
tree = destCopy(tree, 'dist');
44+
45+
this.builder = new broccoli.Builder(tree);
46+
}
47+
48+
doBuild(): Promise<any> {
49+
return this.builder.build()
50+
.then(hash =>
51+
{
52+
console.log('=== Stats for %s (total: %ds) ===', this.outputRoot,
53+
Math.round(hash.totalTime / 1000000) / 1000);
54+
printSlowTrees(hash.graph);
55+
})
56+
.catch(err => {
57+
// Should show file and line/col if present
58+
if (err.file) {
59+
console.error('File: ' + err.file);
60+
}
61+
if (err.stack) {
62+
console.error(err.stack);
63+
} else {
64+
console.error(err);
65+
}
66+
throw err;
67+
});
68+
}
69+
70+
buildOnce(): Promise<any> {
71+
// es6-promise doesn't have finally()
72+
return (<any>this.doBuild())
73+
.finally(() =>
74+
{
75+
this.time('Build cleanup', () => this.builder.cleanup());
76+
console.log('=== Done building %s ===', this.outputRoot);
77+
})
78+
.catch(err => {
79+
console.error('\nBuild failed');
80+
process.exit(1);
81+
});
82+
}
83+
84+
time(label, work) {
85+
var start = Date.now();
86+
work();
87+
var duration = Date.now() - start;
88+
console.log("%s: %dms", label, duration);
89+
}
90+
}

tools/broccoli/gulp/index.ts

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)
X Tutup