Skip to content

@lingui/loader with webpack 5 and jest #999

Closed
@samsaggace

Description

@samsaggace

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions