Skip to content

Commit 5d37262

Browse files
committed
chore: centralize use i18n directive check
1 parent 1de04ad commit 5d37262

5 files changed

Lines changed: 9 additions & 13 deletions

File tree

cmp/compiler/src/plugin/next.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { createLingoConfig } from "../utils/config-factory";
1010
import { logger } from "../utils/logger";
1111
import type { PartialLingoConfig } from "../types";
1212
import { lingoUnplugin } from "./unplugin";
13+
import { useI18nRegex } from "./transform/use-i18n";
1314

1415
export type LingoNextPluginOptions = PartialLingoConfig;
1516

@@ -87,8 +88,7 @@ function buildLingoConfig(
8788
// This is more efficient than checking in the loader itself
8889
if (lingoConfig.useDirective) {
8990
lingoRuleConfig.condition = {
90-
content:
91-
/^\s*(?:["']use (?:strict|client|server)["']\s*;?\s*)*["']use i18n["']\s*;?/m,
91+
content: useI18nRegex,
9292
};
9393
}
9494

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const useI18n = "use i18n";
2+
export const useI18nRegex = new RegExp(`["']${useI18n}["']`);

cmp/compiler/src/plugin/transform/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { NodePath } from "@babel/traverse";
33
import type { VariableDeclaration } from "@babel/types";
44
import * as t from "@babel/types";
55
import { generateTranslationHash } from "../../utils/hash";
6+
import { useI18n } from "./use-i18n";
67

78
type TranslationEntryByType = {
89
[T in TranslationEntry as T["type"]]: T;
@@ -104,7 +105,7 @@ export function inferComponentName(path: NodePath<any>): string | null {
104105
export function hasUseI18nDirective(program: NodePath<t.Program>): boolean {
105106
const directives = program.node.directives || [];
106107
return directives.some(
107-
(directive: t.Directive) => directive.value.value === "use i18n",
108+
(directive: t.Directive) => directive.value.value === useI18n,
108109
);
109110
}
110111

cmp/compiler/src/plugin/turbopack-loader.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { LingoConfig } from "../types";
22
import { loadMetadata, saveMetadata, upsertEntries } from "../metadata/manager";
33
import { shouldTransformFile, transformComponent } from "./transform";
44
import { logger } from "../utils/logger";
5-
import { hasI18nDirective } from "../utils/directive-check";
65

76
/**
87
* Turbopack/Webpack loader for automatic translation
@@ -26,17 +25,12 @@ export default async function lingoCompilerTurbopackLoader(
2625
try {
2726
const config: LingoConfig = this.getOptions();
2827

28+
// TODO (AleksandrSl 07/12/2025): Remove too I think
2929
// Check if this file should be transformed
3030
if (!shouldTransformFile(this.resourcePath, config)) {
3131
return callback(null, source);
3232
}
3333

34-
// Fast path: if useDirective is enabled, check for directive before parsing
35-
// This avoids expensive Babel parsing for files without "use i18n"
36-
if (config.useDirective && !hasI18nDirective(source)) {
37-
return callback(null, source);
38-
}
39-
4034
// Transform the component
4135
const result = transformComponent({
4236
code: source,

cmp/compiler/src/plugin/unplugin.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { saveMetadataWithEntries } from "../metadata/manager";
2323
import { createLingoConfig } from "../utils/config-factory";
2424
import { logger } from "../utils/logger";
2525
import { getCacheDir } from "../utils/path-helpers";
26+
import { useI18nRegex } from "./transform/use-i18n";
2627

2728
export type LingoPluginOptions = PartialLingoConfig;
2829

@@ -84,9 +85,7 @@ export const cacheDir = ${JSON.stringify(cacheDir)};`;
8485
},
8586
// If useDirective is enabled, only process files with "use i18n"
8687
// This is more efficient than checking in the handler
87-
code: config.useDirective
88-
? /^\s*(?:["']use (?:strict|client|server)["']\s*;?\s*)*["']use i18n["']\s*;?/m
89-
: undefined,
88+
code: config.useDirective ? useI18nRegex : undefined,
9089
},
9190
handler: async (code, id) => {
9291
try {

0 commit comments

Comments
 (0)