Skip to content

Commit fa99e47

Browse files
committed
fix: remove useless metadata access in visitor
1 parent 78bee57 commit fa99e47

8 files changed

Lines changed: 30 additions & 40 deletions

File tree

cmp/compiler/src/plugin/esbuild.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020

2121
import { lingoUnplugin, type LingoPluginOptions } from "./unplugin";
2222

23-
/**
24-
* esbuild plugin with framework automatically set
25-
*/
2623
export function lingoCompilerPlugin(options: Partial<LingoPluginOptions> = {}) {
2724
return lingoUnplugin.esbuild({
2825
...options,

cmp/compiler/src/plugin/rollup.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919

2020
import { lingoUnplugin, type LingoPluginOptions } from "./unplugin";
2121

22-
/**
23-
* Rollup plugin with framework automatically set
24-
*/
2522
export function lingoCompilerPlugin(options: Partial<LingoPluginOptions> = {}) {
2623
return lingoUnplugin.rollup({
2724
...options,

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ export interface BabelTransformOptions {
3737
* Loader configuration
3838
*/
3939
config: LoaderConfig;
40-
41-
/**
42-
* Current metadata
43-
*/
44-
metadata: MetadataSchema;
4540
}
4641

4742
/**
@@ -51,7 +46,6 @@ export function transformComponent({
5146
code,
5247
filePath,
5348
config,
54-
metadata,
5549
}: BabelTransformOptions): TransformResult {
5650
// Get relative file path for consistent hashing
5751
const relativeFilePath = path
@@ -70,12 +64,11 @@ export function transformComponent({
7064

7165
const visitorState = {
7266
filePath: relativeFilePath,
73-
metadata,
7467
config,
7568
newEntries: [] as any[],
7669
} satisfies VisitorsSharedState;
7770

78-
logger.debug(`Transforming ${filePath}, isServer: ${config.isServer}`);
71+
logger.debug(`Transforming ${filePath}`);
7972

8073
// TODO (AleksandrSl 02/12/2025): Can I pass state to the traverse here as well?
8174
const visitors = createBabelVisitors({

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import type { NodePath, TraverseOptions } from "@babel/traverse";
77
import type {
88
ComponentType,
99
LoaderConfig,
10-
MetadataSchema,
1110
TranslationEntry,
1211
} from "../../types";
1312
import { generateTranslationHash } from "../../utils/hash";
13+
import { logger } from "../../utils/logger";
1414

1515
type ComponentEntry = {
1616
name: string;
@@ -21,7 +21,6 @@ type ComponentEntry = {
2121
export interface VisitorsSharedState {
2222
newEntries: TranslationEntry[];
2323
filePath: string;
24-
metadata: MetadataSchema;
2524
config: LoaderConfig;
2625
}
2726

@@ -91,6 +90,7 @@ function isReactComponent(
9190
path.traverse({
9291
ReturnStatement(returnPath) {
9392
const argument = returnPath.node.argument;
93+
logger.debug(`Found return statement: ${argument?.type}`);
9494
if (
9595
argument &&
9696
(argument.type === "JSXElement" || argument.type === "JSXFragment")
@@ -1236,6 +1236,7 @@ function handleComponentFunction(
12361236
): void {
12371237
if (!isReactComponent(path)) {
12381238
path.skip();
1239+
logger.debug(`Skipping non-React component: ${path.node.type}`);
12391240
return;
12401241
}
12411242

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,20 @@ export default async function lingoCompilerTurbopackLoader(
3030
return callback(null, source);
3131
}
3232

33-
// Load current metadata
34-
const metadata = await loadMetadata(config);
35-
3633
// Transform the component
3734
const result = transformComponent({
3835
code: source,
3936
filePath: this.resourcePath,
4037
config,
41-
metadata,
4238
});
4339

4440
// If no transformation occurred, return original source
4541
if (!result.transformed) {
4642
return callback(null, source);
4743
}
4844

45+
// Load current metadata
46+
const metadata = await loadMetadata(config);
4947
// Update metadata with new entries
5048
if (result.newEntries && result.newEntries.length > 0) {
5149
const updatedMetadata = upsertEntries(metadata, result.newEntries);

cmp/compiler/src/plugin/unplugin.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313
*/
1414

1515
import { createUnplugin } from "unplugin";
16-
import path from "path";
1716
import { transformComponent } from "./transform";
1817
import type { LoaderConfig } from "../types";
1918
import {
2019
startTranslationServer,
2120
type TranslationServer,
2221
} from "../translation-server";
23-
import { loadMetadata, saveMetadataWithEntries } from "../metadata/manager";
22+
import { saveMetadataWithEntries } from "../metadata/manager";
2423
import { createLoaderConfig } from "../utils/config-factory";
2524
import { logger } from "../utils/logger";
25+
import { getCacheDir } from "../utils/path-helpers";
2626

2727
export interface LingoPluginOptions extends LoaderConfig {
2828
/**
@@ -77,6 +77,26 @@ export const lingoUnplugin = createUnplugin<LingoPluginOptions>(
7777
}
7878
},
7979

80+
resolveId(id) {
81+
if (id === "@lingo.dev/_compiler/dev-config") {
82+
// Return a virtual module ID (prefix with \0 to mark it as virtual)
83+
return "\0virtual:lingo-dev-config";
84+
}
85+
return null;
86+
},
87+
88+
load(id) {
89+
if (id === "\0virtual:lingo-dev-config") {
90+
const serverUrl = globalServer?.getUrl() || "http://127.0.0.1:60000";
91+
const cacheDir = getCacheDir(config);
92+
93+
return `export const serverUrl = ${JSON.stringify(serverUrl)};
94+
export const cacheDir = ${JSON.stringify(cacheDir)};
95+
export const sourceLocale = ${JSON.stringify(config.sourceLocale)};`;
96+
}
97+
return null;
98+
},
99+
80100
transform: {
81101
filter: {
82102
id: {
@@ -86,25 +106,15 @@ export const lingoUnplugin = createUnplugin<LingoPluginOptions>(
86106
},
87107
handler: async (code, id) => {
88108
try {
89-
// Load current metadata
90-
const metadata = await loadMetadata({
91-
sourceRoot: config.sourceRoot,
92-
lingoDir: config.lingoDir,
93-
});
94-
95-
logger.debug(
96-
`Metadata loaded, entries:`,
97-
Object.keys(metadata.entries).length,
98-
);
99-
100109
// Transform the component
101110
const result = transformComponent({
102111
code,
103112
filePath: id,
104113
config,
105-
metadata,
106114
});
107115

116+
logger.debug(`Transforming ${result.code}`);
117+
108118
// If no transformation occurred, return original code
109119
if (!result.transformed) {
110120
logger.debug(`No transformation needed for ${id}`);

cmp/compiler/src/plugin/vite.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020

2121
import { lingoUnplugin, type LingoPluginOptions } from "./unplugin";
2222

23-
/**
24-
* Vite plugin with framework automatically set
25-
*/
2623
export function lingoCompilerPlugin(options: Partial<LingoPluginOptions> = {}) {
2724
const fullOptions = {
2825
...options,

cmp/compiler/src/plugin/webpack.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919

2020
import { lingoUnplugin, type LingoPluginOptions } from "./unplugin";
2121

22-
/**
23-
* Webpack plugin with framework automatically set
24-
*/
2522
export function lingoCompilerPlugin(options: Partial<LingoPluginOptions> = {}) {
2623
return lingoUnplugin.webpack({
2724
...options,

0 commit comments

Comments
 (0)