next-intl 4.6.0 → 4.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/dist/cjs/development/ExtractorCodec-D9Tw618d.cjs +7 -0
  2. package/dist/cjs/development/{JSONCodec-Dlcx71xz.cjs → JSONCodec-CusgMbzf.cjs} +10 -3
  3. package/dist/cjs/development/{POCodec-BW-UDNcq.cjs → POCodec-Bns8JvnL.cjs} +16 -5
  4. package/dist/cjs/development/plugin-B0KIcFlc.cjs +1392 -0
  5. package/dist/cjs/development/plugin.cjs +8 -402
  6. package/dist/esm/development/extractor/ExtractionCompiler.js +1 -1
  7. package/dist/esm/development/extractor/catalog/CatalogManager.js +53 -27
  8. package/dist/esm/development/extractor/catalog/SaveScheduler.js +1 -1
  9. package/dist/esm/development/extractor/extractionLoader.js +5 -25
  10. package/dist/esm/development/extractor/source/SourceFileWatcher.js +99 -5
  11. package/dist/esm/development/extractor/utils.js +14 -8
  12. package/dist/esm/development/plugin/createNextIntlPlugin.js +2 -0
  13. package/dist/esm/development/plugin/declaration/createMessagesDeclaration.js +2 -11
  14. package/dist/esm/development/plugin/extractor/initExtractionCompiler.js +61 -0
  15. package/dist/esm/development/plugin/utils.js +16 -1
  16. package/dist/esm/production/extractor/ExtractionCompiler.js +1 -1
  17. package/dist/esm/production/extractor/catalog/CatalogManager.js +1 -1
  18. package/dist/esm/production/extractor/catalog/SaveScheduler.js +1 -1
  19. package/dist/esm/production/extractor/extractionLoader.js +1 -1
  20. package/dist/esm/production/extractor/source/SourceFileWatcher.js +1 -1
  21. package/dist/esm/production/extractor/utils.js +1 -1
  22. package/dist/esm/production/plugin/createNextIntlPlugin.js +1 -1
  23. package/dist/esm/production/plugin/declaration/createMessagesDeclaration.js +1 -1
  24. package/dist/esm/production/plugin/extractor/initExtractionCompiler.js +1 -0
  25. package/dist/esm/production/plugin/utils.js +1 -1
  26. package/dist/types/extractor/catalog/CatalogManager.d.ts +6 -5
  27. package/dist/types/extractor/catalog/SaveScheduler.d.ts +2 -2
  28. package/dist/types/extractor/source/SourceFileFilter.d.ts +1 -1
  29. package/dist/types/extractor/source/SourceFileWatcher.d.ts +3 -0
  30. package/dist/types/extractor/types.d.ts +5 -3
  31. package/dist/types/extractor/utils.d.ts +2 -1
  32. package/dist/types/plugin/extractor/initExtractionCompiler.d.ts +2 -0
  33. package/dist/types/plugin/utils.d.ts +6 -0
  34. package/package.json +5 -5
  35. package/dist/cjs/development/ExtractorCodec-DZKNn0Zq.cjs +0 -37
  36. package/dist/types/extractor/extractor/ASTScope.d.ts +0 -12
