Skip to content

Commit 9cecef5

Browse files
committed
WIP: working webpack by separating next and generic webpack
1 parent ce4e6f9 commit 9cecef5

14 files changed

Lines changed: 313 additions & 274 deletions

cmp/compiler/package.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,25 +86,25 @@
8686
"import": "./build/plugin/webpack.mjs",
8787
"require": "./build/plugin/webpack.cjs"
8888
},
89-
"./turbopack-loader": {
90-
"types": "./build/plugin/turbopack-loader.d.ts",
91-
"import": "./build/plugin/turbopack-loader.mjs",
92-
"require": "./build/plugin/turbopack-loader.cjs"
89+
"./next-compiler-loader": {
90+
"types": "./build/plugin/next-compiler-loader.d.ts",
91+
"import": "./build/plugin/next-compiler-loader.mjs",
92+
"require": "./build/plugin/next-compiler-loader.cjs"
9393
},
94-
"./dev-server-loader": {
95-
"types": "./build/plugin/dev-server-loader.d.ts",
96-
"import": "./build/plugin/dev-server-loader.mjs",
97-
"require": "./build/plugin/dev-server-loader.cjs"
94+
"./next-dev-config-loader": {
95+
"types": "./build/plugin/next-dev-config-loader.d.ts",
96+
"import": "./build/plugin/next-dev-config-loader.mjs",
97+
"require": "./build/plugin/next-dev-config-loader.cjs"
9898
},
99-
"./turbopack-locale-server-loader": {
100-
"types": "./build/plugin/turbopack-locale-server-loader.d.ts",
101-
"import": "./build/plugin/turbopack-locale-server-loader.mjs",
102-
"require": "./build/plugin/turbopack-locale-server-loader.cjs"
99+
"./next-locale-server-loader": {
100+
"types": "./build/plugin/next-locale-server-loader.d.ts",
101+
"import": "./build/plugin/next-locale-server-loader.mjs",
102+
"require": "./build/plugin/next-locale-server-loader.cjs"
103103
},
104-
"./turbopack-locale-client-loader": {
105-
"types": "./build/plugin/turbopack-locale-client-loader.d.ts",
106-
"import": "./build/plugin/turbopack-locale-client-loader.mjs",
107-
"require": "./build/plugin/turbopack-locale-client-loader.cjs"
104+
"./next-locale-client-loader": {
105+
"types": "./build/plugin/next-locale-client-loader.d.ts",
106+
"import": "./build/plugin/next-locale-client-loader.mjs",
107+
"require": "./build/plugin/next-locale-client-loader.cjs"
108108
}
109109
},
110110
"files": [

cmp/compiler/src/plugin/dev-server-loader.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
11
/**
22
* Shared code generation for locale resolution modules
33
* Used by both unplugin (virtual modules) and Turbopack loaders
4+
*
5+
* This module provides FULL module generators, not just function bodies.
6+
* Both loaders and unplugin use these to generate complete modules.
47
*/
58

69
import type { LingoConfig } from "../types";
10+
import { getCacheDir } from "../utils/path-helpers";
711

812
/**
9-
* Generate server-side locale detection code
10-
* Reads locale from cookie
13+
* Generate complete dev-config module
14+
* Exports serverUrl and cacheDir constants
1115
*/
12-
export function generateServerLocaleCode(config: LingoConfig): string {
16+
export function generateDevConfigModule(config: LingoConfig): string {
17+
const serverUrl = config.dev.translationServerUrl || "http://127.0.0.1:60000";
18+
const cacheDir = getCacheDir(config);
19+
20+
return `export const serverUrl = ${JSON.stringify(serverUrl)};
21+
export const cacheDir = ${JSON.stringify(cacheDir)};
22+
`;
23+
}
24+
25+
/**
26+
* Generate complete locale/server module
27+
* Exports async getServerLocale() function
28+
*/
29+
export function generateServerLocaleModule(config: LingoConfig): string {
1330
return `
31+
export async function getServerLocale() {
1432
try {
1533
const { cookies } = await import('next/headers');
1634
const cookieStore = await cookies();
@@ -19,31 +37,33 @@ export function generateServerLocaleCode(config: LingoConfig): string {
1937
} catch (error) {
2038
// Fallback if cookies are not available
2139
return ${JSON.stringify(config.sourceLocale)};
22-
}`;
40+
}
41+
}
42+
`;
2343
}
2444

2545
/**
26-
* Generate client-side locale detection and persistence code
27-
* Includes both getClientLocale() and persistLocale() functions
46+
* Generate complete locale/client module
47+
* Exports getClientLocale() and persistLocale() functions
2848
*/
29-
export function generateClientLocaleCode(config: LingoConfig): {
30-
getClientLocale: string;
31-
persistLocale: string;
32-
} {
49+
export function generateClientLocaleModule(config: LingoConfig): string {
3350
const cookieName = config.cookieConfig.name;
3451
const maxAge = config.cookieConfig.maxAge;
3552

36-
return {
37-
getClientLocale: `
53+
return `
54+
export function getClientLocale() {
3855
if (typeof document !== 'undefined') {
3956
const match = document.cookie.match(/${cookieName}=([^;]+)/);
4057
if (match) return match[1];
4158
}
4259
// Fallback to source locale
4360
return ${JSON.stringify(config.sourceLocale)};
44-
`,
45-
persistLocale: `if (typeof document !== 'undefined') {
61+
}
62+
63+
export function persistLocale(locale) {
64+
if (typeof document !== 'undefined') {
4665
document.cookie = \`${cookieName}=\${locale}; path=/; max-age=${maxAge}\`;
47-
}`,
48-
};
66+
}
67+
}
68+
`;
4969
}

cmp/compiler/src/plugin/turbopack-loader.ts renamed to cmp/compiler/src/plugin/next-compiler-loader.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ import { MetadataManager } from "../metadata/manager";
1111
* For production builds, translations are generated after compilation completes
1212
* via Next.js's runAfterProductionCompile hook (see next.ts plugin).
1313
*/
14-
export default async function lingoCompilerTurbopackLoader(
14+
export default async function nextCompilerLoader(
1515
this: any,
1616
source: string,
1717
): Promise<void> {
18+
// TODO (AleksandrSl 14/12/2025): Webpack doesn't like callback usage in async function. But asycn function can return only code, so we have to use promises which is sad.
1819
// Ensure we're running in loader context
1920
if (typeof this.async !== "function") {
2021
throw new Error("This module must be run as a loader");
@@ -55,10 +56,27 @@ export default async function lingoCompilerTurbopackLoader(
5556
);
5657
}
5758

58-
callback(null, result.code, result.map);
59+
// Validate source map before passing to webpack
60+
// Webpack crashes if sources array contains undefined values
61+
const validMap =
62+
result.map &&
63+
result.map.sources &&
64+
Array.isArray(result.map.sources) &&
65+
result.map.sources.every((s: any) => typeof s === "string")
66+
? result.map
67+
: undefined;
68+
69+
callback(null, result.code, validMap);
5970
} catch (error) {
6071
logger.error(`Compiler failed for ${this.resourcePath}:`);
61-
logger.error("Details:", error);
72+
logger.error(
73+
"Details:",
74+
error,
75+
typeof error === "object" && error && "message" in error
76+
? error.message
77+
: error,
78+
error instanceof Error ? error.stack : undefined,
79+
);
6280
callback(error as Error);
6381
}
6482
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { LingoConfig } from "../types";
2+
import { generateDevConfigModule } from "./locale-code-generator";
3+
4+
/**
5+
* Loader for dev-config module
6+
* Generates full module code - ignores source file (template is only for types)
7+
*/
8+
export default function nextDevConfigLoader(
9+
this: any,
10+
_source: string,
11+
): string {
12+
const config: LingoConfig = this.getOptions();
13+
return generateDevConfigModule(config);
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Loader for locale/client module
3+
* Generates full module code - ignores source file (template is only for types)
4+
*/
5+
6+
import type { LingoConfig } from "../types";
7+
import { generateClientLocaleModule } from "./locale-code-generator";
8+
9+
export default function nextLocaleClientLoader(
10+
this: any,
11+
_source: string,
12+
): string {
13+
const config: LingoConfig = this.getOptions();
14+
return generateClientLocaleModule(config);
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Loader for locale/server module
3+
* Generates full module code - ignores source file (template is only for types)
4+
*/
5+
6+
import type { LingoConfig } from "../types";
7+
import { generateServerLocaleModule } from "./locale-code-generator";
8+
9+
export default function nextLocaleServerLoader(
10+
this: any,
11+
_source: string,
12+
): string {
13+
const config: LingoConfig = this.getOptions();
14+
return generateServerLocaleModule(config);
15+
}

0 commit comments

Comments
 (0)