chore: karma with JS, Dart

Note: karma with dart is still not working
because of how `karma-dart` loads `package:…` dependencies.

Usage:
```
karma start karma-js.conf.js
karma start karma-dart.conf.js
```

Make sure to set `DARTIUM_BIN` env variable.

Refactors `js2dart`:
- live outside of the traceur module (`tools/js2dart/index.js`)
  so it can be reused by gulp and karma
- automatically build the sources in memory,
  so that `js2dart` can be used without running `gulp build` first
- provide a way to specify the moduleName of a compilation run
  independently of the input filename. This helps error messages
  and source maps (not yet enabled) to report the correct file name

Changes project setup:
- add module `test_lib` that contains the primitives for tests
  (e.g. `describe`, `it`, …)
- clean up some sources that had errors in them
- module names in transpiled js and dart files don’t contain
  `lib`, `test` nor `src` any more (e.g. `di/di`).
This commit is contained in:
Vojta Jina 2014-09-25 14:04:46 -07:00 committed by Tobias Bosch
parent 6335fc407c
commit c3b442ea53
26 changed files with 335 additions and 235 deletions

View File

@ -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));
});

61
karma-dart.conf.js Normal file
View File

@ -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'));
};

50
karma-js.conf.js Normal file
View File

@ -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'));
};

View File

@ -0,0 +1,3 @@
// TODO: Remove these annotations in the JS traceur build as they are only needed in Dart
window.FIELD = function() {};

View File

@ -3,4 +3,5 @@ environment:
sdk: '>=1.4.0'
dependencies:
dev_dependencies:
unittest: '>=0.10.1 <0.12.0'
test_lib:
path: ../test_lib

View File

@ -9,4 +9,5 @@ dependencies:
facade:
path: ../facade
dev_dependencies:
unittest: '>=0.10.1 <0.12.0'
test_lib:
path: ../test_lib

View File

@ -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';

View File

@ -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');

View File

@ -5,4 +5,5 @@ dependencies:
facade:
path: ../facade
dev_dependencies:
unittest: '>=0.10.1 <0.12.0'
test_lib:
path: ../test_lib

3
modules/di/src/key.js Normal file
View File

@ -0,0 +1,3 @@
export class Key {
}

View File

@ -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)

View File

@ -3,4 +3,5 @@ environment:
sdk: '>=1.4.0'
dependencies:
dev_dependencies:
unittest: '>=0.10.1 <0.12.0'
test_lib:
path: ../test_lib

View File

@ -3,4 +3,5 @@ environment:
sdk: '>=1.4.0'
dependencies:
dev_dependencies:
unittest: '>=0.10.1 <0.12.0'
test_lib:
path: ../test_lib

View File

@ -0,0 +1,6 @@
name: test_lib
environment:
sdk: '>=1.4.0'
dependencies:
dev_dependencies:
guinness: ">=0.1.5 <0.2.0"

View File

@ -0,0 +1 @@
export 'package:guinness/guinness.dart' show describe, it, beforeEach, afterEach, expect;

View File

@ -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;

View File

@ -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"
}
}

View File

@ -2,9 +2,13 @@ name: angular
version: 0.0.0
authors:
- Vojta Jina <vojta.jina@gmail.com>
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'

12
test-main.js Normal file
View File

@ -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();
}
});

View File

@ -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'));
}

View File

@ -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);
}
}

View File

@ -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);
});

85
tools/js2dart/index.js Normal file
View File

@ -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;
}

View File

@ -1,61 +1,37 @@
var traceur = require('traceur');
var js2dart = require('./index.js');
var createTraceurPreprocessor = function(args, config, logger, helper) {
config = config || {};
var log = logger.create('preprocessor.traceur');
var defaultOptions = {
sourceMaps: false,
modules: 'amd'
module.exports = {
'preprocessor:traceur': ['factory', createJs2DartPreprocessor]
};
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);
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);
});
if (result.errors.length) {
return done(new Error('TRACEUR COMPILE ERRORS\n' + result.errors.join('\n')));
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]
};
createJs2DartPreprocessor.$inject = ['logger', 'config.basePath', 'config.traceurPreprocessor'];

View File

@ -25,7 +25,7 @@ export class Compiler extends TraceurCompiler {
write(tree, outputName = undefined, sourceRoot = undefined) {
if (this.options_.outputLanguage.toLowerCase() === 'dart') {
var writer = new DartTreeWriter(outputName);
var writer = new DartTreeWriter(this.options_.moduleName, outputName);
writer.visitAny(tree);
return writer.toString();
} else {

View File

@ -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