chore(build): migrate build.js.cjs to broccoli.

This doesn't do the typescript part of the build yet. Also there is a bit
of hackiness left to resolve in a follow-up change.
This commit is contained in:
Alex Eagle 2015-04-09 15:56:54 -07:00
parent 7e2c04e805
commit 564477b8a0
6 changed files with 91 additions and 12 deletions

64
Brocfile-js_cjs.js Normal file
View File

@ -0,0 +1,64 @@
var Funnel = require('broccoli-funnel');
var mergeTrees = require('broccoli-merge-trees');
var stew = require('broccoli-stew');
var TraceurCompiler = require('./tools/broccoli/traceur');
var renderLodashTemplate = require('broccoli-lodash');
var modulesTree = new Funnel('modules', {include: ['**/**'], exclude: ['angular2/src/core/zone/vm_turn_zone.es6']});
// Use Traceur to transpile original sources to ES6
var cjsTree = new TraceurCompiler(modulesTree, '.js', {
sourceMaps: true,
annotations: true, // parse annotations
types: true, // parse types
script: false, // parse as a module
memberVariables: true, // parse class fields
typeAssertionModule: 'rtts_assert/rtts_assert',
// Don't use type assertions since this is partly transpiled by typescript
typeAssertions: false,
modules: 'commonjs'
}, true);
// Munge the filenames since we use an '.es6' extension
cjsTree = stew.rename(cjsTree, function(relativePath) {
return relativePath.replace(/\.(js|es6)\.map$/, '.map').replace(/\.es6$/, '.js');
});
// Now we add a few more files to the es6 tree that Traceur should not see
['angular2', 'benchmarks', 'benchmarks_external', 'benchpress', 'examples', 'rtts_assert'].forEach(
function(destDir) {
var extras = new Funnel('.', {files: ['LICENSE'], destDir: destDir});
cjsTree = mergeTrees([cjsTree, extras]);
});
extras = new Funnel(modulesTree, {include: ['**/*.md', '**/*.png'], exclude: ['**/*.dart.md']});
extras = stew.rename(extras, 'README.js.md', 'README.md');
var BASE_PACKAGE_JSON = require('./package.json');
var COMMON_PACKAGE_JSON = {
version: BASE_PACKAGE_JSON.version,
homepage: BASE_PACKAGE_JSON.homepage,
bugs: BASE_PACKAGE_JSON.bugs,
license: BASE_PACKAGE_JSON.license,
contributors: BASE_PACKAGE_JSON.contributors,
dependencies: BASE_PACKAGE_JSON.dependencies,
devDependencies: {
"yargs": BASE_PACKAGE_JSON.devDependencies['yargs'],
"gulp-sourcemaps": BASE_PACKAGE_JSON.devDependencies['gulp-sourcemaps'],
"gulp-traceur": BASE_PACKAGE_JSON.devDependencies['gulp-traceur'],
"gulp": BASE_PACKAGE_JSON.devDependencies['gulp'],
"gulp-rename": BASE_PACKAGE_JSON.devDependencies['gulp-rename'],
"through2": BASE_PACKAGE_JSON.devDependencies['through2']
}
};
// Add a .template extension since renderLodashTemplate strips one extension
var packageJsons = stew.rename(new Funnel(modulesTree, {include: ['**/package.json']}), '.json', '.json.template');
packageJsons = renderLodashTemplate(packageJsons, {
files: ["**/**"],
context: { 'packageJson': COMMON_PACKAGE_JSON }
});
cjsTree = mergeTrees([cjsTree, extras, packageJsons]);
module.exports = stew.mv(cjsTree, 'js/cjs');

View File

