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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
einatbar opened this issue Mar 8, 2022 · 2 comments 路 Fixed by #1439
Closed

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

einatbar opened this issue Mar 8, 2022 · 2 comments 路 Fixed by #1439

Comments

@einatbar
Copy link
Contributor

einatbar commented Mar 8, 2022

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.

@johnnyreilly
Copy link
Member

If you'd like to submit a PR I'd happily take a look

@einatbar
Copy link
Contributor Author

einatbar commented Mar 8, 2022

@johnnyreilly I opened a PR: #1439

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

Successfully merging a pull request may close this issue.

2 participants