feat(build): transpile to es6

Transpile all sources first to es6 which we can publish and then
to es5. Also merge the generated source maps into once map.
This commit is contained in:
Tobias Bosch 2015-02-06 21:04:43 -08:00
parent 4b24734855
commit 69bba9b5df
5 changed files with 139 additions and 17 deletions

View File

@ -17,6 +17,7 @@ var rundartpackage = require('./tools/build/rundartpackage');
var multicopy = require('./tools/build/multicopy'); var multicopy = require('./tools/build/multicopy');
var karma = require('karma').server; var karma = require('karma').server;
var minimist = require('minimist'); var minimist = require('minimist');
var es5build = require('./tools/build/es5build');
var DART_SDK = require('./tools/build/dartdetect')(gulp); var DART_SDK = require('./tools/build/dartdetect')(gulp);
// ----------------------- // -----------------------
@ -67,8 +68,14 @@ var CONFIG = {
dest: { dest: {
js: { js: {
all: 'dist/js', all: 'dist/js',
dev: 'dist/js/dev', dev: {
prod: 'dist/js/prod', es6: 'dist/js/dev/es6',
es5: 'dist/js/dev/es5'
},
prod: {
es6: 'dist/js/prod/es6',
es5: 'dist/js/prod/es5'
},
dart2js: 'dist/js/dart2js' dart2js: 'dist/js/dart2js'
}, },
dart: 'dist/dart', dart: 'dist/dart',
@ -131,10 +138,12 @@ var CONFIG = {
js: { js: {
dev: merge(true, _COMPILER_CONFIG_JS_DEFAULT, { dev: merge(true, _COMPILER_CONFIG_JS_DEFAULT, {
typeAssertionModule: 'rtts_assert/rtts_assert', typeAssertionModule: 'rtts_assert/rtts_assert',
typeAssertions: true typeAssertions: true,
outputLanguage: 'es6'
}), }),
prod: merge(true, _COMPILER_CONFIG_JS_DEFAULT, { prod: merge(true, _COMPILER_CONFIG_JS_DEFAULT, {
typeAssertions: false typeAssertions: false,
outputLanguage: 'es6'
}) })
}, },
dart: { dart: {
@ -212,12 +221,12 @@ gulp.task('build/clean.docs', clean(gulp, gulpPlugins, {
gulp.task('build/deps.js.dev', deps(gulp, gulpPlugins, { gulp.task('build/deps.js.dev', deps(gulp, gulpPlugins, {
src: CONFIG.deps.js, src: CONFIG.deps.js,
dest: CONFIG.dest.js.dev dest: CONFIG.dest.js.dev.es5
})); }));
gulp.task('build/deps.js.prod', deps(gulp, gulpPlugins, { gulp.task('build/deps.js.prod', deps(gulp, gulpPlugins, {
src: CONFIG.deps.js, src: CONFIG.deps.js,
dest: CONFIG.dest.js.prod dest: CONFIG.dest.js.prod.es5
})); }));
gulp.task('build/deps.js.dart2js', deps(gulp, gulpPlugins, { gulp.task('build/deps.js.dart2js', deps(gulp, gulpPlugins, {
@ -228,24 +237,50 @@ gulp.task('build/deps.js.dart2js', deps(gulp, gulpPlugins, {
// ------------ // ------------
// transpile // transpile
gulp.task('build/transpile.js.dev', transpile(gulp, gulpPlugins, { gulp.task('build/transpile.js.dev.es6', transpile(gulp, gulpPlugins, {
src: CONFIG.transpile.src.js, src: CONFIG.transpile.src.js,
copy: CONFIG.transpile.copy.js, copy: CONFIG.transpile.copy.js,
dest: CONFIG.dest.js.dev, dest: CONFIG.dest.js.dev.es6,
outputExt: 'js', outputExt: 'es6',
options: CONFIG.transpile.options.js.dev, options: CONFIG.transpile.options.js.dev,
srcFolderInsertion: CONFIG.srcFolderInsertion.js srcFolderInsertion: CONFIG.srcFolderInsertion.js
})); }));
gulp.task('build/transpile.js.prod', transpile(gulp, gulpPlugins, { gulp.task('build/transpile.js.dev.es5', es5build({
src: CONFIG.dest.js.dev.es6,
dest: CONFIG.dest.js.dev.es5,
modules: 'instantiate'
}));
gulp.task('build/transpile.js.dev', function() {
return runSequence(
'build/transpile.js.dev.es6',
'build/transpile.js.dev.es5'
);
});
gulp.task('build/transpile.js.prod.es6', transpile(gulp, gulpPlugins, {
src: CONFIG.transpile.src.js, src: CONFIG.transpile.src.js,
copy: CONFIG.transpile.copy.js, copy: CONFIG.transpile.copy.js,
dest: CONFIG.dest.js.prod, dest: CONFIG.dest.js.prod.es6,
outputExt: 'js', outputExt: 'es6',
options: CONFIG.transpile.options.js.prod, options: CONFIG.transpile.options.js.prod,
srcFolderInsertion: CONFIG.srcFolderInsertion.js srcFolderInsertion: CONFIG.srcFolderInsertion.js
})); }));
gulp.task('build/transpile.js.prod.es5', es5build({
src: CONFIG.dest.js.prod.es6,
dest: CONFIG.dest.js.prod.es5,
modules: 'instantiate'
}));
gulp.task('build/transpile.js.prod', function() {
return runSequence(
'build/transpile.js.prod.es6',
'build/transpile.js.prod.es5'
);
});
gulp.task('build/transpile.dart', transpile(gulp, gulpPlugins, { gulp.task('build/transpile.dart', transpile(gulp, gulpPlugins, {
src: CONFIG.transpile.src.dart, src: CONFIG.transpile.src.dart,
copy: CONFIG.transpile.copy.dart, copy: CONFIG.transpile.copy.dart,
@ -278,14 +313,14 @@ gulp.task('build/transpile/e2eTest.cjs', transpile(gulp, gulpPlugins, {
gulp.task('build/html.js.dev', html(gulp, gulpPlugins, { gulp.task('build/html.js.dev', html(gulp, gulpPlugins, {
src: CONFIG.html.src.js, src: CONFIG.html.src.js,
dest: CONFIG.dest.js.dev, dest: CONFIG.dest.js.dev.es5,
srcFolderInsertion: CONFIG.srcFolderInsertion.js, srcFolderInsertion: CONFIG.srcFolderInsertion.js,
scriptsPerFolder: CONFIG.html.scriptsPerFolder.js scriptsPerFolder: CONFIG.html.scriptsPerFolder.js
})); }));
gulp.task('build/html.js.prod', html(gulp, gulpPlugins, { gulp.task('build/html.js.prod', html(gulp, gulpPlugins, {
src: CONFIG.html.src.js, src: CONFIG.html.src.js,
dest: CONFIG.dest.js.prod, dest: CONFIG.dest.js.prod.es5,
srcFolderInsertion: CONFIG.srcFolderInsertion.js, srcFolderInsertion: CONFIG.srcFolderInsertion.js,
scriptsPerFolder: CONFIG.html.scriptsPerFolder.js scriptsPerFolder: CONFIG.html.scriptsPerFolder.js
})); }));
@ -354,12 +389,12 @@ gulp.task('build/format.dart', rundartpackage(gulp, gulpPlugins, {
// ------------------ // ------------------
// web servers // web servers
gulp.task('serve.js.dev', jsserve(gulp, gulpPlugins, { gulp.task('serve.js.dev', jsserve(gulp, gulpPlugins, {
path: CONFIG.dest.js.dev, path: CONFIG.dest.js.dev.es5,
port: 8000 port: 8000
})); }));
gulp.task('serve.js.prod', jsserve(gulp, gulpPlugins, { gulp.task('serve.js.prod', jsserve(gulp, gulpPlugins, {
path: CONFIG.dest.js.prod, path: CONFIG.dest.js.prod.es5,
port: 8001 port: 8001
})); }));

View File

@ -22,6 +22,9 @@
"node-uuid": "1.4.x" "node-uuid": "1.4.x"
}, },
"devDependencies": { "devDependencies": {
"yargs": "2.3.*",
"gulp-sourcemaps": "1.3.*",
"gulp-traceur": "0.16.*",
"angular": "1.3.5", "angular": "1.3.5",
"bower": "^1.3.12", "bower": "^1.3.12",
"canonical-path": "0.0.2", "canonical-path": "0.0.2",

78
tools/build/es5build.js Executable file
View File

@ -0,0 +1,78 @@
#!/usr/bin/env node
// Attention: This file will be distributed with our npm packages!
var gulp = require('gulp');
var traceur = require('gulp-traceur');
var rename = require('gulp-rename');
var sourcemaps = require('gulp-sourcemaps');
var through2 = require('through2');
var fs = require('fs');
var path = require('path');
module.exports = run;
if (!module.parent) {
var argv = require('yargs')
.usage('Transpile to es5.\n\n'+
'Usage: $0 -m [format] -s [folder] -d [folder]')
.example('$0 -d tmp', 'transpile in `instantate` format to tmp/')
.options({
's': {
alias: 'source',
describe: 'source folder',
default: '.'
},
'd': {
alias: 'dest',
describe: 'output folder',
demand: true
},
'm': {
alias: 'modules',
describe: 'module format, https://github.com/google/traceur-compiler/wiki/Options-for-Compiling',
default: 'instantiate'
}
})
.help('help')
.wrap(40)
.strict()
.argv
;
run({
src: argv.s,
dest: argv.d,
modules: argv.m
});
}
function run(config) {
var src = ['!node_modules', '!node_modules/**', './**/*.es6'];
gulp.src(src, {cwd: config.src})
.pipe(rename(function(file) {
file.extname = file.extname.replace('.es6', '.js');
}))
// TODO(tbosch): Using sourcemaps.init({loadMaps:true}) does not combine
// the sourcemaps correctly!
.pipe(sourcemaps.init())
.pipe(through2.obj(function(file, encoding, done) {
var self = this;
fs.readFile(file.path.replace('.js', '.map'), function(error, buffer) {
if (error) {
return done(error);
}
file.sourceMap = JSON.parse(buffer.toString());
// The filename needs to be the same as the one that is transpiled
// so that gulp-sourcemaps can update the mapping
file.sourceMap.file = file.relative;
file.sourceMap.sourceRoot = path.dirname(file.relative);
self.push(file);
done();
});
}))
.pipe(traceur({
modules: config.modules,
sourceMaps: true
}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(config.dest));
};

View File

@ -34,7 +34,7 @@ function gulpTraceur(options, resolveModuleName) {
if (sourceMap) { if (sourceMap) {
var sourceMapFile = cloneFile(file, { var sourceMapFile = cloneFile(file, {
path: file.path.replace(/\.js$/, '.map'), path: file.path.replace(/\.\w+$/, '.map'),
contents: JSON.stringify(sourceMap) contents: JSON.stringify(sourceMap)
}); });

View File

@ -4,6 +4,7 @@ var fs = require('fs');
var glob = require('glob'); var glob = require('glob');
var path = require('path'); var path = require('path');
var traceur = require('traceur'); var traceur = require('traceur');
var assert = require('assert');
exports.RUNTIME_PATH = traceur.RUNTIME_PATH; exports.RUNTIME_PATH = traceur.RUNTIME_PATH;
var TRACEUR_PATH = traceur.RUNTIME_PATH.replace('traceur-runtime.js', 'traceur.js'); var TRACEUR_PATH = traceur.RUNTIME_PATH.replace('traceur-runtime.js', 'traceur.js');
@ -55,6 +56,11 @@ exports.compile = function compile(options, paths, source) {
result.sourceMap = JSON.parse(sourceMapString); result.sourceMap = JSON.parse(sourceMapString);
} }
if (localOptions.outputLanguage === 'es6' && source.indexOf('$traceurRuntime') === -1) {
assert(result.js.indexOf('$traceurRuntime') === -1,
'Transpile to ES6 must not add references to $traceurRuntime, '
+ inputPath + ' is transpiled to:\n' + result.js);
}
return result; return result;
}; };