@@ -0,0 +1,1392 @@
1
+ 'use strict';
2
+
3
+ var fs$1 = require('fs/promises');
4
+ var path = require('path');
5
+ var watcher = require('@parcel/watcher');
6
+ var fs = require('fs');
7
+ var module$1 = require('module');
8
+ var core = require('@swc/core');
9
+
10
+ var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
11
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
12
+
13
+ var fs__default$1 = /*#__PURE__*/_interopDefaultCompat(fs$1);
14
+ var path__default = /*#__PURE__*/_interopDefaultCompat(path);
15
+ var fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
16
+
17
+ function formatMessage(message) {
18
+ return `\n[next-intl] ${message}\n`;
19
+ }
20
+ function throwError(message) {
21
+ throw new Error(formatMessage(message));
22
+ }
23
+ function warn(message) {
24
+ console.warn(formatMessage(message));
25
+ }
26
+
27
+ /**
28
+ * Returns a function that runs the provided callback only once per process.
29
+ * Next.js can call the config multiple times - this ensures we only run once.
30
+ * Uses an environment variable to track execution across config loads.
31
+ */
32
+ function once(namespace) {
33
+ return function runOnce(fn) {
34
+ if (process.env[namespace] === '1') {
35
+ return;
36
+ }
37
+ process.env[namespace] = '1';
38
+ fn();
39
+ };
40
+ }
41
+
42
+ /**
43
+ * Wrapper around `fs.watch` that provides a workaround
44
+ * for https://github.com/nodejs/node/issues/5039.
45
+ */
46
+ function watchFile(filepath, callback) {
47
+ const directory = path__default.default.dirname(filepath);
48
+ const filename = path__default.default.basename(filepath);
49
+ return fs__default.default.watch(directory, {
50
+ persistent: false,
51
+ recursive: false
52
+ }, (event, changedFilename) => {
53
+ if (changedFilename === filename) {
54
+ callback();
55
+ }
56
+ });
57
+ }
58
+
59
+ const runOnce$1 = once('_NEXT_INTL_COMPILE_MESSAGES');
60
+ function createMessagesDeclaration(messagesPaths) {
61
+ // Instead of running _only_ in certain cases, it's
62
+ // safer to _avoid_ running for certain known cases.
63
+ // https://github.com/amannn/next-intl/issues/2006
64
+ const shouldBailOut = ['info', 'start'
65
+
66
+ // Note: These commands don't consult the
67
+ // Next.js config, so we can't detect them here.
68
+ // - telemetry
69
+ // - lint
70
+ //
71
+ // What remains are:
72
+ // - dev
73
+ // - build
74
+ // - typegen
75
+ ].some(arg => process.argv.includes(arg));
76
+ if (shouldBailOut) {
77
+ return;
78
+ }
79
+ runOnce$1(() => {
80
+ for (const messagesPath of messagesPaths) {
81
+ const fullPath = path__default.default.resolve(messagesPath);
82
+ if (!fs__default.default.existsSync(fullPath)) {
83
+ throwError(`\`createMessagesDeclaration\` points to a non-existent file: ${fullPath}`);
84
+ }
85
+ if (!fullPath.endsWith('.json')) {
86
+ throwError(`\`createMessagesDeclaration\` needs to point to a JSON file. Received: ${fullPath}`);
87
+ }
88
+
89
+ // Keep this as a runtime check and don't replace
90
+ // this with a constant during the build process
91
+ const env = process.env['NODE_ENV'.trim()];
92
+ compileDeclaration(messagesPath);
93
+ if (env === 'development') {
94
+ startWatching(messagesPath);
95
+ }
96
+ }
97
+ });
98
+ }
99
+ function startWatching(messagesPath) {
100
+ const watcher = watchFile(messagesPath, () => {
101
+ compileDeclaration(messagesPath, true);
102
+ });
103
+ process.on('exit', () => {
104
+ watcher.close();
105
+ });
106
+ }
107
+ function compileDeclaration(messagesPath, async = false) {
108
+ const declarationPath = messagesPath.replace(/\.json$/, '.d.json.ts');
109
+ function createDeclaration(content) {
110
+ return `// This file is auto-generated by next-intl, do not edit directly.
111
+ // See: https://next-intl.dev/docs/workflows/typescript#messages-arguments
112
+
113
+ declare const messages: ${content.trim()};
114
+ export default messages;`;
115
+ }
116
+ if (async) {
117
+ return fs__default.default.promises.readFile(messagesPath, 'utf-8').then(content => fs__default.default.promises.writeFile(declarationPath, createDeclaration(content)));
118
+ }
119
+ const content = fs__default.default.readFileSync(messagesPath, 'utf-8');
120
+ fs__default.default.writeFileSync(declarationPath, createDeclaration(content));
121
+ }
122
+
123
+ const formats = {
124
+ json: {
125
+ codec: () => Promise.resolve().then(function () { return require('./JSONCodec-CusgMbzf.cjs'); }),
126
+ extension: '.json'
127
+ },
128
+ po: {
129
+ codec: () => Promise.resolve().then(function () { return require('./POCodec-Bns8JvnL.cjs'); }),
130
+ extension: '.po'
131
+ }
132
+ };
133
+ function isBuiltInFormat(format) {
134
+ return typeof format === 'string' && format in formats;
135
+ }
136
+ function getFormatExtension(format) {
137
+ if (isBuiltInFormat(format)) {
138
+ return formats[format].extension;
139
+ } else {
140
+ return format.extension;
141
+ }
142
+ }
143
+ async function resolveCodec(format, projectRoot) {
144
+ if (isBuiltInFormat(format)) {
145
+ const factory = (await formats[format].codec()).default;
146
+ return factory();
147
+ } else {
148
+ const resolvedPath = path__default.default.isAbsolute(format.codec) ? format.codec : path__default.default.resolve(projectRoot, format.codec);
149
+ let module;
150
+ try {
151
+ module = await import(resolvedPath);
152
+ } catch (error) {
153
+ throwError(`Could not load codec from "${resolvedPath}".\n${error}`);
154
+ }
155
+ const factory = module.default;
156
+ if (!factory || typeof factory !== 'function') {
157
+ throwError(`Codec at "${resolvedPath}" must have a default export returned from \`defineCodec\`.`);
158
+ }
159
+ return factory();
160
+ }
161
+ }
162
+
163
+ class SourceFileFilter {
164
+ static EXTENSIONS = ['ts', 'tsx', 'js', 'jsx'];
165
+
166
+ // Will not be entered, except if explicitly asked for
167
+ // TODO: At some point we should infer these from .gitignore
168
+ static IGNORED_DIRECTORIES = ['node_modules', '.next', '.git'];
169
+ static isSourceFile(filePath) {
170
+ const ext = path__default.default.extname(filePath);
171
+ return SourceFileFilter.EXTENSIONS.map(cur => '.' + cur).includes(ext);
172
+ }
173
+ static shouldEnterDirectory(dirPath, srcPaths) {
174
+ const dirName = path__default.default.basename(dirPath);
175
+ if (SourceFileFilter.IGNORED_DIRECTORIES.includes(dirName)) {
176
+ return SourceFileFilter.isIgnoredDirectoryExplicitlyIncluded(dirPath, srcPaths);
177
+ }
178
+ return true;
179
+ }
180
+ static isIgnoredDirectoryExplicitlyIncluded(ignoredDirPath, srcPaths) {
181
+ return srcPaths.some(srcPath => SourceFileFilter.isWithinPath(srcPath, ignoredDirPath));
182
+ }
183
+ static isWithinPath(targetPath, basePath) {
184
+ const relativePath = path__default.default.relative(basePath, targetPath);
185
+ return relativePath === '' || !relativePath.startsWith('..');
186
+ }
187
+ }
188
+
189
+ class SourceFileScanner {
190
+ static async walkSourceFiles(dir, srcPaths, acc = []) {
191
+ const entries = await fs__default$1.default.readdir(dir, {
192
+ withFileTypes: true
193
+ });
194
+ for (const entry of entries) {
195
+ const entryPath = path__default.default.join(dir, entry.name);
196
+ if (entry.isDirectory()) {
197
+ if (!SourceFileFilter.shouldEnterDirectory(entryPath, srcPaths)) {
198
+ continue;
199
+ }
200
+ await SourceFileScanner.walkSourceFiles(entryPath, srcPaths, acc);
201
+ } else {
202
+ if (SourceFileFilter.isSourceFile(entry.name)) {
203
+ acc.push(entryPath);
204
+ }
205
+ }
206
+ }
207
+ return acc;
208
+ }
209
+ static async getSourceFiles(srcPaths) {
210
+ const files = (await Promise.all(srcPaths.map(srcPath => SourceFileScanner.walkSourceFiles(srcPath, srcPaths)))).flat();
211
+ return new Set(files);
212
+ }
213
+ }
214
+
215
+ class SourceFileWatcher {
216
+ subscriptions = [];
217
+ constructor(roots, onChange) {
218
+ this.roots = roots;
219
+ this.onChange = onChange;
220
+ }
221
+ async start() {
222
+ if (this.subscriptions.length > 0) {
223
+ return;
224
+ }
225
+ const ignore = SourceFileFilter.IGNORED_DIRECTORIES.map(dir => `**/${dir}/**`);
226
+ for (const root of this.roots) {
227
+ const sub = await watcher.subscribe(root, async (err, events) => {
228
+ if (err) {
229
+ console.error(err);
230
+ return;
231
+ }
232
+ const filtered = await this.normalizeEvents(events);
233
+ if (filtered.length > 0) {
234
+ void this.onChange(filtered);
235
+ }
236
+ }, {
237
+ ignore
238
+ });
239
+ this.subscriptions.push(sub);
240
+ }
241
+ }
242
+ async normalizeEvents(events) {
243
+ const directoryCreatePaths = [];
244
+ const otherEvents = [];
245
+
246
+ // We need to expand directory creates because during rename operations,
247
+ // @parcel/watcher emits a directory create event but may not emit individual
248
+ // file events for the moved files
249
+ await Promise.all(events.map(async event => {
250
+ if (event.type === 'create') {
251
+ try {
252
+ const stats = await fs__default$1.default.stat(event.path);
253
+ if (stats.isDirectory()) {
254
+ directoryCreatePaths.push(event.path);
255
+ return;
256
+ }
257
+ } catch {
258
+ // Path doesn't exist or is inaccessible, treat as file
259
+ }
260
+ }
261
+ otherEvents.push(event);
262
+ }));
263
+
264
+ // Expand directory create events to find source files inside
265
+ let expandedCreateEvents = [];
266
+ if (directoryCreatePaths.length > 0) {
267
+ try {
268
+ const sourceFiles = await SourceFileScanner.getSourceFiles(directoryCreatePaths);
269
+ expandedCreateEvents = Array.from(sourceFiles).map(filePath => ({
270
+ type: 'create',
271
+ path: filePath
272
+ }));
273
+ } catch {
274
+ // Directories might have been deleted or are inaccessible
275
+ }
276
+ }
277
+
278
+ // Combine original events with expanded directory creates.
279
+ // Deduplicate by path to avoid processing the same file twice
280
+ // in case @parcel/watcher also emitted individual file events.
281
+ const allEvents = [...otherEvents, ...expandedCreateEvents];
282
+ const seenPaths = new Set();
283
+ const deduplicated = [];
284
+ for (const event of allEvents) {
285
+ const key = `${event.type}:${event.path}`;
286
+ if (!seenPaths.has(key)) {
287
+ seenPaths.add(key);
288
+ deduplicated.push(event);
289
+ }
290
+ }
291
+ return deduplicated.filter(event => {
292
+ // Keep all delete events (might be deleted directories that no longer exist)
293
+ if (event.type === 'delete') {
294
+ return true;
295
+ }
296
+ // Keep source files
297
+ return SourceFileFilter.isSourceFile(event.path);
298
+ });
299
+ }
300
+ async expandDirectoryDeleteEvents(events, prevKnownFiles) {
301
+ const expanded = [];
302
+ for (const event of events) {
303
+ if (event.type === 'delete' && !SourceFileFilter.isSourceFile(event.path)) {
304
+ const dirPath = path__default.default.resolve(event.path);
305
+ const filesInDirectory = [];
306
+ for (const filePath of prevKnownFiles) {
307
+ if (SourceFileFilter.isWithinPath(filePath, dirPath)) {
308
+ filesInDirectory.push(filePath);
309
+ }
310
+ }
311
+
312
+ // If we found files within this path, it was a directory
313
+ if (filesInDirectory.length > 0) {
314
+ for (const filePath of filesInDirectory) {
315
+ expanded.push({
316
+ type: 'delete',
317
+ path: filePath
318
+ });
319
+ }
320
+ } else {
321
+ // Not a directory or no files in it, pass through as-is
322
+ expanded.push(event);
323
+ }
324
+ } else {
325
+ // Pass through as-is
326
+ expanded.push(event);
327
+ }
328
+ }
329
+ return expanded;
330
+ }
331
+ async stop() {
332
+ await Promise.all(this.subscriptions.map(sub => sub.unsubscribe()));
333
+ this.subscriptions = [];
334
+ }
335
+ [Symbol.dispose]() {
336
+ void this.stop();
337
+ }
338
+ }
339
+
340
+ // Essentialls lodash/set, but we avoid this dependency
341
+ function setNestedProperty(obj, keyPath, value) {
342
+ const keys = keyPath.split('.');
343
+ let current = obj;
344
+ for (let i = 0; i < keys.length - 1; i++) {
345
+ const key = keys[i];
346
+ if (!(key in current) || typeof current[key] !== 'object' || current[key] === null) {
347
+ current[key] = {};
348
+ }
349
+ current = current[key];
350
+ }
351
+ current[keys[keys.length - 1]] = value;
352
+ }
353
+ function getSortedMessages(messages) {
354
+ return messages.toSorted((messageA, messageB) => {
355
+ const refA = messageA.references?.[0];
356
+ const refB = messageB.references?.[0];
357
+
358
+ // No references: preserve original (extraction) order
359
+ if (!refA || !refB) return 0;
360
+
361
+ // Sort by path, then line. Same path+line: preserve original order
362
+ return compareReferences(refA, refB);
363
+ });
364
+ }
365
+ function localeCompare(a, b) {
366
+ return a.localeCompare(b, 'en');
367
+ }
368
+ function compareReferences(refA, refB) {
369
+ const pathCompare = localeCompare(refA.path, refB.path);
370
+ if (pathCompare !== 0) return pathCompare;
371
+ return (refA.line ?? 0) - (refB.line ?? 0);
372
+ }
373
+ function getDefaultProjectRoot() {
374
+ return process.cwd();
375
+ }
376
+
377
+ class CatalogLocales {
378
+ onChangeCallbacks = (() => new Set())();
379
+ constructor(params) {
380
+ this.messagesDir = params.messagesDir;
381
+ this.sourceLocale = params.sourceLocale;
382
+ this.extension = params.extension;
383
+ this.locales = params.locales;
384
+ }
385
+ async getTargetLocales() {
386
+ if (this.targetLocales) {
387
+ return this.targetLocales;
388
+ }
389
+ if (this.locales === 'infer') {
390
+ this.targetLocales = await this.readTargetLocales();
391
+ } else {
392
+ this.targetLocales = this.locales.filter(locale => locale !== this.sourceLocale);
393
+ }
394
+ return this.targetLocales;
395
+ }
396
+ async readTargetLocales() {
397
+ try {
398
+ const files = await fs__default$1.default.readdir(this.messagesDir);
399
+ return files.filter(file => file.endsWith(this.extension)).map(file => path__default.default.basename(file, this.extension)).filter(locale => locale !== this.sourceLocale);
400
+ } catch {
401
+ return [];
402
+ }
403
+ }
404
+ subscribeLocalesChange(callback) {
405
+ this.onChangeCallbacks.add(callback);
406
+ if (this.locales === 'infer' && !this.watcher) {
407
+ void this.startWatcher();
408
+ }
409
+ }
410
+ unsubscribeLocalesChange(callback) {
411
+ this.onChangeCallbacks.delete(callback);
412
+ if (this.onChangeCallbacks.size === 0) {
413
+ this.stopWatcher();
414
+ }
415
+ }
416
+ async startWatcher() {
417
+ if (this.watcher) {
418
+ return;
419
+ }
420
+ await fs__default$1.default.mkdir(this.messagesDir, {
421
+ recursive: true
422
+ });
423
+ this.watcher = fs__default.default.watch(this.messagesDir, {
424
+ persistent: false,
425
+ recursive: false
426
+ }, (event, filename) => {
427
+ const isCatalogFile = filename != null && filename.endsWith(this.extension) && !filename.includes(path__default.default.sep);
428
+ if (isCatalogFile) {
429
+ void this.onChange();
430
+ }
431
+ });
432
+ }
433
+ stopWatcher() {
434
+ if (this.watcher) {
435
+ this.watcher.close();
436
+ this.watcher = undefined;
437
+ }
438
+ }
439
+ async onChange() {
440
+ const oldLocales = new Set(this.targetLocales || []);
441
+ this.targetLocales = await this.readTargetLocales();
442
+ const newLocalesSet = new Set(this.targetLocales);
443
+ const added = this.targetLocales.filter(locale => !oldLocales.has(locale));
444
+ const removed = Array.from(oldLocales).filter(locale => !newLocalesSet.has(locale));
445
+ if (added.length > 0 || removed.length > 0) {
446
+ for (const callback of this.onChangeCallbacks) {
447
+ callback({
448
+ added,
449
+ removed
450
+ });
451
+ }
452
+ }
453
+ }
454
+ }
455
+
456
+ class CatalogPersister {
457
+ constructor(params) {
458
+ this.messagesPath = params.messagesPath;
459
+ this.codec = params.codec;
460
+ this.extension = params.extension;
461
+ }
462
+ getFileName(locale) {
463
+ return locale + this.extension;
464
+ }
465
+ getFilePath(locale) {
466
+ return path__default.default.join(this.messagesPath, this.getFileName(locale));
467
+ }
468
+ async read(locale) {
469
+ const filePath = this.getFilePath(locale);
470
+ let content;
471
+ try {
472
+ content = await fs__default$1.default.readFile(filePath, 'utf8');
473
+ } catch (error) {
474
+ if (error && typeof error === 'object' && 'code' in error && error.code === 'ENOENT') {
475
+ return [];
476
+ }
477
+ throw new Error(`Error while reading ${this.getFileName(locale)}:\n> ${error}`, {
478
+ cause: error
479
+ });
480
+ }
481
+ try {
482
+ return this.codec.decode(content, {
483
+ locale
484
+ });
485
+ } catch (error) {
486
+ throw new Error(`Error while decoding ${this.getFileName(locale)}:\n> ${error}`, {
487
+ cause: error
488
+ });
489
+ }
490
+ }
491
+ async write(messages, context) {
492
+ const filePath = this.getFilePath(context.locale);
493
+ const content = this.codec.encode(messages, context);
494
+ try {
495
+ const outputDir = path__default.default.dirname(filePath);
496
+ await fs__default$1.default.mkdir(outputDir, {
497
+ recursive: true
498
+ });
499
+ await fs__default$1.default.writeFile(filePath, content);
500
+ } catch (error) {
501
+ console.error(`❌ Failed to write catalog: ${error}`);
502
+ }
503
+ }
504
+ async getLastModified(locale) {
505
+ const filePath = this.getFilePath(locale);
506
+ try {
507
+ const stats = await fs__default$1.default.stat(filePath);
508
+ return stats.mtime;
509
+ } catch {
510
+ return undefined;
511
+ }
512
+ }
513
+ }
514
+
515
+ /**
516
+ * De-duplicates excessive save invocations,
517
+ * while keeping a single one instant.
518
+ */
519
+ class SaveScheduler {
520
+ isSaving = false;
521
+ pendingResolvers = [];
522
+ constructor(delayMs = 50) {
523
+ this.delayMs = delayMs;
524
+ }
525
+ async schedule(saveTask) {
526
+ return new Promise((resolve, reject) => {
527
+ this.pendingResolvers.push({
528
+ resolve,
529
+ reject
530
+ });
531
+ this.nextSaveTask = saveTask;
532
+ if (!this.isSaving && !this.saveTimeout) {
533
+ // Not currently saving and no scheduled save, save immediately
534
+ this.executeSave();
535
+ } else if (this.saveTimeout) {
536
+ // A save is already scheduled, reschedule to debounce
537
+ this.scheduleSave();
538
+ }
539
+ // If isSaving is true and no timeout is scheduled, the current save
540
+ // will check for pending resolvers when it completes and schedule
541
+ // another save if needed (see finally block in executeSave)
542
+ });
543
+ }
544
+ scheduleSave() {
545
+ if (this.saveTimeout) {
546
+ clearTimeout(this.saveTimeout);
547
+ }
548
+ this.saveTimeout = setTimeout(() => {
549
+ this.saveTimeout = undefined;
550
+ this.executeSave();
551
+ }, this.delayMs);
552
+ }
553
+ async executeSave() {
554
+ if (this.isSaving) {
555
+ return;
556
+ }
557
+ const saveTask = this.nextSaveTask;
558
+ if (!saveTask) {
559
+ return;
560
+ }
561
+
562
+ // Capture current pending resolvers for this save
563
+ const resolversForThisSave = this.pendingResolvers;
564
+ this.pendingResolvers = [];
565
+ this.nextSaveTask = undefined;
566
+ this.isSaving = true;
567
+ try {
568
+ const result = await saveTask();
569
+
570
+ // Resolve only the promises that were pending when this save started
571
+ resolversForThisSave.forEach(({
572
+ resolve
573
+ }) => resolve(result));
574
+ } catch (error) {
575
+ // Reject only the promises that were pending when this save started
576
+ resolversForThisSave.forEach(({
577
+ reject
578
+ }) => reject(error));
579
+ } finally {
580
+ this.isSaving = false;
581
+
582
+ // If new saves were requested during this save, schedule another
583
+ if (this.pendingResolvers.length > 0) {
584
+ this.scheduleSave();
585
+ }
586
+ }
587
+ }
588
+ [Symbol.dispose]() {
589
+ if (this.saveTimeout) {
590
+ clearTimeout(this.saveTimeout);
591
+ this.saveTimeout = undefined;
592
+ }
593
+ this.pendingResolvers = [];
594
+ this.nextSaveTask = undefined;
595
+ this.isSaving = false;
596
+ }
597
+ }
598
+
599
+ class CatalogManager {
600
+ /**
601
+ * The source of truth for which messages are used.
602
+ * NOTE: Should be mutated in place to keep `messagesById` and `messagesByFile` in sync.
603
+ */
604
+ messagesByFile = (() => new Map())();
605
+
606
+ /**
607
+ * Fast lookup for messages by ID across all files,
608
+ * contains the same messages as `messagesByFile`.
609
+ * NOTE: Should be mutated in place to keep `messagesById` and `messagesByFile` in sync.
610
+ */
611
+ messagesById = (() => new Map())();
612
+
613
+ /**
614
+ * This potentially also includes outdated ones that were initially available,
615
+ * but are not used anymore. This allows to restore them if they are used again.
616
+ **/
617
+ translationsByTargetLocale = (() => new Map())();
618
+ lastWriteByLocale = (() => new Map())();
619
+
620
+ // Cached instances
621
+
622
+ // Resolves when all catalogs are loaded
623
+
624
+ // Resolves when the initial project scan and processing is complete
625
+
626
+ constructor(config, opts) {
627
+ this.config = config;
628
+ this.saveScheduler = new SaveScheduler(50);
629
+ this.projectRoot = opts.projectRoot ?? getDefaultProjectRoot();
630
+ this.isDevelopment = opts.isDevelopment ?? false;
631
+ this.extractor = opts.extractor;
632
+ if (this.isDevelopment) {
633
+ // We kick this off as early as possible, so we get notified about changes
634
+ // that happen during the initial project scan (while awaiting it to
635
+ // complete though)
636
+ this.sourceWatcher = new SourceFileWatcher(this.getSrcPaths(), this.handleFileEvents.bind(this));
637
+ void this.sourceWatcher.start();
638
+ }
639
+ }
640
+ async getCodec() {
641
+ if (!this.codec) {
642
+ this.codec = await resolveCodec(this.config.messages.format, this.projectRoot);
643
+ }
644
+ return this.codec;
645
+ }
646
+ async getPersister() {
647
+ if (this.persister) {
648
+ return this.persister;
649
+ } else {
650
+ this.persister = new CatalogPersister({
651
+ messagesPath: this.config.messages.path,
652
+ codec: await this.getCodec(),
653
+ extension: getFormatExtension(this.config.messages.format)
654
+ });
655
+ return this.persister;
656
+ }
657
+ }
658
+ getCatalogLocales() {
659
+ if (this.catalogLocales) {
660
+ return this.catalogLocales;
661
+ } else {
662
+ const messagesDir = path__default.default.join(this.projectRoot, this.config.messages.path);
663
+ this.catalogLocales = new CatalogLocales({
664
+ messagesDir,
665
+ sourceLocale: this.config.sourceLocale,
666
+ extension: getFormatExtension(this.config.messages.format),
667
+ locales: this.config.messages.locales
668
+ });
669
+ return this.catalogLocales;
670
+ }
671
+ }
672
+ async getTargetLocales() {
673
+ return this.getCatalogLocales().getTargetLocales();
674
+ }
675
+ getSrcPaths() {
676
+ return (Array.isArray(this.config.srcPath) ? this.config.srcPath : [this.config.srcPath]).map(srcPath => path__default.default.join(this.projectRoot, srcPath));
677
+ }
678
+ async loadMessages() {
679
+ const sourceDiskMessages = await this.loadSourceMessages();
680
+ this.loadCatalogsPromise = this.loadTargetMessages();
681
+ await this.loadCatalogsPromise;
682
+ this.scanCompletePromise = (async () => {
683
+ const sourceFiles = await SourceFileScanner.getSourceFiles(this.getSrcPaths());
684
+ await Promise.all(Array.from(sourceFiles).map(async filePath => this.processFile(filePath)));
685
+ this.mergeSourceDiskMetadata(sourceDiskMessages);
686
+ })();
687
+ await this.scanCompletePromise;
688
+ if (this.isDevelopment) {
689
+ const catalogLocales = this.getCatalogLocales();
690
+ catalogLocales.subscribeLocalesChange(this.onLocalesChange);
691
+ }
692
+ }
693
+ async loadSourceMessages() {
694
+ // Load source catalog to hydrate metadata (e.g. flags) later without
695
+ // treating catalog entries as source of truth.
696
+ const diskMessages = await this.loadLocaleMessages(this.config.sourceLocale);
697
+ const byId = new Map();
698
+ for (const diskMessage of diskMessages) {
699
+ byId.set(diskMessage.id, diskMessage);
700
+ }
701
+ return byId;
702
+ }
703
+ async loadLocaleMessages(locale) {
704
+ const persister = await this.getPersister();
705
+ const messages = await persister.read(locale);
706
+ const fileTime = await persister.getLastModified(locale);
707
+ this.lastWriteByLocale.set(locale, fileTime);
708
+ return messages;
709
+ }
710
+ async loadTargetMessages() {
711
+ const targetLocales = await this.getTargetLocales();
712
+ await Promise.all(targetLocales.map(locale => this.reloadLocaleCatalog(locale)));
713
+ }
714
+ async reloadLocaleCatalog(locale) {
715
+ const diskMessages = await this.loadLocaleMessages(locale);
716
+ if (locale === this.config.sourceLocale) {
717
+ // For source: Merge additional properties like flags
718
+ for (const diskMessage of diskMessages) {
719
+ const prev = this.messagesById.get(diskMessage.id);
720
+ if (prev) {
721
+ // Mutate the existing object instead of creating a copy
722
+ // to keep messagesById and messagesByFile in sync.
723
+ // Unknown properties (like flags): disk wins
724
+ // Known properties: existing (from extraction) wins
725
+ for (const key of Object.keys(diskMessage)) {
726
+ if (!['id', 'message', 'description', 'references'].includes(key)) {
727
+ // For unknown properties (like flags), disk wins
728
+ prev[key] = diskMessage[key];
729
+ }
730
+ }
731
+ }
732
+ }
733
+ } else {
734
+ // For target: disk wins completely, BUT preserve existing translations
735
+ // if we read empty (likely a write in progress by an external tool
736
+ // that causes the file to temporarily be empty)
737
+ const existingTranslations = this.translationsByTargetLocale.get(locale);
738
+ const hasExistingTranslations = existingTranslations && existingTranslations.size > 0;
739
+ if (diskMessages.length > 0) {
740
+ // We got content from disk, replace with it
741
+ const translations = new Map();
742
+ for (const message of diskMessages) {
743
+ translations.set(message.id, message);
744
+ }
745
+ this.translationsByTargetLocale.set(locale, translations);
746
+ } else if (hasExistingTranslations) ; else {
747
+ // We read empty and have no existing translations
748
+ const translations = new Map();
749
+ this.translationsByTargetLocale.set(locale, translations);
750
+ }
751
+ }
752
+ }
753
+ mergeSourceDiskMetadata(diskMessages) {
754
+ for (const [id, diskMessage] of diskMessages) {
755
+ const existing = this.messagesById.get(id);
756
+ if (!existing) continue;
757
+
758
+ // Mutate the existing object instead of creating a copy.
759
+ // This keeps `messagesById` and `messagesByFile` in sync since
760
+ // they reference the same object instance.
761
+ for (const key of Object.keys(diskMessage)) {
762
+ if (existing[key] == null) {
763
+ existing[key] = diskMessage[key];
764
+ }
765
+ }
766
+ }
767
+ }
768
+ async processFile(absoluteFilePath) {
769
+ let messages = [];
770
+ try {
771
+ const content = await fs__default$1.default.readFile(absoluteFilePath, 'utf8');
772
+ let extraction;
773
+ try {
774
+ extraction = await this.extractor.extract(absoluteFilePath, content);
775
+ } catch {
776
+ return false;
777
+ }
778
+ messages = extraction.messages;
779
+ } catch (err) {
780
+ if (err.code !== 'ENOENT') {
781
+ throw err;
782
+ }
783
+ // ENOENT -> treat as no messages
784
+ }
785
+ const prevFileMessages = this.messagesByFile.get(absoluteFilePath);
786
+ const relativeFilePath = path__default.default.relative(this.projectRoot, absoluteFilePath);
787
+
788
+ // Init with all previous ones
789
+ const idsToRemove = Array.from(prevFileMessages?.keys() ?? []);
790
+
791
+ // Replace existing messages with new ones
792
+ const fileMessages = new Map();
793
+ for (let message of messages) {
794
+ const prevMessage = this.messagesById.get(message.id);
795
+
796
+ // Merge with previous message if it exists
797
+ if (prevMessage) {
798
+ message = {
799
+ ...message
800
+ };
801
+ if (message.references) {
802
+ message.references = this.mergeReferences(prevMessage.references ?? [], relativeFilePath, message.references);
803
+ }
804
+
805
+ // Merge other properties like description, or unknown
806
+ // attributes like flags that are opaque to us
807
+ for (const key of Object.keys(prevMessage)) {
808
+ if (message[key] == null) {
809
+ message[key] = prevMessage[key];
810
+ }
811
+ }
812
+ }
813
+ this.messagesById.set(message.id, message);
814
+ fileMessages.set(message.id, message);
815
+
816
+ // This message continues to exist in this file
817
+ const index = idsToRemove.indexOf(message.id);
818
+ if (index !== -1) idsToRemove.splice(index, 1);
819
+ }
820
+
821
+ // Clean up removed messages from `messagesById`
822
+ idsToRemove.forEach(id => {
823
+ const message = this.messagesById.get(id);
824
+ if (!message) return;
825
+ const hasOtherReferences = message.references?.some(ref => ref.path !== relativeFilePath);
826
+ if (!hasOtherReferences) {
827
+ // No other references, delete the message entirely
828
+ this.messagesById.delete(id);
829
+ } else {
830
+ // Message is used elsewhere, remove this file from references
831
+ // Mutate the existing object to keep `messagesById` and `messagesByFile` in sync
832
+ message.references = message.references?.filter(ref => ref.path !== relativeFilePath);
833
+ }
834
+ });
835
+
836
+ // Update the stored messages
837
+ if (messages.length > 0) {
838
+ this.messagesByFile.set(absoluteFilePath, fileMessages);
839
+ } else {
840
+ this.messagesByFile.delete(absoluteFilePath);
841
+ }
842
+ const changed = this.haveMessagesChangedForFile(prevFileMessages, fileMessages);
843
+ return changed;
844
+ }
845
+ mergeReferences(existing, currentFilePath, currentFileRefs) {
846
+ // Keep refs from other files, replace all refs from the current file
847
+ const otherFileRefs = existing.filter(ref => ref.path !== currentFilePath);
848
+ const merged = [...otherFileRefs, ...currentFileRefs];
849
+ return merged.sort(compareReferences);
850
+ }
851
+ haveMessagesChangedForFile(beforeMessages, afterMessages) {
852
+ // If one exists and the other doesn't, there's a change
853
+ if (!beforeMessages) {
854
+ return afterMessages.size > 0;
855
+ }
856
+
857
+ // Different sizes means changes
858
+ if (beforeMessages.size !== afterMessages.size) {
859
+ return true;
860
+ }
861
+
862
+ // Check differences in beforeMessages vs afterMessages
863
+ for (const [id, msg1] of beforeMessages) {
864
+ const msg2 = afterMessages.get(id);
865
+ if (!msg2 || !this.areMessagesEqual(msg1, msg2)) {
866
+ return true; // Early exit on first difference
867
+ }
868
+ }
869
+ return false;
870
+ }
871
+ areMessagesEqual(msg1, msg2) {
872
+ // Note: We intentionally don't compare references here.
873
+ // References are aggregated metadata from multiple files and comparing
874
+ // them would cause false positives due to parallel extraction order.
875
+ return msg1.id === msg2.id && msg1.message === msg2.message && msg1.description === msg2.description;
876
+ }
877
+ async save() {
878
+ return this.saveScheduler.schedule(() => this.saveImpl());
879
+ }
880
+ async saveImpl() {
881
+ await this.saveLocale(this.config.sourceLocale);
882
+ const targetLocales = await this.getTargetLocales();
883
+ await Promise.all(targetLocales.map(locale => this.saveLocale(locale)));
884
+ }
885
+ async saveLocale(locale) {
886
+ await this.loadCatalogsPromise;
887
+ const messages = Array.from(this.messagesById.values());
888
+ const persister = await this.getPersister();
889
+ const isSourceLocale = locale === this.config.sourceLocale;
890
+
891
+ // Check if file was modified externally (poll-at-save is cheaper than
892
+ // watchers here since stat() is fast and avoids continuous overhead)
893
+ const lastWriteTime = this.lastWriteByLocale.get(locale);
894
+ const currentFileTime = await persister.getLastModified(locale);
895
+ if (currentFileTime && lastWriteTime && currentFileTime > lastWriteTime) {
896
+ await this.reloadLocaleCatalog(locale);
897
+ }
898
+ const localeMessages = isSourceLocale ? this.messagesById : this.translationsByTargetLocale.get(locale);
899
+ const messagesToPersist = messages.map(message => {
900
+ const localeMessage = localeMessages?.get(message.id);
901
+ return {
902
+ ...localeMessage,
903
+ id: message.id,
904
+ description: message.description,
905
+ references: message.references,
906
+ message: isSourceLocale ? message.message : localeMessage?.message ?? ''
907
+ };
908
+ });
909
+ await persister.write(messagesToPersist, {
910
+ locale,
911
+ sourceMessagesById: this.messagesById
912
+ });
913
+
914
+ // Update timestamps
915
+ const newTime = await persister.getLastModified(locale);
916
+ this.lastWriteByLocale.set(locale, newTime);
917
+ }
918
+ onLocalesChange = async params => {
919
+ // Chain to existing promise
920
+ this.loadCatalogsPromise = Promise.all([this.loadCatalogsPromise, ...params.added.map(locale => this.reloadLocaleCatalog(locale))]);
921
+ for (const locale of params.added) {
922
+ await this.saveLocale(locale);
923
+ }
924
+ for (const locale of params.removed) {
925
+ this.translationsByTargetLocale.delete(locale);
926
+ this.lastWriteByLocale.delete(locale);
927
+ }
928
+ };
929
+ async handleFileEvents(events) {
930
+ if (this.loadCatalogsPromise) {
931
+ await this.loadCatalogsPromise;
932
+ }
933
+
934
+ // Wait for initial scan to complete to avoid race conditions
935
+ if (this.scanCompletePromise) {
936
+ await this.scanCompletePromise;
937
+ }
938
+ let changed = false;
939
+ const expandedEvents = await this.sourceWatcher.expandDirectoryDeleteEvents(events, Array.from(this.messagesByFile.keys()));
940
+ for (const event of expandedEvents) {
941
+ const hasChanged = await this.processFile(event.path);
942
+ changed ||= hasChanged;
943
+ }
944
+ if (changed) {
945
+ await this.save();
946
+ }
947
+ }
948
+ [Symbol.dispose]() {
949
+ this.sourceWatcher?.stop();
950
+ this.sourceWatcher = undefined;
951
+ this.saveScheduler[Symbol.dispose]();
952
+ if (this.catalogLocales && this.isDevelopment) {
953
+ this.catalogLocales.unsubscribeLocalesChange(this.onLocalesChange);
954
+ }
955
+ }
956
+ }
957
+
958
+ class LRUCache {
959
+ constructor(maxSize) {
960
+ this.maxSize = maxSize;
961
+ this.cache = new Map();
962
+ }
963
+ set(key, value) {
964
+ const isNewKey = !this.cache.has(key);
965
+ if (isNewKey && this.cache.size >= this.maxSize) {
966
+ const lruKey = this.cache.keys().next().value;
967
+ if (lruKey !== undefined) {
968
+ this.cache.delete(lruKey);
969
+ }
970
+ }
971
+ this.cache.set(key, {
972
+ key,
973
+ value
974
+ });
975
+ }
976
+ get(key) {
977
+ const item = this.cache.get(key);
978
+ if (item) {
979
+ this.cache.delete(key);
980
+ this.cache.set(key, item);
981
+ return item.value;
982
+ }
983
+ return undefined;
984
+ }
985
+ }
986
+
987
+ const require$1 = module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('plugin-B0KIcFlc.cjs', document.baseURI).href)));
988
+ class MessageExtractor {
989
+ compileCache = (() => new LRUCache(750))();
990
+ constructor(opts) {
991
+ this.isDevelopment = opts.isDevelopment ?? false;
992
+ this.projectRoot = opts.projectRoot ?? getDefaultProjectRoot();
993
+ this.sourceMap = opts.sourceMap ?? false;
994
+ }
995
+ async extract(absoluteFilePath, source) {
996
+ const cacheKey = [source, absoluteFilePath].join('!');
997
+ const cached = this.compileCache.get(cacheKey);
998
+ if (cached) return cached;
999
+
1000
+ // Shortcut parsing if hook is not used. The Turbopack integration already
1001
+ // pre-filters this, but for webpack this feature doesn't exist, so we need
1002
+ // to do it here.
1003
+ if (!source.includes('useExtracted') && !source.includes('getExtracted')) {
1004
+ return {
1005
+ messages: [],
1006
+ code: source
1007
+ };
1008
+ }
1009
+ const filePath = path__default.default.relative(this.projectRoot, absoluteFilePath);
1010
+ const result = await core.transform(source, {
1011
+ jsc: {
1012
+ target: 'esnext',
1013
+ parser: {
1014
+ syntax: 'typescript',
1015
+ tsx: true,
1016
+ decorators: true
1017
+ },
1018
+ experimental: {
1019
+ cacheRoot: 'node_modules/.cache/swc',
1020
+ disableBuiltinTransformsForInternalTesting: true,
1021
+ disableAllLints: true,
1022
+ plugins: [[require$1.resolve('next-intl-swc-plugin-extractor'), {
1023
+ isDevelopment: this.isDevelopment,
1024
+ filePath
1025
+ }]]
1026
+ }
1027
+ },
1028
+ sourceMaps: this.sourceMap,
1029
+ sourceFileName: filePath,
1030
+ filename: filePath
1031
+ });
1032
+
1033
+ // TODO: Improve the typing of @swc/core
1034
+ const output = result.output;
1035
+ const messages = JSON.parse(JSON.parse(output).results);
1036
+ const extractionResult = {
1037
+ code: result.code,
1038
+ map: result.map,
1039
+ messages
1040
+ };
1041
+ this.compileCache.set(cacheKey, extractionResult);
1042
+ return extractionResult;
1043
+ }
1044
+ }
1045
+
1046
+ class ExtractionCompiler {
1047
+ constructor(config, opts = {}) {
1048
+ const extractor = opts.extractor ?? new MessageExtractor(opts);
1049
+ this.manager = new CatalogManager(config, {
1050
+ ...opts,
1051
+ extractor
1052
+ });
1053
+ this[Symbol.dispose] = this[Symbol.dispose].bind(this);
1054
+ this.installExitHandlers();
1055
+ }
1056
+ async extractAll() {
1057
+ // We can't rely on all files being compiled (e.g. due to persistent
1058
+ // caching), so loading the messages initially is necessary.
1059
+ await this.manager.loadMessages();
1060
+ await this.manager.save();
1061
+ }
1062
+ [Symbol.dispose]() {
1063
+ this.uninstallExitHandlers();
1064
+ this.manager[Symbol.dispose]();
1065
+ }
1066
+ installExitHandlers() {
1067
+ const cleanup = this[Symbol.dispose];
1068
+ process.on('exit', cleanup);
1069
+ process.on('SIGINT', cleanup);
1070
+ process.on('SIGTERM', cleanup);
1071
+ }
1072
+ uninstallExitHandlers() {
1073
+ const cleanup = this[Symbol.dispose];
1074
+ process.off('exit', cleanup);
1075
+ process.off('SIGINT', cleanup);
1076
+ process.off('SIGTERM', cleanup);
1077
+ }
1078
+ }
1079
+
1080
+ // Single compiler instance, initialized once per process
1081
+ let compiler;
1082
+ const runOnce = once('_NEXT_INTL_EXTRACT');
1083
+ function initExtractionCompiler(pluginConfig) {
1084
+ const experimental = pluginConfig.experimental;
1085
+ if (!experimental?.extract) {
1086
+ return;
1087
+ }
1088
+
1089
+ // Avoid rollup's `replace` plugin to compile this away
1090
+ const isDevelopment = process.env['NODE_ENV'.trim()] === 'development';
1091
+
1092
+ // Avoid running for:
1093
+ // - info
1094
+ // - start
1095
+ // - typegen
1096
+ //
1097
+ // Doesn't consult Next.js config anyway:
1098
+ // - telemetry
1099
+ // - lint
1100
+ //
1101
+ // What remains are:
1102
+ // - dev (NODE_ENV=development)
1103
+ // - build (NODE_ENV=production)
1104
+ const shouldRun = isDevelopment || process.argv.includes('build');
1105
+ if (!shouldRun) return;
1106
+ runOnce(() => {
1107
+ const extractorConfig = {
1108
+ srcPath: experimental.srcPath,
1109
+ sourceLocale: experimental.extract.sourceLocale,
1110
+ messages: experimental.messages
1111
+ };
1112
+ compiler = new ExtractionCompiler(extractorConfig, {
1113
+ isDevelopment,
1114
+ projectRoot: process.cwd()
1115
+ });
1116
+
1117
+ // Fire-and-forget: Start extraction, don't block config return.
1118
+ // In dev mode, this also starts the file watcher.
1119
+ // In prod, ideally we would wait until the extraction is complete,
1120
+ // but we can't `await` anywhere (at least for Turbopack).
1121
+ // The result is ok though, as if we encounter untranslated messages,
1122
+ // we'll simply add empty messages to the catalog. So for actually
1123
+ // running the app, there is no difference.
1124
+ compiler.extractAll();
1125
+ function cleanup() {
1126
+ if (compiler) {
1127
+ compiler[Symbol.dispose]();
1128
+ compiler = undefined;
1129
+ }
1130
+ }
1131
+ process.on('exit', cleanup);
1132
+ process.on('SIGINT', cleanup);
1133
+ process.on('SIGTERM', cleanup);
1134
+ });
1135
+ }
1136
+
1137
+ function getCurrentVersion() {
1138
+ try {
1139
+ const require$1 = module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('plugin-B0KIcFlc.cjs', document.baseURI).href)));
1140
+ const pkg = require$1('next/package.json');
1141
+ return pkg.version;
1142
+ } catch (error) {
1143
+ throw new Error('Failed to get current Next.js version. This can happen if next-intl/plugin is imported into your app code outside of your next.config.js.', {
1144
+ cause: error
1145
+ });
1146
+ }
1147
+ }
1148
+ function compareVersions(version1, version2) {
1149
+ const v1Parts = version1.split('.').map(Number);
1150
+ const v2Parts = version2.split('.').map(Number);
1151
+ for (let i = 0; i < 3; i++) {
1152
+ const v1 = v1Parts[i] || 0;
1153
+ const v2 = v2Parts[i] || 0;
1154
+ if (v1 > v2) return 1;
1155
+ if (v1 < v2) return -1;
1156
+ }
1157
+ return 0;
1158
+ }
1159
+ function hasStableTurboConfig() {
1160
+ return compareVersions(getCurrentVersion(), '15.3.0') >= 0;
1161
+ }
1162
+ function isNextJs16OrHigher() {
1163
+ return compareVersions(getCurrentVersion(), '16.0.0') >= 0;
1164
+ }
1165
+
1166
+ function withExtensions(localPath) {
1167
+ return [`${localPath}.ts`, `${localPath}.tsx`, `${localPath}.js`, `${localPath}.jsx`];
1168
+ }
1169
+ function resolveI18nPath(providedPath, cwd) {
1170
+ function resolvePath(pathname) {
1171
+ const parts = [];
1172
+ if (cwd) parts.push(cwd);
1173
+ parts.push(pathname);
1174
+ return path__default.default.resolve(...parts);
1175
+ }
1176
+ function pathExists(pathname) {
1177
+ return fs__default.default.existsSync(resolvePath(pathname));
1178
+ }
1179
+ if (providedPath) {
1180
+ if (!pathExists(providedPath)) {
1181
+ throwError(`Could not find i18n config at ${providedPath}, please provide a valid path.`);
1182
+ }
1183
+ return providedPath;
1184
+ } else {
1185
+ for (const candidate of [...withExtensions('./i18n/request'), ...withExtensions('./src/i18n/request')]) {
1186
+ if (pathExists(candidate)) {
1187
+ return candidate;
1188
+ }
1189
+ }
1190
+ throwError(`Could not locate request configuration module.\n\nThis path is supported by default: ./(src/)i18n/request.{js,jsx,ts,tsx}\n\nAlternatively, you can specify a custom location in your Next.js config:\n\nconst withNextIntl = createNextIntlPlugin(
1191
+
1192
+ Alternatively, you can specify a custom location in your Next.js config:
1193
+
1194
+ const withNextIntl = createNextIntlPlugin(
1195
+ './path/to/i18n/request.tsx'
1196
+ );`);
1197
+ }
1198
+ }
1199
+ function getNextConfig(pluginConfig, nextConfig) {
1200
+ const useTurbo = process.env.TURBOPACK != null;
1201
+ const nextIntlConfig = {};
1202
+ function getExtractMessagesLoaderConfig() {
1203
+ const experimental = pluginConfig.experimental;
1204
+ if (!experimental.srcPath || !pluginConfig.experimental?.messages) {
1205
+ throwError('`srcPath` and `messages` are required when using `extractor`.');
1206
+ }
1207
+ return {
1208
+ loader: 'next-intl/extractor/extractionLoader',
1209
+ options: {
1210
+ srcPath: experimental.srcPath,
1211
+ sourceLocale: experimental.extract.sourceLocale,
1212
+ messages: pluginConfig.experimental.messages
1213
+ }
1214
+ };
1215
+ }
1216
+ function getCatalogLoaderConfig() {
1217
+ return {
1218
+ loader: 'next-intl/extractor/catalogLoader',
1219
+ options: {
1220
+ messages: pluginConfig.experimental.messages
1221
+ }
1222
+ };
1223
+ }
1224
+ function getTurboRules() {
1225
+ return nextConfig?.turbopack?.rules ||
1226
+ // @ts-expect-error -- For Next.js <16
1227
+ nextConfig?.experimental?.turbo?.rules || {};
1228
+ }
1229
+ function addTurboRule(rules, glob, rule) {
1230
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1231
+ if (rules[glob]) {
1232
+ if (Array.isArray(rules[glob])) {
1233
+ rules[glob].push(rule);
1234
+ } else {
1235
+ rules[glob] = [rules[glob], rule];
1236
+ }
1237
+ } else {
1238
+ rules[glob] = rule;
1239
+ }
1240
+ }
1241
+ if (useTurbo) {
1242
+ if (pluginConfig.requestConfig && path__default.default.isAbsolute(pluginConfig.requestConfig)) {
1243
+ throwError("Turbopack support for next-intl currently does not support absolute paths, please provide a relative one (e.g. './src/i18n/config.ts').\n\nFound: " + pluginConfig.requestConfig);
1244
+ }
1245
+
1246
+ // Assign alias for `next-intl/config`
1247
+ const resolveAlias = {
1248
+ // Turbo aliases don't work with absolute
1249
+ // paths (see error handling above)
1250
+ 'next-intl/config': resolveI18nPath(pluginConfig.requestConfig)
1251
+ };
1252
+
1253
+ // Add loaders
1254
+ let rules;
1255
+
1256
+ // Add loader for extractor
1257
+ if (pluginConfig.experimental?.extract) {
1258
+ if (!isNextJs16OrHigher()) {
1259
+ throwError('Message extraction requires Next.js 16 or higher.');
1260
+ }
1261
+ rules ??= getTurboRules();
1262
+ const srcPaths = (Array.isArray(pluginConfig.experimental.srcPath) ? pluginConfig.experimental.srcPath : [pluginConfig.experimental.srcPath]).map(srcPath => srcPath.endsWith('/') ? srcPath.slice(0, -1) : srcPath);
1263
+ addTurboRule(rules, `*.{${SourceFileFilter.EXTENSIONS.join(',')}}`, {
1264
+ loaders: [getExtractMessagesLoaderConfig()],
1265
+ condition: {
1266
+ // Note: We don't need `not: 'foreign'`, because this is
1267
+ // implied by the filter based on `srcPath`.
1268
+ path: `{${srcPaths.join(',')}}` + '/**/*',
1269
+ content: /(useExtracted|getExtracted)/
1270
+ }
1271
+ });
1272
+ }
1273
+
1274
+ // Add loader for catalog
1275
+ if (pluginConfig.experimental?.messages) {
1276
+ if (!isNextJs16OrHigher()) {
1277
+ throwError('Message catalog loading requires Next.js 16 or higher.');
1278
+ }
1279
+ rules ??= getTurboRules();
1280
+ const extension = getFormatExtension(pluginConfig.experimental.messages.format);
1281
+ addTurboRule(rules, `*${extension}`, {
1282
+ loaders: [getCatalogLoaderConfig()],
1283
+ condition: {
1284
+ path: `${pluginConfig.experimental.messages.path}/**/*`
1285
+ },
1286
+ as: '*.js'
1287
+ });
1288
+ }
1289
+ if (hasStableTurboConfig() &&
1290
+ // @ts-expect-error -- For Next.js <16
1291
+ !nextConfig?.experimental?.turbo) {
1292
+ nextIntlConfig.turbopack = {
1293
+ ...nextConfig?.turbopack,
1294
+ ...(rules && {
1295
+ rules
1296
+ }),
1297
+ resolveAlias: {
1298
+ ...nextConfig?.turbopack?.resolveAlias,
1299
+ ...resolveAlias
1300
+ }
1301
+ };
1302
+ } else {
1303
+ nextIntlConfig.experimental = {
1304
+ ...nextConfig?.experimental,
1305
+ // @ts-expect-error -- For Next.js <16
1306
+ turbo: {
1307
+ // @ts-expect-error -- For Next.js <16
1308
+ ...nextConfig?.experimental?.turbo,
1309
+ ...(rules && {
1310
+ rules
1311
+ }),
1312
+ resolveAlias: {
1313
+ // @ts-expect-error -- For Next.js <16
1314
+ ...nextConfig?.experimental?.turbo?.resolveAlias,
1315
+ ...resolveAlias
1316
+ }
1317
+ }
1318
+ };
1319
+ }
1320
+ } else {
1321
+ nextIntlConfig.webpack = function webpack(config, context) {
1322
+ if (!config.resolve) config.resolve = {};
1323
+ if (!config.resolve.alias) config.resolve.alias = {};
1324
+
1325
+ // Assign alias for `next-intl/config`
1326
+ // (Webpack requires absolute paths)
1327
+ config.resolve.alias['next-intl/config'] = path__default.default.resolve(config.context, resolveI18nPath(pluginConfig.requestConfig, config.context));
1328
+
1329
+ // Add loader for extractor
1330
+ if (pluginConfig.experimental?.extract) {
1331
+ if (!config.module) config.module = {};
1332
+ if (!config.module.rules) config.module.rules = [];
1333
+ const srcPath = pluginConfig.experimental.srcPath;
1334
+ config.module.rules.push({
1335
+ test: new RegExp(`\\.(${SourceFileFilter.EXTENSIONS.join('|')})$`),
1336
+ include: Array.isArray(srcPath) ? srcPath.map(cur => path__default.default.resolve(config.context, cur)) : path__default.default.resolve(config.context, srcPath || ''),
1337
+ use: [getExtractMessagesLoaderConfig()]
1338
+ });
1339
+ }
1340
+
1341
+ // Add loader for catalog
1342
+ if (pluginConfig.experimental?.messages) {
1343
+ if (!config.module) config.module = {};
1344
+ if (!config.module.rules) config.module.rules = [];
1345
+ const extension = getFormatExtension(pluginConfig.experimental.messages.format);
1346
+ config.module.rules.push({
1347
+ test: new RegExp(`${extension.replace(/\./g, '\\.')}$`),
1348
+ include: path__default.default.resolve(config.context, pluginConfig.experimental.messages.path),
1349
+ use: [getCatalogLoaderConfig()],
1350
+ type: 'javascript/auto'
1351
+ });
1352
+ }
1353
+ if (typeof nextConfig?.webpack === 'function') {
1354
+ return nextConfig.webpack(config, context);
1355
+ }
1356
+ return config;
1357
+ };
1358
+ }
1359
+
1360
+ // Forward config
1361
+ if (nextConfig?.trailingSlash) {
1362
+ nextIntlConfig.env = {
1363
+ ...nextConfig.env,
1364
+ _next_intl_trailing_slash: 'true'
1365
+ };
1366
+ }
1367
+ return Object.assign({}, nextConfig, nextIntlConfig);
1368
+ }
1369
+
1370
+ function initPlugin(pluginConfig, nextConfig) {
1371
+ if (nextConfig?.i18n != null) {
1372
+ warn("An `i18n` property was found in your Next.js config. This likely causes conflicts and should therefore be removed if you use the App Router.\n\nIf you're in progress of migrating from the Pages Router, you can refer to this example: https://next-intl.dev/examples#app-router-migration\n");
1373
+ }
1374
+ const messagesPathOrPaths = pluginConfig.experimental?.createMessagesDeclaration;
1375
+ if (messagesPathOrPaths) {
1376
+ createMessagesDeclaration(typeof messagesPathOrPaths === 'string' ? [messagesPathOrPaths] : messagesPathOrPaths);
1377
+ }
1378
+ initExtractionCompiler(pluginConfig);
1379
+ return getNextConfig(pluginConfig, nextConfig);
1380
+ }
1381
+ function createNextIntlPlugin(i18nPathOrConfig = {}) {
1382
+ const config = typeof i18nPathOrConfig === 'string' ? {
1383
+ requestConfig: i18nPathOrConfig
1384
+ } : i18nPathOrConfig;
1385
+ return function withNextIntl(nextConfig) {
1386
+ return initPlugin(config, nextConfig);
1387
+ };
1388
+ }
1389
+
1390
+ exports.createNextIntlPlugin = createNextIntlPlugin;
1391
+ exports.getSortedMessages = getSortedMessages;
1392
+ exports.setNestedProperty = setNestedProperty;