@ -7,7 +7,7 @@ var replace = require('broccoli-replace');
var htmlReplace = require('./tools/broccoli/html-replace');
var path = require('path');
var modulesTree = new Funnel('modules', {include: ['**/**'], destDir: '/'});
var modulesTree = new Funnel('modules', {include: ['**/**'], exclude: ['**/*.cjs'], destDir: '/'});
// Use Traceur to transpile original sources to ES6
var es6DevTree = new TraceurCompiler(modulesTree, '.es6', {

View File

@ -773,9 +773,13 @@ gulp.task('build.js.prod', function(done) {
);
});
gulp.task('broccoli.js.cjs', function() {
return broccoliBuild(require('./Brocfile-js_cjs.js'), path.join('js', 'cjs'));
});
gulp.task('build.js.cjs', function(done) {
runSequence(
['build/transpile.js.cjs', 'build/copy.js.cjs', 'build/multicopy.js.cjs'],
'broccoli.js.cjs',
//['build/transpile.js.cjs', 'build/copy.js.cjs', 'build/multicopy.js.cjs'],
// Overwrite the .js.cjs transpilation with typescript outputs
// We still need traceur outputs everywhere else, for now.
'build/transpile.ts.cjs',

View File

@ -45,6 +45,7 @@
"broccoli": "^0.15.3",
"broccoli-flatten": "^0.1.1",
"broccoli-funnel": "igorminar/broccoli-funnel#perf-files",
"broccoli-lodash": "^0.1.1",
"broccoli-merge-trees": "^0.2.1",
"broccoli-replace": "alexeagle/broccoli-replace#angular_patch",
"broccoli-slow-trees": "^1.1.0",

View File

@ -13,19 +13,21 @@ var Writer = require('broccoli-writer');
var xtend = require('xtend');
var TraceurFilter = (function (_super) {
__extends(TraceurFilter, _super);
function TraceurFilter(inputTree, destExtension, options) {
function TraceurFilter(inputTree, destExtension, options, hackSourceMapExtension) {
if (destExtension === void 0) { destExtension = '.js'; }
if (options === void 0) { options = {}; }
if (hackSourceMapExtension === void 0) { hackSourceMapExtension = false; }
this.inputTree = inputTree;
this.destExtension = destExtension;
this.options = options;
this.hackSourceMapExtension = hackSourceMapExtension;
}
TraceurFilter.prototype.write = function (readTree, destDir) {
var _this = this;
return readTree(this.inputTree).then(function (srcDir) {
walkSync(srcDir).filter(function (filepath) {
var extension = path.extname(filepath).toLowerCase();
return extension === '.js' || extension === '.es6';
return extension === '.js' || extension === '.es6' || extension === '.cjs';
}).map(function (filepath) {
var options = xtend({
filename: filepath
@ -37,11 +39,15 @@ var TraceurFilter = (function (_super) {
var result = traceur.compile(options, filepath, sourcecode);
// TODO: we should fix the sourceMappingURL written by Traceur instead of overriding
// (but we might switch to typescript first)
result.js = result.js + '\n//# sourceMappingURL=./' + path.basename(filepath).replace(/\.es6$/, '') + (_this.destExtension === '.js' ? '.js.map' : '.map');
var url = path.basename(filepath).replace(/\.es6$/, '') + (_this.destExtension === '.js' ? '.js.map' : '.map');
if (_this.hackSourceMapExtension) {
url = path.basename(filepath).replace(/\.\w+$/, '') + '.map';
}
result.js = result.js + ("\n//# sourceMappingURL=./" + url);
var destFilepath = filepath.replace(/\.\w+$/, _this.destExtension);
var destFile = path.join(destDir, destFilepath);
fse.mkdirsSync(path.dirname(destFile));
var destMap = path.join(destDir, filepath + '.map');
var destMap = path.join(destDir, destFilepath + '.map');
fs.writeFileSync(destFile, result.js, fsOpts);
result.sourceMap.file = destFilepath;
fs.writeFileSync(destMap, JSON.stringify(result.sourceMap), fsOpts);

View File

@ -7,7 +7,8 @@ var Writer = require('broccoli-writer');
var xtend = require('xtend');
class TraceurFilter extends Writer {
constructor(private inputTree, private destExtension: string = '.js', private options = {}) {}
constructor(private inputTree, private destExtension: string = '.js',
private options = {}, private hackSourceMapExtension: boolean = false) {}
static RUNTIME_PATH = traceur.RUNTIME_PATH;
write(readTree, destDir) {
return readTree(this.inputTree)
@ -16,7 +17,7 @@ class TraceurFilter extends Writer {
.filter(filepath =>
{
var extension = path.extname(filepath).toLowerCase();
return extension === '.js' || extension === '.es6';
return extension === '.js' || extension === '.es6' || extension === '.cjs';
})
.map(filepath => {
var options = xtend({filename: filepath}, this.options);
@ -28,14 +29,17 @@ class TraceurFilter extends Writer {
// TODO: we should fix the sourceMappingURL written by Traceur instead of overriding
// (but we might switch to typescript first)
result.js = result.js + '\n//# sourceMappingURL=./' +
path.basename(filepath).replace(/\.es6$/, '') +
(this.destExtension === '.js' ? '.js.map' : '.map');
var url = path.basename(filepath).replace(/\.es6$/, '') +
(this.destExtension === '.js' ? '.js.map' : '.map');
if (this.hackSourceMapExtension) {
url = path.basename(filepath).replace(/\.\w+$/, '') + '.map';
}
result.js = result.js + `\n//# sourceMappingURL=./${url}`;
var destFilepath = filepath.replace(/\.\w+$/, this.destExtension);
var destFile = path.join(destDir, destFilepath);
fse.mkdirsSync(path.dirname(destFile));
var destMap = path.join(destDir, filepath + '.map');
var destMap = path.join(destDir, destFilepath + '.map');
fs.writeFileSync(destFile, result.js, fsOpts);