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:
parent
6335fc407c
commit
c3b442ea53
48
gulpfile.js
48
gulpfile.js
|
@ -21,19 +21,27 @@ var js2es5Options = {
|
||||||
script: false, // parse as a module
|
script: false, // parse as a module
|
||||||
modules: 'register',
|
modules: 'register',
|
||||||
typeAssertionModule: 'assert',
|
typeAssertionModule: 'assert',
|
||||||
typeAssertions: true,
|
typeAssertions: true
|
||||||
moduleName: true
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var js2dartOptions = {
|
var js2dartOptions = {
|
||||||
annotations: true, // parse annotations
|
annotations: true, // parse annotations
|
||||||
types: true, // parse types
|
types: true, // parse types
|
||||||
script: false, // parse as a module
|
script: false, // parse as a module
|
||||||
outputLanguage: 'dart',
|
outputLanguage: 'dart'
|
||||||
moduleName: true
|
|
||||||
};
|
};
|
||||||
|
|
||||||
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
|
// rtts-assert and traceur runtime
|
||||||
|
@ -45,9 +53,9 @@ gulp.task('jsRuntime/build', function() {
|
||||||
function createJsRuntimeTask(isWatch) {
|
function createJsRuntimeTask(isWatch) {
|
||||||
var srcFn = isWatch ? watch : gulp.src.bind(gulp);
|
var srcFn = isWatch ? watch : gulp.src.bind(gulp);
|
||||||
var rttsAssert = srcFn('tools/rtts-assert/src/assert.js')
|
var rttsAssert = srcFn('tools/rtts-assert/src/assert.js')
|
||||||
.pipe(traceur(js2es5Options))
|
.pipe(gulpTraceur(js2es5Options, resolveModuleName))
|
||||||
.pipe(gulp.dest('build/js'));
|
.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'));
|
.pipe(gulp.dest('build/js'));
|
||||||
return mergeStreams(rttsAssert, traceurRuntime);
|
return mergeStreams(rttsAssert, traceurRuntime);
|
||||||
}
|
}
|
||||||
|
@ -57,7 +65,7 @@ function createJsRuntimeTask(isWatch) {
|
||||||
var sourceTypeConfigs = {
|
var sourceTypeConfigs = {
|
||||||
dart: {
|
dart: {
|
||||||
compiler: function() {
|
compiler: function() {
|
||||||
return traceur(js2dartOptions, true);
|
return gulpTraceur(js2dartOptions, resolveModuleName);
|
||||||
},
|
},
|
||||||
transpileSrc: ['modules/**/*.js'],
|
transpileSrc: ['modules/**/*.js'],
|
||||||
htmlSrc: ['modules/*/src/**/*.html'],
|
htmlSrc: ['modules/*/src/**/*.html'],
|
||||||
|
@ -78,7 +86,7 @@ var sourceTypeConfigs = {
|
||||||
},
|
},
|
||||||
js: {
|
js: {
|
||||||
compiler: function() {
|
compiler: function() {
|
||||||
return traceur(js2es5Options, true);
|
return gulpTraceur(js2es5Options, resolveModuleName);
|
||||||
},
|
},
|
||||||
transpileSrc: ['modules/**/*.js', 'modules/**/*.es6'],
|
transpileSrc: ['modules/**/*.js', 'modules/**/*.es6'],
|
||||||
htmlSrc: ['modules/*/src/**/*.html'],
|
htmlSrc: ['modules/*/src/**/*.html'],
|
||||||
|
@ -174,26 +182,12 @@ gulp.task('serve', connect.server({
|
||||||
|
|
||||||
gulp.task('clean', ['js2dart/clean', 'modules/clean']);
|
gulp.task('clean', ['js2dart/clean', 'modules/clean']);
|
||||||
|
|
||||||
gulp.task('build', function() {
|
gulp.task('build', ['jsRuntime/build', 'modules/build.dart', 'modules/build.js']);
|
||||||
return runSequence(
|
|
||||||
// sequential
|
|
||||||
'js2dart/build',
|
|
||||||
// parallel
|
|
||||||
['jsRuntime/build', 'modules/build.dart', 'modules/build.js']
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
gulp.task('watch', function() {
|
gulp.task('watch', function() {
|
||||||
runSequence('js2dart/test/watch');
|
// parallel is important as both streams are infinite!
|
||||||
var js2dartWatch = watch(js2dartTasks.paths.js2dartSrc, function(_, done) {
|
runSequence(['js2dart/test/watch', 'js2dart/src/watch']);
|
||||||
runSequence(
|
|
||||||
// sequential
|
|
||||||
'js2dart/build', 'js2dart/test',
|
|
||||||
// parallel
|
|
||||||
['jsRuntime/build', 'modules/build.dart', 'modules/build.js'],
|
|
||||||
done);
|
|
||||||
});
|
|
||||||
var dartModuleWatch = createModuleTask(sourceTypeConfigs.dart, true);
|
var dartModuleWatch = createModuleTask(sourceTypeConfigs.dart, true);
|
||||||
var jsModuleWatch = createModuleTask(sourceTypeConfigs.js, true);
|
var jsModuleWatch = createModuleTask(sourceTypeConfigs.js, true);
|
||||||
return mergeStreams(js2dartWatch, dartModuleWatch, jsModuleWatch, createJsRuntimeTask(true));
|
return mergeStreams(dartModuleWatch, jsModuleWatch, createJsRuntimeTask(true));
|
||||||
});
|
});
|
||||||
|
|
|
@ -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'));
|
||||||
|
};
|
|
@ -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'));
|
||||||
|
};
|
|
@ -0,0 +1,3 @@
|
||||||
|
|
||||||
|
// TODO: Remove these annotations in the JS traceur build as they are only needed in Dart
|
||||||
|
window.FIELD = function() {};
|
|
@ -3,4 +3,5 @@ environment:
|
||||||
sdk: '>=1.4.0'
|
sdk: '>=1.4.0'
|
||||||
dependencies:
|
dependencies:
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
unittest: '>=0.10.1 <0.12.0'
|
test_lib:
|
||||||
|
path: ../test_lib
|
||||||
|
|
|
@ -9,4 +9,5 @@ dependencies:
|
||||||
facade:
|
facade:
|
||||||
path: ../facade
|
path: ../facade
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
unittest: '>=0.10.1 <0.12.0'
|
test_lib:
|
||||||
|
path: ../test_lib
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import {Future} from 'facade/lang';
|
import {Future, Type} from 'facade/lang';
|
||||||
import {Element} from 'facade/dom';
|
import {Element} from 'facade/dom';
|
||||||
import {ProtoView} from '../view/proto_view';
|
import {ProtoView} from '../view/proto_view';
|
||||||
import {TemplateLoader} from './template_loader';
|
import {TemplateLoader} from './template_loader';
|
||||||
|
|
|
@ -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', () => {
|
describe('compiler', () => {
|
||||||
it('should hello', () => {
|
it('should hello', () => {
|
||||||
print('I am working');
|
print('I am working');
|
||||||
|
|
|
@ -5,4 +5,5 @@ dependencies:
|
||||||
facade:
|
facade:
|
||||||
path: ../facade
|
path: ../facade
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
unittest: '>=0.10.1 <0.12.0'
|
test_lib:
|
||||||
|
path: ../test_lib
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
export class Key {
|
||||||
|
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
import {Type} from 'facade/lang';
|
import {Type} from 'facade/lang';
|
||||||
import {Map, MapWrapper wraps Map} from 'facade/collection';
|
import {Map, MapWrapper wraps Map} from 'facade/collection';
|
||||||
|
import {Key} from './key';
|
||||||
|
|
||||||
/// becouse we need to know when toValue was not set.
|
/// becouse we need to know when toValue was not set.
|
||||||
/// (it could be that toValue is set to null or undefined in js)
|
/// (it could be that toValue is set to null or undefined in js)
|
||||||
|
|
|
@ -3,4 +3,5 @@ environment:
|
||||||
sdk: '>=1.4.0'
|
sdk: '>=1.4.0'
|
||||||
dependencies:
|
dependencies:
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
unittest: '>=0.10.1 <0.12.0'
|
test_lib:
|
||||||
|
path: ../test_lib
|
||||||
|
|
|
@ -3,4 +3,5 @@ environment:
|
||||||
sdk: '>=1.4.0'
|
sdk: '>=1.4.0'
|
||||||
dependencies:
|
dependencies:
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
unittest: '>=0.10.1 <0.12.0'
|
test_lib:
|
||||||
|
path: ../test_lib
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
name: test_lib
|
||||||
|
environment:
|
||||||
|
sdk: '>=1.4.0'
|
||||||
|
dependencies:
|
||||||
|
dev_dependencies:
|
||||||
|
guinness: ">=0.1.5 <0.2.0"
|
|
@ -0,0 +1 @@
|
||||||
|
export 'package:guinness/guinness.dart' show describe, it, beforeEach, afterEach, expect;
|
|
@ -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;
|
|
@ -12,6 +12,10 @@
|
||||||
"gulp-rename": "^1.2.0",
|
"gulp-rename": "^1.2.0",
|
||||||
"gulp-shell": "^0.2.9",
|
"gulp-shell": "^0.2.9",
|
||||||
"gulp-watch": "^1.0.3",
|
"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",
|
"q": "^1.0.1",
|
||||||
"through2": "^0.6.1",
|
"through2": "^0.6.1",
|
||||||
"event-stream": "^3.1.5",
|
"event-stream": "^3.1.5",
|
||||||
|
@ -19,6 +23,7 @@
|
||||||
"gulp-rimraf": "^0.1.0",
|
"gulp-rimraf": "^0.1.0",
|
||||||
"run-sequence": "^0.3.6",
|
"run-sequence": "^0.3.6",
|
||||||
"glob": "^4.0.6",
|
"glob": "^4.0.6",
|
||||||
"gulp-ejs": "^0.3.1"
|
"gulp-ejs": "^0.3.1",
|
||||||
|
"traceur": "0.0.66"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,13 @@ name: angular
|
||||||
version: 0.0.0
|
version: 0.0.0
|
||||||
authors:
|
authors:
|
||||||
- Vojta Jina <vojta.jina@gmail.com>
|
- 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:
|
environment:
|
||||||
sdk: '>=1.4.0'
|
sdk: '>=1.4.0'
|
||||||
dependencies:
|
dependencies:
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
|
test_lib:
|
||||||
|
path: modules/test_lib
|
||||||
unittest: '>=0.10.1 <0.12.0'
|
unittest: '>=0.10.1 <0.12.0'
|
||||||
|
guinness: ">=0.1.5 <0.2.0"
|
||||||
|
browser: '>=0.10.0 <0.11.0'
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
||||||
|
});
|
|
@ -12,7 +12,6 @@ var traceurDir = baseDir+'/../traceur';
|
||||||
var buildDir = baseDir + '/build';
|
var buildDir = baseDir + '/build';
|
||||||
|
|
||||||
var paths = {
|
var paths = {
|
||||||
traceurSrc: traceurDir+'/src/**/*.js',
|
|
||||||
js2dartSrc: baseDir + '/src/**/*.js',
|
js2dartSrc: baseDir + '/src/**/*.js',
|
||||||
specTranspile: baseDir + '/spec/**/*.js',
|
specTranspile: baseDir + '/spec/**/*.js',
|
||||||
specTemplates: baseDir + '/spec/**/*.template',
|
specTemplates: baseDir + '/spec/**/*.template',
|
||||||
|
@ -26,29 +25,11 @@ module.exports.paths = paths;
|
||||||
function install(gulp) {
|
function install(gulp) {
|
||||||
var runSequence = require('run-sequence').use(gulp);
|
var runSequence = require('run-sequence').use(gulp);
|
||||||
|
|
||||||
// -- js2dart
|
var spec2dartOptions = {
|
||||||
var buildJs2DartOptions = {
|
|
||||||
modules: 'register',
|
|
||||||
moduleName: true,
|
|
||||||
referrer: 'js2dart/src/',
|
|
||||||
script: false // parse as a module
|
|
||||||
};
|
|
||||||
|
|
||||||
var js2dartOptions = {
|
|
||||||
annotations: true, // parse annotations
|
annotations: true, // parse annotations
|
||||||
types: true, // parse types
|
types: true, // parse types
|
||||||
script: false, // parse as a module
|
script: false, // parse as a module
|
||||||
outputLanguage: 'dart',
|
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
|
|
||||||
};
|
};
|
||||||
|
|
||||||
gulp.task('js2dart/clean', function() {
|
gulp.task('js2dart/clean', function() {
|
||||||
|
@ -56,14 +37,6 @@ function install(gulp) {
|
||||||
.pipe(clean());
|
.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() {
|
gulp.task('js2dart/test/build', function() {
|
||||||
return mergeStreams(specTranspile(false), specCopy(false), specRunner(false));
|
return mergeStreams(specTranspile(false), specCopy(false), specRunner(false));
|
||||||
});
|
});
|
||||||
|
@ -76,6 +49,13 @@ function install(gulp) {
|
||||||
runSequence('js2dart/test/build', 'js2dart/test/run', done);
|
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) {
|
gulp.task('js2dart/test/watch', function(done) {
|
||||||
var streams = [];
|
var streams = [];
|
||||||
streams.push(specTranspile(true)
|
streams.push(specTranspile(true)
|
||||||
|
@ -93,7 +73,7 @@ function install(gulp) {
|
||||||
function specTranspile(isWatch) {
|
function specTranspile(isWatch) {
|
||||||
var srcFn = isWatch ? watch : gulp.src.bind(gulp);
|
var srcFn = isWatch ? watch : gulp.src.bind(gulp);
|
||||||
return srcFn(paths.specTranspile)
|
return srcFn(paths.specTranspile)
|
||||||
.pipe(gulpTraceur(js2dartOptions, true))
|
.pipe(gulpTraceur(spec2dartOptions))
|
||||||
.pipe(rename({extname: '.dart'}))
|
.pipe(rename({extname: '.dart'}))
|
||||||
.pipe(gulp.dest(buildDir+'/spec'));
|
.pipe(gulp.dest(buildDir+'/spec'));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,22 +1,12 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
var through = require('through2');
|
var through = require('through2');
|
||||||
var fs = require('fs');
|
var compiler = require('./index');
|
||||||
var path = require('path');
|
|
||||||
var originalTraceur = require('traceur');
|
|
||||||
var glob = require('glob');
|
|
||||||
|
|
||||||
module.exports = gulpTraceur;
|
module.exports = gulpTraceur;
|
||||||
gulpTraceur.reloadPatches = function() {
|
gulpTraceur.RUNTIME_PATH = compiler.RUNTIME_PATH;
|
||||||
loadPatches(true);
|
gulpTraceur.sourcesChanged = compiler.sourcesChanged;
|
||||||
};
|
|
||||||
gulpTraceur.loadPatches = loadPatches;
|
|
||||||
|
|
||||||
|
function gulpTraceur(options, resolveModuleName) {
|
||||||
/// usePatches: whether to use plain traceur or apply
|
|
||||||
/// our patches...
|
|
||||||
function gulpTraceur(options, usePatches) {
|
|
||||||
var lastLoadCounter = loadCounter;
|
|
||||||
var lastCompiler = null;
|
|
||||||
options = options || {};
|
options = options || {};
|
||||||
|
|
||||||
return through.obj(function (file, enc, done) {
|
return through.obj(function (file, enc, done) {
|
||||||
|
@ -29,14 +19,13 @@ function gulpTraceur(options, usePatches) {
|
||||||
throw new Error('gulp-traceur: Streaming not supported');
|
throw new Error('gulp-traceur: Streaming not supported');
|
||||||
}
|
}
|
||||||
|
|
||||||
var compiler = createCompilerIfNeeded();
|
|
||||||
var ret;
|
|
||||||
try {
|
try {
|
||||||
var fileName = file.relative;
|
var moduleName = resolveModuleName ? resolveModuleName(file.relative) : null;
|
||||||
if (options.referrer) {
|
var compiled = compiler.compile(options, {
|
||||||
fileName = options.referrer + '/' + fileName;
|
inputPath: file.relative,
|
||||||
}
|
outputPath: file.relative,
|
||||||
var compiled = compiler.compile(file.contents.toString(), fileName, fileName);
|
moduleName: moduleName
|
||||||
|
}, file.contents.toString());
|
||||||
file.contents = new Buffer(compiled);
|
file.contents = new Buffer(compiled);
|
||||||
this.push(file);
|
this.push(file);
|
||||||
done();
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
@ -6,20 +6,13 @@ var mergeStreams = require('event-stream').merge;
|
||||||
|
|
||||||
tasks.install(gulp);
|
tasks.install(gulp);
|
||||||
|
|
||||||
gulp.task('build', function() {
|
|
||||||
return runSequence('js2dart/build');
|
|
||||||
});
|
|
||||||
|
|
||||||
gulp.task('test', function() {
|
gulp.task('test', function() {
|
||||||
return runSequence('build', 'js2dart/test');
|
return runSequence('js2dart/test');
|
||||||
});
|
});
|
||||||
|
|
||||||
gulp.task('clean', ['js2dart/clean']);
|
gulp.task('clean', ['js2dart/clean']);
|
||||||
|
|
||||||
gulp.task('watch', function() {
|
gulp.task('watch', function(done) {
|
||||||
var js2dartWatch = watch(tasks.paths.js2dartSrc, function(_, done) {
|
// parallel is important as both streams are infinite!
|
||||||
runSequence('js2dart/build', 'js2dart/test', done);
|
runSequence(['js2dart/test/watch', 'js2dart/src/watch'], done);
|
||||||
});
|
|
||||||
runSequence('js2dart/test/watch');
|
|
||||||
return js2dartWatch;
|
|
||||||
});
|
});
|
|
@ -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;
|
||||||
|
}
|
|
@ -1,61 +1,37 @@
|
||||||
var traceur = require('traceur');
|
var js2dart = require('./index.js');
|
||||||
|
|
||||||
var createTraceurPreprocessor = function(args, config, logger, helper) {
|
module.exports = {
|
||||||
config = config || {};
|
'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) {
|
function createJs2DartPreprocessor(logger, basePath, config) {
|
||||||
return filepath.replace(/\.es6.js$/, '.js').replace(/\.es6$/, '.js');
|
var log = logger.create('traceur');
|
||||||
};
|
|
||||||
|
|
||||||
return function(content, file, done) {
|
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);
|
try {
|
||||||
var transpiledContent = result.js;
|
var moduleName = config.resolveModuleName(file.originalPath);
|
||||||
|
if (config.transformPath) {
|
||||||
result.errors.forEach(function(err) {
|
file.path = config.transformPath(file.originalPath);
|
||||||
log.error(err);
|
}
|
||||||
|
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');
|
||||||
if (result.errors.length) {
|
} else {
|
||||||
return done(new Error('TRACEUR COMPILE ERRORS\n' + result.errors.join('\n')));
|
log.error(errors);
|
||||||
|
errorString = errors;
|
||||||
|
}
|
||||||
|
done(new Error('TRACEUR COMPILE ERRORS\n' + errorString));
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(vojta): Tracer should return JS object, rather than a string.
|
createJs2DartPreprocessor.$inject = ['logger', 'config.basePath', 'config.traceurPreprocessor'];
|
||||||
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]
|
|
||||||
};
|
|
||||||
|
|
|
@ -25,7 +25,7 @@ export class Compiler extends TraceurCompiler {
|
||||||
|
|
||||||
write(tree, outputName = undefined, sourceRoot = undefined) {
|
write(tree, outputName = undefined, sourceRoot = undefined) {
|
||||||
if (this.options_.outputLanguage.toLowerCase() === 'dart') {
|
if (this.options_.outputLanguage.toLowerCase() === 'dart') {
|
||||||
var writer = new DartTreeWriter(outputName);
|
var writer = new DartTreeWriter(this.options_.moduleName, outputName);
|
||||||
writer.visitAny(tree);
|
writer.visitAny(tree);
|
||||||
return writer.toString();
|
return writer.toString();
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -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';
|
import {ParseTreeWriter as JavaScriptParseTreeWriter} from 'traceur/src/outputgeneration/ParseTreeWriter';
|
||||||
|
|
||||||
export class DartTreeWriter extends JavaScriptParseTreeWriter {
|
export class DartTreeWriter extends JavaScriptParseTreeWriter {
|
||||||
constructor(moduleName) {
|
constructor(moduleName, outputPath) {
|
||||||
super();
|
super(outputPath);
|
||||||
this.libName = moduleName
|
this.libName = moduleName
|
||||||
.replace(/\//g, '.')
|
.replace(/\//g, '.')
|
||||||
.replace(/[^\w.]/g, '_')
|
.replace(/[^\w.]/g, '_');
|
||||||
.replace('.lib.', '.')
|
|
||||||
.replace(/\.dart$/, '');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// VARIABLES - types
|
// VARIABLES - types
|
||||||
|
|
Loading…
Reference in New Issue