Skip to content

Commit de581ea

Browse files
committed
chore(build): Move broccoli support to own module.
Add support for multiple pipelines in different Brocfile's.
1 parent 9f8a9c6 commit de581ea

File tree

2 files changed

+54
-41
lines changed

2 files changed

+54
-41
lines changed

gulpfile.js

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
var broccoli = require('broccoli');
2-
var copyDereferenceSync = require('copy-dereference').sync;
3-
var fse = require('fs-extra');
1+
var broccoliBuild = require('./tools/broccoli/gulp');
42

53
var format = require('gulp-clang-format');
64
var gulp = require('gulp');
@@ -282,45 +280,8 @@ var CONFIG = {
282280
};
283281
CONFIG.test.js.cjs = CONFIG.test.js.cjs.map(function(s) {return CONFIG.dest.js.cjs + s});
284282

285-
// ------------
286-
// integration point to run broccoli build
287-
288-
var broccoliExecuted = false;
289-
290283
gulp.task('broccoli', function() {
291-
if (broccoliExecuted) {
292-
throw new Error('The broccoli task can be called only once!');
293-
}
294-
broccoliExecuted = true;
295-
296-
var broccoliDist = path.join('dist', 'js', 'dev', 'es6');
297-
fse.removeSync(broccoliDist);
298-
fse.mkdirsSync(path.join('dist', 'js', 'dev'));
299-
300-
var tree = broccoli.loadBrocfile();
301-
var builder = new broccoli.Builder(tree);
302-
return builder.build()
303-
.then(function (hash) {
304-
var dir = hash.directory;
305-
try {
306-
copyDereferenceSync(path.join(dir, 'js', 'dev', 'es6'), broccoliDist);
307-
} catch (err) {
308-
if (err.code === 'EEXIST') err.message += ' (we cannot build into an existing directory)';
309-
throw err;
310-
}
311-
})
312-
.finally(function () {
313-
builder.cleanup();
314-
})
315-
.catch(function (err) {
316-
// Should show file and line/col if present
317-
if (err.file) {
318-
console.error('File: ' + err.file);
319-
}
320-
console.error(err.stack);
321-
console.error('\nBuild failed');
322-
process.exit(1);
323-
});
284+
return broccoliBuild(require('./Brocfile.js'), path.join('js', 'dev'));
324285
});
325286

326287
// ------------

tools/broccoli/gulp/index.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
var broccoli = require('broccoli');
2+
var copyDereferenceSync = require('copy-dereference').sync;
3+
var fse = require('fs-extra');
4+
var path = require('path');
5+
6+
module.exports = broccoliBuild;
7+
var broccoliExecuted = {};
8+
9+
/**
10+
* Integration point to run a broccoli build pipeline under gulp.
11+
* This is mostly derived from the broccoli-cli
12+
* @param tree a broccoli tree, obtained by calling `require` on the Brocfile
13+
* @param outputRoot the path under 'dist' owned exclusively by this Brocfile
14+
* @returns the promise to return back to gulp
15+
*/
16+
function broccoliBuild(tree, outputRoot) {
17+
if (broccoliExecuted.hasOwnProperty(outputRoot)) {
18+
throw new Error('The broccoli task can be called only once for outputRoot ' + outputRoot);
19+
}
20+
broccoliExecuted[outputRoot] = true;
21+
22+
var distPath = path.join('dist', outputRoot);
23+
24+
// We do a clean build every time to avoid stale outputs.
25+
// Broccoli's cache folders allow it to remain incremental without reading this dir.
26+
fse.removeSync(distPath);
27+
fse.mkdirsSync(path.join(distPath, '..'));
28+
29+
var builder = new broccoli.Builder(tree);
30+
return builder.build()
31+
.then(function (hash) {
32+
var dir = hash.directory;
33+
try {
34+
copyDereferenceSync(path.join(dir, outputRoot), distPath);
35+
} catch (err) {
36+
if (err.code === 'EEXIST') err.message += ' (we cannot build into an existing directory)';
37+
throw err;
38+
}
39+
})
40+
.finally(function () {
41+
builder.cleanup();
42+
})
43+
.catch(function (err) {
44+
// Should show file and line/col if present
45+
if (err.file) {
46+
console.error('File: ' + err.file);
47+
}
48+
console.error(err.stack);
49+
console.error('\nBuild failed');
50+
process.exit(1);
51+
});
52+
}

0 commit comments

Comments
 (0)