Skip to content

Commit

Permalink
chore(build): Add traceur transpiler for broccoli.
Browse files Browse the repository at this point in the history
This exactly reproduces the output tree from one of the gulp tasks, which is now removed.
Next step is to migrate another sibling task to broccoli.
  • Loading branch information
alexeagle committed Apr 7, 2015
1 parent bc248e9 commit d6003ee
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 10 deletions.
38 changes: 38 additions & 0 deletions Brocfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Build pipeline for Angular2.
* First time setup:
* $ npm install --global broccoli-cli
*/
var merge = require('merge');
var TraceurCompiler = require('./tools/broccoli/traceur');
var Funnel = require('broccoli-funnel');
var stew = require('broccoli-stew');

var _COMPILER_CONFIG_JS_DEFAULT = {
sourceMaps: true,
annotations: true, // parse annotations
types: true, // parse types
script: false, // parse as a module
memberVariables: true, // parse class fields
modules: 'instantiate'
};

var modulesTree = new Funnel('modules', {include: ['**/**'], destDir: '/'});

var transpiledTree = new TraceurCompiler(modulesTree, merge(true, _COMPILER_CONFIG_JS_DEFAULT, {
typeAssertionModule: 'rtts_assert/rtts_assert',
typeAssertions: true,
outputLanguage: 'es6'
}));

transpiledTree = stew.rename(transpiledTree, function(relativePath) {
return relativePath.replace(/\.(js|es6)\.map$/, '.map')
.replace(/\.js$/, '.es6');
});
transpiledTree = stew.mv(transpiledTree, 'js/dev/es6')

//transpiledTree = stew.log(transpiledTree);


module.exports = transpiledTree;

57 changes: 47 additions & 10 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
var broccoli = require('broccoli');
var copyDereferenceSync = require('copy-dereference').sync;
var fse = require('fs-extra');

var format = require('gulp-clang-format');
var gulp = require('gulp');
var gulpPlugins = require('gulp-load-plugins')();
Expand Down Expand Up @@ -278,6 +282,47 @@ 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);
});
});

// ------------
// clean

Expand Down Expand Up @@ -317,15 +362,6 @@ gulp.task('build/transpile.ts.cjs', function() {
]);
});

gulp.task('build/transpile.js.dev.es6', transpile(gulp, gulpPlugins, {
src: CONFIG.transpile.src.js,
dest: CONFIG.dest.js.dev.es6,
outputExt: 'es6',
options: CONFIG.transpile.options.js.dev,
srcFolderInsertion: CONFIG.srcFolderInsertion.js
}));


gulp.task('build/transpile.ts.dev.es5', function() {
var tsResult = gulp.src(CONFIG.transpile.src.ts)
.pipe(sourcemaps.init())
Expand Down Expand Up @@ -353,7 +389,7 @@ gulp.task('build/transpile.js.dev.es5', function() {

gulp.task('build/transpile.js.dev', function(done) {
runSequence(
'build/transpile.js.dev.es6',
// broccoli runs the ES6 version of this task
'build/transpile.js.dev.es5',
done
);
Expand Down Expand Up @@ -806,6 +842,7 @@ gulp.task('build.dart', function(done) {

gulp.task('build.js.dev', function(done) {
runSequence(
'broccoli',
['build/transpile.js.dev', 'build/html.js.dev', 'build/copy.js.dev', 'build/multicopy.js.dev.es6'],
'build/checkCircularDependencies',
done
Expand Down
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"dependencies": {
"es6-module-loader": "^0.9.2",
"fs-extra": "^0.18.0",
"googleapis": "1.0.x",
"gulp-insert": "^0.4.0",
"gulp-modify": "0.0.4",
Expand All @@ -41,7 +42,12 @@
"devDependencies": {
"angular": "1.3.5",
"bower": "^1.3.12",
"broccoli": "^0.15.3",
"broccoli-funnel": "^0.2.3",
"broccoli-stew": "^0.2.1",
"broccoli-writer": "^0.1.1",
"canonical-path": "0.0.2",
"copy-dereference": "^1.0.0",
"css": "mlaval/css#issue65",
"del": "~1",
"dgeni": "^0.4.1",
Expand Down Expand Up @@ -87,6 +93,8 @@
"through2": "^0.6.1",
"typescript": "alexeagle/TypeScript#93dbbe2a2d0b42cefd02ac949e4bc8ab6b5b5823",
"vinyl": "^0.4.6",
"walk-sync": "^0.1.3",
"xtend": "^4.0.0",
"yargs": "2.3.*"
}
}
50 changes: 50 additions & 0 deletions tools/broccoli/traceur/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions tools/broccoli/traceur/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
var fs = require('fs');
var fse = require('fs-extra');
var path = require('path');
var traceur = require('../../transpiler');
var walkSync = require('walk-sync');
var Writer = require('broccoli-writer');
var xtend = require('xtend');

class TraceurFilter extends Writer {
constructor(private inputTree, private options = {}) {}

write(readTree, destDir) {
return readTree(this.inputTree)
.then(srcDir => {
walkSync(srcDir)
.filter(filepath => {
var extension = path.extname(filepath).toLowerCase();
return extension === '.js' || extension === '.es6';
})
.map(filepath => {
var options = xtend({filename: filepath}, this.options);

var fsOpts = {encoding: 'utf-8'};
var sourcecode = fs.readFileSync(path.join(srcDir, filepath), fsOpts);

var result = traceur.compile(options, filepath, sourcecode);

result.js = result.js + '\n//# sourceMappingURL=./' + path.basename(filepath).replace(/\.\w+$/, '.map');

var destFilepath = filepath.replace(/\.\w+$/, '.es6');
var destFile = path.join(destDir, destFilepath);
fse.mkdirsSync(path.dirname(destFile));
var destMap = path.join(destDir, filepath + '.map');


fs.writeFileSync(destFile, result.js, fsOpts);
result.sourceMap.file = destFilepath;
fs.writeFileSync(destMap, JSON.stringify(result.sourceMap), fsOpts);
});
});
}
}

module.exports = TraceurFilter;
1 change: 1 addition & 0 deletions tools/build/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var path = require('path');
var fs = require('fs');
var VinylFile = require('vinyl');

// used for generating html files and bootstrapping benchmarks and examples
module.exports = function(gulp, plugins, config) {
return function() {
return gulp.src(config.src)
Expand Down

0 comments on commit d6003ee

Please sign in to comment.