From 17e8857efc418e81dd4c40ae5a3d24b0b94cbb35 Mon Sep 17 00:00:00 2001 From: Martin Probst Date: Fri, 3 Apr 2015 11:54:33 -0700 Subject: [PATCH] feat(dart): Use ts2dart for transpilation in Karma Dart. --- karma-dart.conf.js | 21 +++-------- package.json | 1 + .../transpiler/karma-ts2dart-preprocessor.js | 35 +++++++++++++++++++ 3 files changed, 41 insertions(+), 16 deletions(-) create mode 100644 tools/transpiler/karma-ts2dart-preprocessor.js diff --git a/karma-dart.conf.js b/karma-dart.conf.js index 1756386e65..cd42af73d0 100644 --- a/karma-dart.conf.js +++ b/karma-dart.conf.js @@ -58,25 +58,14 @@ module.exports = function(config) { }, preprocessors: { - 'modules/**/*.js': ['traceur'], - 'tools/**/*.js': ['traceur'] + 'modules/**/*.js': ['ts2dart'], + 'tools/**/*.js': ['ts2dart'] }, - traceurPreprocessor: { - options: { - outputLanguage: 'dart', - sourceMaps: true, - script: false, - modules: 'register', - memberVariables: true, - types: true, - // typeAssertions: true, - // typeAssertionModule: 'assert', - annotations: true - }, + ts2dartPreprocessor: { resolveModuleName: file2moduleName, transformPath: function(fileName) { - return fileName.replace('.js', '.dart'); + return fileName.replace(/.js$/, '.dart'); } }, @@ -91,5 +80,5 @@ module.exports = function(config) { }); - config.plugins.push(require('./tools/transpiler/karma-traceur-preprocessor')); + config.plugins.push(require('./tools/transpiler/karma-ts2dart-preprocessor')); }; diff --git a/package.json b/package.json index 0a1236779d..4f8175402b 100644 --- a/package.json +++ b/package.json @@ -96,6 +96,7 @@ "ternary-stream": "^1.2.3", "through2": "^0.6.1", "typescript": "alexeagle/TypeScript#93dbbe2a2d0b42cefd02ac949e4bc8ab6b5b5823", + "ts2dart": "^0.2.0", "vinyl": "^0.4.6", "walk-sync": "^0.1.3", "xtend": "^4.0.0", diff --git a/tools/transpiler/karma-ts2dart-preprocessor.js b/tools/transpiler/karma-ts2dart-preprocessor.js new file mode 100644 index 0000000000..36d47ef972 --- /dev/null +++ b/tools/transpiler/karma-ts2dart-preprocessor.js @@ -0,0 +1,35 @@ +// Transpiles JavaScript and TypeScript code to Dart using ts2dart. + +var ts2dart = require('ts2dart'); +var rundartpackage = require('../build/rundartpackage.js'); + +module.exports = { + 'preprocessor:ts2dart': ['factory', createTs2DartPreprocessor] +}; + +function createTs2DartPreprocessor(logger, basePath, config, emitter) { + var log = logger.create('ts2dart'); + return function(content, file, done) { + try { + var moduleName = config.resolveModuleName(file.originalPath); + file.path = config.transformPath(file.originalPath); + var transpiler = new ts2dart.Transpiler(); + var transpiledContent = transpiler.translateFile(file.originalPath, moduleName); + // TODO(martinprobst): Source maps. + done(null, transpiledContent); + } 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('ts2dart compile errors:\n' + errorString)); + } + }; +} + +createTs2DartPreprocessor + .$inject = ['logger', 'config.basePath', 'config.ts2dartPreprocessor', 'emitter'];