Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { buildIncludeDepsFilter } from "./buildIncludeDepsFilter"

describe("buildExternalizeFilter", () => {
it("should not externalize packages from includeDeps", () => {
const isIncluded = buildIncludeDepsFilter(["package1", "package3/subpath"])

expect(isIncluded("package1")).toBeTruthy()
expect(isIncluded("package1/subpath")).toBeTruthy()
expect(isIncluded("package2")).toBeFalsy()
expect(isIncluded("package3/subpath")).toBeTruthy()
expect(isIncluded("package3/subpath/subpath")).toBeTruthy()
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function createPackageRegExp(packageName: string) {
return new RegExp("^" + packageName + "(?:\\/.+)?")
}

export function buildIncludeDepsFilter(includeDeps: string[]) {
const include = includeDeps.map(createPackageRegExp)

return (id: string) => include.some((regExp) => regExp.test(id))
}
21 changes: 20 additions & 1 deletion packages/cli/src/extract-experimental/bundleSource.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LinguiConfigNormalized } from "@lingui/conf"
import { BuildOptions } from "esbuild"
import { pluginLinguiMacro } from "./linguiEsbuildPlugin"
import { buildIncludeDepsFilter } from "./buildIncludeDepsFilter"

function createExtRegExp(extensions: string[]) {
return new RegExp("\\.(?:" + extensions.join("|") + ")(?:\\?.*)?$")
Expand Down Expand Up @@ -49,11 +50,29 @@ export async function bundleSource(
sourcemap: "inline",
sourceRoot: outDir,
sourcesContent: false,
packages: "external",
metafile: true,

plugins: [
pluginLinguiMacro({ linguiConfig }),
{
name: "externalize-deps",
setup(build) {
const shouldInclude = buildIncludeDepsFilter(config.includeDeps || [])

// considers all import paths that "look like" package imports in the original source code to be package imports.
// Specifically import paths that don't start with a path segment of / or . or .. are considered to be package imports.
// The only two exceptions to this rule are subpath imports (which start with a # character) and deps specified in the `includeDeps`
build.onResolve({ filter: /^[^.#/].*/ }, async (args) => {
if (shouldInclude(args.path)) {
return { external: false }
}

return {
external: true,
}
})
},
},
{
name: "externalize-files",
setup(build) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: @lingui/cli\n"

#: fixtures/pages/about.page.tsx:16
#: fixtures/pages/about.page.tsx:19
msgid "about page message"
msgstr ""

Expand All @@ -18,6 +18,10 @@ msgstr ""
msgid "header message"
msgstr ""

#: fixtures/pages/about.page.tsx:19
#: fixtures/pages/about.page.tsx:22
msgid "JSX: about page message"
msgstr ""

#: fixtures/components/subpath-import.ts:3
msgid "subpath import module message"
msgstr ""

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { t } from "@lingui/core/macro"

export const msg = t`subpath import module message`
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import styles2 from "./styles.css?inline"
// should respect tsconfig path aliases
import { msg as msg2 } from "@alias"

// should respect package.json subpath imports
import { msg as msg3 } from "#subpath-dep"

const msg = t`about page message`

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

console.log(msg, headerMsg, bla, styles, styles2, msg2)
console.log(msg, headerMsg, bla, styles, styles2, msg2, msg3)
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default defineConfig({
extractor: {
entries: ["<rootDir>/fixtures/pages/**/*.page.{ts,tsx}"],
output: "<rootDir>/actual/{entryName}.{locale}",
includeDeps: ["@alias"],
},
},
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"name": "extractor-experimental-template",
"version": "1.0.0",
"private": true,
"imports": {
"#subpath-dep": "./fixtures/components/subpath-import.ts"
},
"dependencies": {
"@lingui/core": "*",
"@lingui/react": "*",
Expand Down
13 changes: 5 additions & 8 deletions packages/cli/test/extractor-experimental-template/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"module": "NodeNext",
"target": "ESNext",
"jsx": "preserve",
"baseUrl": "./",
"paths": {
"@alias": [
"./fixtures/components/aliased-module.ts"
]
"@alias": ["./fixtures/components/aliased-module.ts"]
}
},
"exclude": [
"node_modules"
]
"exclude": ["node_modules"]
}
48 changes: 24 additions & 24 deletions packages/cli/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,18 @@ describe("E2E Extractor Test", () => {
expect(replaceDuration(getConsoleMockCalls(console.log)))
.toMatchInlineSnapshot(`

Done in <n>ms
Catalog statistics for actual/{locale}:
┌─────────────┬─────────────┬─────────┐
│ Language │ Total count │ Missing │
├─────────────┼─────────────┼─────────┤
│ en (source) │ 10 │ - │
│ pl │ 10 │ 10 │
└─────────────┴─────────────┴─────────┘
Done in <n>ms
Catalog statistics for actual/{locale}:
┌─────────────┬─────────────┬─────────┐
│ Language │ Total count │ Missing │
├─────────────┼─────────────┼─────────┤
│ en (source) │ 10 │ - │
│ pl │ 10 │ 10 │
└─────────────┴─────────────┴─────────┘

(Use "yarn extract" to update catalogs with new messages.)
(Use "yarn compile" to compile catalogs for production. Alternatively, use bundler plugins: https://lingui.dev/ref/cli#compiling-catalogs-in-ci)
`)
(Use "yarn extract" to update catalogs with new messages.)
(Use "yarn compile" to compile catalogs for production. Alternatively, use bundler plugins: https://lingui.dev/ref/cli#compiling-catalogs-in-ci)
`)
})

compareFolders(actualPath, expectedPath)
Expand Down Expand Up @@ -222,7 +222,7 @@ describe("E2E Extractor Test", () => {
Experimental features are not covered by semver, and may cause unexpected or broken application behavior. Use at your own risk.

Catalog statistics for fixtures/pages/about.page.tsx:
4 message(s) extracted
5 message(s) extracted

Catalog statistics for fixtures/pages/index.page.ts:
1 message(s) extracted
Expand Down Expand Up @@ -455,18 +455,18 @@ describe("E2E Extractor Test", () => {
expect(replaceDuration(getConsoleMockCalls(console.log)))
.toMatchInlineSnapshot(`

Done in <n>ms
Catalog statistics for actual/{locale}:
┌─────────────┬─────────────┬─────────┐
│ Language │ Total count │ Missing │
├─────────────┼─────────────┼─────────┤
│ en (source) │ 10 │ - │
│ pl │ 10 │ 10 │
└─────────────┴─────────────┴─────────┘

(Use "yarn extract" to update catalogs with new messages.)
(Use "yarn compile" to compile catalogs for production. Alternatively, use bundler plugins: https://lingui.dev/ref/cli#compiling-catalogs-in-ci)
`)
Done in <n>ms
Catalog statistics for actual/{locale}:
┌─────────────┬─────────────┬─────────┐
│ Language │ Total count │ Missing │
├─────────────┼─────────────┼─────────┤
│ en (source) │ 10 │ - │
│ pl │ 10 │ 10 │
└─────────────┴─────────────┴─────────┘

(Use "yarn extract" to update catalogs with new messages.)
(Use "yarn compile" to compile catalogs for production. Alternatively, use bundler plugins: https://lingui.dev/ref/cli#compiling-catalogs-in-ci)
`)
})
})
})
28 changes: 14 additions & 14 deletions packages/conf/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,25 +138,25 @@ export type ExperimentalExtractorOptions = {
entries: string[]

/**
* Explicitly include some dependency for extraction.
* For example, you can include all monorepo's packages as
* ["@mycompany/"]
*/
includeDeps?: string[]

/**
* By default all dependencies from package.json would be ecxluded from analyzing.
* If something was not properly discovered you can add it here.
* List of package name patterns to include for extraction.
*
* Note: it automatically matches also sub imports
* For example, to include all packages from your monorepo:
*
* "next" would match "next" and "next/head"
* ["@mycompany"]
*
* By default, all imports that look like package imports are ignored.
* This means imports that do not start with `/`, `./`, `../`, or `#`
* (used for subpath imports). TypeScript path aliases are also ignored
* because they look like package imports.
*
* Add here the packages you want to include.
*/
excludeDeps?: string[]
includeDeps?: string[]

/**
* svg, jpg and other files which might be imported in application should be exluded from analysis.
* By default extractor provides a comprehensive list of extensions. If you feel like somthing is missing in this list please fill an issue on GitHub
* svg, jpg and other files which might be imported in application should be excluded from analysis.
* By default, extractor provides a comprehensive list of extensions. If you feel like something
* is missing in this list please fill an issue on GitHub
*
* NOTE: changing this param will override default list of extensions.
*/
Expand Down