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

TransformerFactory isn't re-called in webpack watch mode when using pure ts-loader without custom compiler (like ttypescript) #1615

Open
artem1458 opened this issue Jun 15, 2023 · 3 comments

Comments

@artem1458
Copy link

artem1458 commented Jun 15, 2023

Detailed Description

I'm writing typescript compiler plugin that is using ts.Program and ts.TypeChecker, when project is runned in webpack watch (serve) mode and without ttypescript compiler - TransformerFactory function is called only once (on the start of webpack process), it's causing problems, because ts.Program.getSourceFiles() always returning initial version of source files, same as ts.TypeChecker always referring to the initial version of project

Expected Behaviour

I see few possible solutions here:

  • Just call TransformerFactory function in watch mode with updated ts.Program instance. Possible problem here is that it could break existing transformers which rely on the fact that this TransformerFactory function will be called only once
  • Create a Proxy that will always refer to the "fresh" ts.Program. I don't see any problems here, because proxies is supported from Node.js >=6, and ts-loader is requires Node.js >=12

Actual Behaviour

TransformerFactory hold reference to the old (outdated) ts.Program instance, and since we're acquiring ts.TypeChecker from the ts.Program - we're also receiving outdated ts.TypeChecker

Steps to Reproduce the Problem

  1. Create transformer that requires access to the ts.Program and/or ts.TypeChecker
  2. Use ts-loader without any custom compilers (like ttypescript)
  3. Run webpack project in watch mode
  4. Make change in any project file. As an example - change type of class constructor property from string to number
  5. Call ts.Program.getSourceFiles() and find file in which change was made, or acquire ts.TypeChecker from the ts.Program, and try to use it to receive type of class constructor property
  6. Using ts.Program.getSourceFiles() check that file content is not changed (identical to initial content of the file)
  7. Using ts.TypeChecker check that type of class constructor property is not changed

Location of a Minimal Repository that Demonstrates the Issue.

https://github.com/artem1458/ts-loader-old-tsprogram-instance

Also, here is the video with reproducing

Screen.Recording.2023-06-15.at.20.44.01.mov
@artem1458 artem1458 changed the title TransformerFactory isn't re-called in watch webpack watch mode when using pure ts-loader without custom compiler (like ttypescript) TransformerFactory isn't re-called in webpack watch mode when using pure ts-loader without custom compiler (like ttypescript) Jun 16, 2023
@artem1458
Copy link
Author

@johnnyreilly Hey, just find out that everything works fine with getProgram function, but I think if it's expected behaviour - it should be documented

https://github.com/TypeStrong/ts-loader#getcustomtransformers

(program: Program, getProgram: () => Program) => { before?: TransformerFactory<SourceFile>[]; after?: TransformerFactory<SourceFile>[]; afterDeclarations?: TransformerFactory<SourceFile>[]; }

@johnnyreilly
Copy link
Member

Would you like to document it?

@artem1458
Copy link
Author

@johnnyreilly Sure!

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

No branches or pull requests

2 participants