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

Commit

Permalink
0.14.0
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Feb 17, 2015
1 parent e616a94 commit 2cc6f36
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 39 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Dynamically loads ES6 modules in browsers and [NodeJS](#nodejs-use) with support
This project implements dynamic module loading through `System` exactly to the previous ES6-specified loader API at [2014-08-24 ES6 Specification Draft Rev 27, Section 15](http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts#august_24_2014_draft_rev_27) and will continue to track this API as it is re-drafted as a browser specification (currently most likely to be at https://github.com/whatwg/loader).

* Provides an asynchronous loader (`System.import`) to [dynamically load ES6 modules](#getting-started).
* Supports both [Traceur](https://github.com/google/traceur-compiler) and [6to5](https://6to5.org/) for compiling ES6 modules and syntax into ES5 in the browser with source map support.
* Supports both [Traceur](https://github.com/google/traceur-compiler) and [Babel](https://babel.org/) for compiling ES6 modules and syntax into ES5 in the browser with source map support.
* Fully supports [ES6 circular references and live bindings](https://github.com/ModuleLoader/es6-module-loader/wiki/Circular-References-&-Bindings).
* Includes [`baseURL` and `paths` implementations](https://github.com/ModuleLoader/es6-module-loader/wiki/Configuring-the-Loader).
* Can be used as a [tracing tool](https://github.com/ModuleLoader/es6-module-loader/wiki/Tracing-API) for static analysis of modules.
Expand All @@ -28,18 +28,18 @@ For an example of a universal module loader based on this polyfill for loading A

### Getting Started

If using ES6 syntax (optional), include `traceur.js` or `6to5.js` in the page first then include `es6-module-loader.js`:
If using ES6 syntax (optional), include `traceur.js` or `babel.js` in the page first then include `es6-module-loader.js`:

```html
<script src="traceur.js"></script>
<script src="es6-module-loader.js"></script>
```

To use 6to5, set the transpiler to `6to5` with the loader configuration:
To use Babel, set the transpiler to `babel` with the loader configuration:

```html
<script>
System.transpiler = '6to5';
System.transpiler = 'babel';
</script>
```

Expand Down Expand Up @@ -74,7 +74,7 @@ If using Traceur, these can be set with:
System.traceurOptions = {...};
```

Or with 6to5:
Or with Babel:

```javascript
System.to5Options = {...};
Expand Down Expand Up @@ -110,8 +110,8 @@ index.js:
var System = require('es6-module-loader').System;
/*
* Include:
* System.transpiler = '6to5';
* to use 6to5 instead of Traceur
* System.transpiler = 'babel';
* to use Babel instead of Traceur
*/

System.import('some-module').then(function(m) {
Expand All @@ -138,8 +138,8 @@ _Also, please don't edit files in the "dist" subdirectory as they are generated
## Testing

- `npm run test:node` will use node to to run the tests
- `npm run test:browser` will run `npm run test:browser-6to5` and `npm run test:browser-traceur`
- `npm run test:browser-[transpiler]` use karma to run the tests with traceur or 6to5.
- `npm run test:browser` will run `npm run test:browser-babel` and `npm run test:browser-traceur`
- `npm run test:browser-[transpiler]` use karma to run the tests with Traceur or Babel.
- `npm run test:browser:perf` will use karma to run benchmarks

`npm run test:browser-[transpiler]` supports options after a double dash (`--`) :
Expand Down
4 changes: 2 additions & 2 deletions dist/es6-module-loader-sans-promises.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/es6-module-loader-sans-promises.js.map

Large diffs are not rendered by default.

24 changes: 16 additions & 8 deletions dist/es6-module-loader-sans-promises.src.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,12 @@ function logloads(loads) {
for (var i = 0, l = loader.loads.length; i < l; i++) {
if (loader.loads[i].name == name) {
existingLoad = loader.loads[i];

if(step == 'translate' && !existingLoad.source) {
existingLoad.address = stepState.moduleAddress;
proceedToTranslate(loader, existingLoad, Promise.resolve(stepState.moduleSource));
}

return existingLoad.linkSets[0].done.then(function() {
resolve(existingLoad);
});
Expand Down Expand Up @@ -1100,7 +1106,7 @@ function logloads(loads) {
})();

/*
* Traceur and 6to5 transpile hook for Loader
* Traceur and Babel transpile hook for Loader
*/
(function(Loader) {
// Returns an array of ModuleSpecifiers
Expand All @@ -1112,17 +1118,17 @@ function logloads(loads) {

Loader.prototype.transpile = function(load) {
if (!transpiler) {
if (this.transpiler == '6to5') {
transpiler = to5Transpile;
transpilerModule = isNode ? require('6to5-core') : __global.to5;
if (this.transpiler == 'babel') {
transpiler = babelTranspile;
transpilerModule = isNode ? require('babel-core') : __global.babel;
}
else {
transpiler = traceurTranspile;
transpilerModule = isNode ? require('traceur') : __global.traceur;
}

if (!transpilerModule)
throw new TypeError('Include Traceur or 6to5 for module syntax support.');
throw new TypeError('Include Traceur or Babel for module syntax support.');
}

return 'var __moduleAddress = "' + load.address + '";' + transpiler.call(this, load);
Expand Down Expand Up @@ -1154,17 +1160,19 @@ function logloads(loads) {
}
}

function to5Transpile(load) {
var options = this.to5Options || {};
function babelTranspile(load) {
var options = this.babelOptions || {};
options.modules = 'system';
options.sourceMap = 'inline';
options.filename = load.address;
options.code = true;
options.ast = false;
options.blacklist = options.blacklist || [];
options.blacklist.push('react');

var source = transpilerModule.transform(load.source, options).code;

// add "!eval" to end of 6to5 sourceURL
// add "!eval" to end of Babel sourceURL
// I believe this does something?
return source + '\n//# sourceURL=' + load.address + '!eval';
}
Expand Down
4 changes: 2 additions & 2 deletions dist/es6-module-loader.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/es6-module-loader.js.map

Large diffs are not rendered by default.

83 changes: 68 additions & 15 deletions dist/es6-module-loader.src.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ define(function() {
this._async = async;
this._running = false;

this._queue = new Array(1<<16);
this._queue = this;
this._queueLen = 0;
this._afterQueue = new Array(1<<4);
this._afterQueue = {};
this._afterQueueLen = 0;

var self = this;
Expand Down Expand Up @@ -126,6 +126,7 @@ define(function(require) {
var format = require('../format');

return function unhandledRejection(Promise) {

var logError = noop;
var logInfo = noop;
var localConsole;
Expand Down Expand Up @@ -345,6 +346,7 @@ define(function() {
return function makePromise(environment) {

var tasks = environment.scheduler;
var emitRejection = initEmitRejection();

var objectCreate = Object.create ||
function(proto) {
Expand Down Expand Up @@ -811,7 +813,8 @@ define(function() {

Pending.prototype.run = function() {
var q = this.consumers;
var handler = this.join();
var handler = this.handler;
this.handler = this.handler.join();
this.consumers = void 0;

for (var i = 0; i < q.length; ++i) {
Expand Down Expand Up @@ -973,6 +976,8 @@ define(function() {
};

Rejected.prototype.fail = function(context) {
this.reported = true;
emitRejection('unhandledRejection', this);
Promise.onFatalRejection(this, context === void 0 ? this.context : context);
};

Expand All @@ -982,9 +987,10 @@ define(function() {
}

ReportTask.prototype.run = function() {
if(!this.rejection.handled) {
if(!this.rejection.handled && !this.rejection.reported) {
this.rejection.reported = true;
Promise.onPotentiallyUnhandledRejection(this.rejection, this.context);
emitRejection('unhandledRejection', this.rejection) ||
Promise.onPotentiallyUnhandledRejection(this.rejection, this.context);
}
};

Expand All @@ -994,14 +1000,14 @@ define(function() {

UnreportTask.prototype.run = function() {
if(this.rejection.reported) {
Promise.onPotentiallyUnhandledRejectionHandled(this.rejection);
emitRejection('rejectionHandled', this.rejection) ||
Promise.onPotentiallyUnhandledRejectionHandled(this.rejection);
}
};

// Unhandled rejection hooks
// By default, everything is a noop

// TODO: Better names: "annotate"?
Promise.createContext
= Promise.enterContext
= Promise.exitContext
Expand Down Expand Up @@ -1214,6 +1220,45 @@ define(function() {

function noop() {}

function initEmitRejection() {
/*global process, self, CustomEvent*/
if(typeof process !== 'undefined' && process !== null
&& typeof process.emit === 'function') {
// Returning falsy here means to call the default
// onPotentiallyUnhandledRejection API. This is safe even in
// browserify since process.emit always returns falsy in browserify:
// https://github.com/defunctzombie/node-process/blob/master/browser.js#L40-L46
return function(type, rejection) {
return type === 'unhandledRejection'
? process.emit(type, rejection.value, rejection)
: process.emit(type, rejection);
};
} else if(typeof self !== 'undefined' && typeof CustomEvent === 'function') {
return (function(noop, self, CustomEvent) {
var hasCustomEvent = false;
try {
var ev = new CustomEvent('unhandledRejection');
hasCustomEvent = ev instanceof CustomEvent;
} catch (e) {}

return !hasCustomEvent ? noop : function(type, rejection) {
var ev = new CustomEvent(type, {
detail: {
reason: rejection.value,
key: rejection
},
bubbles: false,
cancelable: true
});

return !self.dispatchEvent(ev);
};
}(noop, self, CustomEvent));
}

return noop;
}

return Promise;
};
});
Expand Down Expand Up @@ -1606,6 +1651,12 @@ function logloads(loads) {
for (var i = 0, l = loader.loads.length; i < l; i++) {
if (loader.loads[i].name == name) {
existingLoad = loader.loads[i];

if(step == 'translate' && !existingLoad.source) {
existingLoad.address = stepState.moduleAddress;
proceedToTranslate(loader, existingLoad, Promise.resolve(stepState.moduleSource));
}

return existingLoad.linkSets[0].done.then(function() {
resolve(existingLoad);
});
Expand Down Expand Up @@ -2325,7 +2376,7 @@ function logloads(loads) {
})();

/*
* Traceur and 6to5 transpile hook for Loader
* Traceur and Babel transpile hook for Loader
*/
(function(Loader) {
// Returns an array of ModuleSpecifiers
Expand All @@ -2337,17 +2388,17 @@ function logloads(loads) {

Loader.prototype.transpile = function(load) {
if (!transpiler) {
if (this.transpiler == '6to5') {
transpiler = to5Transpile;
transpilerModule = isNode ? require('6to5-core') : __global.to5;
if (this.transpiler == 'babel') {
transpiler = babelTranspile;
transpilerModule = isNode ? require('babel-core') : __global.babel;
}
else {
transpiler = traceurTranspile;
transpilerModule = isNode ? require('traceur') : __global.traceur;
}

if (!transpilerModule)
throw new TypeError('Include Traceur or 6to5 for module syntax support.');
throw new TypeError('Include Traceur or Babel for module syntax support.');
}

return 'var __moduleAddress = "' + load.address + '";' + transpiler.call(this, load);
Expand Down Expand Up @@ -2379,17 +2430,19 @@ function logloads(loads) {
}
}

function to5Transpile(load) {
var options = this.to5Options || {};
function babelTranspile(load) {
var options = this.babelOptions || {};
options.modules = 'system';
options.sourceMap = 'inline';
options.filename = load.address;
options.code = true;
options.ast = false;
options.blacklist = options.blacklist || [];
options.blacklist.push('react');

var source = transpilerModule.transform(load.source, options).code;

// add "!eval" to end of 6to5 sourceURL
// add "!eval" to end of Babel sourceURL
// I believe this does something?
return source + '\n//# sourceURL=' + load.address + '!eval';
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "es6-module-loader",
"description": "An ES6 Module Loader shim",
"version": "0.13.1",
"version": "0.14.0",
"homepage": "https://github.com/ModuleLoader/es6-module-loader",
"author": {
"name": "Guy Bedford, Luke Hoban, Addy Osmani",
Expand Down

0 comments on commit 2cc6f36

Please sign in to comment.