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

Commit fb9f87f

Browse files
committed
6to5 support
1 parent 4e9550c commit fb9f87f

File tree

7 files changed

+198
-80
lines changed

7 files changed

+198
-80
lines changed

Gruntfile.js

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,53 @@ module.exports = function (grunt) {
1414
jshintrc: '.jshintrc'
1515
},
1616
dist: [
17-
'lib/index.js',
18-
'lib/loader.js',
19-
'lib/system.js'
17+
'lib/index.js'
2018
]
2119
},
2220
concat: {
2321
dist: {
2422
files: {
25-
'dist/<%= pkg.name %>.src.js': [
23+
'dist/<%= pkg.name %>-traceur.src.js': [
2624
'node_modules/when/es6-shim/Promise.js',
2725
'src/polyfill-wrapper-start.js',
28-
'dist/<%= pkg.name %>.js',
26+
'dist/<%= pkg.name %>-traceur.js',
2927
'src/polyfill-wrapper-end.js'
3028
],
31-
'dist/<%= pkg.name %>-sans-promises.src.js': [
29+
'dist/<%= pkg.name %>-6to5.src.js': [
30+
'node_modules/when/es6-shim/Promise.js',
31+
'src/polyfill-wrapper-start.js',
32+
'dist/<%= pkg.name %>-6to5.js',
33+
'src/polyfill-wrapper-end.js'
34+
],
35+
'dist/<%= pkg.name %>-traceur-sp.src.js': [
36+
'src/polyfill-wrapper-start.js',
37+
'dist/<%= pkg.name %>-traceur.js',
38+
'src/polyfill-wrapper-end.js'
39+
],
40+
'dist/<%= pkg.name %>-6to5-sp.src.js': [
3241
'src/polyfill-wrapper-start.js',
33-
'dist/<%= pkg.name %>.js',
42+
'dist/<%= pkg.name %>-6to5.js',
3443
'src/polyfill-wrapper-end.js'
3544
]
3645
}
3746
}
3847
},
3948
esnext: {
40-
dist: {
49+
distTraceur: {
4150
src: [
4251
'src/loader.js',
52+
'src/traceur-loader.js',
4353
'src/system.js'
4454
],
45-
dest: 'dist/<%= pkg.name %>.js'
55+
dest: 'dist/<%= pkg.name %>-traceur.js'
56+
},
57+
dist6to5: {
58+
src: [
59+
'src/loader.js',
60+
'src/6to5-loader.js',
61+
'src/system.js'
62+
],
63+
dest: 'dist/<%= pkg.name %>-6to5.js'
4664
}
4765
},
4866
'string-replace': {
@@ -71,16 +89,27 @@ module.exports = function (grunt) {
7189
},
7290
sourceMap: true
7391
},
74-
dist: {
92+
distTraceur: {
7593
options: {
7694
banner: '<%= meta.banner %>\n'
7795
},
78-
src: 'dist/<%= pkg.name %>.src.js',
79-
dest: 'dist/<%= pkg.name %>.js'
96+
src: 'dist/<%= pkg.name %>-traceur.src.js',
97+
dest: 'dist/<%= pkg.name %>-traceur.js'
98+
},
99+
dist6to5: {
100+
options: {
101+
banner: '<%= meta.banner %>\n'
102+
},
103+
src: 'dist/<%= pkg.name %>-6to5.src.js',
104+
dest: 'dist/<%= pkg.name %>-6to5.js'
105+
},
106+
distTraceurSansPromises: {
107+
src: 'dist/<%= pkg.name %>-traceur-sp.src.js',
108+
dest: 'dist/<%= pkg.name %>-traceur-sp.js'
80109
},
81-
polyfillOnly: {
82-
src: 'dist/<%= pkg.name %>-sans-promises.src.js',
83-
dest: 'dist/<%= pkg.name %>-sans-promises.js'
110+
dist6to5SansPromises: {
111+
src: 'dist/<%= pkg.name %>-6to5-sp.src.js',
112+
dest: 'dist/<%= pkg.name %>-6to5-sp.js'
84113
}
85114
}
86115
});

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,16 @@
5252
"engines": {
5353
"node": ">=0.8.0"
5454
},
55-
"main": "lib/index",
55+
"main": "lib/index-traceur",
5656
"scripts": {
5757
"test": "npm run test:node && npm run test:browsers",
5858
"test:node": "mocha test/_node.js",
5959
"test:browsers": "karma start --single-run",
6060
"test:browsers:perf": "karma start karma-benchmark.conf.js --single-run"
6161
},
6262
"dependencies": {
63+
"6to5": "^2.0.2",
64+
"grunt-contrib-uglify": "0.6.0",
6365
"traceur": "0.0.79",
6466
"when": "^3.6.4"
6567
}

src/6to5-loader.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* 6to5-specific Parsing Code for Loader
3+
*/
4+
(function(Loader) {
5+
// parse function is used to parse a load record
6+
// Returns an array of ModuleSpecifiers
7+
var to5;
8+
9+
Loader.prototype.parse = function(load) {
10+
if (!to5) {
11+
if (typeof window == 'undefined' &&
12+
typeof WorkerGlobalScope == 'undefined')
13+
to5 = require('6to5');
14+
else if (__global.to5)
15+
to5 = __global.to5;
16+
else
17+
throw new TypeError('Include 6to5 for module syntax support');
18+
}
19+
20+
load.isDeclarative = true;
21+
22+
var options = this.parseOptions || {};
23+
options.modules = 'system';
24+
options.sourceMap = 'inline';
25+
options.filename = load.address;
26+
options.code = true;
27+
options.ast = false;
28+
options.runtime = true;
29+
30+
var source = to5.transform(load.source, options).code;
31+
32+
if (!source)
33+
throw new Error('Error evaluating module ' + load.address);
34+
35+
// add "!eval" to end of 6to5 sourceURL
36+
// I believe this does something?
37+
source = 'var __moduleAddress = "' + load.address + '";' + source + '!eval';
38+
39+
__eval(source, __global, load);
40+
}
41+
})(__global.LoaderPolyfill);

src/loader.js

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ function logloads(loads) {
899899
});
900900

901901
// 26.3.3.13 realm not implemented
902-
this.traceurOptions = {};
902+
this.parseOptions = {};
903903
}
904904

