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

Basic TypeScript Support #84

merged 18 commits into from
Dec 8, 2017

Conversation

DeMoorJasper
Copy link
Member

Potential implementation of basic typescript support #3

@@ -10,6 +10,7 @@ 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

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

let typescript = localRequire('typescript', this.name);

let transpilerOptions = {
compilerOptions: {
Copy link

Choose a reason for hiding this comment

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

Might make sense to use this already to load a project's tsconfig.json file: https://www.npmjs.com/package/tsconfig

If you do, be sure to overwrite noEmit to be false, so that people can use tsc as a kind of type-linter by setting noEmit to true in their tsconfig.json.

Copy link
Member Author

Choose a reason for hiding this comment

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

will def look into this

Copy link
Member

@devongovett devongovett left a comment

Choose a reason for hiding this comment

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

Looks awesome thanks so much!! Just a couple things!

Can you also add a few tests? You can check the javascript ones to see how they are written. https://github.com/parcel-bundler/parcel/blob/master/test/javascript.js

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.


let transpilerOptions = {
compilerOptions: {
module: typescript.ModuleKind.CommonJS,
Copy link
Member

Choose a reason for hiding this comment

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

Does this still support ES2015 modules as well?

Copy link
Member Author

@DeMoorJasper DeMoorJasper Dec 7, 2017

Choose a reason for hiding this comment

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

It's configurable by adding a tsconfig.json file, I changed it to default to ES2015 now.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I guess I should ask what this option is for. Does it control parsing behavior or code generation? If codegen, we want commonjs for sure.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's codegen

module: typescript.ModuleKind.CommonJS,
jsx: true
},
fileName: this.basename

Choose a reason for hiding this comment

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

Maybe I'm missing something, but why do we need fileName here?

Copy link
Member Author

@DeMoorJasper DeMoorJasper Dec 7, 2017

Choose a reason for hiding this comment

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

If i'm reading the TypeScript transpile API docs correctly it should autodetect wether the code is ts or tsx based on filename, this is why i included it.
Usually TypeScript compiler uses file extension to determine if file should be parsed as '.tsx' or '.ts'. The same rule is applied during single file transpilation if the file name is provided.

Copy link

@felipe-gdr felipe-gdr Dec 7, 2017

Choose a reason for hiding this comment

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

According to the docs, fileName is an optional parameter of the ts#transpile (which by the way has been deprecated in favour of transpileModule, that is been used here) and not a property of the tsconfig. If you take a look at the Json schema you won't find a fileName property there: http://json.schemastore.org/tsconfig

Copy link
Member Author

@DeMoorJasper DeMoorJasper Dec 7, 2017

Choose a reason for hiding this comment

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

The interface TranspileOptions which this is, does contain a FileName as far as i can see in their source code

export interface TranspileOptions {
    compilerOptions?: CompilerOptions;
    fileName?: string;
    reportDiagnostics?: boolean;
    moduleName?: string;
    renamedDependencies?: Map<string>;
}

export function transpileModule(input: string, transpileOptions: TranspileOptions): TranspileOutput

Copy link

@felipe-gdr felipe-gdr Dec 7, 2017

Choose a reason for hiding this comment

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

Source code doesn't lie :-D

@devongovett devongovett merged commit 757b673 into parcel-bundler:master Dec 8, 2017
@devongovett
Copy link
Member

Awesome thanks for your work on this!! I cleaned up a couple things here and added a test for tsconfig.json support here c27c5c8

@imvetri
Copy link

imvetri commented Dec 9, 2017

Really good. I went through the PR I would also like to know understand how this was done. The code change was minimal but functionality wise I couldn't understand. Is there any doc that says explains how to extend support?

@DeMoorJasper
Copy link
Member Author

DeMoorJasper commented Dec 9, 2017

@imvetri It extends the JSAsset class, which just takes over all functionality.
Overwrites parse function so that this gets used instead of the JSAsset one, to transpile it into JS.
At the end of parse it replaces this.contents with transpiled TS code which makes sure all the other functions in transform (minifying, imports, ...) still work (because they are all expecting JS)
After that it calls the super.parse which just makes sure it is valid to the babel config and puts it into AST format.
That's mainly everything that changed
You might be intrested in taking a look at this Parcel.js/asset_types

devongovett pushed a commit that referenced this pull request Oct 15, 2018
* Tiny gitignore cleanup

* firefox fix, #36

* ts support

* tsx added

* transpileoptions improvements and bugfix

* tsconfig.json support

* add basic typescript test

* Default to ES2015 to stay consistent with the js default

* remove custom transform, implement check in js transform for ts and put codegen back to commonjs

* keep ts transform addition within ts module

* ts testing and bugfix

* improve jsx parameter and default to preserve and let babel handle it

* improve jsx parameter and default to preserve and let babel handle it
devongovett pushed a commit that referenced this pull request Oct 15, 2018
* Tiny gitignore cleanup

* firefox fix, #36

* ts support

* tsx added

* transpileoptions improvements and bugfix

* tsconfig.json support

* add basic typescript test

* Default to ES2015 to stay consistent with the js default

* remove custom transform, implement check in js transform for ts and put codegen back to commonjs

* keep ts transform addition within ts module

* ts testing and bugfix

* improve jsx parameter and default to preserve and let babel handle it

* improve jsx parameter and default to preserve and let babel handle it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants