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

feat(): Experimental Deps extractor #1469

Merged
merged 5 commits into from
Mar 15, 2023
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
2 changes: 1 addition & 1 deletion .github/workflows/main-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
scope: '@lingui'

- name: Install dependencies if needed
run: yarn install
andrii-bodnar marked this conversation as resolved.
Show resolved Hide resolved
run: yarn install --network-timeout 1000000

- name: Unit Testing
run: yarn test:ci
Expand Down
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
**/dist/*
**/test/fixtures/**
**/test/**/actual.js
**/test/**/actual/**
**/test/**/expected.js
**/locale/*
**/fixtures/*
**/expected/*
coverage/
website/
.github
Expand Down

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

2 changes: 2 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"commander": "^10.0.0",
"convert-source-map": "^2.0.0",
"date-fns": "^2.16.1",
"esbuild": "^0.17.10",
"glob": "^7.1.4",
"inquirer": "^7.3.3",
"micromatch": "4.0.2",
Expand All @@ -70,6 +71,7 @@
"normalize-path": "^3.0.0",
"ora": "^5.1.0",
"papaparse": "^5.3.0",
"pkg-up": "^3.1.0",
"plurals-cldr": "^1.0.4",
"pofile": "^1.1.4",
"pseudolocale": "^1.1.0",
Expand Down
17 changes: 4 additions & 13 deletions packages/cli/src/api/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,8 @@ export class Catalog {

const sortedCatalogs = cleanAndSort(catalogs)

if (options.locale) {
this.write(options.locale, sortedCatalogs[options.locale])
} else {
this.writeAll(sortedCatalogs)
}
const locales = options.locale ? [options.locale] : this.locales
locales.forEach((locale) => this.write(locale, sortedCatalogs[locale]))

return sortedCatalogs
}
Expand Down Expand Up @@ -188,10 +185,6 @@ export class Catalog {
return [created, filename]
}

writeAll(catalogs: AllCatalogsType): void {
this.locales.forEach((locale) => this.write(locale, catalogs[locale]))
}

writeTemplate(messages: CatalogType) {
const filename = this.templateFile
const options: CatalogFormatOptionsInternal = {
Expand Down Expand Up @@ -298,9 +291,7 @@ export function order<T extends ExtractedCatalogType>(
* Object keys are in the same order as they were created
* https://stackoverflow.com/a/31102605/1535540
*/
export function orderByMessageId<T extends ExtractedCatalogType>(
messages: T
): T {
function orderByMessageId<T extends ExtractedCatalogType>(messages: T): T {
return Object.keys(messages)
.sort()
.reduce((acc, key) => {
Expand All @@ -309,7 +300,7 @@ export function orderByMessageId<T extends ExtractedCatalogType>(
}, {} as T)
}

export function orderByOrigin<T extends ExtractedCatalogType>(messages: T): T {
function orderByOrigin<T extends ExtractedCatalogType>(messages: T): T {
function getFirstOrigin(messageKey: string) {
const sortedOrigins = messages[messageKey].origin.sort((a, b) => {
if (a[0] < b[0]) return -1
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/src/api/catalog/getCatalogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Catalog } from "../catalog"
import { normalizeRelativePath, PATHSEP, replacePlaceholders } from "../utils"
import micromatch from "micromatch"
import { getFormat } from "../formats"
import { getExperimentalCatalogs } from "../../extract-experimental/getExperimentalCatalogs"

const NAME_PH = "{name}"
const LOCALE_PH = "{locale}"
Expand Down Expand Up @@ -78,6 +79,10 @@ export function getCatalogs(config: LinguiConfigNormalized): Catalog[] {
})
})

if (config.experimental?.extractor?.entries.length) {
catalogs.push(...getExperimentalCatalogs(config))
}

return catalogs
}

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/api/catalog/mergeCatalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function mergeCatalog(
nextCatalog: ExtractedCatalogType,
forSourceLocale: boolean,
options: MergeOptions
) {
): CatalogType {
const nextKeys = Object.keys(nextCatalog)

const prevKeys = R.keys(prevCatalog).map(String)
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/src/api/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ describe("replacePlaceholders", () => {

expect(actual).toBe("/foo/bar/{a}/b-value/{a}")
})

it("should replace to empty string", () => {
const actual = replacePlaceholders("/foo/bar/{a}.file", {
a: "",
})

expect(actual).toBe("/foo/bar/.file")
})
})

describe("normalizeRelativePath", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function replacePlaceholders(
values: Record<string, string>
): string {
return input.replace(/\{([^}]+)}/g, (m, placeholder) => {
return values[placeholder] || m
return values[placeholder] ?? m
})
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { buildExternalizeFilter } from "./buildExternalizeFilter"

describe("buildExternalizeFilter", () => {
it("should exclude packages from package.json", () => {
const isExternal = buildExternalizeFilter({
includeDeps: [],
excludeDeps: [],
packageJson: {
dependencies: {
next: "*",
},
devDependencies: {},
optionalDependencies: {},
peerDependencies: {},
},
})

expect(isExternal("foo")).toBeFalsy()
expect(isExternal("next")).toBeTruthy()
expect(isExternal("next/head")).toBeTruthy()
expect(isExternal("@next/head")).toBeFalsy()
})

it("should respect all packageJson dependencies", () => {
const isExternal = buildExternalizeFilter({
includeDeps: [],
excludeDeps: [],
packageJson: {
dependencies: {
package1: "*",
},
devDependencies: {
package2: "*",
},
optionalDependencies: {
package3: "*",
},
peerDependencies: {
package4: "*",
},
},
})

expect(isExternal("non-deps")).toBeFalsy()
expect(isExternal("package1")).toBeTruthy()
expect(isExternal("package2")).toBeTruthy()
expect(isExternal("package3")).toBeTruthy()
expect(isExternal("package4")).toBeTruthy()
})

it("should not externalize packages from includeDeps", () => {
const isExternal = buildExternalizeFilter({
includeDeps: ["package1"],
excludeDeps: [],
packageJson: {
dependencies: {
package1: "*",
package2: "*",
},
},
})

expect(isExternal("package1")).toBeFalsy()
expect(isExternal("package2")).toBeTruthy()
})

it("should externalize deps from excludeDeps", () => {
const isExternal = buildExternalizeFilter({
includeDeps: ["package1"],
excludeDeps: ["package2"],
packageJson: {
dependencies: {
package1: "*",
package3: "*",
},
},
})

expect(isExternal("package1")).toBeFalsy()
expect(isExternal("package2")).toBeTruthy()
expect(isExternal("package3")).toBeTruthy()
expect(isExternal("non-deps")).toBeFalsy()
})
})
60 changes: 60 additions & 0 deletions packages/cli/src/extract-experimental/buildExternalizeFilter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
type PackageJson = {
dependencies?: Record<string, string>
devDependencies?: Record<string, string>
peerDependencies?: Record<string, string>
optionalDependencies?: Record<string, string>
}

function createPackageRegExp(packageName: string) {
return new RegExp("^" + packageName + "(?:\\/.+)?")
}

function packages(
packages: Record<string, string>,
includeDeps: string[]
): RegExp[] {
return Object.keys(packages || {})
.filter((packageName) => {
return !includeDeps.some((incl) => packageName.startsWith(incl))
})
.map(createPackageRegExp)
}

export function buildExternalizeFilter({
includeDeps,
excludeDeps,
packageJson,
}: {
includeDeps: string[]
excludeDeps: string[]
packageJson: PackageJson
}) {
const external = [
...packages(packageJson.dependencies, includeDeps),
...packages(packageJson.devDependencies, includeDeps),
...packages(packageJson.peerDependencies, includeDeps),
...packages(packageJson.optionalDependencies, includeDeps),
...excludeDeps.map(createPackageRegExp),
]

return (id: string) =>
external.some((regExp) => {
return regExp.test(id)
})
}

export async function getPackageJson(rootDir: string): Promise<PackageJson> {
const { default: pkgUp } = await import("pkg-up")
const packageJsonPath = await pkgUp({
cwd: rootDir,
})

if (!packageJsonPath) {
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)
}
96 changes: 96 additions & 0 deletions packages/cli/src/extract-experimental/bundleSource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { ExperimentalExtractorOptions } from "@lingui/conf"
import { BuildOptions } from "esbuild"
import {
buildExternalizeFilter,
getPackageJson,
} from "./buildExternalizeFilter"

function createExtRegExp(extensions: string[]) {
return new RegExp("\\.(?:" + extensions.join("|") + ")(?:\\?.*)?$")
}

export async function bundleSource(
config: ExperimentalExtractorOptions,
entryPoints: string[],
outDir: string,
rootDir: string
) {
const esbuild = await import("esbuild")

const excludeExtensions = config.excludeExtensions || [
"ico",
"pot",
"xliff",
"woff2",
"woff",
"eot",
"gif",
"otf",
"ttf",
"mp4",
"svg",
"png",
"css",
"sass",
"less",
"jpg",
]

const packageJson = await getPackageJson(rootDir)

const esbuildOptions: BuildOptions = {
entryPoints: entryPoints,
outExtension: { ".js": ".jsx" },
jsx: "preserve",
bundle: true,
platform: "node",
target: ["esnext"],
format: "esm",
splitting: false,
treeShaking: true,
outdir: outDir,
sourcemap: "inline",
sourceRoot: outDir,
sourcesContent: false,
outbase: rootDir,
metafile: true,
plugins: [
{
name: "externalize-deps",
setup(build) {
const isExternal = buildExternalizeFilter({
includeDeps: config.includeDeps || [],
excludeDeps: config.excludeDeps || [],
packageJson,
})

// externalize bare imports
build.onResolve({ filter: /^[^.].*/ }, async ({ path: id }) => {
if (isExternal(id)) {
return {
external: true,
}
}
})
},
},
{
name: "externalize-files",
setup(build) {
build.onResolve(
{ filter: createExtRegExp(excludeExtensions) },
() => ({
external: true,
})
)
},
},
],
}

return await esbuild.build(
config.resolveEsbuildOptions
? config.resolveEsbuildOptions(esbuildOptions)
: esbuildOptions
)
}
2 changes: 2 additions & 0 deletions packages/cli/src/extract-experimental/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const ENTRY_NAME_PH = "{entryName}"
export const DEFAULT_TEMPLATE_NAME = "messages"
7 changes: 7 additions & 0 deletions packages/cli/src/extract-experimental/getEntryPoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import glob from "glob"

export function getEntryPoints(entries: string[]) {
const patterns = entries.length > 1 ? `{${entries.join(",")}}` : entries[0]

return glob.sync(patterns, { mark: true })
}
Loading