@@ -3,34 +3,10 @@ import type { NextConfig } from "next";
3
3
import packageJson from "../package.json" ;
4
4
import _ from "lodash" ;
5
5
import dedent from "dedent" ;
6
- import {
7
- composeMutations ,
8
- createPayload ,
9
- createOutput ,
10
- defaultParams ,
11
- } from "./_base" ;
12
- import i18nDirectiveMutation from "./i18n-directive" ;
13
- import jsxProviderMutation from "./jsx-provider" ;
14
- import jsxRootFlagMutation from "./jsx-root-flag" ;
15
- import jsxScopeFlagMutation from "./jsx-scope-flag" ;
16
- import jsxAttributeFlagMutation from "./jsx-attribute-flag" ;
17
- import path from "path" ;
18
- import { parseParametrizedModuleId } from "./utils/module-params" ;
19
- import { LCP } from "./lib/lcp" ;
20
- import { LCPServer } from "./lib/lcp/server" ;
21
- import { rscDictionaryLoaderMutation } from "./rsc-dictionary-loader" ;
22
- import { reactRouterDictionaryLoaderMutation } from "./react-router-dictionary-loader" ;
23
- import { jsxFragmentMutation } from "./jsx-fragment" ;
24
- import { jsxHtmlLangMutation } from "./jsx-html-lang" ;
25
- import { jsxAttributeScopesExportMutation } from "./jsx-attribute-scopes-export" ;
26
- import { jsxScopesExportMutation } from "./jsx-scopes-export" ;
27
- import { lingoJsxAttributeScopeInjectMutation } from "./jsx-attribute-scope-inject" ;
28
- import { lingoJsxScopeInjectMutation } from "./jsx-scope-inject" ;
29
- import { jsxRemoveAttributesMutation } from "./jsx-remove-attributes" ;
6
+ import { defaultParams } from "./_base" ;
30
7
import { LCP_DICTIONARY_FILE_NAME } from "./_const" ;
31
8
import { LCPCache } from "./lib/lcp/cache" ;
32
9
import { getInvalidLocales } from "./utils/locales" ;
33
- import { clientDictionaryLoaderMutation } from "./client-dictionary-loader" ;
34
10
import {
35
11
getGroqKeyFromEnv ,
36
12
getGroqKeyFromRc ,
@@ -41,6 +17,7 @@ import {
41
17
} from "./utils/llm-api-key" ;
42
18
import { isRunningInCIOrDocker } from "./utils/env" ;
43
19
import { providerDetails } from "./lib/lcp/api/provider-details" ;
20
+ import { loadDictionary , transformComponent } from "./_loader-utils" ;
44
21
45
22
const keyCheckers : Record <
46
23
string ,
@@ -86,12 +63,12 @@ const unplugin = createUnplugin<Partial<typeof defaultParams> | undefined>(
86
63
console . log ( dedent `
87
64
\n
88
65
⚠️ Lingo.dev Localization Compiler requires LLM model setup for the following locales: ${ invalidLocales . join ( ", " ) } .
89
-
66
+
90
67
⭐️ Next steps:
91
68
1. Refer to documentation for help: https://lingo.dev/compiler
92
69
2. If you want to use a different LLM, raise an issue in our open-source repo: https://lingo.dev/go/gh
93
70
3. If you have questions, feature requests, or would like to contribute, join our Discord: https://lingo.dev/go/discord
94
-
71
+
95
72
✨
96
73
` ) ;
97
74
process . exit ( 1 ) ;
@@ -111,27 +88,23 @@ const unplugin = createUnplugin<Partial<typeof defaultParams> | undefined>(
111
88
name : packageJson . name ,
112
89
loadInclude : ( id ) => ! ! id . match ( LCP_DICTIONARY_FILE_NAME ) ,
113
90
async load ( id ) {
114
- const moduleInfo = parseParametrizedModuleId ( id ) ;
115
-
116
- const lcpParams = {
91
+ const dictionary = await loadDictionary ( {
92
+ resourcePath : id ,
93
+ resourceQuery : "" ,
94
+ params : {
95
+ ...params ,
96
+ models : params . models ,
97
+ sourceLocale : params . sourceLocale ,
98
+ targetLocales : params . targetLocales ,
99
+ } ,
117
100
sourceRoot : params . sourceRoot ,
118
101
lingoDir : params . lingoDir ,
119
102
isDev,
120
- } ;
121
-
122
- // wait for LCP file to be generated
123
- await LCP . ready ( lcpParams ) ;
124
- const lcp = LCP . getInstance ( lcpParams ) ;
125
-
126
- const dictionaries = await LCPServer . loadDictionaries ( {
127
- models : params . models ,
128
- lcp : lcp . data ,
129
- sourceLocale : params . sourceLocale ,
130
- targetLocales : params . targetLocales ,
131
- sourceRoot : params . sourceRoot ,
132
- lingoDir : params . lingoDir ,
133
103
} ) ;
134
- const dictionary = dictionaries [ moduleInfo . params . locale ] ;
104
+
105
+ if ( ! dictionary ) {
106
+ return null ;
107
+ }
135
108
136
109
console . log ( JSON . stringify ( dictionary , null , 2 ) ) ;
137
110
@@ -143,44 +116,12 @@ const unplugin = createUnplugin<Partial<typeof defaultParams> | undefined>(
143
116
enforce : "pre" ,
144
117
transform ( code , id ) {
145
118
try {
146
- const result = _ . chain ( {
119
+ const result = transformComponent ( {
147
120
code,
148
121
params,
149
- relativeFilePath : path
150
- . relative ( path . resolve ( process . cwd ( ) , params . sourceRoot ) , id )
151
- . split ( path . sep )
152
- . join ( "/" ) , // Always normalize for consistent dictionaries
153
- } )
154
- . thru ( createPayload )
155
- . thru (
156
- composeMutations (
157
- i18nDirectiveMutation ,
158
- jsxFragmentMutation ,
159
- jsxAttributeFlagMutation ,
160
-
161
- // log here to see transformedfiles
162
- // (input) => {
163
- // console.log(`transform ${id}`);
164
- // return input;
165
- // },
166
-
167
- jsxProviderMutation ,
168
- jsxHtmlLangMutation ,
169
- jsxRootFlagMutation ,
170
- jsxScopeFlagMutation ,
171
- jsxAttributeFlagMutation ,
172
- jsxAttributeScopesExportMutation ,
173
- jsxScopesExportMutation ,
174
- lingoJsxAttributeScopeInjectMutation ,
175
- lingoJsxScopeInjectMutation ,
176
- rscDictionaryLoaderMutation ,
177
- reactRouterDictionaryLoaderMutation ,
178
- jsxRemoveAttributesMutation ,
179
- clientDictionaryLoaderMutation ,
180
- ) ,
181
- )
182
- . thru ( createOutput )
183
- . value ( ) ;
122
+ resourcePath : id ,
123
+ sourceRoot : params . sourceRoot ,
124
+ } ) ;
184
125
185
126
return result ;
186
127
} catch ( error ) {
@@ -196,19 +137,96 @@ const unplugin = createUnplugin<Partial<typeof defaultParams> | undefined>(
196
137
197
138
export default {
198
139
next :
199
- ( compilerParams ?: Partial < typeof defaultParams > ) =>
200
- ( nextConfig : any ) : NextConfig => ( {
201
- ...nextConfig ,
202
- // what if we already have a webpack config?
203
- webpack : ( config , { isServer } ) => {
204
- config . plugins . unshift (
205
- unplugin . webpack (
206
- _ . merge ( { } , defaultParams , { rsc : true } , compilerParams ) ,
207
- ) ,
140
+ (
141
+ compilerParams ?: Partial < typeof defaultParams > & {
142
+ turbopack ?: {
143
+ enabled ?: boolean | "auto" ;
144
+ useLegacyTurbo ?: boolean ;
145
+ } ;
146
+ } ,
147
+ ) =>
148
+ ( nextConfig : any = { } ) : NextConfig => {
149
+ const mergedParams = _ . merge (
150
+ { } ,
151
+ defaultParams ,
152
+ {
153
+ rsc : true ,
154
+ turbopack : {
155
+ enabled : "auto" ,
156
+ useLegacyTurbo : false ,
157
+ } ,
158
+ } ,
159
+ compilerParams ,
160
+ ) ;
161
+
162
+ let turbopackEnabled : boolean ;
163
+ if ( mergedParams . turbopack ?. enabled === "auto" ) {
164
+ turbopackEnabled =
165
+ process . env . TURBOPACK === "1" || process . env . TURBOPACK === "true" ;
166
+ } else {
167
+ turbopackEnabled = mergedParams . turbopack ?. enabled === true ;
168
+ }
169
+
170
+ const supportLegacyTurbo : boolean =
171
+ mergedParams . turbopack ?. useLegacyTurbo === true ;
172
+
173
+ const hasWebpackConfig = typeof nextConfig . webpack === "function" ;
174
+ const hasTurbopackConfig = typeof nextConfig . turbopack === "function" ;
175
+ if ( hasWebpackConfig && turbopackEnabled ) {
176
+ console . warn (
177
+ "⚠️ Turbopack is enabled in the Lingo.dev compiler, but you have webpack config. Lingo.dev will still apply turbopack configuration." ,
208
178
) ;
179
+ }
180
+ if ( hasTurbopackConfig && ! turbopackEnabled ) {
181
+ console . warn (
182
+ "⚠️ Turbopack is disabled in the Lingo.dev compiler, but you have turbopack config. Lingo.dev will not apply turbopack configuration." ,
183
+ ) ;
184
+ }
185
+
186
+ // Webpack
187
+ const originalWebpack = nextConfig . webpack ;
188
+ nextConfig . webpack = ( config : any , options : any ) => {
189
+ if ( ! turbopackEnabled ) {
190
+ console . log ( "Applying Lingo.dev webpack configuration..." ) ;
191
+ config . plugins . unshift ( unplugin . webpack ( mergedParams ) ) ;
192
+ }
193
+
194
+ if ( typeof originalWebpack === "function" ) {
195
+ return originalWebpack ( config , options ) ;
196
+ }
209
197
return config ;
210
- } ,
211
- } ) ,
198
+ } ;
199
+
200
+ // Turbopack
201
+ if ( turbopackEnabled ) {
202
+ console . log ( "Applying Lingo.dev Turbopack configuration..." ) ;
203
+
204
+ // Check if the legacy turbo flag is set
205
+ let turbopackConfigPath = ( nextConfig . turbopack ??= { } ) ;
206
+ if ( supportLegacyTurbo ) {
207
+ turbopackConfigPath = ( nextConfig . experimental ??= { } ) . turbo ??= { } ;
208
+ }
209
+
210
+ turbopackConfigPath . rules ??= { } ;
211
+ const rules = turbopackConfigPath . rules ;
212
+
213
+ // Regex for all relevant files for Lingo.dev
214
+ const lingoGlob = `**/*.{ts,tsx,js,jsx}` ;
215
+
216
+ const lingoLoaderPath = require . resolve ( "./lingo-turbopack-loader" ) ;
217
+
218
+ rules [ lingoGlob ] = {
219
+ loaders : [
220
+ {
221
+ loader : lingoLoaderPath ,
222
+ options : mergedParams ,
223
+ } ,
224
+ ] ,
225
+ } ;
226
+ }
227
+
228
+ return nextConfig ;
229
+ } ,
212
230
vite : ( compilerParams ?: Partial < typeof defaultParams > ) => ( config : any ) => {
213
231
config . plugins . unshift (
214
232
unplugin . vite ( _ . merge ( { } , defaultParams , { rsc : false } , compilerParams ) ) ,
0 commit comments