Skip to content

Commit

Permalink
chore(build): migrate build.js.cjs to broccoli.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alexeagle committed Apr 10, 2015
1 parent 7e2c04e commit 564477b
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 12 deletions.
64 changes: 64 additions & 0 deletions Brocfile-js_cjs.js
Original file line number Diff line number Diff line change
@@ -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');
2 changes: 1 addition & 1 deletion Brocfile-js_dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down
6 changes: 5 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 10 additions & 4 deletions tools/broccoli/traceur/index.js

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

16 changes: 10 additions & 6 deletions tools/broccoli/traceur/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 564477b

Please sign in to comment.