Skip to content

Commit

Permalink
parser -> transpiler, update es6 module loader
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Feb 1, 2015
1 parent e73f4ca commit a24648c
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 47 deletions.
15 changes: 2 additions & 13 deletions bower.json
@@ -1,17 +1,6 @@
{
"name": "system.js",
"version": "0.12.1",
"main": "dist/system.js",
"dependencies": {
"es6-module-loader": "~0.12.0",
"traceur-runtime": "~0.0.82"
},
"name": "systemjs",

This comment has been minimized.

Copy link
@sroucheray

sroucheray May 6, 2015

Why this change ?
"systemjs" does not reflect the name of the project on bower, it is related to mrdoob/systemjs resulting in wrong package installation.

"devDependencies": {
"qunit": "~1.12.0"
},
"ignore": [
"test",
"Makefile",
"package.json"
]
}
}
51 changes: 25 additions & 26 deletions lib/extension-es6.js
Expand Up @@ -5,26 +5,26 @@ function es6(loader) {

loader._extensions.push(es6);

var parser, parserName, parserModule, parserRuntimeModule, parserRuntimeGlobal;
var transpiler, transpilerName, transpilerModule, transpilerRuntimeModule, transpilerRuntimeGlobal;

var isBrowser = typeof window != 'undefined';

function setParser(name) {
parser = name;
parserName = this.parser == '6to5' ? 'to5' : parser;
parserModule = '@' + parser;
parserRuntimeModule = '@' + parser + '-runtime';
parserRuntimeGlobal = (parserName == 'to5' ? parserName : '$' + parserName) + 'Runtime';
function setTranspiler(name) {
transpiler = name;
transpilerName = this.transpiler == '6to5' ? 'to5' : transpiler;
transpilerModule = '@' + transpiler;
transpilerRuntimeModule = '@' + transpiler + '-runtime';
transpilerRuntimeGlobal = (transpilerName == 'to5' ? transpilerName : '$' + transpilerName) + 'Runtime';

// auto-detection of paths to loader parser files
// auto-detection of paths to loader transpiler files
if (typeof $__curScript != 'undefined') {
if (!loader.paths[parserModule])
loader.paths[parserModule] = $__curScript.getAttribute('data-' + loader.parser + '-src')
if (!loader.paths[transpilerModule])
loader.paths[transpilerModule] = $__curScript.getAttribute('data-' + loader.transpiler + '-src')
|| ($__curScript.src ? $__curScript.src.substr(0, $__curScript.src.lastIndexOf('/') + 1)
: loader.baseURL + (loader.baseURL.lastIndexOf('/') == loader.baseURL.length - 1 ? '' : '/')
) + loader.parser + '.js';
if (!loader.paths[parserRuntimeModule])
loader.paths[parserRuntimeModule] = $__curScript.getAttribute('data-' + loader.parser + '-runtime-src') || loader.paths[parserModule].replace(/\.js$/, '-runtime.js');
) + loader.transpiler + '.js';
if (!loader.paths[transpilerRuntimeModule])
loader.paths[transpilerRuntimeModule] = $__curScript.getAttribute('data-' + loader.transpiler + '-runtime-src') || loader.paths[transpilerModule].replace(/\.js$/, '-runtime.js');
}
}

Expand All @@ -33,31 +33,30 @@ function es6(loader) {

var loaderTranslate = loader.translate;
loader.translate = function(load) {
// update parser info if necessary
if (this.parser !== parser)
setParser(this.parser);
// update transpiler info if necessary
if (this.transpiler !== transpiler)
setTranspiler(this.transpiler);

var loader = this;

if (load.name == parserModule || load.name == parserRuntimeModule)
if (load.name == transpilerModule || load.name == transpilerRuntimeModule)
return loaderTranslate.call(loader, load);

// detect ES6
else if (load.metadata.format == 'es6' || !load.metadata.format && load.source.match(es6RegEx)) {
load.metadata.format = 'es6';

// dynamically load parser for ES6 if necessary
if (isBrowser && !loader.global[parserName]) {
return loader['import'](parserModule).then(function() {
// dynamically load transpiler for ES6 if necessary
if (isBrowser && !loader.global[transpilerName])
return loader['import'](transpilerModule).then(function() {
return loaderTranslate.call(loader, load);
});
}
}

// dynamically load parser runtime if necessary
if (isBrowser && !loader.global[parserRuntimeGlobal] && load.source.indexOf(parserRuntimeGlobal) != -1) {
// dynamically load transpiler runtime if necessary
if (isBrowser && !loader.global[transpilerRuntimeGlobal] && load.source.indexOf(transpilerRuntimeGlobal) != -1) {
var System = $__global.System;
return loader['import'](parserRuntimeModule).then(function() {
return loader['import'](transpilerRuntimeModule).then(function() {
// traceur runtme annihilates System global
$__global.System = System;
return loaderTranslate.call(loader, load);
Expand All @@ -67,11 +66,11 @@ function es6(loader) {
return loaderTranslate.call(loader, load);
}

// always load parser as a global
// always load transpiler as a global
var loaderInstantiate = loader.instantiate;
loader.instantiate = function(load) {
var loader = this;
if (isBrowser && (load.name == parserModule || load.name == parserRuntimeModule)) {
if (isBrowser && (load.name == transpilerModule || load.name == transpilerRuntimeModule)) {
loader.__exec(load);
return {
deps: [],
Expand Down
6 changes: 3 additions & 3 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "systemjs",
"version": "0.12.1",
"version": "0.12.0",
"description": "System loader extension for flexible AMD & CommonJS support",
"main": "dist/system.src.js",
"repository": {
Expand All @@ -10,10 +10,10 @@
"author": "Guy Bedford",
"license": "MIT",
"dependencies": {
"es6-module-loader": "~0.12.0"
"es6-module-loader": "~0.13.0"
},
"devDependencies": {
"6to5": "^3.0.0",
"6to5-core": "~3.3.2",
"qunit": "^0.6.2",
"uglify-js": "~2.4.13"
},
Expand Down
4 changes: 2 additions & 2 deletions test/test-6to5.html
Expand Up @@ -15,9 +15,9 @@ <h2 id="qunit-userAgent"></h2>

<!-- <script src="../bower_components/traceur/traceur.js"></script> -->
<script src="../bower_components/es6-module-loader/dist/es6-module-loader.src.js"></script>
<script src="../dist/system.src.js" data-6to5-src="../node_modules/6to5/browser.js" type="text/javascript"></script>
<script src="../dist/system.src.js" data-6to5-src="../node_modules/6to5-core/browser.js" type="text/javascript"></script>
<script>
System.parser = '6to5';
System.transpiler = '6to5';
System['import']('test')['catch'](function(e) {
setTimeout(function() {
throw e;
Expand Down
2 changes: 1 addition & 1 deletion test/test-traceur.html
Expand Up @@ -20,7 +20,7 @@ <h2 id="qunit-userAgent"></h2>
System.traceurOptions = { asyncFunctions: true };
System['import']('test')['catch'](function(e) {
setTimeout(function() {
throw e;
throw e.stack || e;
})
});
</script>
Expand Down
4 changes: 2 additions & 2 deletions test/test.js
Expand Up @@ -654,14 +654,14 @@ asyncTest('Relative dyanamic loading', function() {
asyncTest('ES6 Circular', function() {
System['import']('tests/es6-circular1').then(function(m) {
ok(m.q == 3, 'Binding not allocated');
if (System.parser != '6to5') ok(m.r == 3, 'Binding not updated');
if (System.transpiler != '6to5') ok(m.r == 3, 'Binding not updated');
start();
}, err);
});

asyncTest('AMD & CJS circular, ES6 Circular', function() {
System['import']('tests/all-circular1').then(function(m) {
if (System.parser != '6to5') ok(m.q == 4);
if (System.transpiler != '6to5') ok(m.q == 4);
ok(m.o.checkObj() == 'changed');
start();
}, err);
Expand Down

0 comments on commit a24648c

Please sign in to comment.