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

Function getCatalogForFile not work with Next.js routing with catch-all segments #1792

Closed
1 of 3 tasks
yunsii opened this issue Oct 23, 2023 · 9 comments · Fixed by #1890
Closed
1 of 3 tasks

Function getCatalogForFile not work with Next.js routing with catch-all segments #1792

yunsii opened this issue Oct 23, 2023 · 9 comments · Fixed by #1890

Comments

@yunsii
Copy link
Contributor

yunsii commented Oct 23, 2023

Describe the bug

image

To Reproduce

https://runkit.com/embed/f22cw4gsf94y

Expected behavior

Work with Next.js routing with catch-all segments

  • jsLingui version 4.5.0
  • Macro support:
  • I'm using SWC with @lingui/swc-plugin
  • I'm using Babel with babel-macro-plugin
  • I'm not using macro
@thekip
Copy link
Collaborator

thekip commented Oct 23, 2023

Could you elaborate on this a bit? because i dind't get the problem from you reproduction link

@yunsii
Copy link
Contributor Author

yunsii commented Oct 23, 2023

@thekip The key issue is next page route with catch-all segments, like /test/[...param].page.ts (I use page.ts as page extension), it can not works as expected like my reproduction. First test case should print ar too, but got undefined.

I will provide a Next.js demo laterly.

@thekip
Copy link
Collaborator

thekip commented Oct 23, 2023

But how it's related to lingui? It seems a nextjs issue, not lingui.

@yunsii
Copy link
Contributor Author

yunsii commented Oct 23, 2023

But how it's related to lingui? It seems a nextjs issue, not lingui.

So I think getCatalogForFile with micromatch should cover Next.js catch-all segments routes. Now I patched @lingui/cli with:

diff --git a/dist/api/catalog/getCatalogs.js b/dist/api/catalog/getCatalogs.js
index ecfc08388fa90c7efc4e826a0683488ea124734d..fbcbbe726220119687552d0f148f9da6fca48625 100644
--- a/dist/api/catalog/getCatalogs.js
+++ b/dist/api/catalog/getCatalogs.js
@@ -93,7 +93,7 @@ function getCatalogForFile(file, catalogs) {
     for (const catalog of catalogs) {
         const catalogFile = `${catalog.path}${catalog.format.getCatalogExtension()}`;
         const catalogGlob = (0, utils_1.replacePlaceholders)(catalogFile, { locale: "*" });
-        const match = micromatch_1.default.capture((0, utils_1.normalizeRelativePath)(path_1.default.relative(catalog.config.rootDir, catalogGlob)), (0, utils_1.normalizeRelativePath)(file));
+        const match = micromatch_1.default.capture(((0, utils_1.normalizeRelativePath)(path_1.default.relative(catalog.config.rootDir, catalogGlob))).replace(/\[/g, '\\[').replace(/\]/g, '\\]'), (0, utils_1.normalizeRelativePath)(file));
         if (match) {
             return {
                 locale: match[0],
diff --git a/dist/extract-experimental/buildExternalizeFilter.js b/dist/extract-experimental/buildExternalizeFilter.js
index f836f0f9840a4ec6f382695bb42706fb9ef4292a..ffabcfc8f60e128dd3a855e930d9cb6b86c507c1 100644
--- a/dist/extract-experimental/buildExternalizeFilter.js
+++ b/dist/extract-experimental/buildExternalizeFilter.js
@@ -1,6 +1,9 @@
 "use strict";
 Object.defineProperty(exports, "__esModule", { value: true });
 exports.getPackageJson = exports.buildExternalizeFilter = void 0;
+
+const fs = require('node:fs')
+
 function createPackageRegExp(packageName) {
     return new RegExp("^" + packageName + "(?:\\/.+)?");
 }
@@ -33,6 +36,15 @@ async function getPackageJson(rootDir) {
         throw new Error("We could not able to find your package.json file. " +
             "Check that `rootDir` is pointing to the folder with package.json");
     }
-    return await import(packageJsonPath);
+
+    try {
+        return JSON.parse(await fs.promises.readFile(packageJsonPath, "utf-8"))
+    } catch (e) {
+        throw new Error(
+            `Unable to read package.json file at path ${packageJsonPath}. \n\n Error: ${
+                e.message
+            }`
+        )
+    }
 }
 exports.getPackageJson = getPackageJson;

@thekip
Copy link
Collaborator

thekip commented Oct 23, 2023

Okay, now i've got your point. So it's somehow related to compiling/extracting with CLI and path with "[]" symbols processed incorrectly.

If you provide a nexts repro repo it would be much easier to understand the case.

@yunsii
Copy link
Contributor Author

yunsii commented Oct 23, 2023

After investigation, with Runkit demo I find out the key issue, so I use the demo directly, it's my fault.

@thekip thekip closed this as completed Oct 24, 2023
@yunsii
Copy link
Contributor Author

yunsii commented Mar 21, 2024

@thekip v4.8.0-next.1 still not fixed the issue...

@thekip
Copy link
Collaborator

thekip commented Mar 21, 2024

You can fix it yourself and share with community via PR

@yunsii
Copy link
Contributor Author

yunsii commented Mar 21, 2024

I had thought you closed the issue and fix it later, okey, I will create a PR right now.

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