diff --git a/gulpfile.js b/gulpfile.js index b2ae0a1bc9..340f044e58 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -17,6 +17,7 @@ var rundartpackage = require('./tools/build/rundartpackage'); var multicopy = require('./tools/build/multicopy'); var karma = require('karma').server; var minimist = require('minimist'); +var es5build = require('./tools/build/es5build'); var DART_SDK = require('./tools/build/dartdetect')(gulp); // ----------------------- @@ -67,8 +68,14 @@ var CONFIG = { dest: { js: { all: 'dist/js', - dev: 'dist/js/dev', - prod: 'dist/js/prod', + dev: { + 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' }, dart: 'dist/dart', @@ -131,10 +138,12 @@ var CONFIG = { js: { dev: merge(true, _COMPILER_CONFIG_JS_DEFAULT, { typeAssertionModule: 'rtts_assert/rtts_assert', - typeAssertions: true + typeAssertions: true, + outputLanguage: 'es6' }), prod: merge(true, _COMPILER_CONFIG_JS_DEFAULT, { - typeAssertions: false + typeAssertions: false, + outputLanguage: 'es6' }) }, dart: { @@ -212,12 +221,12 @@ gulp.task('build/clean.docs', clean(gulp, gulpPlugins, { gulp.task('build/deps.js.dev', deps(gulp, gulpPlugins, { src: CONFIG.deps.js, - dest: CONFIG.dest.js.dev + dest: CONFIG.dest.js.dev.es5 })); gulp.task('build/deps.js.prod', deps(gulp, gulpPlugins, { src: CONFIG.deps.js, - dest: CONFIG.dest.js.prod + dest: CONFIG.dest.js.prod.es5 })); gulp.task('build/deps.js.dart2js', deps(gulp, gulpPlugins, { @@ -228,24 +237,50 @@ gulp.task('build/deps.js.dart2js', deps(gulp, gulpPlugins, { // ------------ // 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, copy: CONFIG.transpile.copy.js, - dest: CONFIG.dest.js.dev, - outputExt: 'js', + dest: CONFIG.dest.js.dev.es6, + outputExt: 'es6', options: CONFIG.transpile.options.js.dev, 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, copy: CONFIG.transpile.copy.js, - dest: CONFIG.dest.js.prod, - outputExt: 'js', + dest: CONFIG.dest.js.prod.es6, + outputExt: 'es6', options: CONFIG.transpile.options.js.prod, 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, { src: CONFIG.transpile.src.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, { src: CONFIG.html.src.js, - dest: CONFIG.dest.js.dev, + dest: CONFIG.dest.js.dev.es5, srcFolderInsertion: CONFIG.srcFolderInsertion.js, scriptsPerFolder: CONFIG.html.scriptsPerFolder.js })); gulp.task('build/html.js.prod', html(gulp, gulpPlugins, { src: CONFIG.html.src.js, - dest: CONFIG.dest.js.prod, + dest: CONFIG.dest.js.prod.es5, srcFolderInsertion: CONFIG.srcFolderInsertion.js, scriptsPerFolder: CONFIG.html.scriptsPerFolder.js })); @@ -354,12 +389,12 @@ gulp.task('build/format.dart', rundartpackage(gulp, gulpPlugins, { // ------------------ // web servers gulp.task('serve.js.dev', jsserve(gulp, gulpPlugins, { - path: CONFIG.dest.js.dev, + path: CONFIG.dest.js.dev.es5, port: 8000 })); gulp.task('serve.js.prod', jsserve(gulp, gulpPlugins, { - path: CONFIG.dest.js.prod, + path: CONFIG.dest.js.prod.es5, port: 8001 })); diff --git a/package.json b/package.json index 878841395e..dc45085667 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,9 @@ "node-uuid": "1.4.x" }, "devDependencies": { + "yargs": "2.3.*", + "gulp-sourcemaps": "1.3.*", + "gulp-traceur": "0.16.*", "angular": "1.3.5", "bower": "^1.3.12", "canonical-path": "0.0.2", diff --git a/tools/build/es5build.js b/tools/build/es5build.js new file mode 100755 index 0000000000..3956867856 --- /dev/null +++ b/tools/build/es5build.js @@ -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)); +}; diff --git a/tools/transpiler/gulp-traceur.js b/tools/transpiler/gulp-traceur.js index 5054c5c864..e86def0a34 100644 --- a/tools/transpiler/gulp-traceur.js +++ b/tools/transpiler/gulp-traceur.js @@ -34,7 +34,7 @@ function gulpTraceur(options, resolveModuleName) { if (sourceMap) { var sourceMapFile = cloneFile(file, { - path: file.path.replace(/\.js$/, '.map'), + path: file.path.replace(/\.\w+$/, '.map'), contents: JSON.stringify(sourceMap) }); diff --git a/tools/transpiler/index.js b/tools/transpiler/index.js index 7d12904bc0..022ae8be5d 100644 --- a/tools/transpiler/index.js +++ b/tools/transpiler/index.js @@ -4,6 +4,7 @@ var fs = require('fs'); var glob = require('glob'); var path = require('path'); var traceur = require('traceur'); +var assert = require('assert'); exports.RUNTIME_PATH = traceur.RUNTIME_PATH; 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); } + 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; };