Skip to content

Bug fix: support webpack 5 in ts-loader #1438

@einatbar

Description

@einatbar

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch ts-loader@9.2.6 for the project I'm working on.

Error when running ts-loader with webpack 5 : "times is not iterable"
Previous work was done here by @elf-mouse #1290 but was not accepted.

In my solution I used the new webpack 5 API inside the compilation object, that exposes the _fileTimestamps through fileSystemInfo

Here is the diff that solved my problem:

diff --git a/node_modules/ts-loader/dist/watch-run.js b/node_modules/ts-loader/dist/watch-run.js
index a2f65a7..5e91edc 100644
--- a/node_modules/ts-loader/dist/watch-run.js
+++ b/node_modules/ts-loader/dist/watch-run.js
@@ -25,26 +25,30 @@ function makeWatchRun(instance, loader) {
             instance.reportTranspileErrors = true;
         }
         else {
-            const times = compiler.fileTimestamps;
-            for (const [filePath, date] of times) {
-                const key = instance.filePathKeyMapper(filePath);
-                const lastTime = lastTimes.get(key) || startTime;
-                if (!date ||
-                    date === 'ignore' ||
-                    (date.timestamp || date.safeTime) <= lastTime) {
-                    continue;
-                }
-                lastTimes.set(key, date.timestamp || date.safeTime);
-                promises.push(updateFile(instance, key, filePath, loader, loaderIndex));
-            }
-            // On watch update add all known dts files expect the ones in node_modules
-            // (skip @types/* and modules with typings)
-            for (const [key, { fileName }] of instance.files.entries()) {
-                if (fileName.match(constants.dtsDtsxOrDtsDtsxMapRegex) !== null &&
-                    fileName.match(constants.nodeModules) === null) {
-                    promises.push(updateFile(instance, key, fileName, loader, loaderIndex));
-                }
-            }
+            compiler.hooks.compilation.tap('ts-loader', compiliation => {
+                compiliation.fileSystemInfo._fileTimestamps.stack.forEach(times => {
+                    for (const [filePath, date] of times) {
+                        const key = instance.filePathKeyMapper(filePath);
+                        const lastTime = lastTimes.get(key) || startTime;
+                        if (!date ||
+                          date === 'ignore' ||
+                          (date.timestamp || date.safeTime) <= lastTime) {
+                            continue;
+                        }
+                        lastTimes.set(key, date.timestamp || date.safeTime);
+                        promises.push(updateFile(instance, key, filePath, loader, loaderIndex));
+                    }
+
+                    // On watch update add all known dts files expect the ones in node_modules
+                    // (skip @types/* and modules with typings)
+                    for (const [key, { fileName }] of instance.files.entries()) {
+                        if (fileName.match(constants.dtsDtsxOrDtsDtsxMapRegex) !== null &&
+                          fileName.match(constants.nodeModules) === null) {
+                            promises.push(updateFile(instance, key, fileName, loader, loaderIndex));
+                        }
+                    }
+                })
+            })
         }
         // Update all the watched files from solution builder
         if (instance.solutionBuilderHost) {

This issue body was partially generated by patch-package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions