|
| 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 | +} |
0 commit comments