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

Async Transformers #29100

Open
kaaninel opened this issue Dec 19, 2018 · 7 comments
Open

Async Transformers #29100

kaaninel opened this issue Dec 19, 2018 · 7 comments
Labels
API Relates to the public API for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@kaaninel
Copy link

I was using Compiler API and basically changed program.emit at host.afterProgramCreate just like examples and hooked my transformers into emit function. But one thing i couldn't find anywhere is a async transformer. Im using PostCSS and some other stuff like that and implementing those to a single typescript compiler but some plugins are async in postcss and i cant use them at the moment. Is there any way to implement this that im missing or undocumented ? And in case somebody offers to use webpack my code is mostly javascript based and i need really good static analysis for it with type data included that's also reason i use typescript's api instead of using babel which have those cases mostly covered with plugins. I'm implementing all-in-one solution for my company/community project so ideally it needs to be inside typescript compiler.

@DanielRosenwasser
Copy link
Member

Can you give a concrete example of why the transformations/emit themselves being synchronous is a problem?

@j-oliveras
Copy link
Contributor

@DanielRosenwasser I think he want to integrate an async transform into the compiler, but the compiler expects all transforms to be synchronous.

@kaaninel
Copy link
Author

kaaninel commented Dec 20, 2018

Basically i need

const Template = node.initializer.template as ts.NoSubstitutionTemplateLiteral;
PostCSS.process({ [(Cls as any).Tag]: Template.text }).then(p => {
  node.initializer.template = ts.createNoSubstitutionTemplateLiteral(p.css);
});

instead of

const Template = node.initializer.template as ts.NoSubstitutionTemplateLiteral;
const p = PostCSS.process({ [(Cls as any).Tag]: Template.text });
node.initializer.template = ts.createNoSubstitutionTemplateLiteral(p.css);

Tbh I dont think this is really necessary if i was writing whole system sync is more than enough but for 3rd party solutions i need this.

@weswigham
Copy link
Member

You could walk the tree, process all the postCSS tags asynchronously and save the results, then start a compilation/transform pass and perform the transformations synchronously using the saved results, no?

@kaaninel
Copy link
Author

It would feel like a dirty hack but its possible. I will try it out and comment results. I hope it wont mess with watcher.

@weswigham weswigham added Question An issue which isn't directly actionable in code API Relates to the public API for TypeScript Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature and removed Question An issue which isn't directly actionable in code labels Dec 20, 2018
@longlho
Copy link
Contributor

longlho commented Mar 24, 2019

@kaaninel FWIW, we did what @weswigham suggested and pre-process all CSS into json and inline with https://github.com/longlho/ts-transform-json. The JSONs are then also feed into other toolchain like jest or webpack and prevents things like className gets generated differently. (We regenerate/filter certain class name bc of adblocker)

@kaaninel
Copy link
Author

I implemented it ( in more basic way )
I basically scanned all of the typescript files and generated one big css file and processed it with postcss. Downside is I cant implement string interpolation.
Linker/CSSCompiler (Where i collect all css source)
Linker/CustomElements (Where i detect css sources)
Linker/Compiler (Where I process postcss and emit)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Relates to the public API for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants