diff --git a/packages/next-intl/src/plugin/createMessagesDeclaration.tsx b/packages/next-intl/src/plugin/createMessagesDeclaration.tsx index 692097da9..386db0b35 100644 --- a/packages/next-intl/src/plugin/createMessagesDeclaration.tsx +++ b/packages/next-intl/src/plugin/createMessagesDeclaration.tsx @@ -11,31 +11,35 @@ function runOnce(fn: () => void) { fn(); } -export default function createMessagesDeclaration(messagesPath: string) { - const fullPath = path.resolve(messagesPath); - - if (!fs.existsSync(fullPath)) { - throwError( - `\`createMessagesDeclaration\` points to a non-existent file: ${fullPath}` - ); - } - if (!fullPath.endsWith('.json')) { - throwError( - `\`createMessagesDeclaration\` needs to point to a JSON file. Received: ${fullPath}` - ); - } - - // Keep this as a runtime check and don't replace - // this with a constant during the build process - const env = process.env['NODE_ENV'.trim()]; - +export default function createMessagesDeclaration( + messagesPaths: Array +) { // Next.js can call the Next.js config multiple // times - ensure we only run once. runOnce(() => { - compileDeclaration(messagesPath); + for (const messagesPath of messagesPaths) { + const fullPath = path.resolve(messagesPath); + + if (!fs.existsSync(fullPath)) { + throwError( + `\`createMessagesDeclaration\` points to a non-existent file: ${fullPath}` + ); + } + if (!fullPath.endsWith('.json')) { + throwError( + `\`createMessagesDeclaration\` needs to point to a JSON file. Received: ${fullPath}` + ); + } + + // Keep this as a runtime check and don't replace + // this with a constant during the build process + const env = process.env['NODE_ENV'.trim()]; + + compileDeclaration(messagesPath); - if (env === 'development') { - startWatching(messagesPath); + if (env === 'development') { + startWatching(messagesPath); + } } }); } diff --git a/packages/next-intl/src/plugin/createNextIntlPlugin.tsx b/packages/next-intl/src/plugin/createNextIntlPlugin.tsx index bc60f83fb..62a6af878 100644 --- a/packages/next-intl/src/plugin/createNextIntlPlugin.tsx +++ b/packages/next-intl/src/plugin/createNextIntlPlugin.tsx @@ -14,9 +14,13 @@ function initPlugin( ); } - if (pluginConfig.experimental?.createMessagesDeclaration) { + const messagesPathOrPaths = + pluginConfig.experimental?.createMessagesDeclaration; + if (messagesPathOrPaths) { createMessagesDeclaration( - pluginConfig.experimental.createMessagesDeclaration + typeof messagesPathOrPaths === 'string' + ? [messagesPathOrPaths] + : messagesPathOrPaths ); } diff --git a/packages/next-intl/src/plugin/types.tsx b/packages/next-intl/src/plugin/types.tsx index 915461238..24e5cd7c8 100644 --- a/packages/next-intl/src/plugin/types.tsx +++ b/packages/next-intl/src/plugin/types.tsx @@ -1,6 +1,7 @@ export type PluginConfig = { requestConfig?: string; experimental?: { - createMessagesDeclaration?: string; + /** A path to the messages file that you'd like to create a declaration for. In case you want to consider multiple files, you can pass an array of paths. */ + createMessagesDeclaration?: string | Array; }; };