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

Commit

Permalink
TypeScript support (squashed)
Browse files Browse the repository at this point in the history
Original commit messages:

* initial version of TypeScript support
* added typescript as a transpiler to karma.conf
* add typescript dependency to package.json
* allow TypeScript options to be set from the outside
* updated README.md
* added typescript to .travis.yml
* set CompilerOptions.target if it was not assigned before
* reverted changes in dist
* do not inline source file contents by default
  • Loading branch information
vladima authored and guybedford committed May 30, 2015
1 parent 2bb4023 commit 466616a
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ env:
matrix:
- PARSER="traceur"
- PARSER="babel"
- PARSER="typescript"
matrix:
include:
- node_js: "0.10"
env: SAUCE_LABS=true PARSER="traceur"
- node_js: "0.10"
env: SAUCE_LABS=true PARSER="babel"
- node_js: "0.10"
env: SAUCE_LABS=true PARSER="typescript"
# - node_js: "0.10"
# env: SAUCE_LABS=true PARSER="traceur" OPTIONS="--ie8"
# - node_js: "0.10"
Expand Down
32 changes: 24 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,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). The new loader implementing the [WhatWG loader spec](https://github.com/whatwg/loader) is pending alpha release on the [1.0 branch](https://github.com/ModuleLoader/es6-module-loader/tree/1.0).

* Provides an asynchronous loader (`System.import`) to [dynamically load ES6 modules](#getting-started).
* Supports both [Traceur](https://github.com/google/traceur-compiler) and [Babel](http://babeljs.io/) for compiling ES6 modules and syntax into ES5 in the browser with source map support.
* Supports [Traceur](https://github.com/google/traceur-compiler), [Babel](http://babeljs.io/) and [TypeScript](https://github.com/Microsoft/TypeScript/) 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 @@ -30,7 +30,7 @@ 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 `babel.js` in the page first then include `es6-module-loader.js`:
If using ES6 syntax (optional), include `traceur.js`, `babel.js` or `typescript.js` in the page first then include `es6-module-loader.js`:

```html
<script src="traceur.js"></script>
Expand All @@ -45,6 +45,14 @@ To use Babel, load Babel's `browser.js` instead and set the transpiler to `babel
</script>
```

To use TypeScript, set the transpiler to `typescript` in the loader configuration:

```html
<script>
System.transpiler = 'typescript';
</script>
```

Then we can write any ES6 module:

mymodule.js:
Expand Down Expand Up @@ -76,12 +84,18 @@ If using Traceur, these can be set with:
System.traceurOptions = {...};
```

Or with Babel:
With Babel:

```javascript
System.babelOptions = {...};
```

With TypeScript:

```javascript
System.typescriptOptions = {...};
```

#### Module Tag

As well as defining `window.System`, this polyfill provides support for the `<script type="module">` tag:
Expand All @@ -102,10 +116,10 @@ See the [demo folder](https://github.com/ModuleLoader/es6-module-loader/blob/mas
#### NodeJS Use

```
npm install es6-module-loader babel traceur
npm install es6-module-loader babel traceur typescript
```

It is important that Babel or Traceur is installed into the path in order to be found, since these are no longer project dependencies.
It is important that Babel, Traceur or TypeScript is installed into the path in order to be found, since these are no longer project dependencies.

For use in NodeJS, the `Loader` and `System` globals are provided as exports:

Expand All @@ -115,7 +129,9 @@ index.js:
/*
* Include:
* System.transpiler = 'babel';
* to use Babel instead of Traceur
* to use Babel instead of Traceur or
* System.transpiler = 'typescript';
* to use TypeScript
*/

System.import('some-module').then(function(m) {
Expand All @@ -142,8 +158,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-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` will run `npm run test:browser-babel`, `npm run test:browser-traceur` and `npm run test:browser-typescript`
- `npm run test:browser-[transpiler]` use karma to run the tests with Traceur, Babel or TypeScript.
- `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: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ try {
System.paths.babel = System.paths.babel || filePrefix + require.resolve('babel/browser.js');
}
catch(e) {}
try {
System.paths.typescript = filePrefix + require.resolve('typescript/bin/typescript.js');
}
catch(e) { }

module.exports = {
Loader: global.LoaderPolyfill,
Expand Down
17 changes: 13 additions & 4 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,19 @@ if (options.saucelabs) {
}

////

module.exports = function(config) {

var files = [
'test/_helper.js',
[options['babel'] ? 'node_modules/regenerator/runtime.js' : ''],

[!options.ie8 ? (!options['babel'] ? 'node_modules/traceur/bin/traceur.js' : 'node_modules/babel-core/browser.js') : ''],
[!options.ie8
? (options['babel']
? 'node_modules/babel-core/browser.js'
: options['typescript']
? 'node_modules/typescript/bin/typescript.js'
: 'node_modules/traceur/bin/traceur.js')
: ''],

[options.polyfill ? 'node_modules/when/es6-shim/Promise' : ''],
'dist/es6-module-loader-dev.src.js',
Expand All @@ -51,10 +56,10 @@ module.exports = function(config) {
{pattern: 'test/{loader,loads,syntax,worker}/**/*', included: false},
{pattern: 'node_modules/traceur/bin/traceur.js', included: false},
{pattern: 'node_modules/babel-core/browser.js', included: false},
{pattern: 'node_modules/typescript/bin/typescript.js', included: false},
{pattern: 'node_modules/when/es6-shim/Promise.js', included: false},
{pattern: 'dist/es6-module-loader*.js', included: false}
];

// Default Config
config.set({
basePath: '',
Expand All @@ -68,7 +73,11 @@ module.exports = function(config) {
timeout: 8000
},
system: {
transpiler: options.babel ? 'babel' : 'traceur'
transpiler: options.babel
? 'babel'
: options.typescript
? 'typescript'
: 'traceur'
}
}
});
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"minimist": "^1.1.0",
"mocha": "^2.0.1",
"regenerator": "^0.8.9",
"traceur": "0.0.87"
"traceur": "0.0.87",
"typescript": "mhegazy/typescript#v1.5-beta2"
},
"keywords": [
"script",
Expand All @@ -55,10 +56,11 @@
},
"scripts": {
"test": "npm run test:node && npm run test:browser",
"test:node": "mocha test/_node-traceur.js && mocha test/_node-babel.js",
"test:browser": "npm run test:browser-traceur && npm run test:browser-babel",
"test:node": "mocha test/_node-traceur.js && mocha test/_node-babel.js && mocha test/_node-typescript.js",
"test:browser": "npm run test:browser-traceur && npm run test:browser-babel && npm run test:browser-typescript",
"test:browser-traceur": "karma start --single-run",
"test:browser-babel": "karma start --single-run --babel",
"test:browser-typescript": "karma start --single-run --typescript",
"test:browser:perf": "karma start karma-benchmark.conf.js --single-run"
},
"dependencies": {
Expand Down
10 changes: 6 additions & 4 deletions src/declarative.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@
};
load.status = 'linked';
}

finishLoad(loader, load);
}

Expand Down Expand Up @@ -140,8 +139,11 @@
for (var i = 0, l = module.importers.length; i < l; i++) {
var importerModule = module.importers[i];
if (!importerModule.locked) {
var importerIndex = indexOf.call(importerModule.dependencies, module);
importerModule.setters[importerIndex](moduleObj);
for (var j = 0; j < importerModule.dependencies.length; ++j) {
if (importerModule.dependencies[j] === module) {
importerModule.setters[j](moduleObj);
}
}
}
}

Expand Down Expand Up @@ -283,4 +285,4 @@
module.execute = undefined;
return err;
}
})();
})();
35 changes: 30 additions & 5 deletions src/transpiler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Traceur and Babel transpile hook for Loader
* Traceur, Babel and TypeScript transpile hook for Loader
*/

function setupTranspilers(loader) {
Expand All @@ -8,6 +8,8 @@ function setupTranspilers(loader) {
loader.set('traceur', loader.newModule({ 'default': __global.traceur, __useDefault: true }));
if (__global.babel && !loader.has('babel'))
loader.set('babel', loader.newModule({ 'default': __global.babel, __useDefault: true }));
if (__global.ts && !loader.has('typescript'))
loader.set('typescript', loader.newModule({ 'default': __global.ts, __useDefault: true }));
}

var transpile = (function() {
Expand All @@ -17,13 +19,24 @@ var transpile = (function() {

function transpile(load) {
var self = this;

return (self.pluginLoader || self)['import'](self.transpiler).then(function(transpiler) {
if (transpiler.__useDefault)
transpiler = transpiler['default'];

var transpileFunction;
if (transpiler.Compiler) {
transpileFunction = traceurTranspile;
}
else if (transpiler.createLanguageService) {
transpileFunction = typescriptTranspile;
}
else {
transpileFunction = babelTranspile;
}

return 'var __moduleName = "' + load.name + '", __moduleAddress = "' + load.address + '";'
+ (transpiler.Compiler ? traceurTranspile : babelTranspile).call(self, load, transpiler)
+ transpileFunction.call(self, load, transpiler)
+ '\n//# sourceURL=' + load.address + '!eval';

// sourceURL and sourceMappingURL:
Expand All @@ -49,7 +62,7 @@ var transpile = (function() {
__eval('(function(require,exports,module){' + load.source + '})();', load.address, __global);
__global.System = curSystem;
__global.Reflect.Loader = curLoader;
return self.newModule({ 'default': __global[self.transpiler], __useDefault: true });
return self.newModule({ 'default': __global[self.transpiler == 'typescript' ? 'ts' : self.transpiler], __useDefault: true });
}
};
}
Expand Down Expand Up @@ -90,5 +103,17 @@ var transpile = (function() {
return babel.transform(load.source, options).code;
}

function typescriptTranspile(load, ts) {
var options = this.typescriptOptions || {};
if (options.target === undefined) {
options.target = ts.ScriptTarget.ES5;
}
options.module = ts.ModuleKind.System;
options.inlineSourceMap = true;

var source = ts.transpile(load.source, options, load.address);
return source + '\n//# sourceURL=' + load.address + '!eval';;
}

return transpile;
})();
})();
1 change: 1 addition & 0 deletions test/_browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ System.baseURL += 'base/';

System.paths.traceur = 'node_modules/traceur/bin/traceur.js';
System.paths.babel = 'node_modules/babel-core/browser.js';
System.paths.typescript = 'node_modules/typescript/bin/typescript.js';

System.transpiler = __karma__.config.system.transpiler;
14 changes: 14 additions & 0 deletions test/_node-typescript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

global.expect = require('expect.js');

require('./_helper');

global.System = require('../index').System;
global.ts = require('typescript');
global.System.transpiler = 'typescript';

require('./system.spec');

require('./custom-loader');
require('./custom-loader.spec');
7 changes: 4 additions & 3 deletions test/system.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('System', function () {
it('should support set, get and delete', function(done) {

var testPath = System.baseURL + 'test/loader/module.js';

System.import(testPath).then(function(m) {
expect(m.run).to.equal('first');
System.delete(testPath);
Expand Down Expand Up @@ -88,7 +88,8 @@ describe('System', function () {
.then(done, done);
});

(ie ? it.skip : it)('should import an ES6 script with a generator', function (done) {
// typescript does not support generators yet
(ie || System.transpiler === 'typescript' ? it.skip : it)('should import an ES6 script with a generator', function (done) {
System.import('test/syntax/es6-generator.js')
.then(function (m) {
expect(!!m.generator).to.be.ok();
Expand Down Expand Up @@ -395,7 +396,7 @@ describe('System', function () {
});

});

describe('#System.define', function () {

it.skip('should load System.define', function(done) {
Expand Down
9 changes: 9 additions & 0 deletions test/worker/worker-typescript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
importScripts("../../node_modules/when/es6-shim/Promise.js",
"../../dist/es6-module-loader-dev.src.js");
System.paths['typescript'] = "../../node_modules/typescript/bin/typescript.js";
System.transpiler = 'typescript';
System['import']('es6.js').then(function(m) {
postMessage(m.p);
}, function(err) {
console.error(err, err.stack);
});

0 comments on commit 466616a

Please sign in to comment.