905905
function Module() {}
@@ -1058,66 +1058,6 @@ function logloads(loads) {
10581058

10591059
var _newModule = Loader.prototype.newModule;
10601060

1061-
1062-
/*
1063-
* Traceur-specific Parsing Code for Loader
1064-
*/
1065-
(function() {
1066-
// parse function is used to parse a load record
1067-
// Returns an array of ModuleSpecifiers
1068-
var traceur;
1069-
1070-
function doCompile(source, compiler, filename) {
1071-
try {
1072-
return compiler.compile(source, filename);
1073-
}
1074-
catch(e) {
1075-
// traceur throws an error array
1076-
throw e[0];
1077-
}
1078-
}
1079-
Loader.prototype.parse = function(load) {
1080-
if (!traceur) {
1081-
if (typeof window == 'undefined' &&
1082-
typeof WorkerGlobalScope == 'undefined')
1083-
traceur = require('traceur');
1084-
else if (__global.traceur)
1085-
traceur = __global.traceur;
1086-
else
1087-
throw new TypeError('Include Traceur for module syntax support');
1088-
}
1089-
1090-
console.assert(load.source, 'Non-empty source');
1091-
1092-
load.isDeclarative = true;
1093-
1094-
var options = this.traceurOptions || {};
1095-
options.modules = 'instantiate';
1096-
options.script = false;
1097-
options.sourceMaps = 'inline';
1098-
options.filename = load.address;
1099-
1100-
var compiler = new traceur.Compiler(options);
1101-
1102-
var source = doCompile(load.source, compiler, options.filename);
1103-
1104-
if (!source)
1105-
throw new Error('Error evaluating module ' + load.address);
1106-
1107-
var sourceMap = compiler.getSourceMap();
1108-
1109-
if (__global.btoa && sourceMap) {
1110-
// add "!eval" to end of Traceur sourceURL
1111-
// I believe this does something?
1112-
source += '!eval';
1113-
}
1114-
1115-
source = 'var __moduleAddress = "' + load.address + '";' + source;
1116-
1117-
__eval(source, __global, load);
1118-
}
1119-
})();
1120-
11211061
if (typeof exports === 'object')
11221062
module.exports = Loader;
11231063

src/traceur-loader.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Traceur-specific Parsing Code for Loader
3+
*/
4+
(function(Loader) {
5+
// parse function is used to parse a load record
6+
// Returns an array of ModuleSpecifiers
7+
var traceur;
8+
9+
function doCompile(source, compiler, filename) {
10+
try {
11+
return compiler.compile(source, filename);
12+
}
13+
catch(e) {
14+
// traceur throws an error array
15+
throw e[0];
16+
}
17+
}
18+
Loader.prototype.parse = function(load) {
19+
if (!traceur) {
20+
if (typeof window == 'undefined' &&
21+
typeof WorkerGlobalScope == 'undefined')
22+
traceur = require('traceur');
23+
else if (__global.traceur)
24+
traceur = __global.traceur;
25+
else
26+
throw new TypeError('Include Traceur for module syntax support');
27+
}
28+
29+
console.assert(load.source, 'Non-empty source');
30+
31+
load.isDeclarative = true;
32+
33+
var options = this.parseOptions || {};
34+
options.modules = 'instantiate';
35+
options.script = false;
36+
options.sourceMaps = 'inline';
37+
options.filename = load.address;
38+
39+
var compiler = new traceur.Compiler(options);
40+
41+
var source = doCompile(load.source, compiler, options.filename);
42+
43+
if (!source)
44+
throw new Error('Error evaluating module ' + load.address);
45+
46+
var sourceMap = compiler.getSourceMap();
47+
48+
if (__global.btoa && sourceMap) {
49+
// add "!eval" to end of Traceur sourceURL
50+
// I believe this does something?
51+
source += '!eval';
52+
}
53+
54+
source = 'var __moduleAddress = "' + load.address + '";' + source;
55+
56+
__eval(source, __global, load);
57+
}
58+
})(__global.LoaderPolyfill);

test/test-6to5.html

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<!doctype html>
2+
<html>
3+
4+
<body></body>
5+
6+
<script src="test.js"></script>
7+
8+
<!-- set this to the path to 6to5.js -->
9+
<script src="../node_modules/6to5/browser.js"></script>
10+
11+
<script>
12+
// test promise polyfill
13+
// delete window.Promise;
14+
</script>
15+
16+
<script src="../node_modules/when/es6-shim/Promise.js"></script>
17+
18+
<script src="../dist/es6-module-loader-6to5.src.js"></script>
19+
<script>
20+
// test parseOptions and anonymous errors
21+
// System.parseOptions = { classes: false };
22+
</script>
23+
24+
<script>
25+
// test tracing
26+
System.trace = true;
27+
</script>
28+
29+
<script>
30+
runTests();
31+
</script>
32+
33+
<script type="module">
34+
window.anon = class {
35+
constructor() {
36+
37+
}
38+
}
39+
</script>
40+
41+
42+
<script>
43+
setTimeout(function() {
44+
test('Anonymous &lt;script type="module"> tag', function(assert) {
45+
assert(typeof window.anon, 'function');
46+
});
47+
}, 500);
48+
</script>

test/test.html renamed to test/test-traceur.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515

1616
<script src="../node_modules/when/es6-shim/Promise.js"></script>
1717

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

2424
<script>

0 commit comments

Comments
 (0)