-
-
Notifications
You must be signed in to change notification settings - Fork 431
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
support custom transformers for ts 2.3 #535
Conversation
Sorry for the delay in responding - I've been pretty occupied with other things. Would you mind giving a few more details about this please? I'm guessing that your change is related to: microsoft/TypeScript#14419 ? Having had a quick read I'm not totally clear as to the purpose of custom transformers - could you elaborate a little? Also, it looks like you might need to resupply the PR as the changes for happypack compatibility collide with your changes. Finally, please could also provide a test? Thanks! |
Ping @longlho 😄 |
@johnnyreilly sry this slipped thru my attention. I believe the purpose of custom transformers is like babel-plugin: You have more control over what's being transformed as TSC walks thru the AST. 1 example is loading external asset (https://github.com/longlho/ts-transform-img). This allows you to I'll rebase the PR :) |
Hey @johnnyreilly @longlho here's an example of a TS transformer for styled-components: https://github.com/Igorbek/typescript-plugin-styled-components. Since ts-loader doesn't support them, I have only section for integrating with awesome-typescript-loader. |
yeah sorry I've been tied up. Will try to wrap this up this week |
Great - thanks @longlho! Could we use the same API as awesome-typescript-loader please? getCustomTransformers?(): ts.CustomTransformers We've a vague plan that atl and ts-loader may come together at some point. Either way, we want it to be easy for people to move from one loader to the other as required. |
@johnnyreilly I fixed the PR. I'm trying to find a similar test to mimic but couldn't. Can you point me to something? |
Since there's no way for this to be tested at runtime (I think?) then it makes logical sense to create a comparison test rather than an execution test. I'd be tempted to use this one as a basis: https://github.com/TypeStrong/ts-loader/tree/master/test/comparison-tests/basic Remember you only need generate the 2.3 expected output as we only run comparison tests for the latest released build. Do set me straight if you think this can be covered by an execution test but since it's about specifically using transformed output I'm guessing that doesn't make sense. |
Build failure BTW:
|
As a test input, can be passed a simple transformer. I can write one if you want. |
I think build might not be passing on 2.4.0-rc :-/ I tried changing the sub version and condition to support 2.4 but a bunch failed. |
Don't worry about 2.4 - that's a possible regression in TypeScript 2.4. see here: microsoft/TypeScript#16609 (comment) |
@Igorbek that'd be very helpful - thanks! |
(I haven't had time to fully understand custom transformers as yet) |
Yeah - looking at the build this all seems good. The failure seems to be TypeScript@next related and I don't think that's something that need concern us as I'm expecting that to be resolved in the compiler itself. All we need is a test and I think we're good to go! @Igorbek - if you get the chance it might be good to see if @longlho's fork works with https://github.com/Igorbek/typescript-plugin-styled-components as you would hope |
Here's a simple transformer that makes all string literals uppercase: import * as ts from 'typescript';
const transformer: ts.TransformerFactory<ts.SourceFile> = (context) => {
const visitor: ts.Visitor = (node) => {
if (node.kind === ts.SyntaxKind.StringLiteral) {
const text = (node as ts.StringLiteral).text;
if (text !== text.toUpperCase()) {
return ts.createLiteral(text.toUpperCase());
}
}
return ts.visitEachChild(node, visitor, context);
}
return (node) => ts.visitNode(node, visitor);
};
export default transformer; |
Great! How would you expect that to be plugged into ts-loader's config? |
Also; how would the output of that be expected to look? Am I right when I guess that this input: const greeting = 'hi'; would become transformed into: const greeting = 'HI'; ? |
yeah I think that's the output. The way custom transformer would be passed in is something like:
|
@johnnyreilly that's right, expected output is correct getCustomTransformers: () => ({ // note parens
before: [transformer]
}) |
Okay - I've had a first go at a comparison test based on @Igorbek's sample. Unfortunately it doesn't seem to work - I'm not too sure why though... BTW I built ts-loader with [email protected] but I was using TypeScript 2.3.1 to generate the test data as that's what the test pack runs against. (So post build of ts-loader and pre-test I |
// 2. create a transformer; | ||
// the factory additionally accepts an options object which described below | ||
var styledComponentsTransformer = createStyledComponentsTransformer(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sample transformer exports the transformer itself, and not a factory function that typescript-plugin-styled-components
does. So you don't need to call the function:
var uppercaseStringLiteralTransformer = require('./uppercaseStringLiteralTransformer').default;
loader: 'ts-loader', | ||
options: { | ||
getCustomTransformers: () => ({ // note parens | ||
before: [styledComponentsTransformer] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
before: [uppercaseStringLiteralTransformer]
Thanks @Igorbek - just made those changes. Looks promising! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks very nice!
Asset Size Chunks Chunk Names | ||
bundle.js 2.92 kB 0 [emitted] main | ||
chunk {0} bundle.js (main) 193 bytes [entry] [rendered] | ||
[0] ./.test/customTransformer/SUBMODULE/SUBMODULE.ts 78 bytes {0} [built] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
transformer uppercased the module import as well :) it was unintentional, but ok ))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In actual fact that is the reason the test is failing on Travis right now I think. Case insensitive Windows Vs case sensitive Linux 😄 I'll change the test so it is 1 file only (no imports) tonight and then we should have a reliable test that works cross platform. Then I think we're ready to merge!
Okay - so we've now got a cross platform test! However, it looks like customTransfrmers aren't applied in |
hmm nope those 2 configs should be independent |
If you look at
|
So maybe this isn't supported for transpileOnly? |
Maybe in 2.4 they're supporting it? (https://github.com/Microsoft/TypeScript/blob/4b3e661aaa69e53b6a5992c76f2232c9f5dace10/src/services/transpile.ts#L2-L9) |
yes, 2.4rc supports it. |
Ok - so the test was generated with 2.3 so if it was added with 2.4 that makes sense. If you use it with 2.4 it'll just work? |
I'm happy - let's merge! |
I'm cutting ts-loader 2.2 right now - should be released within the hour. Thanks for the contribution! |
Hey guys, how do I get to the type checker ( |
Do you mean fork-ts-checker-webpack-plugin? It's a separate npm package: https://github.com/Realytics/fork-ts-checker-webpack-plugin |
Nonono, I meant how to get to the TypeScript's internal instance of |
See this custom transformer for example: https://github.com/kimamula/ts-transformer-enumerate/blob/master/transformer.ts He uses argument |
@TomMarius you can't get access to the |
@longlho Okay, I'll probably create a PR in the following days. |
TS 2.3.1 support custom transformer so I think ts-loader should support that too