@astroscope/i18n 0.1.0 → 0.2.1

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.
Files changed (188) hide show
  1. package/README.md +23 -1
  2. package/dist/{chunk-FAKBSLNW.js → chunk-7MSGHEST.js} +14 -7
  3. package/dist/{chunk-JJEDOJBR.js → chunk-EO4FUQXR.js} +49 -18
  4. package/dist/{chunk-QFAAGCP5.js → chunk-RHWEV4L2.js} +58 -1
  5. package/dist/client/directives/idle.d.ts +4 -1
  6. package/dist/client/directives/idle.js +13 -6
  7. package/dist/client/directives/load.d.ts +4 -1
  8. package/dist/client/directives/load.js +6 -6
  9. package/dist/client/directives/media.d.ts +4 -1
  10. package/dist/client/directives/media.js +16 -6
  11. package/dist/client/directives/only.d.ts +6 -1
  12. package/dist/client/directives/only.js +6 -6
  13. package/dist/client/directives/utils.d.ts +6 -5
  14. package/dist/client/directives/utils.js +3 -3
  15. package/dist/client/directives/visible.d.ts +4 -1
  16. package/dist/client/directives/visible.js +23 -6
  17. package/dist/client/t.d.ts +2 -2
  18. package/dist/extraction/index.d.ts +3 -3
  19. package/dist/extraction/index.js +1 -1
  20. package/dist/{i18n-BGfrSNM8.d.ts → i18n-mTHTcD8K.d.ts} +9 -2
  21. package/dist/index.d.ts +26 -3
  22. package/dist/index.js +6 -2
  23. package/dist/internal.d.ts +4 -4
  24. package/dist/internal.js +1 -1
  25. package/dist/server/t.d.ts +1 -1
  26. package/dist/server/t.js +1 -1
  27. package/dist/{types-BaaSiz9n.d.ts → types-BeDFfFIL.d.ts} +1 -1
  28. package/dist/{types-Bzp6KxsI.d.ts → types-DNRFj1XA.d.ts} +1 -0
  29. package/dist/{types-DsNK_9sI.d.ts → types-DTUho6gi.d.ts} +7 -1
  30. package/package.json +8 -7
  31. package/src/components/I18nScript.astro +2 -23
  32. package/dist/babel-plugin-FUTQRKLT.js +0 -121
  33. package/dist/chunk-22ZZFFC2.js +0 -159
  34. package/dist/chunk-2PSXD3JJ.js +0 -144
  35. package/dist/chunk-3VI62RLT.js +0 -176
  36. package/dist/chunk-4DAQPWXI.js +0 -36
  37. package/dist/chunk-4OGX6AF5.js +0 -10
  38. package/dist/chunk-4PHYKXO3.js +0 -227
  39. package/dist/chunk-4UBFISFY.js +0 -376
  40. package/dist/chunk-5DERF6DV.js +0 -266
  41. package/dist/chunk-5KGNHFTC.js +0 -344
  42. package/dist/chunk-5PP4D2VK.js +0 -23
  43. package/dist/chunk-6BW3PN23.js +0 -14
  44. package/dist/chunk-6ESQ4JQI.js +0 -263
  45. package/dist/chunk-A7MPXMOP.js +0 -202
  46. package/dist/chunk-AWZ2SQDW.js +0 -149
  47. package/dist/chunk-AYY2YIBQ.js +0 -52
  48. package/dist/chunk-BMNEJFTD.js +0 -56
  49. package/dist/chunk-BSXQGEUK.js +0 -16
  50. package/dist/chunk-BSYTJFGF.js +0 -21
  51. package/dist/chunk-BVPXEGTT.js +0 -12
  52. package/dist/chunk-C4DJYWF6.js +0 -193
  53. package/dist/chunk-C5EJYE2P.js +0 -245
  54. package/dist/chunk-CLNOQHMS.js +0 -254
  55. package/dist/chunk-E5MCIRSU.js +0 -144
  56. package/dist/chunk-E7ZJS7WX.js +0 -162
  57. package/dist/chunk-ESFGCYYH.js +0 -24
  58. package/dist/chunk-FHBVI3TL.js +0 -372
  59. package/dist/chunk-FI5KZ5IO.js +0 -17
  60. package/dist/chunk-H5R6YMOL.js +0 -233
  61. package/dist/chunk-H62K3FGA.js +0 -236
  62. package/dist/chunk-HDG2R6XT.js +0 -211
  63. package/dist/chunk-HGXU5QAK.js +0 -344
  64. package/dist/chunk-HHRMR3TM.js +0 -17
  65. package/dist/chunk-HS4HVBAD.js +0 -127
  66. package/dist/chunk-IPJWKKHJ.js +0 -324
  67. package/dist/chunk-ITTL4V7J.js +0 -24
  68. package/dist/chunk-IWUBCMOK.js +0 -128
  69. package/dist/chunk-JJBBY2GR.js +0 -14
  70. package/dist/chunk-JTUKXNIJ.js +0 -23
  71. package/dist/chunk-K7YULG4A.js +0 -14
  72. package/dist/chunk-KNONI6DP.js +0 -89
  73. package/dist/chunk-LMTEO22Y.js +0 -17
  74. package/dist/chunk-LRLZEX4D.js +0 -147
  75. package/dist/chunk-MFKPLUAN.js +0 -241
  76. package/dist/chunk-MIWXQOO3.js +0 -316
  77. package/dist/chunk-MJRTAWPR.js +0 -261
  78. package/dist/chunk-MZEPQCXX.js +0 -249
  79. package/dist/chunk-NHHTSFA7.js +0 -237
  80. package/dist/chunk-NTTXM7AG.js +0 -23
  81. package/dist/chunk-NZFAVJXR.js +0 -192
  82. package/dist/chunk-OFNFSDW3.js +0 -13
  83. package/dist/chunk-PG3XDNGF.js +0 -227
  84. package/dist/chunk-PVEVTPNK.js +0 -139
  85. package/dist/chunk-QAO4BXS4.js +0 -244
  86. package/dist/chunk-QCTULQQK.js +0 -34
  87. package/dist/chunk-QKND2KCA.js +0 -163
  88. package/dist/chunk-QZEGY6RJ.js +0 -151
  89. package/dist/chunk-RAE2L6FT.js +0 -193
  90. package/dist/chunk-S3ABJBU3.js +0 -255
  91. package/dist/chunk-SVDVCPCV.js +0 -236
  92. package/dist/chunk-T35ZUWTL.js +0 -153
  93. package/dist/chunk-T6WV5DW6.js +0 -156
  94. package/dist/chunk-TGUXVDA3.js +0 -82
  95. package/dist/chunk-TYMO7K3A.js +0 -173
  96. package/dist/chunk-V4RBFLQL.js +0 -23
  97. package/dist/chunk-V4XWJXXZ.js +0 -23
  98. package/dist/chunk-VQCY3OKK.js +0 -258
  99. package/dist/chunk-WLGOJPDJ.js +0 -15
  100. package/dist/chunk-WUEPDJIL.js +0 -259
  101. package/dist/chunk-X3L7WZLY.js +0 -254
  102. package/dist/chunk-XDD62ZM3.js +0 -255
  103. package/dist/chunk-XFXXEDRD.js +0 -36
  104. package/dist/chunk-XKYOR5UK.js +0 -8
  105. package/dist/chunk-Y26535SI.js +0 -239
  106. package/dist/chunk-Y3PQX5TX.js +0 -152
  107. package/dist/chunk-Y4BE2V6F.js +0 -347
  108. package/dist/chunk-Z4IZYDIA.js +0 -24
  109. package/dist/chunk-Z52G4CYY.js +0 -14
  110. package/dist/chunk-ZGLUYWVM.js +0 -128
  111. package/dist/client/import-with-i18n.d.ts +0 -16
  112. package/dist/client/import-with-i18n.js +0 -8
  113. package/dist/client-directives/idle.d.ts +0 -9
  114. package/dist/client-directives/idle.js +0 -15
  115. package/dist/client-directives/load.d.ts +0 -9
  116. package/dist/client-directives/load.js +0 -15
  117. package/dist/client-directives/media.d.ts +0 -9
  118. package/dist/client-directives/media.js +0 -15
  119. package/dist/client-directives/only.d.ts +0 -9
  120. package/dist/client-directives/only.js +0 -15
  121. package/dist/client-directives/utils.d.ts +0 -9
  122. package/dist/client-directives/utils.js +0 -7
  123. package/dist/client-directives/visible.d.ts +0 -9
  124. package/dist/client-directives/visible.js +0 -15
  125. package/dist/i18n-3cZHoHwP.d.ts +0 -86
  126. package/dist/i18n-BHU81LMA.d.ts +0 -55
  127. package/dist/i18n-BagoeH4_.d.ts +0 -62
  128. package/dist/i18n-BeL84090.d.ts +0 -77
  129. package/dist/i18n-BeRWDfHi.d.ts +0 -55
  130. package/dist/i18n-Bfs2PVcf.d.ts +0 -79
  131. package/dist/i18n-BrL7p3aN.d.ts +0 -99
  132. package/dist/i18n-CNQNCFU0.d.ts +0 -55
  133. package/dist/i18n-CX_uU8V2.d.ts +0 -80
  134. package/dist/i18n-Cr1QZCwe.d.ts +0 -64
  135. package/dist/i18n-CzTRxGeX.d.ts +0 -80
  136. package/dist/i18n-DUOFiC7g.d.ts +0 -77
  137. package/dist/i18n-DcF47BHS.d.ts +0 -77
  138. package/dist/i18n-DifWvtaO.d.ts +0 -60
  139. package/dist/i18n-DumSqX1t.d.ts +0 -55
  140. package/dist/i18n-fWTJw12h.d.ts +0 -80
  141. package/dist/import-with-i18n.d.ts +0 -23
  142. package/dist/import-with-i18n.js +0 -7
  143. package/dist/init.d.ts +0 -2
  144. package/dist/init.js +0 -8
  145. package/dist/middleware-entrypoint-C0Drh9E_.d.ts +0 -29
  146. package/dist/middleware-entrypoint-mEl66IUy.d.ts +0 -29
  147. package/dist/middleware-entrypoint.d.ts +0 -5
  148. package/dist/middleware-entrypoint.js +0 -30
  149. package/dist/server/middleware-entrypoint.d.ts +0 -2
  150. package/dist/server/middleware-entrypoint.js +0 -28
  151. package/dist/t-BY_C9lZ1.d.ts +0 -30
  152. package/dist/t-CG3yDiEa.d.ts +0 -30
  153. package/dist/t-CHsBfrPr.d.ts +0 -30
  154. package/dist/t-DQpXaDhP.d.ts +0 -30
  155. package/dist/t.client.d.ts +0 -11
  156. package/dist/t.client.js +0 -35
  157. package/dist/t.server.d.ts +0 -22
  158. package/dist/t.server.js +0 -12
  159. package/dist/types-8uKCwFda.d.ts +0 -24
  160. package/dist/types-BC6K-pID.d.ts +0 -110
  161. package/dist/types-BUV7BZML.d.ts +0 -55
  162. package/dist/types-BWGB322F.d.ts +0 -46
  163. package/dist/types-B_-RxW4A.d.ts +0 -7
  164. package/dist/types-Bgl2V5xo.d.ts +0 -102
  165. package/dist/types-BwhB6IT4.d.ts +0 -111
  166. package/dist/types-C6rOJFY2.d.ts +0 -24
  167. package/dist/types-C7lD-Woj.d.ts +0 -50
  168. package/dist/types-CLWZeQhD.d.ts +0 -54
  169. package/dist/types-COXt613w.d.ts +0 -19
  170. package/dist/types-CUlkbl9-.d.ts +0 -50
  171. package/dist/types-CVv7GGPj.d.ts +0 -46
  172. package/dist/types-CiIDN2sW.d.ts +0 -64
  173. package/dist/types-Cr86qjYl.d.ts +0 -58
  174. package/dist/types-CxnObiEO.d.ts +0 -7
  175. package/dist/types-D44mFUjA.d.ts +0 -97
  176. package/dist/types-D85m9UTC.d.ts +0 -137
  177. package/dist/types-DWhsmSPP.d.ts +0 -102
  178. package/dist/types-DdprXGJ1.d.ts +0 -46
  179. package/dist/types-DhVJ2ZIo.d.ts +0 -43
  180. package/dist/types-DnbOuM1L.d.ts +0 -7
  181. package/dist/types-DvWBr9Xp.d.ts +0 -17
  182. package/dist/types-DwkIalh6.d.ts +0 -24
  183. package/dist/types-OYRgjtE_.d.ts +0 -110
  184. package/dist/types-SV8S05GX.d.ts +0 -7
  185. package/dist/types-TYn9WnxY.d.ts +0 -46
  186. package/dist/types-aXCBbdhq.d.ts +0 -55
  187. package/dist/types-gn4aNKGx.d.ts +0 -54
  188. package/dist/types-nippsMkm.d.ts +0 -54
