Skip to content

Commit bc7b08e

Browse files
authored
fix(compiler): dictionary path calculation (#1130)
1 parent c702018 commit bc7b08e

5 files changed

Lines changed: 44 additions & 18 deletions

File tree

.changeset/wet-spoons-report.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@lingo.dev/_compiler": patch
3+
"lingo.dev": patch
4+
---
5+
6+
dictionary path calculation

demo/next-app/next.config.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ const nextConfig: NextConfig = {
88
export default lingoCompiler.next({
99
sourceLocale: "en",
1010
targetLocales: ["es", "ja", "fr", "ru", "de", "zh", "ar", "ko"],
11-
models: {
12-
"*:*": "groq:mistral-saba-24b",
13-
},
11+
models: "lingo.dev",
1412
useDirective: true,
1513
})(nextConfig);

demo/react-router-app/vite.config.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ export default defineConfig(({ isSsrBuild }) =>
1111
sourceRoot: "app",
1212
targetLocales: ["es", "fr", "de"],
1313
useDirective: false,
14-
models: {
15-
"*:*": "groq:mistral-saba-24b",
16-
},
14+
models: "lingo.dev",
1715
})({
1816
plugins: [tailwindcss(), reactRouter(), tsconfigPaths()],
1917
}),

packages/compiler/src/_utils.spec.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { afterEach, describe, expect, it, vi } from "vitest";
22

3-
// Helper to build expected relative path
4-
const EXPECTED_RELATIVE_PATH = "../../lingo/dictionary.js";
5-
63
afterEach(() => {
74
// Reset module registry and any mocks so each test gets a fresh copy
85
vi.resetModules();
@@ -11,6 +8,33 @@ afterEach(() => {
118
});
129

1310
describe("getDictionaryPath", () => {
11+
it.each([
12+
{
13+
sourceRoot: "src",
14+
lingoDir: "lingo",
15+
relativeFilePath: "./components/Button.tsx",
16+
expected: "./../lingo/dictionary.js",
17+
},
18+
{
19+
sourceRoot: "src/app/content",
20+
lingoDir: "i18n",
21+
relativeFilePath: "../../components/Button.tsx",
22+
expected: "./../app/content/i18n/dictionary.js",
23+
},
24+
])(
25+
"returns correct path for file $relativeFilePath in $sourceRoot",
26+
async ({ sourceRoot, lingoDir, relativeFilePath, expected }) => {
27+
const { getDictionaryPath } = await import("./_utils");
28+
29+
const result = getDictionaryPath({
30+
sourceRoot,
31+
lingoDir,
32+
relativeFilePath,
33+
});
34+
expect(result).toBe(expected);
35+
},
36+
);
37+
1438
it("returns POSIX-style relative path on POSIX", async () => {
1539
// Import fresh copy with the real Node "path" module (POSIX on *nix, win32 on Windows)
1640
const { getDictionaryPath } = await import("./_utils");
@@ -21,7 +45,7 @@ describe("getDictionaryPath", () => {
2145
relativeFilePath: "/project/src/components/Button.tsx",
2246
});
2347

24-
expect(result).toBe(EXPECTED_RELATIVE_PATH);
48+
expect(result).toBe("./../lingo/dictionary.js");
2549
// Ensure no back-slashes slip through
2650
expect(result).not.toMatch(/\\/);
2751
});
@@ -41,7 +65,7 @@ describe("getDictionaryPath", () => {
4165
relativeFilePath: "C:\\project\\src\\components\\Button.tsx",
4266
});
4367

44-
expect(result).toBe(EXPECTED_RELATIVE_PATH);
68+
expect(result).toBe("./../lingo/dictionary.js");
4569
expect(result).not.toMatch(/\\/);
4670
});
4771
});

packages/compiler/src/_utils.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ export type GetDictionaryPathParams = {
88
relativeFilePath: string;
99
};
1010
export const getDictionaryPath = (params: GetDictionaryPathParams) => {
11-
const absolute = path.resolve(
11+
const toFile = path.resolve(
1212
params.sourceRoot,
1313
params.lingoDir,
1414
LCP_DICTIONARY_FILE_NAME,
1515
);
16-
17-
// let Node figure the relative path first
18-
const rel = path.relative(params.relativeFilePath, absolute);
19-
20-
// then normalise the separator once
21-
return rel.split(path.sep).join(path.posix.sep);
16+
const fromDir = path.dirname(
17+
path.resolve(params.sourceRoot, params.relativeFilePath),
18+
);
19+
const relativePath = path.relative(fromDir, toFile);
20+
const normalizedPath = relativePath.split(path.sep).join(path.posix.sep);
21+
return `./${normalizedPath}`;
2222
};

0 commit comments

Comments
 (0)