Skip to content

Commit

Permalink
feat: change createMessagesDeclaration to an array for monorepo support
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-quotez committed Feb 5, 2025
1 parent c4c5986 commit afff80e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
4 changes: 2 additions & 2 deletions docs/src/pages/docs/workflows/typescript.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,15 @@ TypeScript currently has a [limitation](https://github.com/microsoft/TypeScript/
}
```

2. Configure the `createMessagesDeclaration` setting in your Next.js config:
2. Configure the `createMessagesDeclarations` setting in your Next.js config:

```tsx filename="next.config.mjs"
import {createNextIntlPlugin} from 'next-intl/plugin';

const withNextIntl = createNextIntlPlugin({
experimental: {
// Provide the path to the messages that you're using in `AppConfig`
createMessagesDeclaration: './messages/en.json'
createMessagesDeclarations: ['./messages/en.json']
}
// ...
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 createMessagesDeclarations(
messagesPaths: Array<string>
) {
// 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);
}
}
});
}
Expand Down
8 changes: 4 additions & 4 deletions packages/next-intl/src/plugin/createNextIntlPlugin.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {NextConfig} from 'next';
import createMessagesDeclaration from './createMessagesDeclaration.js';
import createMessagesDeclarations from './createMessagesDeclarations.js';
import getNextConfig from './getNextConfig.js';
import type {PluginConfig} from './types.js';
import {warn} from './utils.js';
Expand All @@ -14,9 +14,9 @@ function initPlugin(
);
}

if (pluginConfig.experimental?.createMessagesDeclaration) {
createMessagesDeclaration(
pluginConfig.experimental.createMessagesDeclaration
if (pluginConfig.experimental?.createMessagesDeclarations) {
createMessagesDeclarations(
pluginConfig.experimental.createMessagesDeclarations
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/next-intl/src/plugin/types.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type PluginConfig = {
requestConfig?: string;
experimental?: {
createMessagesDeclaration?: string;
createMessagesDeclarations?: Array<string>;
};
};

0 comments on commit afff80e

Please sign in to comment.