TypeStrong / ts-loader

TypeScript loader for webpack
https://johnnyreilly.com/ts-loader-goes-webpack-5
MIT License
3.44k stars 429 forks source link

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

Open artem1458 opened 1 year ago

artem1458 commented 1 year ago

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:

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

https://github.com/TypeStrong/ts-loader/assets/33227963/7731ac4e-7eb0-4781-8130-f7c24bdd09bb

artem1458 commented 1 year ago

@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 commented 1 year ago

Would you like to document it?

artem1458 commented 1 year ago

@johnnyreilly Sure!