Repository: lingui/js-lingui
Pull request #740 (2020-09-15 11:38:52)

Make the extractor support Babel's default file extensions. Babel sup…
…ports the

following file extensions by default: .js, .jsx, .es6, .es and .mjs.
In the current version, Lingui only passes files to Babel with the .js and .jsx extension, limiting the set of file extensions Babel would be able to process otherwise. Allowing Babel to process .mjs files is really handy, when someone would like to use it with native ES modules in recent version of Node.

This PR attempts to solve this issue: #739 

vercel[bot]: This pull request is being automatically deployed with Vercel ([learn more](https://vercel.link/github-learn-more)).  
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: [https://vercel.com/lingui-js/js-lingui/hzmyc54fc](https://vercel.com/lingui-js/js-lingui/hzmyc54fc)  
✅ Preview: *In Progress*


codecov[bot]: # [Codecov](https://codecov.io/gh/lingui/js-lingui/pull/740?src=pr&el=h1) Report
> Merging [#740](https://codecov.io/gh/lingui/js-lingui/pull/740?src=pr&el=desc) into [next](https://codecov.io/gh/lingui/js-lingui/commit/8adf7c162129b8771074e8c677d32f94755c3dba?el=desc) will **increase** coverage by `0.09%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/lingui/js-lingui/pull/740/graphs/tree.svg?width=650&height=150&src=pr&token=CuJ1VD5BY5)](https://codecov.io/gh/lingui/js-lingui/pull/740?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##             next     #740      +/-   ##
==========================================
+ Coverage   83.85%   83.95%   +0.09%     
==========================================
  Files          38       38              
  Lines        1214     1215       +1     
  Branches      319      319              
==========================================
+ Hits         1018     1020       +2     
+ Misses        120      119       -1     
  Partials       76       76              
```


| [Impacted Files](https://codecov.io/gh/lingui/js-lingui/pull/740?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [packages/cli/src/api/extractors/babel.ts](https://codecov.io/gh/lingui/js-lingui/pull/740/diff?src=pr&el=tree#diff-cGFja2FnZXMvY2xpL3NyYy9hcGkvZXh0cmFjdG9ycy9iYWJlbC50cw==) | `54.54% <100.00%> (+4.54%)` | :arrow_up: |
| [packages/cli/src/api/extractors/typescript.ts](https://codecov.io/gh/lingui/js-lingui/pull/740/diff?src=pr&el=tree#diff-cGFja2FnZXMvY2xpL3NyYy9hcGkvZXh0cmFjdG9ycy90eXBlc2NyaXB0LnRz) | `16.66% <0.00%> (+5.55%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/lingui/js-lingui/pull/740?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/lingui/js-lingui/pull/740?src=pr&el=footer). Last update [8adf7c1...b814f29](https://codecov.io/gh/lingui/js-lingui/pull/740?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/packages/cli/src/api/extractors/babel.ts b/packages/cli/src/api/extractors/babel.ts
index 4ede39c2..1aef493c 100644
--- a/packages/cli/src/api/extractors/babel.ts
+++ b/packages/cli/src/api/extractors/babel.ts
@@ -1,11 +1,14 @@
-import { transformFileSync } from "@babel/core"
+import { transformFileSync, DEFAULT_EXTENSIONS } from "@babel/core"
 
 import linguiExtractMessages from "@lingui/babel-plugin-extract-messages"
 
 import { BabelOptions, ExtractorType } from "./types"
 import { projectType } from "../detect"
 
-const babelRe = /\.jsx?$/i
+const babelRe = new RegExp(
+	"\\.(" + DEFAULT_EXTENSIONS.map(ext => ext.slice(1)).join("|") + ")$",
+	"i"
+)
 
 const extractor: ExtractorType = {
   match(filename) {
diff --git a/packages/cli/src/lingui-compile.ts b/packages/cli/src/lingui-compile.ts
index 6d8a66d3..85bff1b4 100644
--- a/packages/cli/src/lingui-compile.ts
+++ b/packages/cli/src/lingui-compile.ts
@@ -31,6 +31,9 @@ function command(config, options) {
 
   console.error("Compiling message catalogs…")
 
+  if(config.pseudoLocale) {
+    config.locales.push(config.pseudoLocale)
+  }
   config.locales.forEach((locale) => {
     const [language] = locale.split(/[_-]/)
     if (locale !== config.pseudoLocale && !plurals[language]) {
@@ -44,10 +47,13 @@ function command(config, options) {
     }
 
     catalogs.forEach((catalog) => {
-      const messages = catalog.getTranslations(locale, {
-        fallbackLocale: config.fallbackLocale,
-        sourceLocale: config.sourceLocale,
-      })
+      const messages = catalog.getTranslations(
+        locale === config.pseudoLocale ? config.sourceLocale: locale,
+        {
+          fallbackLocale: config.fallbackLocale,
+          sourceLocale: config.sourceLocale,
+        }
+      )
 
       if (!options.allowEmpty) {
         const missing = R.values(messages)


Test patch:
diff --git a/packages/cli/src/api/extract.test.ts b/packages/cli/src/api/extract.test.ts
index ab131cf4..6f5ed850 100644
--- a/packages/cli/src/api/extract.test.ts
+++ b/packages/cli/src/api/extract.test.ts
@@ -10,13 +10,18 @@ describe("extract", function () {
   let extract, babel, typescript
 
   beforeAll(() => {
+
+    // References to the real match functions
+    const babelMatch = require("./extractors/babel").default.match
+    const tsMatch = require("./extractors/typescript").default.match
+
     jest.doMock("./extractors/babel", () => ({
-      match: jest.fn((filename) => filename.endsWith(".js")),
+      match: jest.fn(babelMatch),
       extract: jest.fn(),
     }))
 
     jest.doMock("./extractors/typescript", () => ({
-      match: jest.fn((filename) => filename.endsWith(".ts")),
+      match: jest.fn(tsMatch),
       extract: jest.fn(),
     }))
 
@@ -31,6 +36,10 @@ describe("extract", function () {
 
         components: {
           "Babel.js": "",
+          "Babel.jsx": "",
+          "Babel.es6": "",
+          "Babel.es": "",
+          "Babel.mjs": "",
           "Typescript.ts": "",
           forbidden: {
             "apple.js": "",
@@ -60,33 +69,80 @@ describe("extract", function () {
     expect(babel.match).toHaveBeenCalledWith(
       path.join("src", "components", "Babel.js")
     )
-    expect(babel.match).toHaveBeenCalledWith(path.join("src", "index.html"))
+    expect(babel.match).toHaveBeenCalledWith(
+      path.join("src", "components", "Babel.jsx")
+    )
+    expect(babel.match).toHaveBeenCalledWith(
+      path.join("src", "components", "Babel.es6")
+    )
+    expect(babel.match).toHaveBeenCalledWith(
+      path.join("src", "components", "Babel.es")
+    )
+    expect(babel.match).toHaveBeenCalledWith(
+      path.join("src", "components", "Babel.mjs")
+    )
+		
+    expect(babel.match).toHaveBeenCalledWith(
+			path.join("src", "index.html")
+    )
 
     // This file is ignored
     expect(babel.extract).not.toHaveBeenCalledWith(
       path.join("src", "index.html")
     )
 
+    const extractArgs = [
+      "locale", 
+      { babelOptions: {}, ignore: ["forbidden"]}
+    ]
     expect(babel.extract).toHaveBeenCalledWith(
       path.join("src", "components", "Babel.js"),
-      "locale",
-      { babelOptions: {}, ignore: ["forbidden"] }
+      ...extractArgs
+    )
+    expect(babel.extract).toHaveBeenCalledWith(
+      path.join("src", "components", "Babel.jsx"),
+      ...extractArgs
+    )
+    expect(babel.extract).toHaveBeenCalledWith(
+      path.join("src", "components", "Babel.es6"),
+      ...extractArgs
+    )
+    expect(babel.extract).toHaveBeenCalledWith(
+      path.join("src", "components", "Babel.es"),
+      ...extractArgs
+    )
+    expect(babel.extract).toHaveBeenCalledWith(
+      path.join("src", "components", "Babel.mjs"),
+      ...extractArgs
     )
     expect(babel.extract).not.toHaveBeenCalledWith(
       path.join("src", "components", "Typescript.ts"),
-      "locale",
-      { babelOptions: {}, ignore: ["forbidden"] }
+      ...extractArgs
     )
 
     expect(typescript.extract).not.toHaveBeenCalledWith(
       path.join("src", "components", "Babel.js"),
-      "locale",
-      { babelOptions: {}, ignore: ["forbidden"] }
+      ...extractArgs
+    )
+    expect(typescript.extract).not.toHaveBeenCalledWith(
+      path.join("src", "components", "Babel.jsx"),
+      ...extractArgs
+    )
+    expect(typescript.extract).not.toHaveBeenCalledWith(
+      path.join("src", "components", "Babel.es6"),
+      ...extractArgs
+    )
+    expect(typescript.extract).not.toHaveBeenCalledWith(
+      path.join("src", "components", "Babel.es"),
+      ...extractArgs
+    )
+    expect(typescript.extract).not.toHaveBeenCalledWith(
+      path.join("src", "components", "Babel.mjs"),
+      ...extractArgs
     )
     expect(typescript.extract).toHaveBeenCalledWith(
       path.join("src", "components", "Typescript.ts"),
-      "locale",
-      { babelOptions: {}, ignore: ["forbidden"] }
+      ...extractArgs
     )
   })
 })