next-intl 4.0.0-beta-9e73cbe → 4.0.0-beta-ca3fcd5

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.
@@ -13,6 +13,23 @@ function warn(message) {
13
13
  console.warn(formatMessage(message));
14
14
  }
15
15
 
16
+ /**
17
+ * Wrapper around `fs.watch` that provides a workaround
18
+ * for https://github.com/nodejs/node/issues/5039.
19
+ */
20
+ function watchFile(filepath, callback) {
21
+ const directory = path.dirname(filepath);
22
+ const filename = path.basename(filepath);
23
+ return fs.watch(directory, {
24
+ persistent: false,
25
+ recursive: false
26
+ }, (event, changedFilename) => {
27
+ if (changedFilename === filename) {
28
+ callback();
29
+ }
30
+ });
31
+ }
32
+
16
33
  function runOnce(fn) {
17
34
  if (process.env._NEXT_INTL_COMPILE_MESSAGES === '1') {
18
35
  return;
@@ -20,33 +37,32 @@ function runOnce(fn) {
20
37
  process.env._NEXT_INTL_COMPILE_MESSAGES = '1';
21
38
  fn();
22
39
  }
23
- function createMessagesDeclaration(messagesPath) {
24
- const fullPath = path.resolve(messagesPath);
25
- if (!fs.existsSync(fullPath)) {
26
- throwError(`\`createMessagesDeclaration\` points to a non-existent file: ${fullPath}`);
27
- }
28
- if (!fullPath.endsWith('.json')) {
29
- throwError(`\`createMessagesDeclaration\` needs to point to a JSON file. Received: ${fullPath}`);
30
- }
31
-
32
- // Keep this as a runtime check and don't replace
33
- // this with a constant during the build process
34
- const env = process.env['NODE_ENV'.trim()];
35
-
40
+ function createMessagesDeclaration(messagesPaths) {
36
41
  // Next.js can call the Next.js config multiple
37
42
  // times - ensure we only run once.
38
43
  runOnce(() => {
39
- compileDeclaration(messagesPath);
40
- if (env === 'development') {
41
- startWatching(messagesPath);
44
+ for (const messagesPath of messagesPaths) {
45
+ const fullPath = path.resolve(messagesPath);
46
+ if (!fs.existsSync(fullPath)) {
47
+ throwError(`\`createMessagesDeclaration\` points to a non-existent file: ${fullPath}`);
48
+ }
49
+ if (!fullPath.endsWith('.json')) {
50
+ throwError(`\`createMessagesDeclaration\` needs to point to a JSON file. Received: ${fullPath}`);
51
+ }
52
+
53
+ // Keep this as a runtime check and don't replace
54
+ // this with a constant during the build process
55
+ const env = process.env['NODE_ENV'.trim()];
56
+ compileDeclaration(messagesPath);
57
+ if (env === 'development') {
58
+ startWatching(messagesPath);
59
+ }
42
60
  }
43
61
  });
44
62
  }
45
63
  function startWatching(messagesPath) {
46
- const watcher = fs.watch(messagesPath, eventType => {
47
- if (eventType === 'change') {
48
- compileDeclaration(messagesPath, true);
49
- }
64
+ const watcher = watchFile(messagesPath, () => {
65
+ compileDeclaration(messagesPath, true);
50
66
  });
51
67
  process.on('exit', () => {
52
68
  watcher.close();
@@ -149,8 +165,9 @@ function initPlugin(pluginConfig, nextConfig) {
149
165
  if (nextConfig?.i18n != null) {
150
166
  warn("\n[next-intl] 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");
151
167
  }
152
- if (pluginConfig.experimental?.createMessagesDeclaration) {
153
- createMessagesDeclaration(pluginConfig.experimental.createMessagesDeclaration);
168
+ const messagesPathOrPaths = pluginConfig.experimental?.createMessagesDeclaration;
169
+ if (messagesPathOrPaths) {
170
+ createMessagesDeclaration(typeof messagesPathOrPaths === 'string' ? [messagesPathOrPaths] : messagesPathOrPaths);
154
171
  }
155
172
  return getNextConfig(pluginConfig, nextConfig);
156
173
  }
@@ -1,6 +1,7 @@
1
1
  import fs from 'fs';
2
2
  import path from 'path';
3
3
  import { throwError } from './utils.js';
4
+ import watchFile from './watchFile.js';
4
5
 
5
6
  function runOnce(fn) {
6
7
  if (process.env._NEXT_INTL_COMPILE_MESSAGES === '1') {
@@ -9,33 +10,32 @@ function runOnce(fn) {
9
10
  process.env._NEXT_INTL_COMPILE_MESSAGES = '1';
10
11
  fn();
11
12
  }
12
- function createMessagesDeclaration(messagesPath) {
13
- const fullPath = path.resolve(messagesPath);
14
- if (!fs.existsSync(fullPath)) {
15
- throwError(`\`createMessagesDeclaration\` points to a non-existent file: ${fullPath}`);
16
- }
17
- if (!fullPath.endsWith('.json')) {
18
- throwError(`\`createMessagesDeclaration\` needs to point to a JSON file. Received: ${fullPath}`);
19
- }
20
-
21
- // Keep this as a runtime check and don't replace
22
- // this with a constant during the build process
23
- const env = process.env['NODE_ENV'.trim()];
24
-
13
+ function createMessagesDeclaration(messagesPaths) {
25
14
  // Next.js can call the Next.js config multiple
26
15
  // times - ensure we only run once.
27
16
  runOnce(() => {
28
- compileDeclaration(messagesPath);
29
- if (env === 'development') {
30
- startWatching(messagesPath);
17
+ for (const messagesPath of messagesPaths) {
18
+ const fullPath = path.resolve(messagesPath);
19
+ if (!fs.existsSync(fullPath)) {
20
+ throwError(`\`createMessagesDeclaration\` points to a non-existent file: ${fullPath}`);
21
+ }
22
+ if (!fullPath.endsWith('.json')) {
23
+ throwError(`\`createMessagesDeclaration\` needs to point to a JSON file. Received: ${fullPath}`);
24
+ }
25
+
26
+ // Keep this as a runtime check and don't replace
27
+ // this with a constant during the build process
28
+ const env = process.env['NODE_ENV'.trim()];
29
+ compileDeclaration(messagesPath);
30
+ if (env === 'development') {
31
+ startWatching(messagesPath);
32
+ }
31
33
  }
32
34
  });
33
35
  }
34
36
  function startWatching(messagesPath) {
35
- const watcher = fs.watch(messagesPath, eventType => {
36
- if (eventType === 'change') {
37
- compileDeclaration(messagesPath, true);
38
- }
37
+ const watcher = watchFile(messagesPath, () => {
38
+ compileDeclaration(messagesPath, true);
39
39
  });
40
40
  process.on('exit', () => {
41
41
  watcher.close();
@@ -6,8 +6,9 @@ function initPlugin(pluginConfig, nextConfig) {
6
6
  if (nextConfig?.i18n != null) {
7
7
  warn("\n[next-intl] 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");
8
8
  }
9
- if (pluginConfig.experimental?.createMessagesDeclaration) {
10
- createMessagesDeclaration(pluginConfig.experimental.createMessagesDeclaration);
9
+ const messagesPathOrPaths = pluginConfig.experimental?.createMessagesDeclaration;
10
+ if (messagesPathOrPaths) {
11
+ createMessagesDeclaration(typeof messagesPathOrPaths === 'string' ? [messagesPathOrPaths] : messagesPathOrPaths);
11
12
  }
12
13
  return getNextConfig(pluginConfig, nextConfig);
13
14
  }
@@ -0,0 +1,21 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+
4
+ /**
5
+ * Wrapper around `fs.watch` that provides a workaround
6
+ * for https://github.com/nodejs/node/issues/5039.
7
+ */
8
+ function watchFile(filepath, callback) {
9
+ const directory = path.dirname(filepath);
10
+ const filename = path.basename(filepath);
11
+ return fs.watch(directory, {
12
+ persistent: false,
13
+ recursive: false
14
+ }, (event, changedFilename) => {
15
+ if (changedFilename === filename) {
16
+ callback();
17
+ }
18
+ });
19
+ }
20
+
21
+ export { watchFile as default };
@@ -1 +1 @@
1
- import e from"fs";import t from"path";import{throwError as s}from"./utils.js";function n(n){const i=t.resolve(n);e.existsSync(i)||s(`\`createMessagesDeclaration\` points to a non-existent file: ${i}`),i.endsWith(".json")||s(`\`createMessagesDeclaration\` needs to point to a JSON file. Received: ${i}`);const r=process.env["NODE_ENV".trim()];var c;c=()=>{o(n),"development"===r&&function(t){const s=e.watch(t,(e=>{"change"===e&&o(t,!0)}));process.on("exit",(()=>{s.close()}))}(n)},"1"!==process.env._NEXT_INTL_COMPILE_MESSAGES&&(process.env._NEXT_INTL_COMPILE_MESSAGES="1",c())}function o(t,s=!1){const n=t.replace(/\.json$/,".d.json.ts");function o(e){return`// This file is auto-generated by next-intl, do not edit directly.\n// See: https://next-intl.dev/docs/workflows/typescript#messages-arguments\n\ndeclare const messages: ${e.trim()};\nexport default messages;`}if(s)return e.promises.readFile(t,"utf-8").then((t=>e.promises.writeFile(n,o(t))));const i=e.readFileSync(t,"utf-8");e.writeFileSync(n,o(i))}export{n as default};
1
+ import e from"fs";import t from"path";import{throwError as s}from"./utils.js";import o from"./watchFile.js";function n(o){var n;n=()=>{for(const n of o){const o=t.resolve(n);e.existsSync(o)||s(`\`createMessagesDeclaration\` points to a non-existent file: ${o}`),o.endsWith(".json")||s(`\`createMessagesDeclaration\` needs to point to a JSON file. Received: ${o}`);const c=process.env["NODE_ENV".trim()];r(n),"development"===c&&i(n)}},"1"!==process.env._NEXT_INTL_COMPILE_MESSAGES&&(process.env._NEXT_INTL_COMPILE_MESSAGES="1",n())}function i(e){const t=o(e,(()=>{r(e,!0)}));process.on("exit",(()=>{t.close()}))}function r(t,s=!1){const o=t.replace(/\.json$/,".d.json.ts");function n(e){return`// This file is auto-generated by next-intl, do not edit directly.\n// See: https://next-intl.dev/docs/workflows/typescript#messages-arguments\n\ndeclare const messages: ${e.trim()};\nexport default messages;`}if(s)return e.promises.readFile(t,"utf-8").then((t=>e.promises.writeFile(o,n(t))));const i=e.readFileSync(t,"utf-8");e.writeFileSync(o,n(i))}export{n as default};
@@ -1 +1 @@
1
- import e from"./createMessagesDeclaration.js";import t from"./getNextConfig.js";import{warn as r}from"./utils.js";function n(n={}){const o="string"==typeof n?{requestConfig:n}:n;return function(n){return function(n,o){return null!=o?.i18n&&r("\n[next-intl] 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"),n.experimental?.createMessagesDeclaration&&e(n.experimental.createMessagesDeclaration),t(n,o)}(o,n)}}export{n as default};
1
+ import e from"./createMessagesDeclaration.js";import t from"./getNextConfig.js";import{warn as n}from"./utils.js";function r(r={}){const o="string"==typeof r?{requestConfig:r}:r;return function(r){return function(r,o){null!=o?.i18n&&n("\n[next-intl] 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");const i=r.experimental?.createMessagesDeclaration;return i&&e("string"==typeof i?[i]:i),t(r,o)}(o,r)}}export{r as default};
@@ -0,0 +1 @@
1
+ import r from"fs";import t from"path";function e(e,a){const n=t.dirname(e),o=t.basename(e);return r.watch(n,{persistent:!1,recursive:!1},((r,t)=>{t===o&&a()}))}export{e as default};
@@ -1 +1 @@
1
- export default function createMessagesDeclaration(messagesPath: string): void;
1
+ export default function createMessagesDeclaration(messagesPaths: Array<string>): void;
@@ -1,6 +1,7 @@
1
1
  export type PluginConfig = {
2
2
  requestConfig?: string;
3
3
  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>;
5
6
  };
6
7
  };
@@ -0,0 +1,6 @@
1
+ import fs from 'fs';
2
+ /**
3
+ * Wrapper around `fs.watch` that provides a workaround
4
+ * for https://github.com/nodejs/node/issues/5039.
5
+ */
6
+ export default function watchFile(filepath: string, callback: () => void): fs.FSWatcher;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-intl",
3
- "version": "4.0.0-beta-9e73cbe",
3
+ "version": "4.0.0-beta-ca3fcd5",
4
4
  "sideEffects": false,
5
5
  "author": "Jan Amann <jan@amann.work>",
6
6
  "funding": [
@@ -112,7 +112,7 @@
112
112
  "dependencies": {
113
113
  "@formatjs/intl-localematcher": "^0.5.4",
114
114
  "negotiator": "^1.0.0",
115
- "use-intl": "4.0.0-beta-9e73cbe"
115
+ "use-intl": "4.0.0-beta-ca3fcd5"
116
116
  },
117
117
  "peerDependencies": {
118
118
  "next": "^12.0.0 || ^13.0.0 || ^14.0.0 || ^15.0.0",
@@ -124,5 +124,5 @@
124
124
  "optional": true
125
125
  }
126
126
  },
127
- "gitHead": "31d0b2dfc66357f02a07f0b434f85dc8f0a7dad3"
127
+ "gitHead": "9ed16adf3aee9906edc15b1b1a29111a8e2cfef9"
128
128
  }