Skip to content

Commit ca3fcd5

Browse files
felix-quotezamannn
andauthored
feat: change createMessagesDeclaration to also accept array for monorepo support (#1700)
Resolves: #1699 This change renames "createMessagesDeclaration" to "createMessagesDeclarations" and changes the type to an array. Each entry of the array will be used to create message declaration files. Supporting multiple message files helps to use next-intl in monorepo setups. Since v4 is in beta, I renamed the property and removed support for a single string. To me, clean types lead to cleaner code. If compatibility is a more of a concern during the beta phase, I could not change the property name and keep the support for single strings. Please let me know. --------- Co-authored-by: Jan Amann <jan@amann.work>
1 parent c4c5986 commit ca3fcd5

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

packages/next-intl/src/plugin/createMessagesDeclaration.tsx

+25-21
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,35 @@ function runOnce(fn: () => void) {
1111
fn();
1212
}
1313

14-
export default function createMessagesDeclaration(messagesPath: string) {
15-
const fullPath = path.resolve(messagesPath);
16-
17-
if (!fs.existsSync(fullPath)) {
18-
throwError(
19-
`\`createMessagesDeclaration\` points to a non-existent file: ${fullPath}`
20-
);
21-
}
22-
if (!fullPath.endsWith('.json')) {
23-
throwError(
24-
`\`createMessagesDeclaration\` needs to point to a JSON file. Received: ${fullPath}`
25-
);
26-
}
27-
28-
// Keep this as a runtime check and don't replace
29-
// this with a constant during the build process
30-
const env = process.env['NODE_ENV'.trim()];
31-
14+
export default function createMessagesDeclaration(
15+
messagesPaths: Array<string>
16+
) {
3217
// Next.js can call the Next.js config multiple
3318
// times - ensure we only run once.
3419
runOnce(() => {
35-
compileDeclaration(messagesPath);
20+
for (const messagesPath of messagesPaths) {
21+
const fullPath = path.resolve(messagesPath);
22+
23+
if (!fs.existsSync(fullPath)) {
24+
throwError(
25+
`\`createMessagesDeclaration\` points to a non-existent file: ${fullPath}`
26+
);
27+
}
28+
if (!fullPath.endsWith('.json')) {
29+
throwError(
30+
`\`createMessagesDeclaration\` needs to point to a JSON file. Received: ${fullPath}`
31+
);
32+
}
33+
34+
// Keep this as a runtime check and don't replace
35+
// this with a constant during the build process
36+
const env = process.env['NODE_ENV'.trim()];
37+
38+
compileDeclaration(messagesPath);
3639

37-
if (env === 'development') {
38-
startWatching(messagesPath);
40+
if (env === 'development') {
41+
startWatching(messagesPath);
42+
}
3943
}
4044
});
4145
}

packages/next-intl/src/plugin/createNextIntlPlugin.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ function initPlugin(
1414
);
1515
}
1616

17-
if (pluginConfig.experimental?.createMessagesDeclaration) {
17+
const messagesPathOrPaths =
18+
pluginConfig.experimental?.createMessagesDeclaration;
19+
if (messagesPathOrPaths) {
1820
createMessagesDeclaration(
19-
pluginConfig.experimental.createMessagesDeclaration
21+
typeof messagesPathOrPaths === 'string'
22+
? [messagesPathOrPaths]
23+
: messagesPathOrPaths
2024
);
2125
}
2226

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export type PluginConfig = {
22
requestConfig?: string;
33
experimental?: {
4-
createMessagesDeclaration?: string;
4+
/** 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. */
5+
createMessagesDeclaration?: string | Array<string>;
56
};
67
};

0 commit comments

Comments
 (0)