Skip to content

Commit cfc5fdc

Browse files
committed
chore: use es6-module-loader
Switch Traceur to use modules=“instantiate” and use es6-module-loader. This setup supports cyclic dependencies.
1 parent 774901c commit cfc5fdc

File tree

6 files changed

+76
-22
lines changed

6 files changed

+76
-22
lines changed

file2modulename.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ function file2moduleName(filePath) {
99
// module name should not include `src`, `test`, `lib`
1010
.replace(/\/src\//, '/')
1111
.replace(/\/lib\//, '/')
12-
.replace(/\/test\//, '/')
1312
// module name should not have a suffix
1413
.replace(/\.\w*$/, '');
1514
}
1615
if (typeof module !== 'undefined') {
1716
module.exports = file2moduleName;
18-
}
17+
}

karma-js.conf.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ module.exports = function(config) {
88
frameworks: ['jasmine'],
99

1010
files: [
11+
// Sources and specs.
12+
// Loaded through the es6-module-loader, in `test-main.js`.
13+
{pattern: 'modules/**', included: false},
14+
{pattern: 'tools/transpiler/**', included: false},
15+
1116
'node_modules/traceur/bin/traceur-runtime.js',
12-
'modules/**/test_lib/**/*.es6',
13-
'modules/**/*.js',
14-
'modules/**/*.es6',
15-
'tools/transpiler/spec/**/*.js',
16-
'tools/transpiler/spec/**/*.es6',
17+
'node_modules/es6-module-loader/dist/es6-module-loader-sans-promises.src.js',
18+
'node_modules/systemjs/lib/extension-register.js',
19+
1720
'file2modulename.js',
1821
'test-main.js'
1922
],
@@ -29,15 +32,15 @@ module.exports = function(config) {
2932
options: {
3033
outputLanguage: 'es5',
3134
script: false,
32-
modules: 'register',
35+
modules: 'instantiate',
3336
types: true,
3437
typeAssertions: true,
3538
typeAssertionModule: 'rtts_assert/rtts_assert',
3639
annotations: true
3740
},
3841
resolveModuleName: file2moduleName,
3942
transformPath: function(fileName) {
40-
return fileName.replace('.es6', '');
43+
return fileName.replace(/\.es6$/, '.js');
4144
}
4245
},
4346

modules/rtts_assert/test/rtts_assert_spec.es6

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import {assert} from 'rtts_assert/rtts_assert';
1414

1515

16+
export function main() {
1617

1718
// ## Basic Type Check
1819
// By default, `instanceof` is used to check the type.
@@ -375,3 +376,5 @@ describe('Traceur', function() {
375376
// <center><small>
376377
// This documentation was generated from [assert.spec.js](https://github.com/vojtajina/assert/blob/master/test/assert.spec.js) using [Docco](http://jashkenas.github.io/docco/).
377378
// </small></center>
379+
380+
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"author": "Tobias Bosch <[email protected]>",
99
"license": "MIT",
1010
"dependencies": {
11+
"es6-module-loader": "^0.9.2",
12+
"systemjs": "^0.9.1",
1113
"gulp": "^3.8.8",
1214
"gulp-rename": "^1.2.0",
1315
"gulp-watch": "^1.0.3",

test-main.js

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,60 @@
1-
var TEST_REGEXP = /_spec.*/;
2-
3-
Object.keys(window.__karma__.files).forEach(function(path) {
4-
if (TEST_REGEXP.test(path)) {
5-
var moduleName = window.file2moduleName(path);
6-
var mod = System.get(moduleName);
7-
if (mod && mod.main) {
8-
mod.main();
9-
}
10-
}
1+
// Use "register" extension from systemjs.
2+
// That's what Traceur outputs: `System.register()`.
3+
register(System);
4+
5+
6+
// Cancel Karma's synchronous start,
7+
// we will call `__karma__.start()` later, once all the specs are loaded.
8+
__karma__.loaded = function() {};
9+
10+
11+
System.baseURL = '/base/modules/';
12+
13+
// So that we can import packages like `core/foo`, instead of `core/src/foo`.
14+
System.paths = {
15+
'core/*': './core/src/*.js',
16+
'core/test/*': './core/test/*.js',
17+
18+
'change_detection/*': './change_detection/src/*.js',
19+
'change_detection/test/*': './change_detection/test/*.js',
20+
21+
'facade/*': './facade/src/*.js',
22+
'facade/test/*': './facade/test/*.js',
23+
24+
'di/*': './di/src/*.js',
25+
'di/test/*': './di/test/*.js',
26+
27+
'rtts_assert/*': './rtts_assert/src/*.js',
28+
'rtts_assert/test/*': './rtts_assert/test/*.js',
29+
30+
'test_lib/*': './test_lib/src/*.js',
31+
'test_lib/test/*': './test_lib/test/*.js',
32+
33+
'transpiler/*': '../tools/transpiler/*.js'
34+
}
35+
36+
37+
// Import all the specs, execute their `main()` method and kick off Karma (Jasmine).
38+
Promise.all(
39+
Object.keys(window.__karma__.files) // All files served by Karma.
40+
.filter(onlySpecFiles)
41+
.map(window.file2moduleName) // Normalize paths to module names.
42+
.map(function(path) {
43+
return System.import(path).then(function(module) {
44+
if (module.hasOwnProperty('main')) {
45+
module.main()
46+
} else {
47+
throw new Error('Module ' + path + ' does not implement main() method.');
48+
}
49+
});
50+
})).then(function() {
51+
__karma__.start();
52+
}, function(error) {
53+
console.error(error.stack || error)
54+
__karma__.start();
1155
});
56+
57+
58+
function onlySpecFiles(path) {
59+
return /_spec\.js$/.test(path);
60+
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
function main() {
2-
assert(true);
3-
}
1+
export function main() {}

0 commit comments

Comments
 (0)