Skip to content
This repository was archived by the owner on Jul 13, 2020. It is now read-only.

Commit 83a9d3f

Browse files
committed
use 6to5-core, parser -> transpiler
1 parent 226ad68 commit 83a9d3f

10 files changed

+36
-48
lines changed

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module.exports = function (grunt) {
3838
dist: {
3939
src: [
4040
'src/loader.js',
41-
'src/parser.js',
41+
'src/transpiler.js',
4242
'src/system.js'
4343
],
4444
dest: 'dist/<%= pkg.name %>.js'

karma.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module.exports = function(config) {
3737
'test/_helper.js',
3838
[options['6to5'] ? 'node_modules/regenerator/runtime.js' : ''],
3939

40-
[!options.ie8 ? (!options['6to5'] ? 'node_modules/traceur/bin/traceur.js' : 'node_modules/6to5/browser.js') : ''],
40+
[!options.ie8 ? (!options['6to5'] ? 'node_modules/traceur/bin/traceur.js' : 'node_modules/6to5-core/browser.js') : ''],
4141

4242
'dist/es6-module-loader' + (options.polyfill ? '' : '-sans-promises') + '.src.js',
4343

lib/index-6to5.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var System = require('../dist/es6-module-loader.src');
22

3-
System.parser = '6to5';
3+
System.transpiler = '6to5';
44

55
module.exports = {
66
Loader: global.LoaderPolyfill,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"test:browser:perf": "karma start karma-benchmark.conf.js --single-run"
6464
},
6565
"dependencies": {
66-
"6to5": "^3.0.0",
66+
"6to5-core": "~3.3.1",
6767
"grunt-contrib-uglify": "0.6.0",
6868
"traceur": "0.0.82",
6969
"when": "^3.6.4"

src/loader.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,7 @@ function logloads(loads) {
251251

252252
// instead of load.kind, use load.isDeclarative
253253
load.isDeclarative = true;
254-
// parse sets load.declare, load.depsList
255-
loader.loaderObj.parse(load);
254+
__eval(loader.loaderObj.transpile(load), __global, load);
256255
}
257256
else if (typeof instantiateResult == 'object') {
258257
load.depsList = instantiateResult.deps || [];
@@ -1047,9 +1046,6 @@ function logloads(loads) {
10471046
translate: function(load) {
10481047
return load.source;
10491048
},
1050-
parse: function(load) {
1051-
throw new TypeError('Loader.parse is not implemented');
1052-
},
10531049
// 26.3.3.18.5
10541050
instantiate: function(load) {
10551051
}

src/parser.js renamed to src/transpiler.js

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,40 @@
11
/*
2-
* Traceur and 6to5 Parsing Code for Loader
2+
* Traceur and 6to5 transpile hook for Loader
33
*/
44
(function(Loader) {
5-
// parse function is used to parse a load record
65
// Returns an array of ModuleSpecifiers
7-
var parser, parserModule, parserName, parserOptionsName;
6+
var transpiler, transpilerModule;
7+
var isNode = typeof window == 'undefined' && typeof WorkerGlobalScope == 'undefined';
88

99
// use Traceur by default
10-
Loader.prototype.parser = 'traceur';
11-
12-
Loader.prototype.parse = function(load) {
13-
if (!parser) {
14-
parserName = this.parser == '6to5' ? 'to5' : this.parser;
15-
16-
// try to pick up parser from global or require
17-
if (typeof window == 'undefined' && typeof WorkerGlobalScope == 'undefined')
18-
parserModule = require(this.parser);
19-
else
20-
parserModule = __global[parserName];
10+
Loader.prototype.transpiler = 'traceur';
11+
12+
Loader.prototype.transpile = function(load) {
13+
if (!transpiler) {
14+
if (this.transpiler == '6to5') {
15+
transpiler = to5Transpile;
16+
transpilerModule = isNode ? require('6to5-core') : __global.to5;
17+
}
18+
else {
19+
transpiler = traceurTranspile;
20+
transpilerModule = isNode ? require('traceur') : __global.traceur;
21+
}
2122

22-
if (!parserModule)
23-
throw new TypeError('Include Traceur or 6to5 for module syntax support');
24-
25-
parser = this.parser == '6to5' ? to5Parse : traceurParse;
23+
if (!transpilerModule)
24+
throw new TypeError('Include Traceur or 6to5 for module syntax support.');
2625
}
2726

28-
var source = parser.call(this, load);
29-
30-
source = 'var __moduleAddress = "' + load.address + '";' + source;
31-
32-
__eval(source, __global, load);
27+
return 'var __moduleAddress = "' + load.address + '";' + transpiler.call(this, load);
3328
}
3429

35-
function traceurParse(load) {
30+
function traceurTranspile(load) {
3631
var options = this.traceurOptions || {};
3732
options.modules = 'instantiate';
3833
options.script = false;
3934
options.sourceMaps = 'inline';
4035
options.filename = load.address;
4136

42-
var compiler = new parserModule.Compiler(options);
37+
var compiler = new transpilerModule.Compiler(options);
4338
var source = doTraceurCompile(load.source, compiler, options.filename);
4439

4540
// add "!eval" to end of Traceur sourceURL
@@ -58,15 +53,15 @@
5853
}
5954
}
6055

61-
function to5Parse(load) {
56+
function to5Transpile(load) {
6257
var options = this.to5Options || {};
6358
options.modules = 'system';
6459
options.sourceMap = 'inline';
6560
options.filename = load.address;
6661
options.code = true;
6762
options.ast = false;
6863

69-
var source = parserModule.transform(load.source, options).code;
64+
var source = transpilerModule.transform(load.source, options).code;
7065

7166
// add "!eval" to end of 6to5 sourceURL
7267
// I believe this does something?

test/system.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22

33
if (typeof to5 != 'undefined')
4-
System.parser = '6to5';
4+
System.transpiler = '6to5';
55

66
var ie = typeof window != 'undefined' && window.navigator.userAgent.match(/Trident/);
77

@@ -99,7 +99,7 @@ describe('System', function () {
9999

100100
describe('with circular dependencies', function () {
101101

102-
(System.parser == 'traceur' ? it : it.skip)('should resolve circular dependencies', function (done) {
102+
(System.transpiler == 'traceur' ? it : it.skip)('should resolve circular dependencies', function (done) {
103103
System.import('test/syntax/circular1')
104104
.then(function (m1) {
105105
return System.import('test/syntax/circular2').then(function (m2) {
@@ -288,7 +288,7 @@ describe('System', function () {
288288
.then(done, done);
289289
});
290290

291-
(System.parser != 'traceur' ? it.skip : it)('should support re-exporting overwriting', function (done) {
291+
(System.transpiler != 'traceur' ? it.skip : it)('should support re-exporting overwriting', function (done) {
292292
System.import('test/syntax/export-star2')
293293
.then(function (m) {
294294
expect(m.bar, 'should re-export "./export-star" bar variable')
@@ -380,7 +380,7 @@ describe('System', function () {
380380
typeof window != 'undefined' && window.Worker,
381381
'with Web Worker', function () {
382382
(ie ? it.skip : it)('should loading inside of a Web Worker', function (done) {
383-
var worker = new Worker(System.baseURL + 'test/worker/worker-' + System.parser + '.js');
383+
var worker = new Worker(System.baseURL + 'test/worker/worker-' + System.transpiler + '.js');
384384

385385
worker.onmessage = function (e) {
386386
expect(e.data).to.be.equal('p');

test/test-6to5.html

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<script src="test.js"></script>
77

88
<!-- set this to the path to 6to5.js -->
9-
<script src="../node_modules/6to5/browser.js"></script>
9+
<script src="../node_modules/6to5-core/browser.js"></script>
1010
<script src="../node_modules/regenerator/runtime.js"></script>
1111

1212
<script>
@@ -18,9 +18,7 @@
1818

1919
<script src="../dist/es6-module-loader.src.js"></script>
2020
<script>
21-
System.parser = '6to5';
22-
// test parseOptions and anonymous errors
23-
// System.parseOptions = { classes: false };
21+
System.transpiler = '6to5';
2422
</script>
2523

2624
<script>

test/test-traceur.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717

1818
<script src="../dist/es6-module-loader.src.js"></script>
1919
<script>
20-
// test parseOptions and anonymous errors
21-
// System.parseOptions = { classes: false };
20+
// System.traceurOptions = { classes: false };
2221
</script>
2322

2423
<script>

test/worker/worker-6to5.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
importScripts("../../node_modules/6to5/browser.js",
1+
importScripts("../../node_modules/6to5-core/browser.js",
22
"../../node_modules/when/es6-shim/Promise.js",
33
"../../dist/es6-module-loader.src.js"
44
);
55

6-
System.parser = '6to5';
6+
System.transpiler = '6to5';
77

88
System['import']('es6').then(function(m) {
99
postMessage(m.p);

0 commit comments

Comments
 (0)