Skip to content

Commit

Permalink
fix(experimental-extractor): fix various bugs (#1958)
Browse files Browse the repository at this point in the history
- update esbuild to 0.21.5
- apply lingui macro on each file as esbuild plugin
- remove buildExternalizeFilter in favour of `{packages: external}`
  • Loading branch information
timofei-iatsenko authored Jun 18, 2024
1 parent 46ac32f commit 8b35acf
Show file tree
Hide file tree
Showing 22 changed files with 428 additions and 465 deletions.
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"commander": "^10.0.0",
"convert-source-map": "^2.0.0",
"date-fns": "^2.16.1",
"esbuild": "^0.17.10",
"esbuild": "^0.21.5",
"glob": "^7.1.4",
"inquirer": "^7.3.3",
"micromatch": "4.0.2",
Expand Down
91 changes: 53 additions & 38 deletions packages/cli/src/api/extractors/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ import type {
} from "@lingui/babel-plugin-extract-messages"
import linguiExtractMessages from "@lingui/babel-plugin-extract-messages"

import type { ExtractorType } from "@lingui/conf"
import {
ExtractorType,
LinguiConfig,
ExtractedMessage,
ExtractorCtx,
} from "@lingui/conf"
import { ParserPlugin } from "@babel/parser"

import { type LinguiPluginOpts } from "@lingui/babel-plugin-lingui-macro"
import linguiMacroPlugin from "@lingui/babel-plugin-lingui-macro"
import { ExtractedMessage, ExtractorCtx } from "@lingui/conf"
import linguiMacroPlugin, {
type LinguiPluginOpts,
} from "@lingui/babel-plugin-lingui-macro"

const babelRe = new RegExp(
export const babelRe = new RegExp(
"\\.(" +
[...DEFAULT_EXTENSIONS, ".ts", ".mts", ".cts", ".tsx"]
.map((ext) => ext.slice(1))
Expand Down Expand Up @@ -103,7 +108,8 @@ export async function extractFromFileWithBabel(
code: string,
onMessageExtracted: (msg: ExtractedMessage) => void,
ctx: ExtractorCtx,
parserOpts: ParserOptions
parserOpts: ParserOptions,
skipMacroPlugin = false
) {
const mapper = await createSourceMapper(code, ctx?.sourceMaps)

Expand All @@ -120,13 +126,17 @@ export async function extractFromFileWithBabel(
parserOpts,

plugins: [
[
linguiMacroPlugin,
{
extract: true,
linguiConfig: ctx.linguiConfig,
} satisfies LinguiPluginOpts,
],
...(!skipMacroPlugin
? [
[
linguiMacroPlugin,
{
extract: true,
linguiConfig: ctx.linguiConfig,
} satisfies LinguiPluginOpts,
],
]
: []),
[
linguiExtractMessages,
{
Expand All @@ -144,6 +154,35 @@ export async function extractFromFileWithBabel(
mapper.destroy()
}

export function getBabelParserOptions(
filename: string,
parserOptions: LinguiConfig["extractorParserOptions"]
) {
// https://babeljs.io/docs/en/babel-parser#latest-ecmascript-features
const parserPlugins: ParserPlugin[] = []

if ([/\.ts$/, /\.mts$/, /\.cts$/, /\.tsx$/].some((r) => filename.match(r))) {
parserPlugins.push("typescript")
if (parserOptions.tsExperimentalDecorators) {
parserPlugins.push("decorators-legacy")
} else {
parserPlugins.push("decorators")
}
} else {
parserPlugins.push("decorators")

if (parserOptions?.flow) {
parserPlugins.push("flow")
}
}

if ([/\.js$/, /\.jsx$/, /\.tsx$/].some((r) => filename.match(r))) {
parserPlugins.push("jsx")
}

return parserPlugins
}

const extractor: ExtractorType = {
match(filename) {
return babelRe.test(filename)
Expand All @@ -152,32 +191,8 @@ const extractor: ExtractorType = {
async extract(filename, code, onMessageExtracted, ctx) {
const parserOptions = ctx.linguiConfig.extractorParserOptions

// https://babeljs.io/docs/en/babel-parser#latest-ecmascript-features
const parserPlugins: ParserPlugin[] = []

if (
[/\.ts$/, /\.mts$/, /\.cts$/, /\.tsx$/].some((r) => filename.match(r))
) {
parserPlugins.push("typescript")
if (parserOptions.tsExperimentalDecorators) {
parserPlugins.push("decorators-legacy")
} else {
parserPlugins.push("decorators")
}
} else {
parserPlugins.push("decorators")

if (parserOptions?.flow) {
parserPlugins.push("flow")
}
}

if ([/\.js$/, /\.jsx$/, /\.tsx$/].some((r) => filename.match(r))) {
parserPlugins.push("jsx")
}

return extractFromFileWithBabel(filename, code, onMessageExtracted, ctx, {
plugins: parserPlugins,
plugins: getBabelParserOptions(filename, parserOptions),
})
},
}
Expand Down

This file was deleted.

70 changes: 0 additions & 70 deletions packages/cli/src/extract-experimental/buildExternalizeFilter.ts

This file was deleted.

34 changes: 7 additions & 27 deletions packages/cli/src/extract-experimental/bundleSource.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import { ExperimentalExtractorOptions } from "@lingui/conf"
import { LinguiConfigNormalized } from "@lingui/conf"
import { BuildOptions } from "esbuild"
import {
buildExternalizeFilter,
getPackageJson,
} from "./buildExternalizeFilter"
import { pluginLinguiMacro } from "./linguiEsbuildPlugin"

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

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

const config = linguiConfig.experimental.extractor
const excludeExtensions = config.excludeExtensions || [
"ico",
"pot",
Expand All @@ -36,8 +34,6 @@ export async function bundleSource(
"jpg",
]

const packageJson = await getPackageJson(rootDir)

const esbuildOptions: BuildOptions = {
entryPoints: entryPoints,
outExtension: { ".js": ".jsx" },
Expand All @@ -52,28 +48,12 @@ export async function bundleSource(
sourcemap: "inline",
sourceRoot: outDir,
sourcesContent: false,
packages: "external",
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,
}
}
})
},
},
plugins: [
pluginLinguiMacro({ linguiConfig }),
{
name: "externalize-files",
setup(build) {
Expand Down
Loading

0 comments on commit 8b35acf

Please sign in to comment.