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

@lingui/loader with webpack 5 and jest #999

Closed
samsaggace opened this issue Mar 10, 2021 · 5 comments
Closed

@lingui/loader with webpack 5 and jest #999

samsaggace opened this issue Mar 10, 2021 · 5 comments

Comments

@samsaggace
Copy link

Hi! 👋

Firstly, thanks for your work on this project! 🙂

I use @lingui/loader to dynamically generate the js locales from the po and created a transformer in jest to use the loader (instead of webpack).
It was working fine with webpack 4 but when trying to migrate to webpack 5, I have an issue with my unit tests failing with:

TypeError: Cannot read property 'dependencyFactories' of undefined

From what I see in the source code of the loader, there is specific code when running inside webpack 5 context that fails because in my case, I've installed webpack 5 but I'm not currently running it ( in the context of the unit tests), so this._compilation is not defined.

I used patch-package to patch @lingui/loader@3.6.0 for the project I'm working on.
Here is the diff that solved my problem:

diff --git a/node_modules/@lingui/loader/index.js b/node_modules/@lingui/loader/index.js
index 5d542b8..cfdf99e 100644
--- a/node_modules/@lingui/loader/index.js
+++ b/node_modules/@lingui/loader/index.js
@@ -39,7 +39,7 @@ var requiredType = "javascript/auto";
 function _default(source) {
   var options = _loaderUtils.default.getOptions(this) || {};
 
-  if (isWebpack5) {
+  if (isWebpack5 && this._compilation) {
     var LoaderDependency = require("webpack/lib/dependencies/LoaderDependency");
 
     var factory = this._compilation.dependencyFactories.get(LoaderDependency);

I don't know if it's the correct way to solve my problem as I found very little documentation about how to use the loader in the context of unit tests with jest, so please any feedback is welcome !

Thanks for your help

This issue body was partially generated by patch-package.

@semoal
Copy link
Contributor

semoal commented Mar 14, 2021

Released on 3.7.2. Thanks for reporting this issue mate.

@semoal semoal closed this as completed Mar 14, 2021
@samsaggace
Copy link
Author

Wow, that's awesome, thanks! 👍

@ggascoigne
Copy link
Contributor

@samsaggace I don't suppose that you can share your jest transform that uses @lingui/loader. I'm facing the same problem.

@samsaggace
Copy link
Author

So sorry, I don't have access to this codebase anymore (I changed company) and don't really remember what I did 😅
Sorry

@ggascoigne
Copy link
Contributor

Pity, thanks for the reply though. Take care.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants