Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic TypeScript Support #84

Merged
merged 18 commits into from
Dec 8, 2017
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ coverage
dist
lib
!test/**/node_modules
.vscode/
3 changes: 2 additions & 1 deletion src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Parser {
this.registerExtension('js', './assets/JSAsset');
this.registerExtension('jsx', './assets/JSAsset');
this.registerExtension('es6', './assets/JSAsset');
this.registerExtension('ts', './assets/TypeScriptAsset');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add tsx as well

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

this.registerExtension('tsx', './assets/TypeScriptAsset');
this.registerExtension('json', './assets/JSONAsset');
this.registerExtension('yaml', './assets/YAMLAsset');
this.registerExtension('yml', './assets/YAMLAsset');
Expand Down Expand Up @@ -46,7 +48,6 @@ class Parser {
if (typeof parser === 'string') {
parser = this.extensions[extension] = require(parser);
}

return parser;
}

Expand Down
23 changes: 23 additions & 0 deletions src/assets/TypeScriptAsset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const JSAsset = require('./JSAsset');
const localRequire = require('../utils/localRequire');

class TypeScriptAsset extends JSAsset {
async transform() {
this.ast = await this.parse(this.contents);
this.isAstDirty = true;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method shouldn't be necessary, and overriding it here disables other transforms like babel (post typescript) if needed, and uglify. parse should already be called by the super Asset class.


async parse(code) {
// require typescript, installed locally in the app
let typescript = localRequire('typescript', this.name);

let parserOptions = {
module: typescript.ModuleKind.CommonJS

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might need to add jsx: true to the options

}

// Transpile Module using TypeScript and parse result as ast format through babylon
return await super.parse(typescript.transpileModule(code, parserOptions).outputText);
}
}

module.exports = TypeScriptAsset;
3 changes: 1 addition & 2 deletions src/builtins/prelude.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ require = (function (modules, cache, entry) {
if (previousRequire) {
return previousRequire(name, true);
}

var err = new Error('Cannot find module \'' + name + '\'');
err.code = 'MODULE_NOT_FOUND';
throw err;
Expand Down Expand Up @@ -58,7 +57,7 @@ require = (function (modules, cache, entry) {
newRequire.modules = modules;
newRequire.cache = cache;
newRequire.parent = previousRequire;

for (var i = 0; i < entry.length; i++) {
newRequire(entry[i]);
}
Expand Down