TypeStrong / TypeStrong/ts-loader

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

Open
#1,615 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
3.5k
Forks
439
Avg merge
17h 17m
Merged PRs (30d)
2

Description

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

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked minimal repository and reproduce the issue in webpack watch mode using ts-loader without a custom compiler. Trace the transformer path that retains the ts.Program and ts.TypeChecker, then verify that changed source files and types are reflected after rebuilds without breaking existing transformer behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript, webpack
Domain
build-system, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.