@intlayer/sync-json-plugin 8.1.2 → 8.6.10
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.
- package/README.md +41 -32
- package/dist/cjs/loadJSON.cjs +37 -38
- package/dist/cjs/loadJSON.cjs.map +1 -1
- package/dist/cjs/syncJSON-CTiXS5Pk.cjs +2 -0
- package/dist/cjs/syncJSON-CTiXS5Pk.cjs.map +1 -0
- package/dist/cjs/syncJSON.cjs +62 -62
- package/dist/cjs/syncJSON.cjs.map +1 -1
- package/dist/esm/loadJSON.mjs +37 -38
- package/dist/esm/loadJSON.mjs.map +1 -1
- package/dist/esm/syncJSON.mjs +62 -62
- package/dist/esm/syncJSON.mjs.map +1 -1
- package/dist/types/loadJSON.d.ts +5 -9
- package/dist/types/loadJSON.d.ts.map +1 -1
- package/dist/types/syncJSON.d.ts +6 -10
- package/dist/types/syncJSON.d.ts.map +1 -1
- package/package.json +17 -18
package/README.md
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
<h1 align="center">
|
|
8
8
|
<strong>Per-component i18n</strong>
|
|
9
9
|
</h1>
|
|
10
|
+
|
|
10
11
|
<h2 align="center">
|
|
11
12
|
<strong>AI-powered translation. Visual Editor. Multilingual CMS.</strong>
|
|
12
13
|
</h2>
|
|
@@ -46,25 +47,26 @@ With **per-locale content files**, **TypeScript autocompletion**, **tree-shakabl
|
|
|
46
47
|
|
|
47
48
|
## Keys benefits of Intlayer:
|
|
48
49
|
|
|
49
|
-
| Feature | Description
|
|
50
|
-
| --------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
51
|
-
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/frameworks.png?raw=true" alt="Feature" width="700"> | **Cross-Frameworks Support**<br><br>Intlayer is compatible with all major frameworks and libraries, including Next.js, React, Vite, Vue.js, Nuxt, Preact, Express, and more.
|
|
52
|
-
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/javascript_content_management.jpg?raw=true" alt="Feature" width="700"> | **JavaScript-Powered Content Management**<br><br>Harness the flexibility of JavaScript to define and manage your content efficiently. <br><br> - [Content declaration](https://intlayer.org/doc/concept/content)
|
|
53
|
-
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/per_locale_content_declaration_file.png?raw=true" alt="Feature" width="700"> | **Per-Locale Content Declaration File**<br><br>Speed up your development by declaring your content once, before auto generation.<br><br> - [Per-Locale Content Declaration File](https://intlayer.org/doc/concept/per-locale-file)
|
|
54
|
-
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/
|
|
55
|
-
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/
|
|
56
|
-
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/
|
|
57
|
-
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/
|
|
58
|
-
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/
|
|
59
|
-
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/
|
|
60
|
-
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/
|
|
61
|
-
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/
|
|
62
|
-
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/
|
|
63
|
-
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/
|
|
64
|
-
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/
|
|
65
|
-
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/
|
|
66
|
-
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/
|
|
67
|
-
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/
|
|
50
|
+
| Feature | Description |
|
|
51
|
+
| --------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
52
|
+
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/frameworks.png?raw=true" alt="Feature" width="700"> | **Cross-Frameworks Support**<br><br>Intlayer is compatible with all major frameworks and libraries, including Next.js, React, Vite, Vue.js, Nuxt, Preact, Express, and more. |
|
|
53
|
+
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/javascript_content_management.jpg?raw=true" alt="Feature" width="700"> | **JavaScript-Powered Content Management**<br><br>Harness the flexibility of JavaScript to define and manage your content efficiently. <br><br> - [Content declaration](https://intlayer.org/doc/concept/content) |
|
|
54
|
+
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/per_locale_content_declaration_file.png?raw=true" alt="Feature" width="700"> | **Per-Locale Content Declaration File**<br><br>Speed up your development by declaring your content once, before auto generation.<br><br> - [Per-Locale Content Declaration File](https://intlayer.org/doc/concept/per-locale-file) |
|
|
55
|
+
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/compiler.jpg?raw=true" alt="Feature" width="700"> | **Compiler**<br><br>The Intlayer Compiler extract automatically the content from the components and generate the dictionary files.<br><br> - [Compiler](https://intlayer.org/doc/compiler) |
|
|
56
|
+
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/autocompletion.png?raw=true" alt="Feature" width="700"> | **Type-Safe Environment**<br><br>Leverage TypeScript to ensure your content definitions and code are error-free, while also benefiting from IDE autocompletion.<br><br> - [TypeScript configuration](https://intlayer.org/doc/environment/vite-and-react#configure-typescript) |
|
|
57
|
+
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/config_file.png?raw=true" alt="Feature" width="700"> | **Simplified Setup**<br><br>Get up and running quickly with minimal configuration. Adjust settings for internationalization, routing, AI, build, and content handling with ease. <br><br> - [Explore Next.js integration](https://intlayer.org/doc/environment/nextjs) |
|
|
58
|
+
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/content_retrieval.png?raw=true" alt="Feature" width="700"> | **Simplified Content Retrieval**<br><br>No need to call your `t` function for each piece of content. Retrieve all your content directly using a single hook.<br><br> - [React integration](https://intlayer.org/doc/environment/create-react-app) |
|
|
59
|
+
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/server_component.png?raw=true" alt="Feature" width="700"> | **Consistent Server Component Implementation**<br><br>Perfectly suited for Next.js server components, use the same implementation for both client and server components, no need to pass your `t` function across each server component. <br><br> - [Server Components](https://intlayer.org/doc/environment/nextjs#step-7-utilize-content-in-your-code) |
|
|
60
|
+
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/file_tree.png?raw=true" alt="Feature" width="700"> | **Organized Codebase**<br><br>Keep your codebase more organized: 1 component = 1 dictionary in the same folder. Translations close to their respective components, enhance maintainability and clarity. <br><br> - [How Intlayer works](https://intlayer.org/doc/concept/how-works-intlayer) |
|
|
61
|
+
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/url_routing.png?raw=true" alt="Feature" width="700"> | **Enhanced Routing**<br><br>Full support of app routing, adapting seamlessly to complex application structures, for Next.js, React, Vite, Vue.js, etc.<br><br> - [Explore Next.js integration](https://intlayer.org/doc/environment/nextjs) |
|
|
62
|
+
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/markdown.png?raw=true" alt="Feature" width="700"> | **Markdown Support**<br><br>Import and interpret, locale files and remote Markdown for multilingual content like privacy policies, documentation, etc. Interpret and make Markdown metadata accessible in your code.<br><br> - [Content files](https://intlayer.org/doc/concept/content/file) |
|
|
63
|
+
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/visual_editor.png?raw=true" alt="Feature" width="700"> | **Free Visual Editor & CMS**<br><br>A free visual editor and CMS are available for content writers, removing the need for a localization platform. Keep your content synchronized using Git, or externalize it totally or partially with the CMS.<br><br> - [Intlayer Editor](https://intlayer.org/doc/concept/editor) <br> - [Intlayer CMS](https://intlayer.org/doc/concept/cms) |
|
|
64
|
+
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/bundle.png?raw=true" alt="Feature" width="700"> | **Tree-shakable Content**<br><br>Tree-shakable content, reducing the size of the final bundle. Loads content per component, excluding any unused content from your bundle. Supports lazy loading to enhance app loading efficiency. <br><br> - [App build optimization](https://intlayer.org/doc/concept/how-works-intlayer#app-build-optimization) |
|
|
65
|
+
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/static_rendering.png?raw=true" alt="Feature" width="700"> | **Static Rendering**<br><br>Doesn't block Static Rendering. <br><br> - [Next.js integration](https://intlayer.org/doc/environment/nextjs) |
|
|
66
|
+
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/AI_translation.png?raw=true" alt="Feature" width="700"> | **AI-Powered Translation**<br><br>Transform your website into 231 languages with just one click using Intlayer's advanced AI-powered translation tools using your own AI provider / API key. <br><br> - [CI/CD integration](https://intlayer.org/doc/concept/ci-cd) <br> - [Intlayer CLI](https://intlayer.org/doc/concept/cli) <br> - [Auto fill](https://intlayer.org/doc/concept/auto-fill) |
|
|
67
|
+
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/mcp.png?raw=true" alt="Feature" width="700"> | **MCP Server Integration**<br><br>Provides an MCP (Model Context Protocol) server for IDE automation, enabling seamless content management and i18n workflows directly within your development environment. <br><br> - [MCP Server](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/mcp_server.md) |
|
|
68
|
+
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/vscode_extension.png?raw=true" alt="Feature" width="700"> | **VSCode Extension**<br><br>Intlayer provides a VSCode extension to help you manage your content and translations, building your dictionaries, translating your content, and more. <br><br> - [VSCode Extension](https://intlayer.org/doc/vs-code-extension) |
|
|
69
|
+
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/interoperability.png?raw=true" alt="Feature" width="700"> | **Interoperability**<br><br>Allow interoperability with react-i18next, next-i18next, next-intl, react-intl, vue-i18n. <br><br> - [Intlayer and react-intl](https://intlayer.org/blog/intlayer-with-react-intl) <br> - [Intlayer and next-intl](https://intlayer.org/blog/intlayer-with-next-intl) <br> - [Intlayer and next-i18next](https://intlayer.org/blog/intlayer-with-next-i18next) <br> - [Intlayer and vue-i18n](https://intlayer.org/blog/intlayer-with-vue-i18n) |
|
|
68
70
|
|
|
69
71
|
---
|
|
70
72
|
|
|
@@ -153,6 +155,8 @@ Explore our comprehensive documentation to get started with Intlayer and learn h
|
|
|
153
155
|
<li><a href="https://intlayer.org/doc/concept/how-works-intlayer" rel=''>How Intlayer Works</a></li>
|
|
154
156
|
<li><a href="https://intlayer.org/doc/concept/configuration" rel=''>Configuration</a></li>
|
|
155
157
|
<li><a href="https://intlayer.org/doc/concept/cli" rel=''>Intlayer CLI</a></li>
|
|
158
|
+
<li><a href="https://intlayer.org/doc/compiler" rel=''>Compiler</a></li>
|
|
159
|
+
|
|
156
160
|
<li><a href="https://intlayer.org/doc/concept/editor" rel=''>Intlayer Editor</a></li>
|
|
157
161
|
<li><a href="https://intlayer.org/doc/concept/cms" rel=''>Intlayer CMS</a></li>
|
|
158
162
|
<li><a href="https://intlayer.org/doc/concept/content" rel=''>Dictionary</a>
|
|
@@ -174,29 +178,37 @@ Explore our comprehensive documentation to get started with Intlayer and learn h
|
|
|
174
178
|
<details open>
|
|
175
179
|
<summary style="font-size:16px; font-weight:bold;">🌐 Environment</summary>
|
|
176
180
|
<ul>
|
|
177
|
-
<li><a href="https://intlayer.org/doc/environment/nextjs" rel=''>Intlayer with Next.js
|
|
181
|
+
<li><a href="https://intlayer.org/doc/environment/nextjs" rel=''>Intlayer with Next.js 16</a>
|
|
178
182
|
<ul>
|
|
183
|
+
<li><a href="https://intlayer.org/doc/environment/nextjs/15" rel=''>Next.js 15</a></li>
|
|
179
184
|
<li><a href="https://intlayer.org/doc/environment/nextjs/14" rel=''>Next.js 14 (App Router)</a></li>
|
|
180
185
|
<li><a href="https://intlayer.org/doc/environment/nextjs/next-with-Page-Router" rel=''>Next.js Page Router</a></li>
|
|
186
|
+
<li><a href="https://intlayer.org/doc/environment/nextjs/compiler" rel=''>Next.js using Compiler</a></li>
|
|
181
187
|
</ul>
|
|
182
188
|
</li>
|
|
183
189
|
<li><a href="https://intlayer.org/doc/environment/create-react-app" rel=''>React CRA</a></li>
|
|
184
|
-
<li><a href="https://intlayer.org/doc/environment/vite-and-react" rel=''>Vite + React</a>
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
</ul>
|
|
189
|
-
</li>
|
|
190
|
+
<li><a href="https://intlayer.org/doc/environment/vite-and-react" rel=''>Vite + React</a></li>
|
|
191
|
+
<li><a href="https://intlayer.org/doc/environment/vite-and-react" rel=''>Vite + React using Compiler</a></li>
|
|
192
|
+
<li><a href="https://intlayer.org/doc/environment/vite-and-react/compiler" rel=''>React-router-v7</a></li>
|
|
193
|
+
<li><a href="https://intlayer.org/doc/environment/vite-and-react/tanstack-start" rel=''>Tanstack start</a></li>
|
|
190
194
|
<li><a href="https://intlayer.org/doc/environment/react-native-and-expo" rel=''>React Native</a></li>
|
|
191
|
-
<li><a href="https://intlayer.org/doc/environment/lynx-and-react" rel=''>Lynx + React</a></li>
|
|
192
195
|
<li><a href="https://intlayer.org/doc/environment/vite-and-svelte" rel=''>Vite + Svelte</a></li>
|
|
196
|
+
<li><a href="https://intlayer.org/doc/environment/sveltekit" rel=''>SvelteKit</a></li>
|
|
193
197
|
<li><a href="https://intlayer.org/doc/environment/vite-and-preact" rel=''>Vite + Preact</a></li>
|
|
194
198
|
<li><a href="https://intlayer.org/doc/environment/vite-and-vue" rel=''>Vite + Vue</a></li>
|
|
195
199
|
<li><a href="https://intlayer.org/doc/environment/vite-and-nuxt" rel=''>Vite + Nuxt</a></li>
|
|
196
200
|
<li><a href="https://intlayer.org/doc/environment/vite-and-solid" rel=''>Vite + Solid</a></li>
|
|
197
201
|
<li><a href="https://intlayer.org/doc/environment/angular" rel=''>Angular</a></li>
|
|
198
|
-
<li
|
|
199
|
-
|
|
202
|
+
<li>
|
|
203
|
+
<a href="https://intlayer.org/doc/environment/express" rel=''>Backend</a>
|
|
204
|
+
<ul>
|
|
205
|
+
<li><a href="https://intlayer.org/doc/environment/express" rel=''>Express</a></li>
|
|
206
|
+
<li><a href="https://intlayer.org/doc/environment/nest" rel=''>NestJS</a></li>
|
|
207
|
+
<li><a href="https://intlayer.org/doc/environment/fastify" rel=''>Fastify</a></li>
|
|
208
|
+
<li><a href="https://intlayer.org/doc/environment/adonisjs" rel=''>AdonisJS</a></li>
|
|
209
|
+
<li><a href="https://intlayer.org/doc/environment/hono" rel=''>Hono</a></li>
|
|
210
|
+
</ul>
|
|
211
|
+
</li>
|
|
200
212
|
</ul>
|
|
201
213
|
</details>
|
|
202
214
|
|
|
@@ -249,9 +261,6 @@ You can also follow us on :
|
|
|
249
261
|
<a href="https://www.linkedin.com/company/intlayerorg" target="blank" rel='noopener noreferrer nofollow'><img align="center"
|
|
250
262
|
src="https://img.shields.io/badge/linkedin-%231DA1F2.svg?style=for-the-badge&logo=linkedin&logoColor=white"
|
|
251
263
|
alt="Intlayer LinkedIn" height="30"/></a>
|
|
252
|
-
<a href="https://www.facebook.com/intlayer" target="blank" rel='noopener noreferrer nofollow'><img align="center"
|
|
253
|
-
src="https://img.shields.io/badge/facebook-4267B2.svg?style=for-the-badge&logo=facebook&logoColor=white"
|
|
254
|
-
alt="Intlayer Facebook" height="30"/></a>
|
|
255
264
|
<a href="https://www.instagram.com/intlayer/" target="blank" rel='noopener noreferrer nofollow'><img align="center"
|
|
256
265
|
src="https://img.shields.io/badge/instagram-%23E4405F.svg?style=for-the-badge&logo=Instagram&logoColor=white"
|
|
257
266
|
alt="Intlayer Instagram" height="30"/></a>
|
package/dist/cjs/loadJSON.cjs
CHANGED
|
@@ -2,44 +2,49 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
2
2
|
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
3
3
|
const require_syncJSON = require('./syncJSON.cjs');
|
|
4
4
|
let node_path = require("node:path");
|
|
5
|
-
let
|
|
5
|
+
let _intlayer_config_file = require("@intlayer/config/file");
|
|
6
|
+
let _intlayer_config_utils = require("@intlayer/config/utils");
|
|
6
7
|
let fast_glob = require("fast-glob");
|
|
7
8
|
fast_glob = require_runtime.__toESM(fast_glob);
|
|
8
9
|
|
|
9
10
|
//#region src/loadJSON.ts
|
|
10
|
-
const listMessages = (
|
|
11
|
-
const {
|
|
12
|
-
const baseDir =
|
|
13
|
-
const locales = internationalization
|
|
14
|
-
const globPattern = builder({
|
|
15
|
-
key: "*",
|
|
16
|
-
locale: `{${locales.map((locale) => locale).join(",")}}`
|
|
17
|
-
});
|
|
18
|
-
const maskPattern = builder({
|
|
19
|
-
key: "{{__KEY__}}",
|
|
20
|
-
locale: "{{__LOCALE__}}"
|
|
21
|
-
});
|
|
22
|
-
const files = fast_glob.default.sync(globPattern, { cwd: baseDir });
|
|
11
|
+
const listMessages = async (source, configuration, selectedLocale) => {
|
|
12
|
+
const { system, internationalization } = configuration;
|
|
13
|
+
const { baseDir } = system;
|
|
14
|
+
const { locales } = internationalization;
|
|
23
15
|
const result = {};
|
|
24
|
-
for (const
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
16
|
+
for (const locale of locales) {
|
|
17
|
+
const globPatternLocale = await (0, _intlayer_config_utils.parseFilePathPattern)(source, {
|
|
18
|
+
key: "*",
|
|
19
|
+
locale
|
|
20
|
+
});
|
|
21
|
+
const maskPatternLocale = await (0, _intlayer_config_utils.parseFilePathPattern)(source, {
|
|
22
|
+
key: "{{__KEY__}}",
|
|
23
|
+
locale
|
|
24
|
+
});
|
|
25
|
+
if (!globPatternLocale || !maskPatternLocale) continue;
|
|
26
|
+
const files = await (0, fast_glob.default)(globPatternLocale.startsWith("./") ? globPatternLocale.slice(2) : globPatternLocale, { cwd: baseDir });
|
|
27
|
+
for (const file of files) {
|
|
28
|
+
const extraction = require_syncJSON.extractKeyAndLocaleFromPath(file, maskPatternLocale, locales, selectedLocale);
|
|
29
|
+
if (!extraction) continue;
|
|
30
|
+
const { key, locale: extractedLocale } = extraction;
|
|
31
|
+
const absolutePath = (0, node_path.isAbsolute)(file) ? file : (0, node_path.resolve)(baseDir, file);
|
|
32
|
+
const usedLocale = extractedLocale;
|
|
33
|
+
if (!result[usedLocale]) result[usedLocale] = {};
|
|
34
|
+
result[usedLocale][key] = absolutePath;
|
|
35
|
+
}
|
|
31
36
|
}
|
|
32
37
|
return result;
|
|
33
38
|
};
|
|
34
|
-
const loadMessagePathMap = (source, configuration, selectedLocale) => {
|
|
35
|
-
const
|
|
36
|
-
const messages = listMessages(
|
|
37
|
-
return (
|
|
39
|
+
const loadMessagePathMap = async (source, configuration, selectedLocale) => {
|
|
40
|
+
const sourcePattern = source;
|
|
41
|
+
const messages = await listMessages(sourcePattern, configuration, selectedLocale);
|
|
42
|
+
return ((await (0, _intlayer_config_utils.parseFilePathPattern)(sourcePattern, {
|
|
38
43
|
key: "{{__KEY__}}",
|
|
39
44
|
locale: "{{__LOCALE__}}"
|
|
40
|
-
}).includes("{{__LOCALE__}}") && selectedLocale ? Object.entries(messages).filter(([locale]) => locale === selectedLocale) : Object.entries(messages)).flatMap(([locale, keysRecord]) => Object.entries(keysRecord).map(([key, path]) => {
|
|
45
|
+
})).includes("{{__LOCALE__}}") && selectedLocale ? Object.entries(messages).filter(([locale]) => locale === selectedLocale) : Object.entries(messages)).flatMap(([locale, keysRecord]) => Object.entries(keysRecord).map(([key, path]) => {
|
|
41
46
|
return {
|
|
42
|
-
path: (0, node_path.isAbsolute)(path) ? path : (0, node_path.resolve)(configuration.
|
|
47
|
+
path: (0, node_path.isAbsolute)(path) ? path : (0, node_path.resolve)(configuration.system.baseDir, path),
|
|
43
48
|
locale,
|
|
44
49
|
key
|
|
45
50
|
};
|
|
@@ -55,19 +60,13 @@ const loadJSON = (options) => {
|
|
|
55
60
|
name: "load-json",
|
|
56
61
|
loadDictionaries: async ({ configuration }) => {
|
|
57
62
|
const usedLocale = locale ?? configuration.internationalization.defaultLocale;
|
|
58
|
-
const dictionariesMap = loadMessagePathMap(options.source, configuration, usedLocale);
|
|
59
|
-
let filePath = options.source
|
|
60
|
-
if (filePath && !(0, node_path.isAbsolute)(filePath)) filePath = (0, node_path.join)(configuration.
|
|
63
|
+
const dictionariesMap = await loadMessagePathMap(options.source, configuration, usedLocale);
|
|
64
|
+
let filePath = await (0, _intlayer_config_utils.parseFilePathPattern)(options.source, { key: "{{key}}" });
|
|
65
|
+
if (filePath && !(0, node_path.isAbsolute)(filePath)) filePath = (0, node_path.join)(configuration.system.baseDir, filePath);
|
|
61
66
|
const dictionaries = [];
|
|
62
67
|
for (const { path, key } of dictionariesMap) {
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
try {
|
|
66
|
-
json = requireFunction(path);
|
|
67
|
-
} catch {
|
|
68
|
-
json = {};
|
|
69
|
-
}
|
|
70
|
-
const filePath = (0, node_path.relative)(configuration.content.baseDir, path);
|
|
68
|
+
const json = await (0, _intlayer_config_file.loadExternalFile)(path, { logError: false }) ?? {};
|
|
69
|
+
const filePath = (0, node_path.relative)(configuration.system.baseDir, path);
|
|
71
70
|
const dictionary = {
|
|
72
71
|
key,
|
|
73
72
|
locale: usedLocale,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loadJSON.cjs","names":["
|
|
1
|
+
{"version":3,"file":"loadJSON.cjs","names":["extractKeyAndLocaleFromPath"],"sources":["../../src/loadJSON.ts"],"sourcesContent":["import { isAbsolute, join, relative, resolve } from 'node:path';\nimport { loadExternalFile } from '@intlayer/config/file';\nimport { parseFilePathPattern } from '@intlayer/config/utils';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport type { IntlayerConfig } from '@intlayer/types/config';\nimport type {\n Dictionary,\n DictionaryFormat,\n LocalDictionaryId,\n} from '@intlayer/types/dictionary';\nimport type {\n FilePathPattern,\n FilePathPatternContext,\n} from '@intlayer/types/filePathPattern';\nimport type { Plugin } from '@intlayer/types/plugin';\nimport fg from 'fast-glob';\nimport { extractKeyAndLocaleFromPath } from './syncJSON';\n\ntype JSONContent = Record<string, any>;\n\ntype FilePath = string;\n\ntype MessagesRecord = Record<Locale, Record<Dictionary['key'], FilePath>>;\n\nconst listMessages = async (\n source: FilePathPattern,\n configuration: IntlayerConfig,\n selectedLocale: Locale\n): Promise<MessagesRecord> => {\n const { system, internationalization } = configuration;\n\n const { baseDir } = system;\n const { locales } = internationalization;\n\n const result: MessagesRecord = {} as MessagesRecord;\n\n for (const locale of locales) {\n const globPatternLocale = await parseFilePathPattern(source, {\n key: '*',\n locale,\n } as any as FilePathPatternContext);\n\n const maskPatternLocale = await parseFilePathPattern(source, {\n key: '{{__KEY__}}',\n locale,\n } as any as FilePathPatternContext);\n\n if (!globPatternLocale || !maskPatternLocale) {\n continue;\n }\n\n const normalizedGlobPattern = globPatternLocale.startsWith('./')\n ? globPatternLocale.slice(2)\n : globPatternLocale;\n\n const files = await fg(normalizedGlobPattern, {\n cwd: baseDir,\n });\n\n for (const file of files) {\n const extraction = extractKeyAndLocaleFromPath(\n file,\n maskPatternLocale,\n locales,\n selectedLocale\n );\n\n if (!extraction) {\n continue;\n }\n\n const { key, locale: extractedLocale } = extraction;\n\n const absolutePath = isAbsolute(file) ? file : resolve(baseDir, file);\n\n const usedLocale = extractedLocale as Locale;\n if (!result[usedLocale]) {\n result[usedLocale] = {};\n }\n\n result[usedLocale][key as Dictionary['key']] = absolutePath;\n }\n }\n\n // For the load plugin we only use actual discovered files; do not fabricate\n // missing locales or keys, since we don't write outputs.\n return result;\n};\n\ntype DictionariesMap = { path: string; locale: Locale; key: string }[];\n\nconst loadMessagePathMap = async (\n source: MessagesRecord | FilePathPattern,\n configuration: IntlayerConfig,\n selectedLocale: Locale\n) => {\n const sourcePattern = source as FilePathPattern;\n const messages: MessagesRecord = await listMessages(\n sourcePattern,\n configuration,\n selectedLocale\n );\n\n const maskPattern = await parseFilePathPattern(sourcePattern, {\n key: '{{__KEY__}}',\n locale: '{{__LOCALE__}}',\n } as any as FilePathPatternContext);\n const hasLocaleInMask = maskPattern.includes('{{__LOCALE__}}');\n\n const entries = (\n hasLocaleInMask && selectedLocale\n ? Object.entries(messages).filter(([locale]) => locale === selectedLocale)\n : Object.entries(messages)\n ) as [Locale, Record<Dictionary['key'], FilePath>][];\n\n const dictionariesPathMap: DictionariesMap = entries.flatMap(\n ([locale, keysRecord]) =>\n Object.entries(keysRecord).map(([key, path]) => {\n const absolutePath = isAbsolute(path)\n ? path\n : resolve(configuration.system.baseDir, path);\n\n return {\n path: absolutePath,\n locale,\n key,\n } as DictionariesMap[number];\n })\n );\n\n return dictionariesPathMap;\n};\n\ntype LoadJSONPluginOptions = {\n /**\n * The source of the plugin.\n * Is a function to build the source from the key and locale.\n *\n * @example\n * ```ts\n * loadJSON({\n * source: ({ key }) => `blog/${'**'}/${key}.i18n.json`,\n * })\n * ```\n */\n source: FilePathPattern;\n\n /**\n * Locale\n *\n * If not provided, the plugin will consider the default locale.\n *\n * @example\n * ```ts\n * loadJSON({\n * source: ({ key }) => `blog/${'**'}/${key}.i18n.json`,\n * locale: Locales.ENGLISH,\n * })\n * ```\n */\n locale?: Locale;\n\n /**\n * Because Intlayer transform the JSON files into Dictionary, we need to identify the plugin in the dictionary.\n * Used to identify the plugin in the dictionary.\n *\n * In the case you have multiple plugins, you can use this to identify the plugin in the dictionary.\n *\n * @example\n * ```ts\n * const config = {\n * plugins: [\n * loadJSON({\n * source: ({ key }) => `./resources/${key}.json`,\n * location: 'plugin-i18next',\n * }),\n * loadJSON({\n * source: ({ key }) => `./messages/${key}.json`,\n * location: 'plugin-next-intl',\n * }),\n * ]\n * }\n * ```\n */\n location?: string;\n\n /**\n * The priority of the dictionaries created by the plugin.\n *\n * In the case of conflicts with remote dictionaries, or .content files, the dictionary with the highest priority will override the other dictionaries.\n *\n * Default is -1. (.content file priority is 0)\n *\n */\n priority?: number;\n\n /**\n * The format of the dictionary content.\n *\n * @example\n * ```ts\n * loadJSON({\n * format: 'icu',\n * })\n * ```\n */\n format?: DictionaryFormat;\n};\n\nexport const loadJSON = (options: LoadJSONPluginOptions): Plugin => {\n const { location, priority, locale, format } = {\n location: 'plugin',\n priority: 0,\n ...options,\n } as const;\n\n return {\n name: 'load-json',\n\n loadDictionaries: async ({ configuration }) => {\n const usedLocale = (locale ??\n configuration.internationalization.defaultLocale) as Locale;\n\n const dictionariesMap: DictionariesMap = await loadMessagePathMap(\n options.source,\n configuration,\n usedLocale\n );\n\n let filePath: string = await parseFilePathPattern(options.source, {\n key: '{{key}}',\n } as any as FilePathPatternContext);\n\n if (filePath && !isAbsolute(filePath)) {\n filePath = join(configuration.system.baseDir, filePath);\n }\n\n const dictionaries: Dictionary[] = [];\n\n for (const { path, key } of dictionariesMap) {\n // loadExternalFile swallows errors and returns undefined for missing files;\n // use ?? {} to guarantee a plain object regardless.\n const json: JSONContent =\n (await loadExternalFile(path, { logError: false })) ?? {};\n\n const filePath = relative(configuration.system.baseDir, path);\n\n const dictionary: Dictionary = {\n key,\n locale: usedLocale,\n fill: filePath,\n format,\n localId: `${key}::${location}::${filePath}` as LocalDictionaryId,\n location: location as Dictionary['location'],\n filled:\n usedLocale !== configuration.internationalization.defaultLocale\n ? true\n : undefined,\n content: json,\n filePath,\n priority,\n };\n\n dictionaries.push(dictionary);\n }\n\n return dictionaries;\n },\n };\n};\n"],"mappings":";;;;;;;;;;AAwBA,MAAM,eAAe,OACnB,QACA,eACA,mBAC4B;CAC5B,MAAM,EAAE,QAAQ,yBAAyB;CAEzC,MAAM,EAAE,YAAY;CACpB,MAAM,EAAE,YAAY;CAEpB,MAAM,SAAyB,EAAE;AAEjC,MAAK,MAAM,UAAU,SAAS;EAC5B,MAAM,oBAAoB,uDAA2B,QAAQ;GAC3D,KAAK;GACL;GACD,CAAkC;EAEnC,MAAM,oBAAoB,uDAA2B,QAAQ;GAC3D,KAAK;GACL;GACD,CAAkC;AAEnC,MAAI,CAAC,qBAAqB,CAAC,kBACzB;EAOF,MAAM,QAAQ,6BAJgB,kBAAk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OAAO,SAAS,SAAS;GAGzD,MAAM,eAA6B,EAAE;AAErC,QAAK,MAAM,EAAE,MAAM,SAAS,iBAAiB;IAG3C,MAAM,OACH,kDAAuB,MAAM,EAAE,UAAU,OAAO,CAAC,IAAK,EAAE;IAE3D,MAAM,mCAAoB,cAAc,OAAO,SAAS,KAAK;IAE7D,MAAM,aAAyB;KAC7B;KACA,QAAQ;KACR,MAAM;KACN;KACA,SAAS,GAAG,IAAI,IAAI,SAAS,IAAI;KACvB;KACV,QACE,eAAe,cAAc,qBAAqB,gBAC9C,OACA;KACN,SAAS;KACT;KACA;KACD;AAED,iBAAa,KAAK,WAAW;;AAG/B,UAAO;;EAEV"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require(`node:path`),l=require(`@intlayer/config/file`),u=require(`@intlayer/config/utils`),d=require(`fast-glob`);d=s(d);let f=require(`node:fs/promises`);const p=e=>e.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`),m=(e,t,n,r)=>{let i=`{{__KEY__}}`,a=p(t),o=n.join(`|`),s=`^${a}$`;s=s.replace(p(`{{__LOCALE__}}`),`(?<locale>${o})`),t.includes(i)&&(s=s.replace(p(i),`(?<key>.+)`));let c=new RegExp(s).exec(e);if(!c||!c.groups)return null;let l=c.groups.locale,u=c.groups.key??`index`;return u===void 0&&(u=`index`),l===void 0&&(l=r),{key:u,locale:l}},h=async(e,t)=>{let{system:n,internationalization:r}=t,{baseDir:i}=n,{locales:a}=r,o={};for(let t of a){let n=await(0,u.parseFilePathPattern)(e,{key:`**`,locale:t}),r=await(0,u.parseFilePathPattern)(e,{key:`{{__KEY__}}`,locale:t});if(!n||!r)continue;let s=await(0,d.default)(n,{cwd:i});for(let n of s){let s=m(n,r,a,t);if(!s)continue;let{key:l,locale:d}=s,f=await(0,u.parseFilePathPattern)(e,{key:l,locale:d}),p=(0,c.isAbsolute)(n)?n:(0,c.resolve)(i,n);if(p!==((0,c.isAbsolute)(f)?f:(0,c.resolve)(i,f)))continue;let h=d;o[h]||(o[h]={}),o[h][l]=p}}let s=new Set;for(let e of Object.keys(o))for(let t of Object.keys(o[e]??{}))s.add(t);let l=s.size>0?Array.from(s):[];for(let t of a){o[t]||(o[t]={});for(let n of l)if(!o[t][n]){let r=await(0,u.parseFilePathPattern)(e,{key:n,locale:t}),a=(0,c.isAbsolute)(r)?r:(0,c.resolve)(i,r);o[t][n]=a}}return o},g=async(e,t)=>{let n=await h(e,t);return Object.entries(n).flatMap(([e,n])=>Object.entries(n).map(([n,r])=>({path:(0,c.isAbsolute)(r)?r:(0,c.resolve)(t.system.baseDir,r),locale:e,key:n})))},_=async e=>{let{location:t,priority:n,format:r}={location:`sync-json::${await(0,u.parseFilePathPattern)(e.source,{key:`{{key}}`,locale:`{{locale}}`})}`,priority:0,...e};return{name:`sync-json`,loadDictionaries:async({configuration:i})=>{let a=await g(e.source,i),o=await(0,u.parseFilePathPattern)(e.source,{key:`{{key}}`,locale:`{{locale}}`});o&&!(0,c.isAbsolute)(o)&&(o=(0,c.join)(i.system.baseDir,o));let s=[];for(let{locale:e,path:u,key:d}of a){let a={};try{a=await(0,l.loadExternalFile)(u,{logError:!1})}catch{a={}}let f=(0,c.relative)(i.system.baseDir,u),p={key:d,locale:e,fill:o,format:r,localId:`${d}::${t}::${f}`,location:t,filled:e===i.internationalization.defaultLocale?void 0:!0,content:a,filePath:f,priority:n};s.push(p)}return s},formatOutput:async({dictionary:t})=>{let{formatDictionaryOutput:n}=await import(`@intlayer/chokidar/build`);return!t.filePath||!t.locale||(0,c.resolve)(await(0,u.parseFilePathPattern)(e.source,{key:t.key,locale:t.locale}))!==(0,c.resolve)(t.filePath)?t:n({...t,format:r}).content},afterBuild:async({dictionaries:n,configuration:i})=>{let{getPerLocaleDictionary:a}=await import(`@intlayer/core/plugins`),{parallelize:o}=await import(`@intlayer/chokidar/utils`),{formatDictionaryOutput:s}=await import(`@intlayer/chokidar/build`),{locales:l}=i.internationalization;await o(Object.entries(n.mergedDictionaries).flatMap(([e,t])=>l.map(n=>({key:e,dictionary:t.dictionary,locale:n}))),async({key:n,dictionary:i,locale:o})=>{if(i.location!==t)return;let l=await(0,u.parseFilePathPattern)(e.source,{key:n,locale:o}),d=s({...a(i,o),format:r}),p=JSON.parse(JSON.stringify(d.content));p===void 0||typeof p==`object`&&Object.keys(p).length===0||(await(0,f.mkdir)((0,c.dirname)(l),{recursive:!0}),await(0,f.writeFile)(l,`${JSON.stringify(p,null,2)}\n`,`utf-8`))})}}};Object.defineProperty(exports,`n`,{enumerable:!0,get:function(){return _}}),Object.defineProperty(exports,`r`,{enumerable:!0,get:function(){return s}}),Object.defineProperty(exports,`t`,{enumerable:!0,get:function(){return m}});
|
|
2
|
+
//# sourceMappingURL=syncJSON-CTiXS5Pk.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"syncJSON-CTiXS5Pk.cjs","names":[],"sources":["../../src/syncJSON.ts"],"sourcesContent":["import { mkdir, writeFile } from 'node:fs/promises';\nimport { dirname, isAbsolute, join, relative, resolve } from 'node:path';\nimport { loadExternalFile } from '@intlayer/config/file';\nimport { parseFilePathPattern } from '@intlayer/config/utils';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport type { IntlayerConfig } from '@intlayer/types/config';\nimport type {\n Dictionary,\n DictionaryFormat,\n DictionaryLocation,\n LocalDictionaryId,\n} from '@intlayer/types/dictionary';\nimport type {\n FilePathPattern,\n FilePathPatternContext,\n} from '@intlayer/types/filePathPattern';\nimport type { Plugin } from '@intlayer/types/plugin';\nimport fg from 'fast-glob';\n\ntype JSONContent = Record<string, any>;\n\ntype FilePath = string;\n\ntype MessagesRecord = Record<Locale, Record<Dictionary['key'], FilePath>>;\n\nconst escapeRegex = (str: string) => str.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n\nexport const extractKeyAndLocaleFromPath = (\n filePath: string,\n maskPattern: string,\n locales: Locale[],\n defaultLocale: Locale\n): { key: string; locale: Locale } | null => {\n const keyPlaceholder = '{{__KEY__}}';\n const localePlaceholder = '{{__LOCALE__}}';\n\n const escapedMask = escapeRegex(maskPattern);\n const localesAlternation = locales.join('|');\n\n // Build a regex from the mask to capture locale (and key if present)\n let regexStr = `^${escapedMask}$`;\n\n regexStr = regexStr.replace(\n escapeRegex(localePlaceholder),\n `(?<locale>${localesAlternation})`\n );\n\n if (maskPattern.includes(keyPlaceholder)) {\n // FIX: Allow key to match multiple directory levels (e.g. \"nested/file\" or \"folder/index\")\n // Previous value: '(?<key>[^/]+)'\n regexStr = regexStr.replace(escapeRegex(keyPlaceholder), '(?<key>.+)');\n }\n\n const maskRegex = new RegExp(regexStr);\n const match = maskRegex.exec(filePath);\n\n if (!match || !match.groups) {\n return null;\n }\n\n let locale = match.groups.locale as Locale | undefined;\n let key = (match.groups.key as string | undefined) ?? 'index';\n\n if (typeof key === 'undefined') {\n key = 'index';\n }\n\n if (typeof locale === 'undefined') {\n locale = defaultLocale;\n }\n\n return {\n key,\n locale,\n };\n};\n\nconst listMessages = async (\n source: FilePathPattern,\n configuration: IntlayerConfig\n): Promise<MessagesRecord> => {\n const { system, internationalization } = configuration;\n\n const { baseDir } = system;\n const { locales } = internationalization;\n\n const result: MessagesRecord = {} as MessagesRecord;\n\n for (const locale of locales) {\n const globPatternLocale = await parseFilePathPattern(source, {\n key: '**',\n locale,\n } as any as FilePathPatternContext);\n\n const maskPatternLocale = await parseFilePathPattern(source, {\n key: '{{__KEY__}}',\n locale,\n } as any as FilePathPatternContext);\n\n if (!globPatternLocale || !maskPatternLocale) {\n continue;\n }\n\n const files = await fg(globPatternLocale, {\n cwd: baseDir,\n });\n\n for (const file of files) {\n const extraction = extractKeyAndLocaleFromPath(\n file,\n maskPatternLocale,\n locales,\n locale\n );\n\n if (!extraction) {\n continue;\n }\n\n const { key, locale: extractedLocale } = extraction;\n\n // Generate what the path SHOULD be for this key/locale using the current builder\n const expectedPath = await parseFilePathPattern(source, {\n key,\n locale: extractedLocale,\n } as any as FilePathPatternContext);\n\n // Resolve both to absolute paths to ensure safe comparison\n const absoluteFoundPath = isAbsolute(file)\n ? file\n : resolve(baseDir, file);\n const absoluteExpectedPath = isAbsolute(expectedPath)\n ? expectedPath\n : resolve(baseDir, expectedPath);\n\n // If the file found doesn't exactly match the file expected, it belongs to another plugin/structure\n if (absoluteFoundPath !== absoluteExpectedPath) {\n continue;\n }\n\n const usedLocale = extractedLocale as Locale;\n if (!result[usedLocale]) {\n result[usedLocale] = {};\n }\n\n result[usedLocale][key as Dictionary['key']] = absoluteFoundPath;\n }\n }\n\n // Ensure all declared locales are present even if the file doesn't exist yet\n const hasKeyInMask = true; // We check keys manually below\n const discoveredKeys = new Set<string>();\n\n for (const locale of Object.keys(result)) {\n for (const key of Object.keys(result[locale as Locale] ?? {})) {\n discoveredKeys.add(key);\n }\n }\n\n if (!hasKeyInMask) {\n discoveredKeys.add('index');\n }\n\n const keysToEnsure =\n discoveredKeys.size > 0 ? Array.from(discoveredKeys) : [];\n\n for (const locale of locales) {\n if (!result[locale]) {\n result[locale] = {} as Record<Dictionary['key'], FilePath>;\n }\n\n for (const key of keysToEnsure) {\n if (!result[locale][key as Dictionary['key']]) {\n const builtPath = await parseFilePathPattern(source, {\n key,\n locale,\n } as any as FilePathPatternContext);\n const absoluteBuiltPath = isAbsolute(builtPath)\n ? builtPath\n : resolve(baseDir, builtPath);\n\n result[locale][key as Dictionary['key']] = absoluteBuiltPath;\n }\n }\n }\n\n return result;\n};\n\ntype DictionariesMap = { path: string; locale: Locale; key: string }[];\n\nconst loadMessagePathMap = async (\n source: MessagesRecord | FilePathPattern,\n configuration: IntlayerConfig\n) => {\n const messages: MessagesRecord = await listMessages(\n source as FilePathPattern,\n configuration\n );\n\n const dictionariesPathMap: DictionariesMap = Object.entries(messages).flatMap(\n ([locale, keysRecord]) =>\n Object.entries(keysRecord).map(([key, path]) => {\n const absolutePath = isAbsolute(path)\n ? path\n : resolve(configuration.system.baseDir, path);\n\n return {\n path: absolutePath,\n locale,\n key,\n } as DictionariesMap[number];\n })\n );\n\n return dictionariesPathMap;\n};\n\ntype SyncJSONPluginOptions = {\n /**\n * The source of the plugin.\n * Is a function to build the source from the key and locale.\n *\n * ```ts\n * syncJSON({\n * source: ({ key, locale }) => `./messages/${locale}/${key}.json`\n * })\n * ```\n */\n source: FilePathPattern;\n\n /**\n * Because Intlayer transform the JSON files into Dictionary, we need to identify the plugin in the dictionary.\n * Used to identify the plugin in the dictionary.\n *\n * In the case you have multiple plugins, you can use this to identify the plugin in the dictionary.\n *\n * ```ts\n * // Example usage:\n * const config = {\n * plugins: [\n * syncJSON({\n * source: ({ key, locale }) => `./resources/${locale}/${key}.json`,\n * location: 'plugin-i18next',\n * }),\n * syncJSON({\n * source: ({ key, locale }) => `./messages/${locale}/${key}.json`,\n * location: 'plugin-next-intl',\n * }),\n * ]\n * }\n * ```\n */\n location?: DictionaryLocation | (string & {});\n\n /**\n * The priority of the dictionaries created by the plugin.\n *\n * In the case of conflicts with remote dictionaries, or .content files, the dictionary with the highest priority will override the other dictionaries.\n *\n * Default is -1. (.content file priority is 0)\n *\n */\n priority?: number;\n\n /**\n * The format of the dictionaries created by the plugin.\n *\n * Default: 'intlayer'\n *\n * The format of the dictionaries created by the plugin.\n */\n format?: DictionaryFormat;\n};\n\nexport const syncJSON = async (\n options: SyncJSONPluginOptions\n): Promise<Plugin> => {\n // Generate a unique default location based on the source pattern.\n // This ensures that if you have multiple plugins, they don't share the same 'plugin' ID.\n const patternMarker = await parseFilePathPattern(options.source, {\n key: '{{key}}',\n locale: '{{locale}}',\n } as any as FilePathPatternContext);\n const defaultLocation = `sync-json::${patternMarker}`;\n\n const { location, priority, format } = {\n location: defaultLocation,\n priority: 0,\n ...options,\n };\n\n return {\n name: 'sync-json',\n\n loadDictionaries: async ({ configuration }) => {\n const dictionariesMap: DictionariesMap = await loadMessagePathMap(\n options.source,\n configuration\n );\n\n let fill: string = await parseFilePathPattern(options.source, {\n key: '{{key}}',\n locale: '{{locale}}',\n } as any as FilePathPatternContext);\n\n if (fill && !isAbsolute(fill)) {\n fill = join(configuration.system.baseDir, fill);\n }\n\n const dictionaries: Dictionary[] = [];\n\n for (const { locale, path, key } of dictionariesMap) {\n let json: JSONContent = {};\n try {\n json = await loadExternalFile(path, { logError: false });\n } catch {\n json = {};\n }\n\n const filePath = relative(configuration.system.baseDir, path);\n\n const dictionary: Dictionary = {\n key,\n locale,\n fill,\n format,\n localId: `${key}::${location}::${filePath}` as LocalDictionaryId,\n location: location as Dictionary['location'],\n filled:\n locale !== configuration.internationalization.defaultLocale\n ? true\n : undefined,\n content: json,\n filePath,\n priority,\n };\n\n dictionaries.push(dictionary);\n }\n\n return dictionaries;\n },\n\n formatOutput: async ({ dictionary }) => {\n // Lazy import intlayer modules to avoid circular dependencies\n const { formatDictionaryOutput } = await import(\n '@intlayer/chokidar/build'\n );\n\n if (!dictionary.filePath || !dictionary.locale) return dictionary;\n\n const builderPath = await parseFilePathPattern(options.source, {\n key: dictionary.key,\n locale: dictionary.locale,\n } as any as FilePathPatternContext);\n\n // Verification to ensure we are formatting the correct file\n if (resolve(builderPath) !== resolve(dictionary.filePath)) {\n return dictionary;\n }\n\n const dictionaryWithFormat = {\n ...dictionary,\n format,\n };\n\n const formattedOutput = formatDictionaryOutput(\n dictionaryWithFormat as Dictionary\n );\n\n return formattedOutput.content;\n },\n\n afterBuild: async ({ dictionaries, configuration }) => {\n // Lazy import intlayer modules to avoid circular dependencies\n const { getPerLocaleDictionary } = await import('@intlayer/core/plugins');\n const { parallelize } = await import('@intlayer/chokidar/utils');\n const { formatDictionaryOutput } = await import(\n '@intlayer/chokidar/build'\n );\n\n const { locales } = configuration.internationalization;\n\n type RecordList = {\n key: string;\n dictionary: Dictionary;\n locale: Locale;\n };\n\n // We get all dictionaries, but we need to filter them\n const recordList: RecordList[] = Object.entries(\n dictionaries.mergedDictionaries\n ).flatMap(([key, dictionary]) =>\n locales.map((locale) => ({\n key,\n dictionary: dictionary.dictionary as Dictionary,\n locale,\n }))\n );\n\n await parallelize(recordList, async ({ key, dictionary, locale }) => {\n // Only process dictionaries that belong to THIS plugin instance.\n if (dictionary.location !== location) {\n return;\n }\n\n const builderPath = await parseFilePathPattern(options.source, {\n key,\n locale,\n } as any as FilePathPatternContext);\n\n const localizedDictionary = getPerLocaleDictionary(dictionary, locale);\n\n const dictionaryWithFormat = {\n ...localizedDictionary,\n format,\n };\n\n const formattedOutput = formatDictionaryOutput(dictionaryWithFormat);\n\n const content = JSON.parse(JSON.stringify(formattedOutput.content));\n\n if (\n typeof content === 'undefined' ||\n (typeof content === 'object' &&\n Object.keys(content as Record<string, unknown>).length === 0)\n ) {\n return;\n }\n\n await mkdir(dirname(builderPath), { recursive: true });\n\n const stringContent = JSON.stringify(content, null, 2);\n\n await writeFile(builderPath, `${stringContent}\\n`, 'utf-8');\n });\n },\n };\n};\n"],"mappings":"goBAyBA,MAAM,EAAe,GAAgB,EAAI,QAAQ,sBAAuB,OAAO,CAElE,GACX,EACA,EACA,EACA,IAC2C,CAC3C,IAAM,EAAiB,cAGjB,EAAc,EAAY,EAAY,CACtC,EAAqB,EAAQ,KAAK,IAAI,CAGxC,EAAW,IAAI,EAAY,GAE/B,EAAW,EAAS,QAClB,EAAY,iBAAkB,CAC9B,aAAa,EAAmB,GACjC,CAEG,EAAY,SAAS,EAAe,GAGtC,EAAW,EAAS,QAAQ,EAAY,EAAe,CAAE,aAAa,EAIxE,IAAM,EADY,I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D,CAAkC,CAS7B,EAAkB,EALK,CAC3B,GAH0B,EAAuB,EAAY,EAAO,CAIpE,SACD,CAEmE,CAE9D,EAAU,KAAK,MAAM,KAAK,UAAU,EAAgB,QAAQ,CAAC,CAG1D,IAAY,QAClB,OAAO,GAAY,UAClB,OAAO,KAAK,EAAmC,CAAC,SAAW,IAK/D,MAAA,EAAA,EAAA,QAAA,EAAA,EAAA,SAAoB,EAAY,CAAE,CAAE,UAAW,GAAM,CAAC,CAItD,MAAA,EAAA,EAAA,WAAgB,EAAa,GAFP,KAAK,UAAU,EAAS,KAAM,EAAE,CAER,IAAK,QAAQ,GAC3D,EAEL"}
|
package/dist/cjs/syncJSON.cjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
2
|
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
3
3
|
let node_path = require("node:path");
|
|
4
|
-
let
|
|
4
|
+
let _intlayer_config_file = require("@intlayer/config/file");
|
|
5
|
+
let _intlayer_config_utils = require("@intlayer/config/utils");
|
|
5
6
|
let fast_glob = require("fast-glob");
|
|
6
7
|
fast_glob = require_runtime.__toESM(fast_glob);
|
|
7
8
|
let node_fs_promises = require("node:fs/promises");
|
|
@@ -11,13 +12,17 @@ const escapeRegex = (str) => str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
|
11
12
|
const extractKeyAndLocaleFromPath = (filePath, maskPattern, locales, defaultLocale) => {
|
|
12
13
|
const keyPlaceholder = "{{__KEY__}}";
|
|
13
14
|
const localePlaceholder = "{{__LOCALE__}}";
|
|
14
|
-
const
|
|
15
|
+
const normalize = (path) => path.startsWith("./") ? path.slice(2) : path;
|
|
16
|
+
const normalizedFilePath = normalize(filePath);
|
|
17
|
+
const normalizedMask = normalize(maskPattern);
|
|
15
18
|
const localesAlternation = locales.join("|");
|
|
16
|
-
let regexStr = `^${
|
|
19
|
+
let regexStr = `^${escapeRegex(normalizedMask)}$`;
|
|
20
|
+
regexStr = regexStr.replace(/\\\*\\\*/g, ".*");
|
|
21
|
+
regexStr = regexStr.replace(/\\\*/g, "[^/]*");
|
|
17
22
|
regexStr = regexStr.replace(escapeRegex(localePlaceholder), `(?<locale>${localesAlternation})`);
|
|
18
|
-
if (
|
|
19
|
-
const match = new RegExp(regexStr).exec(
|
|
20
|
-
if (!match
|
|
23
|
+
if (normalizedMask.includes(keyPlaceholder)) regexStr = regexStr.replace(escapeRegex(keyPlaceholder), "(?<key>.+)");
|
|
24
|
+
const match = new RegExp(regexStr).exec(normalizedFilePath);
|
|
25
|
+
if (!match?.groups) return null;
|
|
21
26
|
let locale = match.groups.locale;
|
|
22
27
|
let key = match.groups.key ?? "index";
|
|
23
28
|
if (typeof key === "undefined") key = "index";
|
|
@@ -27,35 +32,41 @@ const extractKeyAndLocaleFromPath = (filePath, maskPattern, locales, defaultLoca
|
|
|
27
32
|
locale
|
|
28
33
|
};
|
|
29
34
|
};
|
|
30
|
-
const listMessages = (
|
|
31
|
-
const {
|
|
32
|
-
const baseDir =
|
|
33
|
-
const locales = internationalization
|
|
34
|
-
const defaultLocale = internationalization.defaultLocale;
|
|
35
|
-
const globPattern = builder({
|
|
36
|
-
key: "**",
|
|
37
|
-
locale: `{${locales.map((locale) => locale).join(",")}}`
|
|
38
|
-
});
|
|
39
|
-
const maskPattern = builder({
|
|
40
|
-
key: "{{__KEY__}}",
|
|
41
|
-
locale: "{{__LOCALE__}}"
|
|
42
|
-
});
|
|
43
|
-
const files = fast_glob.default.sync(globPattern, { cwd: baseDir });
|
|
35
|
+
const listMessages = async (source, configuration) => {
|
|
36
|
+
const { system, internationalization } = configuration;
|
|
37
|
+
const { baseDir } = system;
|
|
38
|
+
const { locales } = internationalization;
|
|
44
39
|
const result = {};
|
|
45
|
-
for (const
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
const { key, locale } = extraction;
|
|
49
|
-
const expectedPath = builder({
|
|
50
|
-
key,
|
|
40
|
+
for (const locale of locales) {
|
|
41
|
+
const globPatternLocale = await (0, _intlayer_config_utils.parseFilePathPattern)(source, {
|
|
42
|
+
key: "**",
|
|
51
43
|
locale
|
|
52
44
|
});
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
45
|
+
const maskPatternLocale = await (0, _intlayer_config_utils.parseFilePathPattern)(source, {
|
|
46
|
+
key: "{{__KEY__}}",
|
|
47
|
+
locale
|
|
48
|
+
});
|
|
49
|
+
if (!globPatternLocale || !maskPatternLocale) continue;
|
|
50
|
+
const files = await (0, fast_glob.default)(globPatternLocale.startsWith("./") ? globPatternLocale.slice(2) : globPatternLocale, { cwd: baseDir });
|
|
51
|
+
for (const file of files) {
|
|
52
|
+
const extraction = extractKeyAndLocaleFromPath(file, maskPatternLocale, locales, locale);
|
|
53
|
+
if (!extraction) continue;
|
|
54
|
+
const { key, locale: extractedLocale } = extraction;
|
|
55
|
+
const expectedPath = await (0, _intlayer_config_utils.parseFilePathPattern)(source, {
|
|
56
|
+
key,
|
|
57
|
+
locale: extractedLocale
|
|
58
|
+
});
|
|
59
|
+
const absoluteFoundPath = (0, node_path.isAbsolute)(file) ? file : (0, node_path.resolve)(baseDir, file);
|
|
60
|
+
if (absoluteFoundPath !== ((0, node_path.isAbsolute)(expectedPath) ? expectedPath : (0, node_path.resolve)(baseDir, expectedPath))) continue;
|
|
61
|
+
const usedLocale = extractedLocale;
|
|
62
|
+
if (!result[usedLocale]) result[usedLocale] = {};
|
|
63
|
+
result[usedLocale][key] = absoluteFoundPath;
|
|
64
|
+
}
|
|
57
65
|
}
|
|
58
|
-
const hasKeyInMask =
|
|
66
|
+
const hasKeyInMask = (await (0, _intlayer_config_utils.parseFilePathPattern)(source, {
|
|
67
|
+
key: "{{__KEY__}}",
|
|
68
|
+
locale: locales[0]
|
|
69
|
+
})).includes("{{__KEY__}}");
|
|
59
70
|
const discoveredKeys = /* @__PURE__ */ new Set();
|
|
60
71
|
for (const locale of Object.keys(result)) for (const key of Object.keys(result[locale] ?? {})) discoveredKeys.add(key);
|
|
61
72
|
if (!hasKeyInMask) discoveredKeys.add("index");
|
|
@@ -63,7 +74,7 @@ const listMessages = (builder, configuration) => {
|
|
|
63
74
|
for (const locale of locales) {
|
|
64
75
|
if (!result[locale]) result[locale] = {};
|
|
65
76
|
for (const key of keysToEnsure) if (!result[locale][key]) {
|
|
66
|
-
const builtPath =
|
|
77
|
+
const builtPath = await (0, _intlayer_config_utils.parseFilePathPattern)(source, {
|
|
67
78
|
key,
|
|
68
79
|
locale
|
|
69
80
|
});
|
|
@@ -73,19 +84,19 @@ const listMessages = (builder, configuration) => {
|
|
|
73
84
|
}
|
|
74
85
|
return result;
|
|
75
86
|
};
|
|
76
|
-
const loadMessagePathMap = (source, configuration) => {
|
|
77
|
-
const messages = listMessages(source, configuration);
|
|
87
|
+
const loadMessagePathMap = async (source, configuration) => {
|
|
88
|
+
const messages = await listMessages(source, configuration);
|
|
78
89
|
return Object.entries(messages).flatMap(([locale, keysRecord]) => Object.entries(keysRecord).map(([key, path]) => {
|
|
79
90
|
return {
|
|
80
|
-
path: (0, node_path.isAbsolute)(path) ? path : (0, node_path.resolve)(configuration.
|
|
91
|
+
path: (0, node_path.isAbsolute)(path) ? path : (0, node_path.resolve)(configuration.system.baseDir, path),
|
|
81
92
|
locale,
|
|
82
93
|
key
|
|
83
94
|
};
|
|
84
95
|
}));
|
|
85
96
|
};
|
|
86
|
-
const syncJSON = (options) => {
|
|
97
|
+
const syncJSON = async (options) => {
|
|
87
98
|
const { location, priority, format } = {
|
|
88
|
-
location: `sync-json::${options.source
|
|
99
|
+
location: `sync-json::${await (0, _intlayer_config_utils.parseFilePathPattern)(options.source, {
|
|
89
100
|
key: "{{key}}",
|
|
90
101
|
locale: "{{locale}}"
|
|
91
102
|
})}`,
|
|
@@ -95,22 +106,16 @@ const syncJSON = (options) => {
|
|
|
95
106
|
return {
|
|
96
107
|
name: "sync-json",
|
|
97
108
|
loadDictionaries: async ({ configuration }) => {
|
|
98
|
-
const dictionariesMap = loadMessagePathMap(options.source, configuration);
|
|
99
|
-
let fill = options.source
|
|
109
|
+
const dictionariesMap = await loadMessagePathMap(options.source, configuration);
|
|
110
|
+
let fill = await (0, _intlayer_config_utils.parseFilePathPattern)(options.source, {
|
|
100
111
|
key: "{{key}}",
|
|
101
112
|
locale: "{{locale}}"
|
|
102
113
|
});
|
|
103
|
-
if (fill && !(0, node_path.isAbsolute)(fill)) fill = (0, node_path.join)(configuration.
|
|
114
|
+
if (fill && !(0, node_path.isAbsolute)(fill)) fill = (0, node_path.join)(configuration.system.baseDir, fill);
|
|
104
115
|
const dictionaries = [];
|
|
105
116
|
for (const { locale, path, key } of dictionariesMap) {
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
try {
|
|
109
|
-
json = requireFunction(path);
|
|
110
|
-
} catch {
|
|
111
|
-
json = {};
|
|
112
|
-
}
|
|
113
|
-
const filePath = (0, node_path.relative)(configuration.content.baseDir, path);
|
|
117
|
+
const json = await (0, _intlayer_config_file.loadExternalFile)(path, { logError: false }) ?? {};
|
|
118
|
+
const filePath = (0, node_path.relative)(configuration.system.baseDir, path);
|
|
114
119
|
const dictionary = {
|
|
115
120
|
key,
|
|
116
121
|
locale,
|
|
@@ -128,35 +133,30 @@ const syncJSON = (options) => {
|
|
|
128
133
|
return dictionaries;
|
|
129
134
|
},
|
|
130
135
|
formatOutput: async ({ dictionary }) => {
|
|
131
|
-
const { formatDictionaryOutput } = await import("@intlayer/chokidar");
|
|
136
|
+
const { formatDictionaryOutput } = await import("@intlayer/chokidar/build");
|
|
132
137
|
if (!dictionary.filePath || !dictionary.locale) return dictionary;
|
|
133
|
-
if ((0, node_path.resolve)(options.source
|
|
138
|
+
if ((0, node_path.resolve)(await (0, _intlayer_config_utils.parseFilePathPattern)(options.source, {
|
|
134
139
|
key: dictionary.key,
|
|
135
140
|
locale: dictionary.locale
|
|
136
141
|
})) !== (0, node_path.resolve)(dictionary.filePath)) return dictionary;
|
|
137
|
-
return formatDictionaryOutput(
|
|
138
|
-
...dictionary,
|
|
139
|
-
format
|
|
140
|
-
}).content;
|
|
142
|
+
return formatDictionaryOutput(dictionary, format).content;
|
|
141
143
|
},
|
|
142
144
|
afterBuild: async ({ dictionaries, configuration }) => {
|
|
143
|
-
const { getPerLocaleDictionary } = await import("@intlayer/core");
|
|
144
|
-
const { parallelize
|
|
145
|
-
const
|
|
145
|
+
const { getPerLocaleDictionary } = await import("@intlayer/core/plugins");
|
|
146
|
+
const { parallelize } = await import("@intlayer/chokidar/utils");
|
|
147
|
+
const { formatDictionaryOutput } = await import("@intlayer/chokidar/build");
|
|
148
|
+
const { locales } = configuration.internationalization;
|
|
146
149
|
await parallelize(Object.entries(dictionaries.mergedDictionaries).flatMap(([key, dictionary]) => locales.map((locale) => ({
|
|
147
150
|
key,
|
|
148
151
|
dictionary: dictionary.dictionary,
|
|
149
152
|
locale
|
|
150
153
|
}))), async ({ key, dictionary, locale }) => {
|
|
151
154
|
if (dictionary.location !== location) return;
|
|
152
|
-
const builderPath = options.source
|
|
155
|
+
const builderPath = await (0, _intlayer_config_utils.parseFilePathPattern)(options.source, {
|
|
153
156
|
key,
|
|
154
157
|
locale
|
|
155
158
|
});
|
|
156
|
-
const formattedOutput = formatDictionaryOutput(
|
|
157
|
-
...getPerLocaleDictionary(dictionary, locale),
|
|
158
|
-
format
|
|
159
|
-
});
|
|
159
|
+
const formattedOutput = formatDictionaryOutput(getPerLocaleDictionary(dictionary, locale), format);
|
|
160
160
|
const content = JSON.parse(JSON.stringify(formattedOutput.content));
|
|
161
161
|
if (typeof content === "undefined" || typeof content === "object" && Object.keys(content).length === 0) return;
|
|
162
162
|
await (0, node_fs_promises.mkdir)((0, node_path.dirname)(builderPath), { recursive: true });
|