@gen-epix/merge-locales 0.0.1 → 0.0.2
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 +1 -1
- package/bin/index.d.ts +13 -3
- package/bin/index.js +19 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -35,4 +35,4 @@ export default {
|
|
|
35
35
|
};
|
|
36
36
|
```
|
|
37
37
|
|
|
38
|
-
Paths are resolved relative to Vite's configured root. Later source directories take precedence.
|
|
38
|
+
Paths are resolved relative to Vite's configured root. Locale files are merged when the dev server starts and whenever a source locale changes, with a full page reload. Production builds merge the files during the build lifecycle. Later source directories take precedence.
|
package/bin/index.d.ts
CHANGED
|
@@ -4,12 +4,22 @@ interface MergeLocalesOptions {
|
|
|
4
4
|
}
|
|
5
5
|
|
|
6
6
|
interface MergeLocalesPlugin {
|
|
7
|
-
buildStart: (
|
|
7
|
+
buildStart: () => void;
|
|
8
8
|
configResolved: (config: ResolvedConfig) => void;
|
|
9
|
+
configureServer: (server: DevServer) => void;
|
|
9
10
|
name: string;
|
|
10
11
|
}
|
|
11
|
-
interface
|
|
12
|
-
|
|
12
|
+
interface DevServer {
|
|
13
|
+
watcher: {
|
|
14
|
+
add: (paths: string[]) => void;
|
|
15
|
+
on: (event: 'add' | 'change' | 'unlink', handler: (filePath: string) => void) => void;
|
|
16
|
+
};
|
|
17
|
+
ws: {
|
|
18
|
+
send: (payload: {
|
|
19
|
+
path: string;
|
|
20
|
+
type: 'full-reload';
|
|
21
|
+
}) => void;
|
|
22
|
+
};
|
|
13
23
|
}
|
|
14
24
|
interface ResolvedConfig {
|
|
15
25
|
root: string;
|
package/bin/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { join, extname, basename, resolve } from 'path';
|
|
1
|
+
import { join, extname, basename, resolve, sep } from 'path';
|
|
2
2
|
import { mkdirSync, writeFileSync, readdirSync, readFileSync } from 'fs';
|
|
3
3
|
|
|
4
4
|
const isJsonObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
@@ -26,7 +26,6 @@ const readLocaleFiles = (sourceDirectory) => {
|
|
|
26
26
|
}
|
|
27
27
|
return localeFiles;
|
|
28
28
|
};
|
|
29
|
-
const getLocaleFilePaths = (sourceDirectories) => sourceDirectories.flatMap((sourceDirectory) => [...readLocaleFiles(sourceDirectory).values()].flat());
|
|
30
29
|
const readJsonFile = (filePath) => {
|
|
31
30
|
try {
|
|
32
31
|
return JSON.parse(readFileSync(filePath, "utf-8"));
|
|
@@ -71,11 +70,8 @@ const mergeLocales = (options) => {
|
|
|
71
70
|
sourceDirectories: options.sourceDirectories.map((sourceDirectory) => resolve(sourceDirectory))
|
|
72
71
|
};
|
|
73
72
|
return {
|
|
74
|
-
buildStart() {
|
|
73
|
+
buildStart: () => {
|
|
75
74
|
mergeLocales$1(resolvedOptions);
|
|
76
|
-
for (const sourceFilePath of getLocaleFilePaths(resolvedOptions.sourceDirectories)) {
|
|
77
|
-
this.addWatchFile(sourceFilePath);
|
|
78
|
-
}
|
|
79
75
|
},
|
|
80
76
|
configResolved: (config) => {
|
|
81
77
|
resolvedOptions = {
|
|
@@ -83,6 +79,23 @@ const mergeLocales = (options) => {
|
|
|
83
79
|
sourceDirectories: options.sourceDirectories.map((sourceDirectory) => resolve(config.root, sourceDirectory))
|
|
84
80
|
};
|
|
85
81
|
},
|
|
82
|
+
configureServer: (server) => {
|
|
83
|
+
const mergeAndReload = () => {
|
|
84
|
+
mergeLocales$1(resolvedOptions);
|
|
85
|
+
server.ws.send({ path: "*", type: "full-reload" });
|
|
86
|
+
};
|
|
87
|
+
mergeLocales$1(resolvedOptions);
|
|
88
|
+
server.watcher.add(resolvedOptions.sourceDirectories);
|
|
89
|
+
for (const event of ["add", "change", "unlink"]) {
|
|
90
|
+
server.watcher.on(event, (filePath) => {
|
|
91
|
+
const isLocaleFile = filePath.endsWith(".json");
|
|
92
|
+
const isSourceFile = resolvedOptions.sourceDirectories.some((sourceDirectory) => filePath.startsWith(`${sourceDirectory}${sep}`));
|
|
93
|
+
if (isLocaleFile && isSourceFile) {
|
|
94
|
+
mergeAndReload();
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
},
|
|
86
99
|
name: "merge-locales"
|
|
87
100
|
};
|
|
88
101
|
};
|