@@ -1,344 +0,0 @@
1
- import {
2
- chunkIdToName
3
- } from "./chunk-4A5BKHFN.js";
4
-
5
- // src/extraction/vite-plugin.ts
6
- import { transformAsync } from "@babel/core";
7
- import fs from "fs";
8
- import path from "path";
9
-
10
- // src/extraction/babel-plugin.ts
11
- function getStringValue(t, node) {
12
- if (t.isStringLiteral(node)) {
13
- return node.value;
14
- }
15
- if (t.isTemplateLiteral(node) && node.quasis.length === 1 && node.expressions.length === 0) {
16
- return node.quasis[0]?.value.cooked ?? null;
17
- }
18
- return null;
19
- }
20
- function extractMeta(t, node) {
21
- const stringValue = getStringValue(t, node);
22
- if (stringValue !== null) {
23
- return { fallback: stringValue };
24
- }
25
- if (t.isObjectExpression(node)) {
26
- return extractMetaFromObject(t, node);
27
- }
28
- return { fallback: "" };
29
- }
30
- function extractMetaFromObject(t, obj) {
31
- const meta = { fallback: "" };
32
- for (const prop of obj.properties) {
33
- if (!t.isObjectProperty(prop)) continue;
34
- if (!t.isIdentifier(prop.key)) continue;
35
- const name = prop.key.name;
36
- if (name === "fallback") {
37
- const value = getStringValue(t, prop.value);
38
- if (value !== null) {
39
- meta.fallback = value;
40
- }
41
- } else if (name === "description") {
42
- const value = getStringValue(t, prop.value);
43
- if (value !== null) {
44
- meta.description = value;
45
- }
46
- } else if (name === "variables" && t.isObjectExpression(prop.value)) {
47
- meta.variables = extractVariables(t, prop.value);
48
- }
49
- }
50
- return meta;
51
- }
52
- function extractVariables(t, obj) {
53
- const result = {};
54
- for (const prop of obj.properties) {
55
- if (!t.isObjectProperty(prop)) continue;
56
- if (!t.isIdentifier(prop.key)) continue;
57
- const varName = prop.key.name;
58
- if (t.isObjectExpression(prop.value)) {
59
- const varDef = {};
60
- for (const varProp of prop.value.properties) {
61
- if (!t.isObjectProperty(varProp)) continue;
62
- if (!t.isIdentifier(varProp.key)) continue;
63
- const varPropName = varProp.key.name;
64
- const value = getStringValue(t, varProp.value);
65
- if (value !== null) {
66
- if (varPropName === "fallback") {
67
- varDef.fallback = value;
68
- } else if (varPropName === "description") {
69
- varDef.description = value;
70
- }
71
- }
72
- }
73
- result[varName] = varDef;
74
- }
75
- }
76
- return Object.keys(result).length > 0 ? result : void 0;
77
- }
78
- function isMetaArgument(t, node) {
79
- if (t.isStringLiteral(node) || t.isTemplateLiteral(node)) {
80
- return true;
81
- }
82
- if (t.isObjectExpression(node)) {
83
- for (const prop of node.properties) {
84
- if (t.isObjectProperty(prop) && t.isIdentifier(prop.key) && prop.key.name === "fallback") {
85
- return true;
86
- }
87
- }
88
- }
89
- return false;
90
- }
91
- function i18nExtractPlugin({ types: t }) {
92
- return {
93
- name: "@astroscope/i18n/extract",
94
- visitor: {
95
- CallExpression(path2, state) {
96
- if (!path2.get("callee").isIdentifier({ name: "t" })) return;
97
- const args = path2.node.arguments;
98
- if (args.length < 1) return;
99
- const keyArg = args[0];
100
- if (!t.isExpression(keyArg)) return;
101
- const key = getStringValue(t, keyArg);
102
- if (key === null) return;
103
- let meta = { fallback: "" };
104
- if (args.length >= 2 && t.isExpression(args[1]) && isMetaArgument(t, args[1])) {
105
- meta = extractMeta(t, args[1]);
106
- }
107
- state.opts.onKeyExtracted({
108
- key,
109
- meta,
110
- file: state.filename ?? "",
111
- line: path2.node.loc?.start.line ?? 0
112
- });
113
- if (state.opts.stripFallbacks && args.length >= 2) {
114
- const secondArg = args[1];
115
- if (!t.isExpression(secondArg)) return;
116
- if (isMetaArgument(t, secondArg)) {
117
- if (args.length === 2) {
118
- path2.node.arguments = [args[0]];
119
- } else if (args.length >= 3) {
120
- path2.node.arguments = [args[0], args[2]];
121
- }
122
- }
123
- }
124
- }
125
- }
126
- };
127
- }
128
-
129
- // src/extraction/vite-plugin.ts
130
- var VIRTUAL_MODULE_ID = "virtual:@astroscope/i18n/manifest";
131
- var RESOLVED_VIRTUAL_MODULE_ID = `\0${VIRTUAL_MODULE_ID}`;
132
- var MANIFEST_FILE_NAME = "i18n-manifest.json";
133
- var I18N_GLOBAL_KEY = "__astroscope_i18n_manifest__";
134
- function getGlobalState() {
135
- const g = globalThis;
136
- if (!g[I18N_GLOBAL_KEY]) {
137
- g[I18N_GLOBAL_KEY] = {
138
- extractedKeys: [],
139
- chunkManifest: {},
140
- projectRoot: ""
141
- };
142
- }
143
- return g[I18N_GLOBAL_KEY];
144
- }
145
- function getManifest() {
146
- const state = getGlobalState();
147
- const keysWithRelativePaths = state.extractedKeys.map((key) => ({
148
- ...key,
149
- file: state.projectRoot ? path.relative(state.projectRoot, key.file) : key.file
150
- }));
151
- return {
152
- keys: keysWithRelativePaths,
153
- chunks: state.chunkManifest
154
- };
155
- }
156
- function hasI18nImport(code) {
157
- return /@astroscope\/i18n\/t/.test(code);
158
- }
159
- function getParserPlugins(filename) {
160
- const plugins = [];
161
- if (/\.tsx?$/.test(filename)) {
162
- plugins.push("typescript");
163
- }
164
- if (/\.(jsx|tsx)$/.test(filename)) {
165
- plugins.push("jsx");
166
- }
167
- return plugins;
168
- }
169
- function i18nVitePlugin(options) {
170
- const { include = [".ts", ".tsx", ".js", ".jsx", ".astro"], logger } = options;
171
- const extractedKeys = [];
172
- const fileToKeys = /* @__PURE__ */ new Map();
173
- const filesWithI18n = /* @__PURE__ */ new Set();
174
- let chunkManifest = {};
175
- let projectRoot = "";
176
- let isBuild = false;
177
- let isSSR = false;
178
- return {
179
- name: "@astroscope/i18n/extract",
180
- enforce: "pre",
181
- configResolved(config) {
182
- projectRoot = config.root;
183
- getGlobalState().projectRoot = config.root;
184
- isBuild = config.command === "build";
185
- isSSR = !!config.build.ssr;
186
- },
187
- resolveId(id) {
188
- if (id === VIRTUAL_MODULE_ID) {
189
- return RESOLVED_VIRTUAL_MODULE_ID;
190
- }
191
- if (id.startsWith(`${VIRTUAL_MODULE_ID}?`)) {
192
- return `\0${id}`;
193
- }
194
- },
195
- load(id) {
196
- if (id === RESOLVED_VIRTUAL_MODULE_ID || id.startsWith(`\0${VIRTUAL_MODULE_ID}?`)) {
197
- if (isBuild) {
198
- return `
199
- import { readFileSync, existsSync } from 'node:fs';
200
- import { fileURLToPath } from 'node:url';
201
- import { dirname, join } from 'node:path';
202
-
203
- const __dirname = dirname(fileURLToPath(import.meta.url));
204
- const manifestName = '${MANIFEST_FILE_NAME}';
205
-
206
- // manifest is emitted to server directory (not publicly accessible)
207
- const searchPaths = [
208
- join(__dirname, manifestName), // same dir (server/)
209
- join(__dirname, '..', manifestName), // parent dir (from chunks/)
210
- ];
211
-
212
- let manifestPath = searchPaths.find(p => existsSync(p));
213
- if (!manifestPath) {
214
- console.warn('[i18n] manifest not found, using empty manifest');
215
- manifestPath = null;
216
- }
217
-
218
- const manifestJson = manifestPath
219
- ? JSON.parse(readFileSync(manifestPath, 'utf-8'))
220
- : { keys: [], chunks: {} };
221
-
222
- export const manifest = manifestJson;
223
- export function getManifest() { return manifestJson; }
224
- `;
225
- }
226
- return `
227
- import { getManifest as _getManifest } from '@astroscope/i18n/extraction';
228
- export const manifest = { keys: [], chunks: {} }; // placeholder, use getManifest()
229
- export function getManifest() { return _getManifest(); }
230
- `;
231
- }
232
- },
233
- async transform(code, id) {
234
- if (!include.some((ext) => id.endsWith(ext))) {
235
- return null;
236
- }
237
- if (id.includes("node_modules")) {
238
- return null;
239
- }
240
- if (!hasI18nImport(code)) {
241
- return null;
242
- }
243
- filesWithI18n.add(id);
244
- const fileKeys = [];
245
- const result = await transformAsync(code, {
246
- filename: id,
247
- sourceMaps: true,
248
- babelrc: false,
249
- configFile: false,
250
- parserOpts: {
251
- plugins: getParserPlugins(id)
252
- },
253
- plugins: [
254
- [
255
- i18nExtractPlugin,
256
- {
257
- onKeyExtracted: (key) => fileKeys.push(key),
258
- stripFallbacks: isBuild
259
- // only strip in production
260
- }
261
- ]
262
- ]
263
- });
264
- if (fileKeys.length > 0) {
265
- extractedKeys.push(...fileKeys);
266
- getGlobalState().extractedKeys = extractedKeys;
267
- fileToKeys.set(
268
- id,
269
- fileKeys.map((k) => k.key)
270
- );
271
- }
272
- if (isBuild && result?.code) {
273
- return {
274
- code: result.code,
275
- map: result.map
276
- };
277
- }
278
- return null;
279
- },
280
- generateBundle(_, bundle) {
281
- chunkManifest = {};
282
- for (const [fileName, chunk] of Object.entries(bundle)) {
283
- if (chunk.type === "chunk" && chunk.moduleIds) {
284
- const keys = /* @__PURE__ */ new Set();
285
- let hasI18nModule = false;
286
- for (const moduleId of chunk.moduleIds) {
287
- const moduleKeys = fileToKeys.get(moduleId);
288
- if (moduleKeys) {
289
- moduleKeys.forEach((k) => keys.add(k));
290
- }
291
- if (filesWithI18n.has(moduleId)) {
292
- hasI18nModule = true;
293
- }
294
- }
295
- if (keys.size > 0) {
296
- const chunkName = chunkIdToName(fileName.replace(/\.js$/, ""));
297
- chunkManifest[chunkName] = Array.from(keys);
298
- }
299
- if (hasI18nModule && chunk.type === "chunk") {
300
- const chunkName = chunkIdToName(fileName.replace(/\.js$/, ""));
301
- const loaderCode = `
302
- /* @astroscope/i18n loader */
303
- if (typeof window !== 'undefined' && window.__i18n__) {
304
- const __i18n__ = window.__i18n__;
305
- const __i18n_hash__ = __i18n__.hashes[${JSON.stringify(chunkName)}];
306
- if (__i18n_hash__) {
307
- await import(\`/_i18n/\${__i18n__.locale}/${chunkName}.\${__i18n_hash__}.js\`);
308
- }
309
- }
310
- /* end @astroscope/i18n loader */
311
- `;
312
- chunk.code = loaderCode + chunk.code;
313
- }
314
- }
315
- }
316
- getGlobalState().chunkManifest = chunkManifest;
317
- const chunkCount = Object.keys(chunkManifest).length;
318
- const keyCount = extractedKeys.length;
319
- logger?.info(`manifest: ${chunkCount} chunks, ${keyCount} keys`);
320
- },
321
- writeBundle(outputOptions) {
322
- if (!isSSR && outputOptions.dir) {
323
- const keysWithRelativePaths = extractedKeys.map((key) => ({
324
- ...key,
325
- file: projectRoot ? path.relative(projectRoot, key.file) : key.file
326
- }));
327
- const manifest = {
328
- keys: keysWithRelativePaths,
329
- chunks: chunkManifest
330
- };
331
- const serverDir = path.resolve(outputOptions.dir, "..", "server");
332
- const manifestPath = path.join(serverDir, MANIFEST_FILE_NAME);
333
- if (fs.existsSync(serverDir)) {
334
- fs.writeFileSync(manifestPath, JSON.stringify(manifest));
335
- }
336
- }
337
- }
338
- };
339
- }
340
-
341
- export {
342
- getManifest,
343
- i18nVitePlugin
344
- };
@@ -1,23 +0,0 @@
1
- import {
2
- buildI18nChunkUrl,
3
- chunkIdToName
4
- } from "./chunk-HHRMR3TM.js";
5
-
6
- // src/client/import-with-i18n.ts
7
- async function importx(importFn, chunkId) {
8
- const i18n = window.__i18n__;
9
- const chunkName = chunkId ? chunkIdToName(chunkId) : void 0;
10
- const hash = chunkName ? i18n.hashes[chunkName] : void 0;
11
- const [module] = await Promise.all([
12
- importFn(),
13
- hash && chunkName ? import(
14
- /* @vite-ignore */
15
- buildI18nChunkUrl(i18n.locale, chunkName, hash)
16
- ) : Promise.resolve()
17
- ]);
18
- return module;
19
- }
20
-
21
- export {
22
- importx
23
- };
@@ -1,14 +0,0 @@
1
- // src/import-with-i18n.ts
2
- async function importWithI18n(importFn, chunkId) {
3
- const hash = chunkId ? window.__i18n_hashes__?.[chunkId] : void 0;
4
- const locale = window.__i18n_locale__ || "en";
5
- const [module] = await Promise.all([
6
- importFn(),
7
- hash ? import(`/_i18n/${locale}/${chunkId}.${hash}.js`) : Promise.resolve()
8
- ]);
9
- return module;
10
- }
11
-
12
- export {
13
- importWithI18n
14
- };
@@ -1,263 +0,0 @@
1
- import {
2
- chunkIdToName
3
- } from "./chunk-4A5BKHFN.js";
4
-
5
- // src/extraction/vite-plugin.ts
6
- import fs from "fs";
7
- import path from "path";
8
- var VIRTUAL_MODULE_ID = "virtual:@astroscope/i18n/manifest";
9
- var RESOLVED_VIRTUAL_MODULE_ID = `\0${VIRTUAL_MODULE_ID}`;
10
- var MANIFEST_FILE_NAME = "i18n-manifest.json";
11
- var globalExtractedKeys = [];
12
- var globalChunkManifest = {};
13
- var globalProjectRoot = "";
14
- function getManifest() {
15
- const keysWithRelativePaths = globalExtractedKeys.map((key) => ({
16
- ...key,
17
- file: globalProjectRoot ? path.relative(globalProjectRoot, key.file) : key.file
18
- }));
19
- return {
20
- keys: keysWithRelativePaths,
21
- chunks: globalChunkManifest
22
- };
23
- }
24
- function extractBalancedBraces(code, startIndex) {
25
- if (code[startIndex] !== "{") return null;
26
- let depth = 0;
27
- let i = startIndex;
28
- while (i < code.length) {
29
- if (code[i] === "{") depth++;
30
- else if (code[i] === "}") {
31
- depth--;
32
- if (depth === 0) return code.slice(startIndex, i + 1);
33
- }
34
- i++;
35
- }
36
- return null;
37
- }
38
- function extractTranslationCalls(code, filename) {
39
- const keys = [];
40
- const regex = /\bt\s*\(\s*['"`]([^'"`]+)['"`]\s*,\s*/g;
41
- let match;
42
- while ((match = regex.exec(code)) !== null) {
43
- const key = match[1];
44
- const afterKey = code.slice(match.index + match[0].length);
45
- const upToMatch = code.slice(0, match.index);
46
- const lineNumber = (upToMatch.match(/\n/g) || []).length + 1;
47
- try {
48
- const stringMatch = afterKey.match(/^['"`]([^'"`]*)['"`]/);
49
- if (stringMatch) {
50
- keys.push({
51
- key,
52
- meta: { fallback: stringMatch[1] },
53
- file: filename,
54
- line: lineNumber
55
- });
56
- } else if (afterKey.startsWith("{")) {
57
- const metaStr = extractBalancedBraces(afterKey, 0);
58
- if (metaStr) {
59
- const fallbackMatch = metaStr.match(/fallback\s*:\s*['"`]([^'"`]+)['"`]/);
60
- const descMatch = metaStr.match(/description\s*:\s*['"`]([^'"`]+)['"`]/);
61
- const fallback = fallbackMatch ? fallbackMatch[1] : "";
62
- const description = descMatch ? descMatch[1] : void 0;
63
- const varsBlockMatch = metaStr.match(/variables\s*:\s*\{/);
64
- let variables;
65
- if (varsBlockMatch) {
66
- const varsStart = metaStr.indexOf("{", varsBlockMatch.index + varsBlockMatch[0].length - 1);
67
- const varsStr = extractBalancedBraces(metaStr, varsStart);
68
- if (varsStr) {
69
- variables = parseVariables(varsStr);
70
- }
71
- }
72
- keys.push({
73
- key,
74
- meta: { fallback, description, variables },
75
- file: filename,
76
- line: lineNumber
77
- });
78
- }
79
- }
80
- } catch {
81
- }
82
- }
83
- return keys;
84
- }
85
- function parseVariables(str) {
86
- const result = {};
87
- const varNameRegex = /(\w+)\s*:\s*\{/g;
88
- let nameMatch;
89
- while ((nameMatch = varNameRegex.exec(str)) !== null) {
90
- const varName = nameMatch[1];
91
- const varStart = str.indexOf("{", nameMatch.index + nameMatch[0].length - 1);
92
- const varObj = extractBalancedBraces(str, varStart);
93
- if (varObj) {
94
- const fallbackMatch = varObj.match(/fallback\s*:\s*['"`]([^'"`]*)['"`]/);
95
- const descMatch = varObj.match(/description\s*:\s*['"`]([^'"`]*)['"`]/);
96
- result[varName] = {
97
- fallback: fallbackMatch ? fallbackMatch[1] : void 0,
98
- description: descMatch ? descMatch[1] : void 0
99
- };
100
- }
101
- }
102
- return Object.keys(result).length > 0 ? result : void 0;
103
- }
104
- function hasI18nImport(code) {
105
- return /@astroscope\/i18n\/t/.test(code);
106
- }
107
- function i18nVitePlugin(options) {
108
- const { include = [".ts", ".tsx", ".js", ".jsx", ".astro"], logger } = options;
109
- const extractedKeys = [];
110
- const fileToKeys = /* @__PURE__ */ new Map();
111
- const filesWithI18n = /* @__PURE__ */ new Set();
112
- let chunkManifest = {};
113
- let projectRoot = "";
114
- let isBuild = false;
115
- let isSSR = false;
116
- return {
117
- name: "@astroscope/i18n/extract",
118
- enforce: "pre",
119
- configResolved(config) {
120
- projectRoot = config.root;
121
- globalProjectRoot = config.root;
122
- isBuild = config.command === "build";
123
- isSSR = !!config.build.ssr;
124
- },
125
- resolveId(id) {
126
- if (id === VIRTUAL_MODULE_ID) {
127
- return RESOLVED_VIRTUAL_MODULE_ID;
128
- }
129
- if (id.startsWith(`${VIRTUAL_MODULE_ID}?`)) {
130
- return `\0${id}`;
131
- }
132
- },
133
- load(id) {
134
- if (id === RESOLVED_VIRTUAL_MODULE_ID || id.startsWith(`\0${VIRTUAL_MODULE_ID}?`)) {
135
- if (isBuild) {
136
- return `
137
- import { readFileSync, existsSync } from 'node:fs';
138
- import { fileURLToPath } from 'node:url';
139
- import { dirname, join } from 'node:path';
140
-
141
- const __dirname = dirname(fileURLToPath(import.meta.url));
142
- const manifestName = '${MANIFEST_FILE_NAME}';
143
-
144
- // manifest is emitted to server directory (not publicly accessible)
145
- const searchPaths = [
146
- join(__dirname, manifestName), // same dir (server/)
147
- join(__dirname, '..', manifestName), // parent dir (from chunks/)
148
- ];
149
-
150
- let manifestPath = searchPaths.find(p => existsSync(p));
151
- if (!manifestPath) {
152
- console.warn('[i18n] manifest not found, using empty manifest');
153
- manifestPath = null;
154
- }
155
-
156
- const manifestJson = manifestPath
157
- ? JSON.parse(readFileSync(manifestPath, 'utf-8'))
158
- : { keys: [], chunks: {} };
159
-
160
- export const manifest = manifestJson;
161
- export function getManifest() { return manifestJson; }
162
- `;
163
- }
164
- const keysWithRelativePaths = extractedKeys.map((key) => ({
165
- ...key,
166
- file: projectRoot ? path.relative(projectRoot, key.file) : key.file
167
- }));
168
- const manifest = {
169
- keys: keysWithRelativePaths,
170
- chunks: chunkManifest
171
- };
172
- return `
173
- export const manifest = ${JSON.stringify(manifest)};
174
- export function getManifest() { return ${JSON.stringify(manifest)}; }
175
- `;
176
- }
177
- },
178
- transform(code, id) {
179
- if (!include.some((ext) => id.endsWith(ext))) {
180
- return null;
181
- }
182
- if (id.includes("node_modules")) {
183
- return null;
184
- }
185
- if (hasI18nImport(code)) {
186
- filesWithI18n.add(id);
187
- }
188
- const keys = extractTranslationCalls(code, id);
189
- if (keys.length > 0) {
190
- extractedKeys.push(...keys);
191
- globalExtractedKeys = extractedKeys;
192
- fileToKeys.set(
193
- id,
194
- keys.map((k) => k.key)
195
- );
196
- }
197
- return null;
198
- },
199
- generateBundle(_, bundle) {
200
- chunkManifest = {};
201
- for (const [fileName, chunk] of Object.entries(bundle)) {
202
- if (chunk.type === "chunk" && chunk.moduleIds) {
203
- const keys = /* @__PURE__ */ new Set();
204
- let hasI18nModule = false;
205
- for (const moduleId of chunk.moduleIds) {
206
- const moduleKeys = fileToKeys.get(moduleId);
207
- if (moduleKeys) {
208
- moduleKeys.forEach((k) => keys.add(k));
209
- }
210
- if (filesWithI18n.has(moduleId)) {
211
- hasI18nModule = true;
212
- }
213
- }
214
- if (keys.size > 0) {
215
- const chunkName = chunkIdToName(fileName.replace(/\.js$/, ""));
216
- chunkManifest[chunkName] = Array.from(keys);
217
- }
218
- if (hasI18nModule && chunk.type === "chunk") {
219
- const chunkName = chunkIdToName(fileName.replace(/\.js$/, ""));
220
- const loaderCode = `
221
- /* @astroscope/i18n loader */
222
- if (typeof window !== 'undefined' && window.__i18n__) {
223
- const __i18n__ = window.__i18n__;
224
- const __i18n_hash__ = __i18n__.hashes[${JSON.stringify(chunkName)}];
225
- if (__i18n_hash__) {
226
- await import(\`/_i18n/\${__i18n__.locale}/${chunkName}.\${__i18n_hash__}.js\`);
227
- }
228
- }
229
- /* end @astroscope/i18n loader */
230
- `;
231
- chunk.code = loaderCode + chunk.code;
232
- }
233
- }
234
- }
235
- globalChunkManifest = chunkManifest;
236
- const chunkCount = Object.keys(chunkManifest).length;
237
- const keyCount = extractedKeys.length;
238
- logger?.info(`manifest: ${chunkCount} chunks, ${keyCount} keys`);
239
- },
240
- writeBundle(outputOptions) {
241
- if (!isSSR && outputOptions.dir) {
242
- const keysWithRelativePaths = extractedKeys.map((key) => ({
243
- ...key,
244
- file: projectRoot ? path.relative(projectRoot, key.file) : key.file
245
- }));
246
- const manifest = {
247
- keys: keysWithRelativePaths,
248
- chunks: chunkManifest
249
- };
250
- const serverDir = path.resolve(outputOptions.dir, "..", "server");
251
- const manifestPath = path.join(serverDir, MANIFEST_FILE_NAME);
252
- if (fs.existsSync(serverDir)) {
253
- fs.writeFileSync(manifestPath, JSON.stringify(manifest));
254
- }
255
- }
256
- }
257
- };
258
- }
259
-
260
- export {
261
- getManifest,
262
- i18nVitePlugin
263
- };