@intlify/unplugin-vue-i18n 12.0.0-alpha.2 → 12.0.0-alpha.4
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/lib/index.cjs +94 -47
- package/lib/index.mjs +93 -45
- package/lib/rspack.cjs +3 -2
- package/lib/rspack.mjs +3 -2
- package/lib/vite.cjs +3 -2
- package/lib/vite.mjs +3 -2
- package/lib/webpack.cjs +3 -2
- package/lib/webpack.mjs +3 -2
- package/package.json +20 -14
package/lib/index.cjs
CHANGED
|
@@ -5,15 +5,16 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
5
5
|
const createDebug = require('debug');
|
|
6
6
|
const unplugin$1 = require('unplugin');
|
|
7
7
|
const defu = require('defu');
|
|
8
|
-
const
|
|
8
|
+
const pathe = require('pathe');
|
|
9
9
|
const bundleUtils = require('@intlify/bundle-utils');
|
|
10
10
|
const shared = require('@intlify/shared');
|
|
11
11
|
const pluginutils = require('@rollup/pluginutils');
|
|
12
|
-
const fg = require('fast-glob');
|
|
13
12
|
const knitwork = require('knitwork');
|
|
14
13
|
const mlly = require('mlly');
|
|
15
14
|
const node_crypto = require('node:crypto');
|
|
16
15
|
const fs = require('node:fs');
|
|
16
|
+
const node_path = require('node:path');
|
|
17
|
+
const tinyglobby = require('tinyglobby');
|
|
17
18
|
const compilerSfc = require('vue/compiler-sfc');
|
|
18
19
|
const pc = require('picocolors');
|
|
19
20
|
|
|
@@ -21,7 +22,6 @@ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'defau
|
|
|
21
22
|
|
|
22
23
|
const createDebug__default = /*#__PURE__*/_interopDefaultCompat(createDebug);
|
|
23
24
|
const defu__default = /*#__PURE__*/_interopDefaultCompat(defu);
|
|
24
|
-
const fg__default = /*#__PURE__*/_interopDefaultCompat(fg);
|
|
25
25
|
const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
|
|
26
26
|
const pc__default = /*#__PURE__*/_interopDefaultCompat(pc);
|
|
27
27
|
|
|
@@ -54,8 +54,8 @@ function resolveOptions(options) {
|
|
|
54
54
|
dropMessageCompiler: false,
|
|
55
55
|
transformI18nBlock: void 0
|
|
56
56
|
});
|
|
57
|
-
const include = res.include ? toArray(res.include).map((item) =>
|
|
58
|
-
const _exclude = res.exclude ? toArray(res.exclude).map((item) =>
|
|
57
|
+
const include = res.include ? toArray(res.include).map((item) => pathe.normalize(item)) : void 0;
|
|
58
|
+
const _exclude = res.exclude ? toArray(res.exclude).map((item) => pathe.normalize(item)) : void 0;
|
|
59
59
|
const exclude = res.include ? _exclude : ["**/**"];
|
|
60
60
|
const onlyLocales = toArray(res.onlyLocales);
|
|
61
61
|
const fullInstall = res.module !== "vue-i18n" ? false : res.fullInstall;
|
|
@@ -88,23 +88,6 @@ function resolveNamespace(name) {
|
|
|
88
88
|
function getVitePlugin(config, name) {
|
|
89
89
|
return config.plugins.find((p) => p.name === name);
|
|
90
90
|
}
|
|
91
|
-
function getVitePluginTransform(plugin) {
|
|
92
|
-
if (plugin.transform) {
|
|
93
|
-
return "handler" in plugin.transform ? [plugin.transform.handler, "handler"] : [plugin.transform, "transform"];
|
|
94
|
-
} else {
|
|
95
|
-
return [void 0, void 0];
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
function overrideVitePluginTransform(plugin, override, to) {
|
|
99
|
-
if (plugin.transform == void 0) {
|
|
100
|
-
throw new Error("plugin.transform is undefined");
|
|
101
|
-
}
|
|
102
|
-
if (to === "handler" && "handler" in plugin.transform) {
|
|
103
|
-
plugin.transform.handler = override;
|
|
104
|
-
} else {
|
|
105
|
-
plugin.transform = override;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
91
|
function checkVuePlugin(vuePlugin) {
|
|
109
92
|
if (vuePlugin == null || !vuePlugin.api) {
|
|
110
93
|
error(
|
|
@@ -157,11 +140,37 @@ function getVueCompiler(vuePlugin) {
|
|
|
157
140
|
|
|
158
141
|
const INTLIFY_BUNDLE_IMPORT_ID = "@intlify/unplugin-vue-i18n/messages";
|
|
159
142
|
const VIRTUAL_PREFIX = "\0";
|
|
143
|
+
const supportedFileExtensionsRE = /\.(?:json5?|ya?ml|[c|m]?[j|t]s)$/;
|
|
144
|
+
const RE_SFC_I18N_CUSTOM_BLOCK = /\?vue&type=i18n/;
|
|
145
|
+
const RE_SFC_I18N_WEBPACK_CUSTOM_BLOCK = /blockType=i18n/;
|
|
160
146
|
const debug$1 = createDebug__default(resolveNamespace("resource"));
|
|
161
147
|
function resourcePlugin(opts, meta) {
|
|
162
|
-
const _filter = pluginutils.createFilter(opts.include, opts.exclude);
|
|
163
148
|
const importedResources = /* @__PURE__ */ new Set();
|
|
164
|
-
|
|
149
|
+
function resolveIncludeExclude() {
|
|
150
|
+
const customBlockInclude = meta.framework === "vite" ? RE_SFC_I18N_CUSTOM_BLOCK : RE_SFC_I18N_WEBPACK_CUSTOM_BLOCK;
|
|
151
|
+
return opts.include ? [[...opts.include, customBlockInclude], opts.exclude] : [[customBlockInclude], void 0];
|
|
152
|
+
}
|
|
153
|
+
function resolveIncludeExcludeForLegacy() {
|
|
154
|
+
const customBlockInclude = meta.framework === "vite" ? RE_SFC_I18N_CUSTOM_BLOCK : RE_SFC_I18N_WEBPACK_CUSTOM_BLOCK;
|
|
155
|
+
return opts.include ? [[...opts.include, customBlockInclude], opts.exclude] : [void 0, opts.exclude];
|
|
156
|
+
}
|
|
157
|
+
let _filter = null;
|
|
158
|
+
let hasViteJsonPlugin = false;
|
|
159
|
+
async function getFilter() {
|
|
160
|
+
if (_filter == null) {
|
|
161
|
+
if (meta.framework === "webpack" || meta.framework === "rspack") {
|
|
162
|
+
debug$1("Using filter for webpack/rspack");
|
|
163
|
+
_filter = pluginutils.createFilter(...resolveIncludeExclude());
|
|
164
|
+
} else if (hasViteJsonPlugin) {
|
|
165
|
+
debug$1("Using filter for rollup-vite");
|
|
166
|
+
_filter = pluginutils.createFilter(...resolveIncludeExcludeForLegacy());
|
|
167
|
+
} else {
|
|
168
|
+
debug$1("Using filter for rolldown-vite");
|
|
169
|
+
_filter = pluginutils.createFilter(...resolveIncludeExclude());
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return (val) => _filter(val) || importedResources.has(val);
|
|
173
|
+
}
|
|
165
174
|
debug$1(`vue-i18n alias name: ${opts.module}`);
|
|
166
175
|
const ctx = {
|
|
167
176
|
prod: false,
|
|
@@ -171,13 +180,17 @@ function resourcePlugin(opts, meta) {
|
|
|
171
180
|
const getSfcParser = () => vuePlugin ? getVueCompiler(vuePlugin).parse : compilerSfc.parse;
|
|
172
181
|
const resourcePaths = /* @__PURE__ */ new Set();
|
|
173
182
|
for (const inc of opts.include || []) {
|
|
174
|
-
for (const resourcePath of
|
|
183
|
+
for (const resourcePath of tinyglobby.globSync(inc, {
|
|
184
|
+
ignore: opts.exclude,
|
|
185
|
+
expandDirectories: false,
|
|
186
|
+
absolute: true
|
|
187
|
+
})) {
|
|
175
188
|
resourcePaths.add(resourcePath);
|
|
176
189
|
}
|
|
177
190
|
}
|
|
178
191
|
function result(code) {
|
|
179
192
|
if (meta.framework === "vite") {
|
|
180
|
-
return { code, map: { mappings: "" } };
|
|
193
|
+
return { code, map: { mappings: "" }, moduleType: "js" };
|
|
181
194
|
}
|
|
182
195
|
return { code };
|
|
183
196
|
}
|
|
@@ -185,6 +198,8 @@ function resourcePlugin(opts, meta) {
|
|
|
185
198
|
ctx.prod = compiler.options.mode !== "development";
|
|
186
199
|
ctx.sourceMap = !!compiler.options.devtool;
|
|
187
200
|
debug$1(`${meta.framework}: isProduction = ${ctx.prod}, sourceMap = ${ctx.sourceMap}`);
|
|
201
|
+
const syncFilter = pluginutils.createFilter(...resolveIncludeExclude());
|
|
202
|
+
_filter = syncFilter;
|
|
188
203
|
compiler.options.resolve.alias = {
|
|
189
204
|
...compiler.options.resolve.alias,
|
|
190
205
|
[opts.module]: opts.vueI18nAliasPath
|
|
@@ -210,10 +225,9 @@ function resourcePlugin(opts, meta) {
|
|
|
210
225
|
compiler.options.module.rules.push({
|
|
211
226
|
test: /\.(json5?|ya?ml)$/,
|
|
212
227
|
type: "javascript/auto",
|
|
213
|
-
include: (resource) =>
|
|
228
|
+
include: (resource) => syncFilter(resource) || importedResources.has(resource)
|
|
214
229
|
});
|
|
215
230
|
}
|
|
216
|
-
const supportedFileExtensionsRE = /\.(json5?|ya?ml|[c|m]?[j|t]s)$/;
|
|
217
231
|
return {
|
|
218
232
|
name: resolveNamespace("resource"),
|
|
219
233
|
/**
|
|
@@ -246,7 +260,7 @@ function resourcePlugin(opts, meta) {
|
|
|
246
260
|
debug$1("alias Config:", aliasConfig);
|
|
247
261
|
return shared.assign(defineConfig, aliasConfig);
|
|
248
262
|
},
|
|
249
|
-
configResolved(config) {
|
|
263
|
+
async configResolved(config) {
|
|
250
264
|
vuePlugin = getVitePlugin(config, "vite:vue");
|
|
251
265
|
if (!checkVuePlugin(vuePlugin)) {
|
|
252
266
|
return;
|
|
@@ -255,12 +269,13 @@ function resourcePlugin(opts, meta) {
|
|
|
255
269
|
ctx.sourceMap = config.command === "build" ? !!config.build.sourcemap : false;
|
|
256
270
|
debug$1(`configResolved: isProduction = ${ctx.prod}, sourceMap = ${ctx.sourceMap}`);
|
|
257
271
|
const jsonPlugin = getVitePlugin(config, "vite:json");
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
async function
|
|
272
|
+
hasViteJsonPlugin = !!jsonPlugin;
|
|
273
|
+
if (jsonPlugin && jsonPlugin.transform) {
|
|
274
|
+
const transform = jsonPlugin.transform;
|
|
275
|
+
const isObjectHook = typeof transform !== "function" && "handler" in transform;
|
|
276
|
+
const orgTransform = isObjectHook ? transform.handler : transform;
|
|
277
|
+
async function overrideJson(code, id) {
|
|
278
|
+
const filter = await getFilter();
|
|
264
279
|
if (!/\.json$/.test(id) || filter(id)) {
|
|
265
280
|
return;
|
|
266
281
|
}
|
|
@@ -271,12 +286,16 @@ function resourcePlugin(opts, meta) {
|
|
|
271
286
|
debug$1("org json plugin");
|
|
272
287
|
return orgTransform.apply(this, [code, id]);
|
|
273
288
|
}
|
|
274
|
-
|
|
289
|
+
if (isObjectHook) {
|
|
290
|
+
transform.handler = overrideJson;
|
|
291
|
+
} else {
|
|
292
|
+
jsonPlugin.transform = overrideJson;
|
|
293
|
+
}
|
|
275
294
|
}
|
|
276
295
|
}
|
|
277
296
|
},
|
|
278
|
-
resolveId(id, importer) {
|
|
279
|
-
debug$1("resolveId", id, importer);
|
|
297
|
+
async resolveId(id, importer) {
|
|
298
|
+
debug$1("resolveId", id, "from", importer);
|
|
280
299
|
if (id === INTLIFY_BUNDLE_IMPORT_ID) {
|
|
281
300
|
return asVirtualId(id, meta.framework);
|
|
282
301
|
}
|
|
@@ -284,9 +303,14 @@ function resourcePlugin(opts, meta) {
|
|
|
284
303
|
loadInclude(id) {
|
|
285
304
|
return INTLIFY_BUNDLE_IMPORT_ID === getVirtualId(id, meta.framework);
|
|
286
305
|
},
|
|
287
|
-
load(id) {
|
|
306
|
+
async load(id) {
|
|
288
307
|
debug$1("load", id);
|
|
289
|
-
if (INTLIFY_BUNDLE_IMPORT_ID === getVirtualId(id, meta.framework)
|
|
308
|
+
if (INTLIFY_BUNDLE_IMPORT_ID === getVirtualId(id, meta.framework)) {
|
|
309
|
+
if (resourcePaths.size === 0) {
|
|
310
|
+
warn(
|
|
311
|
+
`\`${INTLIFY_BUNDLE_IMPORT_ID}\` is imported, but no locale resources match \`include\``
|
|
312
|
+
);
|
|
313
|
+
}
|
|
290
314
|
const code = generateBundleResources(resourcePaths);
|
|
291
315
|
for (const p of resourcePaths) {
|
|
292
316
|
this.addWatchFile(p);
|
|
@@ -299,6 +323,11 @@ function resourcePlugin(opts, meta) {
|
|
|
299
323
|
if (meta.framework === "vite") {
|
|
300
324
|
return true;
|
|
301
325
|
}
|
|
326
|
+
const f = _filter ?? pluginutils.createFilter(...resolveIncludeExclude());
|
|
327
|
+
if (_filter == null) {
|
|
328
|
+
_filter = f;
|
|
329
|
+
}
|
|
330
|
+
const filterWithImported = (v) => f(v) || importedResources.has(v);
|
|
302
331
|
const { filename, query } = parseVueRequest(id);
|
|
303
332
|
if (filename.endsWith("vue") || filename.endsWith(INTLIFY_BUNDLE_IMPORT_ID)) {
|
|
304
333
|
return true;
|
|
@@ -307,12 +336,13 @@ function resourcePlugin(opts, meta) {
|
|
|
307
336
|
if (!isResourcePath && "issuerPath" in query) {
|
|
308
337
|
isResourcePath = resourcePaths.has(query.issuerPath);
|
|
309
338
|
}
|
|
310
|
-
return supportedFileExtensionsRE.test(filename) &&
|
|
339
|
+
return supportedFileExtensionsRE.test(filename) && filterWithImported(filename) && isResourcePath;
|
|
311
340
|
},
|
|
312
341
|
async transform(code, id) {
|
|
313
342
|
const { filename, query } = parseVueRequest(id);
|
|
314
343
|
debug$1("transform", id, JSON.stringify(query), filename);
|
|
315
344
|
let langInfo = opts.defaultSFCLang;
|
|
345
|
+
const filter = await getFilter();
|
|
316
346
|
if (filter(id)) {
|
|
317
347
|
const imports = mlly.findStaticImports(code);
|
|
318
348
|
for (const p of imports) {
|
|
@@ -329,7 +359,7 @@ function resourcePlugin(opts, meta) {
|
|
|
329
359
|
});
|
|
330
360
|
debug$1("parseOptions", parseOptions);
|
|
331
361
|
try {
|
|
332
|
-
const { code: generatedCode, map } =
|
|
362
|
+
const { code: generatedCode, map } = generate(code, parseOptions);
|
|
333
363
|
debug$1("generated code", generatedCode);
|
|
334
364
|
debug$1("sourcemap", map, ctx.sourceMap);
|
|
335
365
|
if (code === generatedCode) return;
|
|
@@ -369,7 +399,7 @@ function resourcePlugin(opts, meta) {
|
|
|
369
399
|
}
|
|
370
400
|
}
|
|
371
401
|
const generate = getGenerator(langInfo, bundleUtils.generateYAML);
|
|
372
|
-
const { code: generatedCode, map } =
|
|
402
|
+
const { code: generatedCode, map } = /\.?[cm]?[jt]s$/.test(langInfo) ? generateJsTsBlock(generate, source, parseOptions) : generate(source, parseOptions);
|
|
373
403
|
debug$1("generated code", generatedCode);
|
|
374
404
|
debug$1("sourcemap", map, ctx.sourceMap);
|
|
375
405
|
if (code !== generatedCode) {
|
|
@@ -383,17 +413,28 @@ function getGenerator(ext, fallback = bundleUtils.generateJSON) {
|
|
|
383
413
|
if (/\.?json5?$/.test(ext)) {
|
|
384
414
|
return bundleUtils.generateJSON;
|
|
385
415
|
}
|
|
386
|
-
if (
|
|
416
|
+
if (/\.?ya?ml$/.test(ext)) {
|
|
387
417
|
return bundleUtils.generateYAML;
|
|
388
418
|
}
|
|
389
|
-
if (
|
|
419
|
+
if (/\.?[cm]?js$/.test(ext)) {
|
|
390
420
|
return bundleUtils.generateJavaScript;
|
|
391
421
|
}
|
|
392
|
-
if (
|
|
422
|
+
if (/\.?[cm]?ts$/.test(ext)) {
|
|
393
423
|
return bundleUtils.generateTypescript;
|
|
394
424
|
}
|
|
395
425
|
return fallback;
|
|
396
426
|
}
|
|
427
|
+
function generateJsTsBlock(generate, source, options) {
|
|
428
|
+
try {
|
|
429
|
+
return generate(source, options);
|
|
430
|
+
} catch (error2) {
|
|
431
|
+
try {
|
|
432
|
+
return generate(`export default ${source.replace(/^[\s;]+/, "")}`, options);
|
|
433
|
+
} catch (wrappedError) {
|
|
434
|
+
throw /^\s*export\s+default\b/m.test(source) ? error2 : wrappedError;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
397
438
|
async function loadWebpack() {
|
|
398
439
|
const mod = await import('webpack');
|
|
399
440
|
return mod.default || mod;
|
|
@@ -456,7 +497,7 @@ function getCode(source, filename, sourceMap, query, parser, meta) {
|
|
|
456
497
|
const result = parser(readFile(filename), { sourceMap, filename });
|
|
457
498
|
const block = result.descriptor.customBlocks[query.index];
|
|
458
499
|
if (block) {
|
|
459
|
-
const code = block.src ? readFile(block.src) : block.content;
|
|
500
|
+
const code = block.src ? readFile(resolveBlockSrc(block.src, filename)) : block.content;
|
|
460
501
|
debug$1(`getCode (${meta.framework}) ${query.index} from SFC`, code);
|
|
461
502
|
return code;
|
|
462
503
|
}
|
|
@@ -504,6 +545,12 @@ function getOptions(filename, ctx, query, opts) {
|
|
|
504
545
|
isGlobal: false
|
|
505
546
|
});
|
|
506
547
|
}
|
|
548
|
+
function resolveBlockSrc(src, sfcFilename) {
|
|
549
|
+
if (src.startsWith(".") || !src.startsWith("/") && !/^[a-z]:[/\\]/i.test(src)) {
|
|
550
|
+
return node_path.resolve(node_path.dirname(sfcFilename), src);
|
|
551
|
+
}
|
|
552
|
+
return src;
|
|
553
|
+
}
|
|
507
554
|
function getVirtualId(id, framework = "vite") {
|
|
508
555
|
return framework === "vite" ? id.startsWith(VIRTUAL_PREFIX) ? id.slice(VIRTUAL_PREFIX.length) : "" : id;
|
|
509
556
|
}
|
package/lib/index.mjs
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import createDebug from 'debug';
|
|
2
2
|
import { createUnplugin } from 'unplugin';
|
|
3
3
|
import defu from 'defu';
|
|
4
|
-
import { normalize
|
|
5
|
-
import {
|
|
4
|
+
import { normalize } from 'pathe';
|
|
5
|
+
import { DynamicResourceError, generateJSON, generateYAML, generateJavaScript, generateTypescript } from '@intlify/bundle-utils';
|
|
6
6
|
import { assign, isEmptyObject, generateCodeFrame } from '@intlify/shared';
|
|
7
7
|
import { createFilter } from '@rollup/pluginutils';
|
|
8
|
-
import fg from 'fast-glob';
|
|
9
8
|
import { genSafeVariableName, genImport } from 'knitwork';
|
|
10
9
|
import { findStaticImports } from 'mlly';
|
|
11
10
|
import { createHash } from 'node:crypto';
|
|
12
11
|
import fs from 'node:fs';
|
|
12
|
+
import { resolve, dirname, parse } from 'node:path';
|
|
13
|
+
import { globSync } from 'tinyglobby';
|
|
13
14
|
import { parse as parse$1 } from 'vue/compiler-sfc';
|
|
14
15
|
import pc from 'picocolors';
|
|
15
16
|
|
|
@@ -76,23 +77,6 @@ function resolveNamespace(name) {
|
|
|
76
77
|
function getVitePlugin(config, name) {
|
|
77
78
|
return config.plugins.find((p) => p.name === name);
|
|
78
79
|
}
|
|
79
|
-
function getVitePluginTransform(plugin) {
|
|
80
|
-
if (plugin.transform) {
|
|
81
|
-
return "handler" in plugin.transform ? [plugin.transform.handler, "handler"] : [plugin.transform, "transform"];
|
|
82
|
-
} else {
|
|
83
|
-
return [void 0, void 0];
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
function overrideVitePluginTransform(plugin, override, to) {
|
|
87
|
-
if (plugin.transform == void 0) {
|
|
88
|
-
throw new Error("plugin.transform is undefined");
|
|
89
|
-
}
|
|
90
|
-
if (to === "handler" && "handler" in plugin.transform) {
|
|
91
|
-
plugin.transform.handler = override;
|
|
92
|
-
} else {
|
|
93
|
-
plugin.transform = override;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
80
|
function checkVuePlugin(vuePlugin) {
|
|
97
81
|
if (vuePlugin == null || !vuePlugin.api) {
|
|
98
82
|
error(
|
|
@@ -145,11 +129,37 @@ function getVueCompiler(vuePlugin) {
|
|
|
145
129
|
|
|
146
130
|
const INTLIFY_BUNDLE_IMPORT_ID = "@intlify/unplugin-vue-i18n/messages";
|
|
147
131
|
const VIRTUAL_PREFIX = "\0";
|
|
132
|
+
const supportedFileExtensionsRE = /\.(?:json5?|ya?ml|[c|m]?[j|t]s)$/;
|
|
133
|
+
const RE_SFC_I18N_CUSTOM_BLOCK = /\?vue&type=i18n/;
|
|
134
|
+
const RE_SFC_I18N_WEBPACK_CUSTOM_BLOCK = /blockType=i18n/;
|
|
148
135
|
const debug$1 = createDebug(resolveNamespace("resource"));
|
|
149
136
|
function resourcePlugin(opts, meta) {
|
|
150
|
-
const _filter = createFilter(opts.include, opts.exclude);
|
|
151
137
|
const importedResources = /* @__PURE__ */ new Set();
|
|
152
|
-
|
|
138
|
+
function resolveIncludeExclude() {
|
|
139
|
+
const customBlockInclude = meta.framework === "vite" ? RE_SFC_I18N_CUSTOM_BLOCK : RE_SFC_I18N_WEBPACK_CUSTOM_BLOCK;
|
|
140
|
+
return opts.include ? [[...opts.include, customBlockInclude], opts.exclude] : [[customBlockInclude], void 0];
|
|
141
|
+
}
|
|
142
|
+
function resolveIncludeExcludeForLegacy() {
|
|
143
|
+
const customBlockInclude = meta.framework === "vite" ? RE_SFC_I18N_CUSTOM_BLOCK : RE_SFC_I18N_WEBPACK_CUSTOM_BLOCK;
|
|
144
|
+
return opts.include ? [[...opts.include, customBlockInclude], opts.exclude] : [void 0, opts.exclude];
|
|
145
|
+
}
|
|
146
|
+
let _filter = null;
|
|
147
|
+
let hasViteJsonPlugin = false;
|
|
148
|
+
async function getFilter() {
|
|
149
|
+
if (_filter == null) {
|
|
150
|
+
if (meta.framework === "webpack" || meta.framework === "rspack") {
|
|
151
|
+
debug$1("Using filter for webpack/rspack");
|
|
152
|
+
_filter = createFilter(...resolveIncludeExclude());
|
|
153
|
+
} else if (hasViteJsonPlugin) {
|
|
154
|
+
debug$1("Using filter for rollup-vite");
|
|
155
|
+
_filter = createFilter(...resolveIncludeExcludeForLegacy());
|
|
156
|
+
} else {
|
|
157
|
+
debug$1("Using filter for rolldown-vite");
|
|
158
|
+
_filter = createFilter(...resolveIncludeExclude());
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return (val) => _filter(val) || importedResources.has(val);
|
|
162
|
+
}
|
|
153
163
|
debug$1(`vue-i18n alias name: ${opts.module}`);
|
|
154
164
|
const ctx = {
|
|
155
165
|
prod: false,
|
|
@@ -159,13 +169,17 @@ function resourcePlugin(opts, meta) {
|
|
|
159
169
|
const getSfcParser = () => vuePlugin ? getVueCompiler(vuePlugin).parse : parse$1;
|
|
160
170
|
const resourcePaths = /* @__PURE__ */ new Set();
|
|
161
171
|
for (const inc of opts.include || []) {
|
|
162
|
-
for (const resourcePath of
|
|
172
|
+
for (const resourcePath of globSync(inc, {
|
|
173
|
+
ignore: opts.exclude,
|
|
174
|
+
expandDirectories: false,
|
|
175
|
+
absolute: true
|
|
176
|
+
})) {
|
|
163
177
|
resourcePaths.add(resourcePath);
|
|
164
178
|
}
|
|
165
179
|
}
|
|
166
180
|
function result(code) {
|
|
167
181
|
if (meta.framework === "vite") {
|
|
168
|
-
return { code, map: { mappings: "" } };
|
|
182
|
+
return { code, map: { mappings: "" }, moduleType: "js" };
|
|
169
183
|
}
|
|
170
184
|
return { code };
|
|
171
185
|
}
|
|
@@ -173,6 +187,8 @@ function resourcePlugin(opts, meta) {
|
|
|
173
187
|
ctx.prod = compiler.options.mode !== "development";
|
|
174
188
|
ctx.sourceMap = !!compiler.options.devtool;
|
|
175
189
|
debug$1(`${meta.framework}: isProduction = ${ctx.prod}, sourceMap = ${ctx.sourceMap}`);
|
|
190
|
+
const syncFilter = createFilter(...resolveIncludeExclude());
|
|
191
|
+
_filter = syncFilter;
|
|
176
192
|
compiler.options.resolve.alias = {
|
|
177
193
|
...compiler.options.resolve.alias,
|
|
178
194
|
[opts.module]: opts.vueI18nAliasPath
|
|
@@ -198,10 +214,9 @@ function resourcePlugin(opts, meta) {
|
|
|
198
214
|
compiler.options.module.rules.push({
|
|
199
215
|
test: /\.(json5?|ya?ml)$/,
|
|
200
216
|
type: "javascript/auto",
|
|
201
|
-
include: (resource) =>
|
|
217
|
+
include: (resource) => syncFilter(resource) || importedResources.has(resource)
|
|
202
218
|
});
|
|
203
219
|
}
|
|
204
|
-
const supportedFileExtensionsRE = /\.(json5?|ya?ml|[c|m]?[j|t]s)$/;
|
|
205
220
|
return {
|
|
206
221
|
name: resolveNamespace("resource"),
|
|
207
222
|
/**
|
|
@@ -234,7 +249,7 @@ function resourcePlugin(opts, meta) {
|
|
|
234
249
|
debug$1("alias Config:", aliasConfig);
|
|
235
250
|
return assign(defineConfig, aliasConfig);
|
|
236
251
|
},
|
|
237
|
-
configResolved(config) {
|
|
252
|
+
async configResolved(config) {
|
|
238
253
|
vuePlugin = getVitePlugin(config, "vite:vue");
|
|
239
254
|
if (!checkVuePlugin(vuePlugin)) {
|
|
240
255
|
return;
|
|
@@ -243,12 +258,13 @@ function resourcePlugin(opts, meta) {
|
|
|
243
258
|
ctx.sourceMap = config.command === "build" ? !!config.build.sourcemap : false;
|
|
244
259
|
debug$1(`configResolved: isProduction = ${ctx.prod}, sourceMap = ${ctx.sourceMap}`);
|
|
245
260
|
const jsonPlugin = getVitePlugin(config, "vite:json");
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
async function
|
|
261
|
+
hasViteJsonPlugin = !!jsonPlugin;
|
|
262
|
+
if (jsonPlugin && jsonPlugin.transform) {
|
|
263
|
+
const transform = jsonPlugin.transform;
|
|
264
|
+
const isObjectHook = typeof transform !== "function" && "handler" in transform;
|
|
265
|
+
const orgTransform = isObjectHook ? transform.handler : transform;
|
|
266
|
+
async function overrideJson(code, id) {
|
|
267
|
+
const filter = await getFilter();
|
|
252
268
|
if (!/\.json$/.test(id) || filter(id)) {
|
|
253
269
|
return;
|
|
254
270
|
}
|
|
@@ -259,12 +275,16 @@ function resourcePlugin(opts, meta) {
|
|
|
259
275
|
debug$1("org json plugin");
|
|
260
276
|
return orgTransform.apply(this, [code, id]);
|
|
261
277
|
}
|
|
262
|
-
|
|
278
|
+
if (isObjectHook) {
|
|
279
|
+
transform.handler = overrideJson;
|
|
280
|
+
} else {
|
|
281
|
+
jsonPlugin.transform = overrideJson;
|
|
282
|
+
}
|
|
263
283
|
}
|
|
264
284
|
}
|
|
265
285
|
},
|
|
266
|
-
resolveId(id, importer) {
|
|
267
|
-
debug$1("resolveId", id, importer);
|
|
286
|
+
async resolveId(id, importer) {
|
|
287
|
+
debug$1("resolveId", id, "from", importer);
|
|
268
288
|
if (id === INTLIFY_BUNDLE_IMPORT_ID) {
|
|
269
289
|
return asVirtualId(id, meta.framework);
|
|
270
290
|
}
|
|
@@ -272,9 +292,14 @@ function resourcePlugin(opts, meta) {
|
|
|
272
292
|
loadInclude(id) {
|
|
273
293
|
return INTLIFY_BUNDLE_IMPORT_ID === getVirtualId(id, meta.framework);
|
|
274
294
|
},
|
|
275
|
-
load(id) {
|
|
295
|
+
async load(id) {
|
|
276
296
|
debug$1("load", id);
|
|
277
|
-
if (INTLIFY_BUNDLE_IMPORT_ID === getVirtualId(id, meta.framework)
|
|
297
|
+
if (INTLIFY_BUNDLE_IMPORT_ID === getVirtualId(id, meta.framework)) {
|
|
298
|
+
if (resourcePaths.size === 0) {
|
|
299
|
+
warn(
|
|
300
|
+
`\`${INTLIFY_BUNDLE_IMPORT_ID}\` is imported, but no locale resources match \`include\``
|
|
301
|
+
);
|
|
302
|
+
}
|
|
278
303
|
const code = generateBundleResources(resourcePaths);
|
|
279
304
|
for (const p of resourcePaths) {
|
|
280
305
|
this.addWatchFile(p);
|
|
@@ -287,6 +312,11 @@ function resourcePlugin(opts, meta) {
|
|
|
287
312
|
if (meta.framework === "vite") {
|
|
288
313
|
return true;
|
|
289
314
|
}
|
|
315
|
+
const f = _filter ?? createFilter(...resolveIncludeExclude());
|
|
316
|
+
if (_filter == null) {
|
|
317
|
+
_filter = f;
|
|
318
|
+
}
|
|
319
|
+
const filterWithImported = (v) => f(v) || importedResources.has(v);
|
|
290
320
|
const { filename, query } = parseVueRequest(id);
|
|
291
321
|
if (filename.endsWith("vue") || filename.endsWith(INTLIFY_BUNDLE_IMPORT_ID)) {
|
|
292
322
|
return true;
|
|
@@ -295,12 +325,13 @@ function resourcePlugin(opts, meta) {
|
|
|
295
325
|
if (!isResourcePath && "issuerPath" in query) {
|
|
296
326
|
isResourcePath = resourcePaths.has(query.issuerPath);
|
|
297
327
|
}
|
|
298
|
-
return supportedFileExtensionsRE.test(filename) &&
|
|
328
|
+
return supportedFileExtensionsRE.test(filename) && filterWithImported(filename) && isResourcePath;
|
|
299
329
|
},
|
|
300
330
|
async transform(code, id) {
|
|
301
331
|
const { filename, query } = parseVueRequest(id);
|
|
302
332
|
debug$1("transform", id, JSON.stringify(query), filename);
|
|
303
333
|
let langInfo = opts.defaultSFCLang;
|
|
334
|
+
const filter = await getFilter();
|
|
304
335
|
if (filter(id)) {
|
|
305
336
|
const imports = findStaticImports(code);
|
|
306
337
|
for (const p of imports) {
|
|
@@ -317,7 +348,7 @@ function resourcePlugin(opts, meta) {
|
|
|
317
348
|
});
|
|
318
349
|
debug$1("parseOptions", parseOptions);
|
|
319
350
|
try {
|
|
320
|
-
const { code: generatedCode, map } =
|
|
351
|
+
const { code: generatedCode, map } = generate(code, parseOptions);
|
|
321
352
|
debug$1("generated code", generatedCode);
|
|
322
353
|
debug$1("sourcemap", map, ctx.sourceMap);
|
|
323
354
|
if (code === generatedCode) return;
|
|
@@ -357,7 +388,7 @@ function resourcePlugin(opts, meta) {
|
|
|
357
388
|
}
|
|
358
389
|
}
|
|
359
390
|
const generate = getGenerator(langInfo, generateYAML);
|
|
360
|
-
const { code: generatedCode, map } =
|
|
391
|
+
const { code: generatedCode, map } = /\.?[cm]?[jt]s$/.test(langInfo) ? generateJsTsBlock(generate, source, parseOptions) : generate(source, parseOptions);
|
|
361
392
|
debug$1("generated code", generatedCode);
|
|
362
393
|
debug$1("sourcemap", map, ctx.sourceMap);
|
|
363
394
|
if (code !== generatedCode) {
|
|
@@ -371,17 +402,28 @@ function getGenerator(ext, fallback = generateJSON) {
|
|
|
371
402
|
if (/\.?json5?$/.test(ext)) {
|
|
372
403
|
return generateJSON;
|
|
373
404
|
}
|
|
374
|
-
if (
|
|
405
|
+
if (/\.?ya?ml$/.test(ext)) {
|
|
375
406
|
return generateYAML;
|
|
376
407
|
}
|
|
377
|
-
if (
|
|
408
|
+
if (/\.?[cm]?js$/.test(ext)) {
|
|
378
409
|
return generateJavaScript;
|
|
379
410
|
}
|
|
380
|
-
if (
|
|
411
|
+
if (/\.?[cm]?ts$/.test(ext)) {
|
|
381
412
|
return generateTypescript;
|
|
382
413
|
}
|
|
383
414
|
return fallback;
|
|
384
415
|
}
|
|
416
|
+
function generateJsTsBlock(generate, source, options) {
|
|
417
|
+
try {
|
|
418
|
+
return generate(source, options);
|
|
419
|
+
} catch (error2) {
|
|
420
|
+
try {
|
|
421
|
+
return generate(`export default ${source.replace(/^[\s;]+/, "")}`, options);
|
|
422
|
+
} catch (wrappedError) {
|
|
423
|
+
throw /^\s*export\s+default\b/m.test(source) ? error2 : wrappedError;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
}
|
|
385
427
|
async function loadWebpack() {
|
|
386
428
|
const mod = await import('webpack');
|
|
387
429
|
return mod.default || mod;
|
|
@@ -444,7 +486,7 @@ function getCode(source, filename, sourceMap, query, parser, meta) {
|
|
|
444
486
|
const result = parser(readFile(filename), { sourceMap, filename });
|
|
445
487
|
const block = result.descriptor.customBlocks[query.index];
|
|
446
488
|
if (block) {
|
|
447
|
-
const code = block.src ? readFile(block.src) : block.content;
|
|
489
|
+
const code = block.src ? readFile(resolveBlockSrc(block.src, filename)) : block.content;
|
|
448
490
|
debug$1(`getCode (${meta.framework}) ${query.index} from SFC`, code);
|
|
449
491
|
return code;
|
|
450
492
|
}
|
|
@@ -492,6 +534,12 @@ function getOptions(filename, ctx, query, opts) {
|
|
|
492
534
|
isGlobal: false
|
|
493
535
|
});
|
|
494
536
|
}
|
|
537
|
+
function resolveBlockSrc(src, sfcFilename) {
|
|
538
|
+
if (src.startsWith(".") || !src.startsWith("/") && !/^[a-z]:[/\\]/i.test(src)) {
|
|
539
|
+
return resolve(dirname(sfcFilename), src);
|
|
540
|
+
}
|
|
541
|
+
return src;
|
|
542
|
+
}
|
|
495
543
|
function getVirtualId(id, framework = "vite") {
|
|
496
544
|
return framework === "vite" ? id.startsWith(VIRTUAL_PREFIX) ? id.slice(VIRTUAL_PREFIX.length) : "" : id;
|
|
497
545
|
}
|
package/lib/rspack.cjs
CHANGED
|
@@ -4,15 +4,16 @@ const unplugin = require('unplugin');
|
|
|
4
4
|
const index = require('./index.cjs');
|
|
5
5
|
require('debug');
|
|
6
6
|
require('defu');
|
|
7
|
-
require('
|
|
7
|
+
require('pathe');
|
|
8
8
|
require('@intlify/bundle-utils');
|
|
9
9
|
require('@intlify/shared');
|
|
10
10
|
require('@rollup/pluginutils');
|
|
11
|
-
require('fast-glob');
|
|
12
11
|
require('knitwork');
|
|
13
12
|
require('mlly');
|
|
14
13
|
require('node:crypto');
|
|
15
14
|
require('node:fs');
|
|
15
|
+
require('node:path');
|
|
16
|
+
require('tinyglobby');
|
|
16
17
|
require('vue/compiler-sfc');
|
|
17
18
|
require('picocolors');
|
|
18
19
|
|
package/lib/rspack.mjs
CHANGED
|
@@ -2,15 +2,16 @@ import { createRspackPlugin } from 'unplugin';
|
|
|
2
2
|
import { unpluginFactory } from './index.mjs';
|
|
3
3
|
import 'debug';
|
|
4
4
|
import 'defu';
|
|
5
|
-
import '
|
|
5
|
+
import 'pathe';
|
|
6
6
|
import '@intlify/bundle-utils';
|
|
7
7
|
import '@intlify/shared';
|
|
8
8
|
import '@rollup/pluginutils';
|
|
9
|
-
import 'fast-glob';
|
|
10
9
|
import 'knitwork';
|
|
11
10
|
import 'mlly';
|
|
12
11
|
import 'node:crypto';
|
|
13
12
|
import 'node:fs';
|
|
13
|
+
import 'node:path';
|
|
14
|
+
import 'tinyglobby';
|
|
14
15
|
import 'vue/compiler-sfc';
|
|
15
16
|
import 'picocolors';
|
|
16
17
|
|
package/lib/vite.cjs
CHANGED
|
@@ -4,15 +4,16 @@ const unplugin = require('unplugin');
|
|
|
4
4
|
const index = require('./index.cjs');
|
|
5
5
|
require('debug');
|
|
6
6
|
require('defu');
|
|
7
|
-
require('
|
|
7
|
+
require('pathe');
|
|
8
8
|
require('@intlify/bundle-utils');
|
|
9
9
|
require('@intlify/shared');
|
|
10
10
|
require('@rollup/pluginutils');
|
|
11
|
-
require('fast-glob');
|
|
12
11
|
require('knitwork');
|
|
13
12
|
require('mlly');
|
|
14
13
|
require('node:crypto');
|
|
15
14
|
require('node:fs');
|
|
15
|
+
require('node:path');
|
|
16
|
+
require('tinyglobby');
|
|
16
17
|
require('vue/compiler-sfc');
|
|
17
18
|
require('picocolors');
|
|
18
19
|
|
package/lib/vite.mjs
CHANGED
|
@@ -2,15 +2,16 @@ import { createVitePlugin } from 'unplugin';
|
|
|
2
2
|
import { unpluginFactory } from './index.mjs';
|
|
3
3
|
import 'debug';
|
|
4
4
|
import 'defu';
|
|
5
|
-
import '
|
|
5
|
+
import 'pathe';
|
|
6
6
|
import '@intlify/bundle-utils';
|
|
7
7
|
import '@intlify/shared';
|
|
8
8
|
import '@rollup/pluginutils';
|
|
9
|
-
import 'fast-glob';
|
|
10
9
|
import 'knitwork';
|
|
11
10
|
import 'mlly';
|
|
12
11
|
import 'node:crypto';
|
|
13
12
|
import 'node:fs';
|
|
13
|
+
import 'node:path';
|
|
14
|
+
import 'tinyglobby';
|
|
14
15
|
import 'vue/compiler-sfc';
|
|
15
16
|
import 'picocolors';
|
|
16
17
|
|
package/lib/webpack.cjs
CHANGED
|
@@ -4,15 +4,16 @@ const unplugin = require('unplugin');
|
|
|
4
4
|
const index = require('./index.cjs');
|
|
5
5
|
require('debug');
|
|
6
6
|
require('defu');
|
|
7
|
-
require('
|
|
7
|
+
require('pathe');
|
|
8
8
|
require('@intlify/bundle-utils');
|
|
9
9
|
require('@intlify/shared');
|
|
10
10
|
require('@rollup/pluginutils');
|
|
11
|
-
require('fast-glob');
|
|
12
11
|
require('knitwork');
|
|
13
12
|
require('mlly');
|
|
14
13
|
require('node:crypto');
|
|
15
14
|
require('node:fs');
|
|
15
|
+
require('node:path');
|
|
16
|
+
require('tinyglobby');
|
|
16
17
|
require('vue/compiler-sfc');
|
|
17
18
|
require('picocolors');
|
|
18
19
|
|
package/lib/webpack.mjs
CHANGED
|
@@ -2,15 +2,16 @@ import { createWebpackPlugin } from 'unplugin';
|
|
|
2
2
|
import { unpluginFactory } from './index.mjs';
|
|
3
3
|
import 'debug';
|
|
4
4
|
import 'defu';
|
|
5
|
-
import '
|
|
5
|
+
import 'pathe';
|
|
6
6
|
import '@intlify/bundle-utils';
|
|
7
7
|
import '@intlify/shared';
|
|
8
8
|
import '@rollup/pluginutils';
|
|
9
|
-
import 'fast-glob';
|
|
10
9
|
import 'knitwork';
|
|
11
10
|
import 'mlly';
|
|
12
11
|
import 'node:crypto';
|
|
13
12
|
import 'node:fs';
|
|
13
|
+
import 'node:path';
|
|
14
|
+
import 'tinyglobby';
|
|
14
15
|
import 'vue/compiler-sfc';
|
|
15
16
|
import 'picocolors';
|
|
16
17
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intlify/unplugin-vue-i18n",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "12.0.0-alpha.
|
|
4
|
+
"version": "12.0.0-alpha.4",
|
|
5
5
|
"description": "unplugin for Vue I18n",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "kazuya kawaguchi",
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
},
|
|
13
13
|
"peerDependencies": {
|
|
14
14
|
"petite-vue-i18n": "^12.0.0-alpha.2",
|
|
15
|
+
"vite": "^6.0.0 || ^7.0.0 || ^8.0.0",
|
|
15
16
|
"vue": "^3.2.25",
|
|
16
17
|
"vue-i18n": "^12.0.0-alpha.2"
|
|
17
18
|
},
|
|
@@ -19,6 +20,9 @@
|
|
|
19
20
|
"petite-vue-i18n": {
|
|
20
21
|
"optional": true
|
|
21
22
|
},
|
|
23
|
+
"vite": {
|
|
24
|
+
"optional": true
|
|
25
|
+
},
|
|
22
26
|
"vue-i18n": {
|
|
23
27
|
"optional": true
|
|
24
28
|
}
|
|
@@ -28,33 +32,35 @@
|
|
|
28
32
|
"@rollup/pluginutils": "^5.1.0",
|
|
29
33
|
"debug": "^4.3.3",
|
|
30
34
|
"defu": "^6.1.4",
|
|
31
|
-
"fast-glob": "^3.2.12",
|
|
32
35
|
"knitwork": "^1.2.0",
|
|
33
36
|
"mlly": "^1.2.0",
|
|
37
|
+
"pathe": "^2.0.3",
|
|
34
38
|
"picocolors": "^1.0.0",
|
|
39
|
+
"tinyglobby": "^0.2.15",
|
|
35
40
|
"unplugin": "^2.2.0",
|
|
36
|
-
"vue": "
|
|
37
|
-
"@intlify/bundle-utils": "12.0.0-alpha.
|
|
41
|
+
"vue": "~3.6.0-rc.12",
|
|
42
|
+
"@intlify/bundle-utils": "12.0.0-alpha.4"
|
|
38
43
|
},
|
|
39
44
|
"devDependencies": {
|
|
40
45
|
"@intlify/core-base": "next",
|
|
41
46
|
"@rspack/core": "^1.2.7",
|
|
42
47
|
"@types/debug": "^4.1.5",
|
|
43
|
-
"@types/jsdom": "^
|
|
48
|
+
"@types/jsdom": "^28.0.0",
|
|
44
49
|
"@types/memory-fs": "^0.3.2",
|
|
45
|
-
"@types/node": "^
|
|
46
|
-
"@vitejs/plugin-vue": "^
|
|
47
|
-
"jsdom": "^
|
|
50
|
+
"@types/node": "^25.7.0",
|
|
51
|
+
"@vitejs/plugin-vue": "^6.0.6",
|
|
52
|
+
"jsdom": "^29.0.0",
|
|
48
53
|
"memory-fs": "^0.5.0",
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
+
"rollup-vite": "npm:vite@6.4.4",
|
|
55
|
+
"ts-loader": "~9.5.7",
|
|
56
|
+
"unbuild": "^3.6.1",
|
|
57
|
+
"vite": "^8.0.0",
|
|
58
|
+
"vue-loader": "~17.4.2",
|
|
59
|
+
"webpack": "~5.106.2",
|
|
54
60
|
"webpack-merge": "^5.9.0"
|
|
55
61
|
},
|
|
56
62
|
"engines": {
|
|
57
|
-
"node": ">=
|
|
63
|
+
"node": ">= 22.13"
|
|
58
64
|
},
|
|
59
65
|
"files": [
|
|
60
66
|
"lib",
|