Skip to content

Commit a4883fc

Browse files
author
Timofei Iatsenko
authored
feat(experimental-extractor): reconsider includeDeps (#2362)
1 parent c1f2800 commit a4883fc

13 files changed

Lines changed: 104 additions & 52 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { buildIncludeDepsFilter } from "./buildIncludeDepsFilter"
2+
3+
describe("buildExternalizeFilter", () => {
4+
it("should not externalize packages from includeDeps", () => {
5+
const isIncluded = buildIncludeDepsFilter(["package1", "package3/subpath"])
6+
7+
expect(isIncluded("package1")).toBeTruthy()
8+
expect(isIncluded("package1/subpath")).toBeTruthy()
9+
expect(isIncluded("package2")).toBeFalsy()
10+
expect(isIncluded("package3/subpath")).toBeTruthy()
11+
expect(isIncluded("package3/subpath/subpath")).toBeTruthy()
12+
})
13+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function createPackageRegExp(packageName: string) {
2+
return new RegExp("^" + packageName + "(?:\\/.+)?")
3+
}
4+
5+
export function buildIncludeDepsFilter(includeDeps: string[]) {
6+
const include = includeDeps.map(createPackageRegExp)
7+
8+
return (id: string) => include.some((regExp) => regExp.test(id))
9+
}

packages/cli/src/extract-experimental/bundleSource.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { LinguiConfigNormalized } from "@lingui/conf"
22
import { BuildOptions } from "esbuild"
33
import { pluginLinguiMacro } from "./linguiEsbuildPlugin"
4+
import { buildIncludeDepsFilter } from "./buildIncludeDepsFilter"
45

56
function createExtRegExp(extensions: string[]) {
67
return new RegExp("\\.(?:" + extensions.join("|") + ")(?:\\?.*)?$")
@@ -49,11 +50,29 @@ export async function bundleSource(
4950
sourcemap: "inline",
5051
sourceRoot: outDir,
5152
sourcesContent: false,
52-
packages: "external",
5353
metafile: true,
5454

5555
plugins: [
5656
pluginLinguiMacro({ linguiConfig }),
57+
{
58+
name: "externalize-deps",
59+
setup(build) {
60+
const shouldInclude = buildIncludeDepsFilter(config.includeDeps || [])
61+
62+
// considers all import paths that "look like" package imports in the original source code to be package imports.
63+
// Specifically import paths that don't start with a path segment of / or . or .. are considered to be package imports.
64+
// The only two exceptions to this rule are subpath imports (which start with a # character) and deps specified in the `includeDeps`
65+
build.onResolve({ filter: /^[^.#/].*/ }, async (args) => {
66+
if (shouldInclude(args.path)) {
67+
return { external: false }
68+
}
69+
70+
return {
71+
external: true,
72+
}
73+
})
74+
},
75+
},
5776
{
5877
name: "externalize-files",
5978
setup(build) {

packages/cli/test/extractor-experimental-template/expected/about.page.en.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cli/test/extractor-experimental-template/expected/about.page.messages.pot

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ msgstr ""
66
"Content-Transfer-Encoding: 8bit\n"
77
"X-Generator: @lingui/cli\n"
88

9-
#: fixtures/pages/about.page.tsx:16
9+
#: fixtures/pages/about.page.tsx:19
1010
msgid "about page message"
1111
msgstr ""
1212

@@ -18,6 +18,10 @@ msgstr ""
1818
msgid "header message"
1919
msgstr ""
2020

21-
#: fixtures/pages/about.page.tsx:19
21+
#: fixtures/pages/about.page.tsx:22
2222
msgid "JSX: about page message"
2323
msgstr ""
24+
25+
#: fixtures/components/subpath-import.ts:3
26+
msgid "subpath import module message"
27+
msgstr ""

packages/cli/test/extractor-experimental-template/expected/about.page.pl.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { t } from "@lingui/core/macro"
2+
3+
export const msg = t`subpath import module message`

packages/cli/test/extractor-experimental-template/fixtures/pages/about.page.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ import styles2 from "./styles.css?inline"
1313
// should respect tsconfig path aliases
1414
import { msg as msg2 } from "@alias"
1515

16+
// should respect package.json subpath imports
17+
import { msg as msg3 } from "#subpath-dep"
18+
1619
const msg = t`about page message`
1720

1821
export default function Page() {
1922
return <Trans>JSX: about page message</Trans>
2023
}
2124

22-
console.log(msg, headerMsg, bla, styles, styles2, msg2)
25+
console.log(msg, headerMsg, bla, styles, styles2, msg2, msg3)

packages/cli/test/extractor-experimental-template/lingui.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default defineConfig({
99
extractor: {
1010
entries: ["<rootDir>/fixtures/pages/**/*.page.{ts,tsx}"],
1111
output: "<rootDir>/actual/{entryName}.{locale}",
12+
includeDeps: ["@alias"],
1213
},
1314
},
1415
})

packages/cli/test/extractor-experimental-template/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"name": "extractor-experimental-template",
33
"version": "1.0.0",
44
"private": true,
5+
"imports": {
6+
"#subpath-dep": "./fixtures/components/subpath-import.ts"
7+
},
58
"dependencies": {
69
"@lingui/core": "*",
710
"@lingui/react": "*",

0 commit comments

Comments
 (0)