chore(build): Move broccoli support to own module.

Add support for multiple pipelines in different Brocfile's.
This commit is contained in:
Alex Eagle 2015-04-08 08:28:12 -07:00
parent 9f8a9c6fc7
commit de581ea8b3
2 changed files with 54 additions and 41 deletions

View File

@ -1,6 +1,4 @@
var broccoli = require('broccoli');
var copyDereferenceSync = require('copy-dereference').sync;
var fse = require('fs-extra');
var broccoliBuild = require('./tools/broccoli/gulp');
var format = require('gulp-clang-format');
var gulp = require('gulp');
@ -282,45 +280,8 @@ var CONFIG = {
};
CONFIG.test.js.cjs = CONFIG.test.js.cjs.map(function(s) {return CONFIG.dest.js.cjs + s});
// ------------
// integration point to run broccoli build
var broccoliExecuted = false;
gulp.task('broccoli', function() {
if (broccoliExecuted) {
throw new Error('The broccoli task can be called only once!');
}
broccoliExecuted = true;
var broccoliDist = path.join('dist', 'js', 'dev', 'es6');
fse.removeSync(broccoliDist);
fse.mkdirsSync(path.join('dist', 'js', 'dev'));
var tree = broccoli.loadBrocfile();
var builder = new broccoli.Builder(tree);
return builder.build()
.then(function (hash) {
var dir = hash.directory;
try {
copyDereferenceSync(path.join(dir, 'js', 'dev', 'es6'), broccoliDist);
} catch (err) {
if (err.code === 'EEXIST') err.message += ' (we cannot build into an existing directory)';
throw err;
}
})
.finally(function () {
builder.cleanup();
})
.catch(function (err) {
// Should show file and line/col if present
if (err.file) {
console.error('File: ' + err.file);
}
console.error(err.stack);
console.error('\nBuild failed');
process.exit(1);
});
return broccoliBuild(require('./Brocfile.js'), path.join('js', 'dev'));
});
// ------------

View File

@ -0,0 +1,52 @@
var broccoli = require('broccoli');
var copyDereferenceSync = require('copy-dereference').sync;
var fse = require('fs-extra');
var path = require('path');
module.exports = broccoliBuild;
var broccoliExecuted = {};
/**
* Integration point to run a broccoli build pipeline under gulp.
* This is mostly derived from the broccoli-cli
* @param tree a broccoli tree, obtained by calling `require` on the Brocfile
* @param outputRoot the path under 'dist' owned exclusively by this Brocfile
* @returns the promise to return back to gulp
*/
function broccoliBuild(tree, outputRoot) {
if (broccoliExecuted.hasOwnProperty(outputRoot)) {
throw new Error('The broccoli task can be called only once for outputRoot ' + outputRoot);
}
broccoliExecuted[outputRoot] = true;
var distPath = path.join('dist', outputRoot);
// We do a clean build every time to avoid stale outputs.
// Broccoli's cache folders allow it to remain incremental without reading this dir.
fse.removeSync(distPath);
fse.mkdirsSync(path.join(distPath, '..'));
var builder = new broccoli.Builder(tree);
return builder.build()
.then(function (hash) {
var dir = hash.directory;
try {
copyDereferenceSync(path.join(dir, outputRoot), distPath);
} catch (err) {
if (err.code === 'EEXIST') err.message += ' (we cannot build into an existing directory)';
throw err;
}
})
.finally(function () {
builder.cleanup();
})
.catch(function (err) {
// Should show file and line/col if present
if (err.file) {
console.error('File: ' + err.file);
}
console.error(err.stack);
console.error('\nBuild failed');
process.exit(1);
});
}