diff --git a/gulpfile.js b/gulpfile.js index 83b53a0972..e40833556e 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -21,19 +21,27 @@ var js2es5Options = { script: false, // parse as a module modules: 'register', typeAssertionModule: 'assert', - typeAssertions: true, - moduleName: true + typeAssertions: true }; var js2dartOptions = { annotations: true, // parse annotations types: true, // parse types script: false, // parse as a module - outputLanguage: 'dart', - moduleName: true + outputLanguage: 'dart' }; -var traceur = require('./tools/js2dart/gulp-traceur'); +var gulpTraceur = require('./tools/js2dart/gulp-traceur'); + +function resolveModuleName(fileName) { + var moduleName = fileName + .replace(/.*\/modules\//, '') + .replace(/\/src\//, '/') + .replace(/\/lib\//, '/') + .replace(/\/test\//, '/'); + return moduleName; +} + // --------- // rtts-assert and traceur runtime @@ -45,9 +53,9 @@ gulp.task('jsRuntime/build', function() { function createJsRuntimeTask(isWatch) { var srcFn = isWatch ? watch : gulp.src.bind(gulp); var rttsAssert = srcFn('tools/rtts-assert/src/assert.js') - .pipe(traceur(js2es5Options)) + .pipe(gulpTraceur(js2es5Options, resolveModuleName)) .pipe(gulp.dest('build/js')); - var traceurRuntime = srcFn('tools/js2dart/node_modules/traceur/bin/traceur-runtime.js') + var traceurRuntime = srcFn(gulpTraceur.RUNTIME_PATH) .pipe(gulp.dest('build/js')); return mergeStreams(rttsAssert, traceurRuntime); } @@ -57,7 +65,7 @@ function createJsRuntimeTask(isWatch) { var sourceTypeConfigs = { dart: { compiler: function() { - return traceur(js2dartOptions, true); + return gulpTraceur(js2dartOptions, resolveModuleName); }, transpileSrc: ['modules/**/*.js'], htmlSrc: ['modules/*/src/**/*.html'], @@ -78,7 +86,7 @@ var sourceTypeConfigs = { }, js: { compiler: function() { - return traceur(js2es5Options, true); + return gulpTraceur(js2es5Options, resolveModuleName); }, transpileSrc: ['modules/**/*.js', 'modules/**/*.es6'], htmlSrc: ['modules/*/src/**/*.html'], @@ -174,26 +182,12 @@ gulp.task('serve', connect.server({ gulp.task('clean', ['js2dart/clean', 'modules/clean']); -gulp.task('build', function() { - return runSequence( - // sequential - 'js2dart/build', - // parallel - ['jsRuntime/build', 'modules/build.dart', 'modules/build.js'] - ); -}); +gulp.task('build', ['jsRuntime/build', 'modules/build.dart', 'modules/build.js']); gulp.task('watch', function() { - runSequence('js2dart/test/watch'); - var js2dartWatch = watch(js2dartTasks.paths.js2dartSrc, function(_, done) { - runSequence( - // sequential - 'js2dart/build', 'js2dart/test', - // parallel - ['jsRuntime/build', 'modules/build.dart', 'modules/build.js'], - done); - }); + // parallel is important as both streams are infinite! + runSequence(['js2dart/test/watch', 'js2dart/src/watch']); var dartModuleWatch = createModuleTask(sourceTypeConfigs.dart, true); var jsModuleWatch = createModuleTask(sourceTypeConfigs.js, true); - return mergeStreams(js2dartWatch, dartModuleWatch, jsModuleWatch, createJsRuntimeTask(true)); + return mergeStreams(dartModuleWatch, jsModuleWatch, createJsRuntimeTask(true)); }); diff --git a/karma-dart.conf.js b/karma-dart.conf.js new file mode 100644 index 0000000000..81643b456e --- /dev/null +++ b/karma-dart.conf.js @@ -0,0 +1,61 @@ +// Karma configuration +// Generated on Thu Sep 25 2014 11:52:02 GMT-0700 (PDT) + +module.exports = function(config) { + config.set({ + + frameworks: ['dart-unittest'], + + files: [ + {pattern: 'packages/**/*.dart', included: false}, + {pattern: 'modules/*/src/**/*.js', included: false}, + {pattern: 'modules/*/test/**/*.js', included: true}, + {pattern: 'modules/**/*.dart', included: false}, + 'packages/browser/dart.js' + ], + + karmaDartImports: { + guinness: 'package:guinness/guinness_html.dart' + }, + + preprocessors: { + 'modules/**/*.js': ['traceur'] + }, + customFileHandlers: [{ + urlRegex: /.*\/packages\/.*$/, + handler: function(request, response, fa, fb, basePath) { + var url = request.url; + var path = url.indexOf('?') > -1 ? url.substring(0, url.indexOf('?')) : url; + var contets = fs.readFileSync(basePath + path); + response.writeHead(200); + response.end(contets); + } + }], + traceurPreprocessor: { + options: { + outputLanguage: 'dart', + script: false, + modules: 'register', + types: true, + // typeAssertions: true, + // typeAssertionModule: 'assert', + annotations: true + }, + resolveModuleName: function(fileName) { + var moduleName = fileName + .replace(/.*\/modules\//, '') + .replace(/\/src\//, '/') + .replace(/\/test\//, '/'); + return moduleName; + }, + transformPath: function(fileName) { + return fileName.replace('.js', '.dart'); + } + }, + + browsers: ['Dartium'] + }); + + + config.plugins.push(require('./tools/js2dart/karma-traceur-preprocessor')); +}; diff --git a/karma-js.conf.js b/karma-js.conf.js new file mode 100644 index 0000000000..8f91d870eb --- /dev/null +++ b/karma-js.conf.js @@ -0,0 +1,50 @@ +// Karma configuration +// Generated on Thu Sep 25 2014 11:52:02 GMT-0700 (PDT) + +module.exports = function(config) { + config.set({ + + frameworks: ['jasmine'], + + files: [ + 'node_modules/traceur/bin/traceur-runtime.js', + './karma-mock-annotations.js', + 'modules/**/test_lib/**/*.es6', + 'modules/**/*.js', + 'modules/**/*.es6', + 'test-main.js' + ], + + preprocessors: { + 'modules/**/*.js': ['traceur'], + 'modules/**/*.es6': ['traceur'] + }, + + traceurPreprocessor: { + options: { + outputLanguage: 'es5', + script: false, + modules: 'register', + types: true, + // TODO: turn this on! + // typeAssertions: true, + // typeAssertionModule: 'assert', + annotations: true + }, + resolveModuleName: function(fileName) { + var moduleName = fileName + .replace(/.*\/modules\//, '') + .replace(/\/src\//, '/') + .replace(/\/test\//, '/'); + return moduleName; + }, + transformPath: function(fileName) { + return fileName.replace('.es6', ''); + } + }, + + browsers: ['Chrome'] + }); + + config.plugins.push(require('./tools/js2dart/karma-traceur-preprocessor')); +}; diff --git a/karma-mock-annotations.js b/karma-mock-annotations.js new file mode 100644 index 0000000000..c59b287bac --- /dev/null +++ b/karma-mock-annotations.js @@ -0,0 +1,3 @@ + +// TODO: Remove these annotations in the JS traceur build as they are only needed in Dart +window.FIELD = function() {}; \ No newline at end of file diff --git a/modules/change_detection/pubspec.yaml b/modules/change_detection/pubspec.yaml index cbcecb376a..70f6b4688a 100644 --- a/modules/change_detection/pubspec.yaml +++ b/modules/change_detection/pubspec.yaml @@ -3,4 +3,5 @@ environment: sdk: '>=1.4.0' dependencies: dev_dependencies: - unittest: '>=0.10.1 <0.12.0' + test_lib: + path: ../test_lib diff --git a/modules/core/pubspec.yaml b/modules/core/pubspec.yaml index 56df496cee..79e6e01bb4 100644 --- a/modules/core/pubspec.yaml +++ b/modules/core/pubspec.yaml @@ -9,4 +9,5 @@ dependencies: facade: path: ../facade dev_dependencies: - unittest: '>=0.10.1 <0.12.0' + test_lib: + path: ../test_lib diff --git a/modules/core/src/compiler/compiler.js b/modules/core/src/compiler/compiler.js index 3ff9b4471a..0c4290a1e6 100644 --- a/modules/core/src/compiler/compiler.js +++ b/modules/core/src/compiler/compiler.js @@ -1,4 +1,4 @@ -import {Future} from 'facade/lang'; +import {Future, Type} from 'facade/lang'; import {Element} from 'facade/dom'; import {ProtoView} from '../view/proto_view'; import {TemplateLoader} from './template_loader'; diff --git a/modules/core/test/compiler/compiler_spec.js b/modules/core/test/compiler/compiler_spec.js index f4d50cd01e..aef400851f 100644 --- a/modules/core/test/compiler/compiler_spec.js +++ b/modules/core/test/compiler/compiler_spec.js @@ -1,6 +1,7 @@ -import {describe, id} from 'spec/spec'; +import {describe, id} from 'test_lib/test_lib'; +import {Compiler} from './compiler'; -function main() { +export function main() { describe('compiler', () => { it('should hello', () => { print('I am working'); diff --git a/modules/di/pubspec.yaml b/modules/di/pubspec.yaml index 29ee7a0ee0..47f23d1078 100644 --- a/modules/di/pubspec.yaml +++ b/modules/di/pubspec.yaml @@ -5,4 +5,5 @@ dependencies: facade: path: ../facade dev_dependencies: - unittest: '>=0.10.1 <0.12.0' + test_lib: + path: ../test_lib diff --git a/modules/di/src/key.js b/modules/di/src/key.js new file mode 100644 index 0000000000..3495ba68ce --- /dev/null +++ b/modules/di/src/key.js @@ -0,0 +1,3 @@ +export class Key { + +} \ No newline at end of file diff --git a/modules/di/src/module.js b/modules/di/src/module.js index ed453bbb8e..62cf0bb562 100644 --- a/modules/di/src/module.js +++ b/modules/di/src/module.js @@ -1,5 +1,6 @@ import {Type} from 'facade/lang'; import {Map, MapWrapper wraps Map} from 'facade/collection'; +import {Key} from './key'; /// becouse we need to know when toValue was not set. /// (it could be that toValue is set to null or undefined in js) diff --git a/modules/examples/pubspec.yaml b/modules/examples/pubspec.yaml index 03b2478582..4fef0f5046 100644 --- a/modules/examples/pubspec.yaml +++ b/modules/examples/pubspec.yaml @@ -3,4 +3,5 @@ environment: sdk: '>=1.4.0' dependencies: dev_dependencies: - unittest: '>=0.10.1 <0.12.0' + test_lib: + path: ../test_lib diff --git a/modules/facade/pubspec.yaml b/modules/facade/pubspec.yaml index e5bd9d2b19..7814562608 100644 --- a/modules/facade/pubspec.yaml +++ b/modules/facade/pubspec.yaml @@ -3,4 +3,5 @@ environment: sdk: '>=1.4.0' dependencies: dev_dependencies: - unittest: '>=0.10.1 <0.12.0' + test_lib: + path: ../test_lib diff --git a/modules/test_lib/pubspec.yaml b/modules/test_lib/pubspec.yaml new file mode 100644 index 0000000000..5e4aefe794 --- /dev/null +++ b/modules/test_lib/pubspec.yaml @@ -0,0 +1,6 @@ +name: test_lib +environment: + sdk: '>=1.4.0' +dependencies: +dev_dependencies: + guinness: ">=0.1.5 <0.2.0" diff --git a/modules/test_lib/src/test_lib.dart b/modules/test_lib/src/test_lib.dart new file mode 100644 index 0000000000..812d464e61 --- /dev/null +++ b/modules/test_lib/src/test_lib.dart @@ -0,0 +1 @@ +export 'package:guinness/guinness.dart' show describe, it, beforeEach, afterEach, expect; diff --git a/modules/test_lib/src/test_lib.es6 b/modules/test_lib/src/test_lib.es6 new file mode 100644 index 0000000000..cfe98c6c19 --- /dev/null +++ b/modules/test_lib/src/test_lib.es6 @@ -0,0 +1,8 @@ +export var describe = window.describe; +export var it = window.it; +export var beforeEach = window.beforeEach; +export var afterEach = window.afterEach; +export var expect = window.expect; + +// To make testing consistent between dart and js +window.print = window.dump || window.console.log; diff --git a/package.json b/package.json index d06447da5a..3fb7d97630 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,10 @@ "gulp-rename": "^1.2.0", "gulp-shell": "^0.2.9", "gulp-watch": "^1.0.3", + "karma": "^0.12.23", + "karma-chrome-launcher": "^0.1.4", + "karma-dart": "^0.2.8", + "karma-jasmine": "^0.2.2", "q": "^1.0.1", "through2": "^0.6.1", "event-stream": "^3.1.5", @@ -19,6 +23,7 @@ "gulp-rimraf": "^0.1.0", "run-sequence": "^0.3.6", "glob": "^4.0.6", - "gulp-ejs": "^0.3.1" - } + "gulp-ejs": "^0.3.1", + "traceur": "0.0.66" + } } diff --git a/pubspec.yaml b/pubspec.yaml index 6cedec6afc..0a174e8213 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,9 +2,13 @@ name: angular version: 0.0.0 authors: - Vojta Jina -description: Compile JavaScript to Dart so that you can compile it back to JavaScript and run. +description: Angular environment: sdk: '>=1.4.0' dependencies: dev_dependencies: + test_lib: + path: modules/test_lib unittest: '>=0.10.1 <0.12.0' + guinness: ">=0.1.5 <0.2.0" + browser: '>=0.10.0 <0.11.0' diff --git a/test-main.js b/test-main.js new file mode 100644 index 0000000000..536f040d2f --- /dev/null +++ b/test-main.js @@ -0,0 +1,12 @@ +var TEST_REGEXP = /^\/base\/modules\/[^\/]*\/test\/.*/; + +Object.keys(window.__karma__.files).forEach(function(path) { + if (TEST_REGEXP.test(path)) { + var moduleName = path + .replace(/.*\/modules\//, '') + .replace(/\/src\//, '/') + .replace(/\/test\//, '/') + .replace(/\.\w*$/, ''); + System.get(moduleName).main(); + } +}); diff --git a/tools/js2dart/gulp-tasks.js b/tools/js2dart/gulp-tasks.js index 2c4a2af844..de2cfac5ad 100644 --- a/tools/js2dart/gulp-tasks.js +++ b/tools/js2dart/gulp-tasks.js @@ -12,7 +12,6 @@ var traceurDir = baseDir+'/../traceur'; var buildDir = baseDir + '/build'; var paths = { - traceurSrc: traceurDir+'/src/**/*.js', js2dartSrc: baseDir + '/src/**/*.js', specTranspile: baseDir + '/spec/**/*.js', specTemplates: baseDir + '/spec/**/*.template', @@ -26,29 +25,11 @@ module.exports.paths = paths; function install(gulp) { var runSequence = require('run-sequence').use(gulp); - // -- js2dart - var buildJs2DartOptions = { - modules: 'register', - moduleName: true, - referrer: 'js2dart/src/', - script: false // parse as a module - }; - - var js2dartOptions = { + var spec2dartOptions = { annotations: true, // parse annotations types: true, // parse types script: false, // parse as a module - outputLanguage: 'dart', - moduleName: true - }; - - var js2es5Options = { - annotations: true, // parse annotations - types: true, // parse types - script: false, // parse as a module - modules: 'register', - typeAssertions: true, - moduleName: true + outputLanguage: 'dart' }; gulp.task('js2dart/clean', function() { @@ -56,14 +37,6 @@ function install(gulp) { .pipe(clean()); }); - gulp.task('js2dart/build', function() { - return gulp - .src(paths.js2dartSrc) - .pipe(gulpTraceur(buildJs2DartOptions, false)) - .pipe(gulp.dest(buildDir + '/js2dart')) - .on('end', gulpTraceur.reloadPatches); - }); - gulp.task('js2dart/test/build', function() { return mergeStreams(specTranspile(false), specCopy(false), specRunner(false)); }); @@ -76,6 +49,13 @@ function install(gulp) { runSequence('js2dart/test/build', 'js2dart/test/run', done); }); + gulp.task('js2dart/src/watch', function(done) { + return watch(paths.js2dartSrc, function(changes, done) { + gulpTraceur.sourcesChanged(); + runSequence('js2dart/test', done); + }); + }); + gulp.task('js2dart/test/watch', function(done) { var streams = []; streams.push(specTranspile(true) @@ -93,7 +73,7 @@ function install(gulp) { function specTranspile(isWatch) { var srcFn = isWatch ? watch : gulp.src.bind(gulp); return srcFn(paths.specTranspile) - .pipe(gulpTraceur(js2dartOptions, true)) + .pipe(gulpTraceur(spec2dartOptions)) .pipe(rename({extname: '.dart'})) .pipe(gulp.dest(buildDir+'/spec')); } diff --git a/tools/js2dart/gulp-traceur.js b/tools/js2dart/gulp-traceur.js index 39f4cbca86..e59390a1d0 100644 --- a/tools/js2dart/gulp-traceur.js +++ b/tools/js2dart/gulp-traceur.js @@ -1,22 +1,12 @@ 'use strict'; var through = require('through2'); -var fs = require('fs'); -var path = require('path'); -var originalTraceur = require('traceur'); -var glob = require('glob'); +var compiler = require('./index'); module.exports = gulpTraceur; -gulpTraceur.reloadPatches = function() { - loadPatches(true); -}; -gulpTraceur.loadPatches = loadPatches; +gulpTraceur.RUNTIME_PATH = compiler.RUNTIME_PATH; +gulpTraceur.sourcesChanged = compiler.sourcesChanged; - -/// usePatches: whether to use plain traceur or apply -/// our patches... -function gulpTraceur(options, usePatches) { - var lastLoadCounter = loadCounter; - var lastCompiler = null; +function gulpTraceur(options, resolveModuleName) { options = options || {}; return through.obj(function (file, enc, done) { @@ -29,14 +19,13 @@ function gulpTraceur(options, usePatches) { throw new Error('gulp-traceur: Streaming not supported'); } - var compiler = createCompilerIfNeeded(); - var ret; try { - var fileName = file.relative; - if (options.referrer) { - fileName = options.referrer + '/' + fileName; - } - var compiled = compiler.compile(file.contents.toString(), fileName, fileName); + var moduleName = resolveModuleName ? resolveModuleName(file.relative) : null; + var compiled = compiler.compile(options, { + inputPath: file.relative, + outputPath: file.relative, + moduleName: moduleName + }, file.contents.toString()); file.contents = new Buffer(compiled); this.push(file); done(); @@ -48,79 +37,4 @@ function gulpTraceur(options, usePatches) { } } }); - - function createCompilerIfNeeded() { - loadPatches(false); - if (!lastCompiler || lastLoadCounter !== loadCounter) { - lastLoadCounter = loadCounter; - var CompilerBase; - if (usePatches) { - CompilerBase = System.get('js2dart/src/compiler').Compiler; - } else { - CompilerBase = System.get(System.map.traceur+'/src/Compiler').Compiler; - } - var Compiler = createCompilerConstructor(CompilerBase); - lastCompiler = new Compiler(options); - } - return lastCompiler; - } }; - -function createCompilerConstructor(CompilerBase) { - // See traceur/src/NodeCompiler.js - // Needed here as we want to be able to reload - // traceur sources once they changed - function NodeCompiler(options, sourceRoot) { - var sourceRoot = sourceRoot || process.cwd(); - CompilerBase.call(this, options, sourceRoot); - } - - NodeCompiler.prototype = { - __proto__: CompilerBase.prototype, - - resolveModuleName: function(filename) { - debugger; - if (!filename) - return; - var moduleName = filename.replace(/\.js$/, ''); - return path.relative(this.sourceRoot, moduleName).replace(/\\/g,'/'); - }, - - sourceName: function(filename) { - return path.relative(this.sourceRoot, filename); - } - } - - return NodeCompiler; -} - -var loadCounter = 0; -function loadPatches(reload) { - if (loadCounter && !reload) { - return; - } - loadCounter++; - // see traceur/src/traceur.js - // To reload the js2dart modules we need - // to clear the registry. To do that we - // reload the traceur module... - loadModule(path.dirname(module.filename), './node_modules/traceur/bin/traceur.js'); - - var buildDir = __dirname + '/build/js2dart'; - var moduleNames = [].slice.call(glob.sync('**/*.js', { - cwd: buildDir - })); - moduleNames.forEach(function(filename) { - loadModule(buildDir, filename); - }); - - function loadModule(baseFolder, filename) { - filename = path.join(baseFolder, filename); - var data = fs.readFileSync(filename, 'utf8'); - if (!data) - throw new Error('Failed to import ' + filename); - - ('global', eval)(data); - } - -} diff --git a/tools/js2dart/gulpfile.js b/tools/js2dart/gulpfile.js index f6ef366e1a..636631ef00 100644 --- a/tools/js2dart/gulpfile.js +++ b/tools/js2dart/gulpfile.js @@ -6,20 +6,13 @@ var mergeStreams = require('event-stream').merge; tasks.install(gulp); -gulp.task('build', function() { - return runSequence('js2dart/build'); -}); - gulp.task('test', function() { - return runSequence('build', 'js2dart/test'); + return runSequence('js2dart/test'); }); gulp.task('clean', ['js2dart/clean']); -gulp.task('watch', function() { - var js2dartWatch = watch(tasks.paths.js2dartSrc, function(_, done) { - runSequence('js2dart/build', 'js2dart/test', done); - }); - runSequence('js2dart/test/watch'); - return js2dartWatch; +gulp.task('watch', function(done) { + // parallel is important as both streams are infinite! + runSequence(['js2dart/test/watch', 'js2dart/src/watch'], done); }); \ No newline at end of file diff --git a/tools/js2dart/index.js b/tools/js2dart/index.js new file mode 100644 index 0000000000..51d48535db --- /dev/null +++ b/tools/js2dart/index.js @@ -0,0 +1,85 @@ +// Entry point for Node. + +var fs = require('fs'); +var glob = require('glob'); +var path = require('path'); +var traceur = require('traceur'); + +exports.RUNTIME_PATH = traceur.RUNTIME_PATH; +var TRACEUR_PATH = traceur.RUNTIME_PATH.replace('traceur-runtime.js', 'traceur.js'); +var SELF_SOURCE_REGEX = /js2dart\/src/; +var SELF_COMPILE_OPTIONS = { + modules: 'register', + moduleName: true, + script: false // parse as a module +}; + +var needsReload = true; + +// TODO(vojta): call this if sources changed +exports.sourcesChanged = function() { + needsReload = true; +}; + +exports.compile = function compile(options, paths, source) { + if (needsReload) { + reloadCompiler(); + needsReload = false; + } + var inputPath, outputPath, moduleName; + if (typeof paths === 'string') { + inputPath = outputPath = paths; + } else { + inputPath = paths.inputPath; + outputPath = paths.inputPath; + moduleName = paths.moduleName; + } + outputPath = outputPath || inputPath; + moduleName = moduleName || inputPath; + moduleName = moduleName.replace(/\.\w*$/, ''); + + var localOptions = extend(options, { + moduleName: moduleName + }); + var CompilerCls = System.get('js2dart/src/compiler').Compiler; + return (new CompilerCls(localOptions)).compile(source, inputPath, outputPath); +}; + +// Transpile and evaluate the code in `src`. +// Use existing traceur to compile our sources. +function reloadCompiler() { + loadModule(TRACEUR_PATH, false); + glob.sync(__dirname + '/src/**/*.js').forEach(function(fileName) { + loadModule(fileName, true); + }); +} + +function loadModule(filepath, transpile) { + var data = fs.readFileSync(filepath, 'utf8'); + + if (!data) { + throw new Error('Failed to import ' + filepath); + } + + if (transpile) { + var moduleName = filepath + .replace(__dirname, 'js2dart') + .replace(/\.\w*$/, ''); + data = (new traceur.NodeCompiler( + extend(SELF_COMPILE_OPTIONS, { moduleName: moduleName } ) + )).compile(data, filepath, filepath); + } + + ('global', eval)(data); +} + +function extend(source, props) { + var res = {}; + for (var prop in source) { + res[prop] = source[prop]; + } + for (var prop in props) { + res[prop] = props[prop]; + } + return res; +} \ No newline at end of file diff --git a/tools/js2dart/karma-traceur-preprocessor.js b/tools/js2dart/karma-traceur-preprocessor.js index 10a937a748..f7a247242f 100644 --- a/tools/js2dart/karma-traceur-preprocessor.js +++ b/tools/js2dart/karma-traceur-preprocessor.js @@ -1,61 +1,37 @@ -var traceur = require('traceur'); +var js2dart = require('./index.js'); -var createTraceurPreprocessor = function(args, config, logger, helper) { - config = config || {}; +module.exports = { + 'preprocessor:traceur': ['factory', createJs2DartPreprocessor] +}; - var log = logger.create('preprocessor.traceur'); - var defaultOptions = { - sourceMaps: false, - modules: 'amd' - }; - var options = helper.merge(defaultOptions, args.options || {}, config.options || {}); - - var transformPath = args.transformPath || config.transformPath || function(filepath) { - return filepath.replace(/\.es6.js$/, '.js').replace(/\.es6$/, '.js'); - }; +function createJs2DartPreprocessor(logger, basePath, config) { + var log = logger.create('traceur'); return function(content, file, done) { - log.debug('Processing "%s".', file.originalPath); - file.path = transformPath(file.originalPath); - options.filename = file.originalPath; - var result = traceur.compile(content, options); - var transpiledContent = result.js; - - result.errors.forEach(function(err) { - log.error(err); - }); - - if (result.errors.length) { - return done(new Error('TRACEUR COMPILE ERRORS\n' + result.errors.join('\n'))); + try { + var moduleName = config.resolveModuleName(file.originalPath); + if (config.transformPath) { + file.path = config.transformPath(file.originalPath); + } + done(null, js2dart.compile(config.options, { + inputPath: file.originalPath, + moduleName: moduleName + }, content)); + } catch (errors) { + var errorString; + if (errors.forEach) { + errors.forEach(function(error) { + log.error(error); + }); + errorString = errors.join('\n'); + } else { + log.error(errors); + errorString = errors; + } + done(new Error('TRACEUR COMPILE ERRORS\n' + errorString)); } - - // TODO(vojta): Tracer should return JS object, rather than a string. - if (result.generatedSourceMap) { - var map = JSON.parse(result.generatedSourceMap); - map.file = file.path; - transpiledContent += '\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,'; - transpiledContent += new Buffer(JSON.stringify(map)).toString('base64') + '\n'; - - file.sourceMap = map; - } - - return done(null, transpiledContent); }; -}; +} -createTraceurPreprocessor.$inject = ['args', 'config.traceurPreprocessor', 'logger', 'helper']; - - -var initTraceurFramework = function(files) { - files.unshift({pattern: traceur.RUNTIME_PATH, included: true, served: true, watched: false}); -}; - -initTraceurFramework.$inject = ['config.files']; - - -// PUBLISH DI MODULE -module.exports = { - 'preprocessor:traceur': ['factory', createTraceurPreprocessor], - 'framework:traceur': ['factory', initTraceurFramework] -}; \ No newline at end of file +createJs2DartPreprocessor.$inject = ['logger', 'config.basePath', 'config.traceurPreprocessor']; diff --git a/tools/js2dart/src/compiler.js b/tools/js2dart/src/compiler.js index 2724c44619..04d6b54a05 100644 --- a/tools/js2dart/src/compiler.js +++ b/tools/js2dart/src/compiler.js @@ -24,8 +24,8 @@ export class Compiler extends TraceurCompiler { } write(tree, outputName = undefined, sourceRoot = undefined) { - if (this.options_.outputLanguage.toLowerCase() === 'dart') { - var writer = new DartTreeWriter(outputName); + if (this.options_.outputLanguage.toLowerCase() === 'dart') { + var writer = new DartTreeWriter(this.options_.moduleName, outputName); writer.visitAny(tree); return writer.toString(); } else { diff --git a/tools/js2dart/src/dart_writer.js b/tools/js2dart/src/dart_writer.js index e8999945bb..e0a23e4617 100644 --- a/tools/js2dart/src/dart_writer.js +++ b/tools/js2dart/src/dart_writer.js @@ -4,13 +4,11 @@ import {EQUAL_EQUAL_EQUAL, OPEN_PAREN, CLOSE_PAREN, IMPORT, SEMI_COLON, STAR, OP import {ParseTreeWriter as JavaScriptParseTreeWriter} from 'traceur/src/outputgeneration/ParseTreeWriter'; export class DartTreeWriter extends JavaScriptParseTreeWriter { - constructor(moduleName) { - super(); + constructor(moduleName, outputPath) { + super(outputPath); this.libName = moduleName .replace(/\//g, '.') - .replace(/[^\w.]/g, '_') - .replace('.lib.', '.') - .replace(/\.dart$/, ''); + .replace(/[^\w.]/g, '_'); } // VARIABLES - types