版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

665 строки
22 KiB

  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. const unplugin$1 = require('unplugin');
  4. const pathe = require('pathe');
  5. const createDebug = require('debug');
  6. const fg = require('fast-glob');
  7. const shared = require('@intlify/shared');
  8. const pluginutils = require('@rollup/pluginutils');
  9. const bundleUtils = require('@intlify/bundle-utils');
  10. const compilerSfc = require('@vue/compiler-sfc');
  11. const JSON5 = require('json5');
  12. const yaml = require('js-yaml');
  13. const fs = require('fs');
  14. const pc = require('picocolors');
  15. function parseVueRequest(id) {
  16. const [filename, rawQuery] = id.split(`?`, 2);
  17. const params = new URLSearchParams(rawQuery);
  18. const ret = {};
  19. const langPart = Object.keys(Object.fromEntries(params)).find(
  20. (key) => /lang\./i.test(key)
  21. );
  22. ret.vue = params.has("vue");
  23. ret.global = params.has("global");
  24. ret.src = params.has("src");
  25. ret.raw = params.has("raw");
  26. if (params.has("type")) {
  27. ret.type = params.get("type");
  28. }
  29. if (params.has("blockType")) {
  30. ret.blockType = params.get("blockType");
  31. }
  32. if (params.has("index")) {
  33. ret.index = Number(params.get("index"));
  34. }
  35. if (params.has("locale")) {
  36. ret.locale = params.get("locale");
  37. }
  38. if (langPart) {
  39. const [, lang] = langPart.split(".");
  40. ret.lang = lang;
  41. } else if (params.has("lang")) {
  42. ret.lang = params.get("lang");
  43. }
  44. if (params.has("issuerPath")) {
  45. ret.issuerPath = params.get("issuerPath");
  46. }
  47. return {
  48. filename,
  49. query: ret
  50. };
  51. }
  52. function createBridgeCodeGenerator(source, query) {
  53. return () => {
  54. const data = convert(source, query.lang);
  55. let value = JSON.parse(data);
  56. if (shared.isString(query.locale)) {
  57. value = Object.assign({}, { [query.locale]: value });
  58. }
  59. return JSON.stringify(value).replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029").replace(/\\/g, "\\\\").replace(/\u0027/g, "\\u0027");
  60. };
  61. }
  62. function convert(source, lang) {
  63. switch (lang) {
  64. case "yaml":
  65. case "yml":
  66. const data = yaml.load(source);
  67. return JSON.stringify(data, void 0, " ");
  68. case "json5":
  69. return JSON.stringify(JSON5.parse(source));
  70. default:
  71. return source;
  72. }
  73. }
  74. function warn(...args) {
  75. console.warn(pc.yellow(pc.bold(`[unplugin-vue-i18n] `)), ...args);
  76. }
  77. function error(...args) {
  78. console.error(pc.red(pc.bold(`[unplugin-vue-i18n] `)), ...args);
  79. }
  80. async function getRaw(path) {
  81. return fs.promises.readFile(path, { encoding: "utf-8" });
  82. }
  83. function raiseError(message) {
  84. throw new Error(`[unplugin-vue-i18n] ${message}`);
  85. }
  86. const INTLIFY_BUNDLE_IMPORT_ID = "@intlify/unplugin-vue-i18n/messages";
  87. const INTLIFY_BUNDLE_IMPORT_DEPRECTED_ID = "@intlify/vite-plugin-vue-i18n/messages";
  88. const VIRTUAL_PREFIX = "\0";
  89. const debug = createDebug("unplugin-vue-i18n");
  90. const installedPkg = bundleUtils.checkInstallPackage("@intlify/unplugin-vue-i18n", debug);
  91. const installedVueI18nBridge = bundleUtils.checkVueI18nBridgeInstallPackage(debug);
  92. const vueI18nVersion = bundleUtils.getVueI18nVersion(debug);
  93. const unplugin = unplugin$1.createUnplugin((options = {}, meta) => {
  94. debug("plugin options:", options, meta.framework);
  95. if (!["vite", "webpack"].includes(meta.framework)) {
  96. raiseError(`This plugin is supported 'vite' and 'webpack' only`);
  97. }
  98. let include = options.include;
  99. let exclude = null;
  100. if (include) {
  101. if (shared.isArray(include)) {
  102. include = include.map((item) => pathe.normalize(item));
  103. } else if (shared.isString(include)) {
  104. include = pathe.normalize(include);
  105. }
  106. } else {
  107. exclude = "**/**";
  108. }
  109. const filter = pluginutils.createFilter(include, exclude);
  110. const forceStringify = !!options.forceStringify;
  111. const defaultSFCLang = shared.isString(options.defaultSFCLang) ? options.defaultSFCLang : "json";
  112. const globalSFCScope = !!options.globalSFCScope;
  113. const useClassComponent = !!options.useClassComponent;
  114. const bridge = !!options.bridge;
  115. debug("bridge", bridge);
  116. const runtimeOnly = shared.isBoolean(options.runtimeOnly) ? options.runtimeOnly : true;
  117. debug("runtimeOnly", runtimeOnly);
  118. const compositionOnly = installedPkg === "vue-i18n" ? shared.isBoolean(options.compositionOnly) ? options.compositionOnly : true : true;
  119. debug("compositionOnly", compositionOnly);
  120. const fullInstall = installedPkg === "vue-i18n" ? shared.isBoolean(options.fullInstall) ? options.fullInstall : true : false;
  121. debug("fullInstall", fullInstall);
  122. const useVueI18nImportName = options.useVueI18nImportName;
  123. if (useVueI18nImportName != null) {
  124. warn(`'useVueI18nImportName' option is experimental`);
  125. }
  126. debug("useVueI18nImportName", useVueI18nImportName);
  127. const getVueI18nAliasName = () => vueI18nVersion === "9" || vueI18nVersion === "8" ? "vue-i18n" : vueI18nVersion === "unknown" && installedPkg === "petite-vue-i18n" && shared.isBoolean(useVueI18nImportName) && useVueI18nImportName ? "vue-i18n" : installedPkg;
  128. const getVueI18nBridgeAliasPath = () => `vue-i18n-bridge/dist/vue-i18n-bridge.runtime.esm-bundler.js`;
  129. const getVueI18nAliasPath = (aliasName) => vueI18nVersion === "8" ? `${aliasName}/dist/${aliasName}.esm.js` : `${aliasName}/dist/${installedPkg}.runtime.esm-bundler.js`;
  130. const esm = shared.isBoolean(options.esm) ? options.esm : true;
  131. debug("esm", esm);
  132. const allowDynamic = !!options.allowDynamic;
  133. debug("allowDynamic", allowDynamic);
  134. const strictMessage = shared.isBoolean(options.strictMessage) ? options.strictMessage : true;
  135. debug("strictMessage", strictMessage);
  136. const escapeHtml = !!options.escapeHtml;
  137. debug("escapeHtml", escapeHtml);
  138. let isProduction = false;
  139. let sourceMap = false;
  140. const vueI18nAliasName = getVueI18nAliasName();
  141. return {
  142. name: "unplugin-vue-i18n",
  143. enforce: meta.framework === "vite" ? "pre" : "post",
  144. vite: {
  145. config(config, { command }) {
  146. config.resolve = normalizeConfigResolveAlias(
  147. config.resolve,
  148. meta.framework
  149. );
  150. if (command === "build" && runtimeOnly) {
  151. debug(`vue-i18n alias name: ${vueI18nAliasName}`);
  152. if (shared.isArray(config.resolve.alias)) {
  153. config.resolve.alias.push({
  154. find: vueI18nAliasName,
  155. replacement: getVueI18nAliasPath(vueI18nAliasName)
  156. });
  157. if (installedVueI18nBridge) {
  158. config.resolve.alias.push({
  159. find: "vue-i18n-bridge",
  160. replacement: getVueI18nBridgeAliasPath()
  161. });
  162. }
  163. } else if (shared.isObject(config.resolve.alias)) {
  164. config.resolve.alias[vueI18nAliasName] = getVueI18nAliasPath(vueI18nAliasName);
  165. if (installedVueI18nBridge) {
  166. config.resolve.alias["vue-i18n-bridge"] = getVueI18nBridgeAliasPath();
  167. }
  168. }
  169. debug(
  170. `set ${vueI18nAliasName} runtime only: ${getVueI18nAliasPath(
  171. vueI18nAliasName
  172. )}`
  173. );
  174. if (installedVueI18nBridge) {
  175. debug(
  176. `set vue-i18n-bridge runtime only: ${getVueI18nBridgeAliasPath()}`
  177. );
  178. }
  179. } else if (command === "serve" && installedPkg === "petite-vue-i18n" && useVueI18nImportName) {
  180. config.resolve = normalizeConfigResolveAlias(
  181. config.resolve,
  182. meta.framework
  183. );
  184. if (shared.isArray(config.resolve.alias)) {
  185. config.resolve.alias.push({
  186. find: vueI18nAliasName,
  187. replacement: `petite-vue-i18n/dist/petite-vue-i18n.esm-bundler.js`
  188. });
  189. } else {
  190. config.resolve.alias[vueI18nAliasName] = `petite-vue-i18n/dist/petite-vue-i18n.esm-bundler.js`;
  191. }
  192. debug(`petite-vue-i18n alias name: ${vueI18nAliasName}`);
  193. }
  194. config.define = config.define || {};
  195. config.define["__VUE_I18N_LEGACY_API__"] = !compositionOnly;
  196. debug(
  197. `set __VUE_I18N_LEGACY_API__ is '${config.define["__VUE_I18N_LEGACY_API__"]}'`
  198. );
  199. config.define["__VUE_I18N_FULL_INSTALL__"] = fullInstall;
  200. debug(
  201. `set __VUE_I18N_FULL_INSTALL__ is '${config.define["__VUE_I18N_FULL_INSTALL__"]}'`
  202. );
  203. config.define["__VUE_I18N_PROD_DEVTOOLS__"] = false;
  204. },
  205. configResolved(config) {
  206. isProduction = config.isProduction;
  207. sourceMap = config.command === "build" ? !!config.build.sourcemap : false;
  208. debug(
  209. `configResolved: isProduction = ${isProduction}, sourceMap = ${sourceMap}`
  210. );
  211. const jsonPlugin = config.plugins.find((p) => p.name === "vite:json");
  212. if (jsonPlugin) {
  213. const orgTransform = jsonPlugin.transform;
  214. jsonPlugin.transform = async function(code, id) {
  215. if (!/\.json$/.test(id) || filter(id)) {
  216. return;
  217. }
  218. const { query } = parseVueRequest(id);
  219. if (query.vue) {
  220. return;
  221. }
  222. debug("org json plugin");
  223. return orgTransform.apply(this, [code, id]);
  224. };
  225. }
  226. const esbuildPlugin = config.plugins.find(
  227. (p) => p.name === "vite:esbuild"
  228. );
  229. if (esbuildPlugin) {
  230. const orgTransform = esbuildPlugin.transform;
  231. esbuildPlugin.transform = async function(code, id) {
  232. const result = await orgTransform.apply(this, [
  233. code,
  234. id
  235. ]);
  236. if (result == null) {
  237. return result;
  238. }
  239. const { filename, query } = parseVueRequest(id);
  240. if (!query.vue && filter(id) && /\.[c|m]?ts$/.test(id)) {
  241. const [_code, inSourceMap] = shared.isString(result) ? [result, void 0] : [result.code, result.map];
  242. let langInfo = defaultSFCLang;
  243. langInfo = pathe.parse(filename).ext;
  244. const generate = getGenerator(langInfo);
  245. const parseOptions = getOptions(
  246. filename,
  247. isProduction,
  248. query,
  249. sourceMap,
  250. {
  251. inSourceMap,
  252. isGlobal: globalSFCScope,
  253. useClassComponent,
  254. allowDynamic,
  255. strictMessage,
  256. escapeHtml,
  257. bridge,
  258. exportESM: esm,
  259. forceStringify
  260. }
  261. );
  262. debug("parseOptions", parseOptions);
  263. const { code: generatedCode, map } = generate(
  264. _code,
  265. parseOptions,
  266. bridge ? createBridgeCodeGenerator(_code, query) : void 0
  267. );
  268. debug("generated code", generatedCode);
  269. debug("sourcemap", map, sourceMap);
  270. if (_code === generatedCode)
  271. return;
  272. return {
  273. code: generatedCode,
  274. map: sourceMap ? map : { mappings: "" }
  275. };
  276. } else {
  277. return result;
  278. }
  279. };
  280. }
  281. },
  282. async handleHotUpdate({ file, server }) {
  283. if (/\.(json5?|ya?ml)$/.test(file)) {
  284. const module = server.moduleGraph.getModuleById(
  285. asVirtualId(INTLIFY_BUNDLE_IMPORT_ID, meta.framework)
  286. );
  287. if (module) {
  288. server.moduleGraph.invalidateModule(module);
  289. return [module];
  290. }
  291. }
  292. }
  293. },
  294. webpack(compiler) {
  295. isProduction = compiler.options.mode !== "development";
  296. sourceMap = !!compiler.options.devtool;
  297. debug(`webpack: isProduction = ${isProduction}, sourceMap = ${sourceMap}`);
  298. compiler.options.resolve = normalizeConfigResolveAlias(
  299. compiler.options.resolve,
  300. meta.framework
  301. );
  302. if (isProduction && runtimeOnly) {
  303. compiler.options.resolve.alias[vueI18nAliasName] = getVueI18nAliasPath(vueI18nAliasName);
  304. if (installedVueI18nBridge) {
  305. compiler.options.resolve.alias["vue-i18n-bridge"] = getVueI18nBridgeAliasPath();
  306. }
  307. debug(
  308. `set ${vueI18nAliasName} runtime only: ${getVueI18nAliasPath(
  309. vueI18nAliasName
  310. )}`
  311. );
  312. if (installedVueI18nBridge) {
  313. debug(
  314. `set vue-i18n-bridge runtime only: ${getVueI18nBridgeAliasPath()}`
  315. );
  316. }
  317. } else if (!isProduction && installedPkg === "petite-vue-i18n" && useVueI18nImportName) {
  318. compiler.options.resolve.alias[vueI18nAliasName] = `petite-vue-i18n/dist/petite-vue-i18n.esm-bundler.js`;
  319. debug(`petite-vue-i18n alias name: ${vueI18nAliasName}`);
  320. }
  321. loadWebpack().then((webpack) => {
  322. if (webpack) {
  323. compiler.options.plugins.push(
  324. new webpack.DefinePlugin({
  325. __VUE_I18N_LEGACY_API__: JSON.stringify(compositionOnly),
  326. __VUE_I18N_FULL_INSTALL__: JSON.stringify(fullInstall),
  327. __INTLIFY_PROD_DEVTOOLS__: "false"
  328. })
  329. );
  330. debug(`set __VUE_I18N_LEGACY_API__ is '${compositionOnly}'`);
  331. debug(`set __VUE_I18N_FULL_INSTALL__ is '${fullInstall}'`);
  332. } else {
  333. debug("ignore vue-i18n feature flags with webpack.DefinePlugin");
  334. }
  335. });
  336. if (compiler.options.module) {
  337. compiler.options.module.rules.push({
  338. test: /\.(json5?|ya?ml)$/,
  339. type: "javascript/auto",
  340. include(resource) {
  341. return filter(resource);
  342. }
  343. });
  344. }
  345. },
  346. resolveId(id, importer) {
  347. debug("resolveId", id, importer);
  348. if (id === INTLIFY_BUNDLE_IMPORT_DEPRECTED_ID) {
  349. warn(
  350. `deprected '${INTLIFY_BUNDLE_IMPORT_DEPRECTED_ID}', you should switch to '${INTLIFY_BUNDLE_IMPORT_ID}'`
  351. );
  352. return asVirtualId(id, meta.framework);
  353. }
  354. if (id === INTLIFY_BUNDLE_IMPORT_ID) {
  355. return asVirtualId(id, meta.framework);
  356. }
  357. },
  358. async load(id) {
  359. debug("load", id);
  360. const { query } = parseVueRequest(id);
  361. if ([INTLIFY_BUNDLE_IMPORT_DEPRECTED_ID, INTLIFY_BUNDLE_IMPORT_ID].includes(
  362. getVirtualId(id, meta.framework)
  363. ) && include) {
  364. let resourcePaths = [];
  365. const includePaths = shared.isArray(include) ? include : [include];
  366. for (const inc of includePaths) {
  367. resourcePaths = [...resourcePaths, ...await fg(inc)];
  368. }
  369. resourcePaths = resourcePaths.filter(
  370. (el, pos) => resourcePaths.indexOf(el) === pos
  371. );
  372. const code = await generateBundleResources(
  373. resourcePaths,
  374. query,
  375. isProduction,
  376. {
  377. forceStringify,
  378. bridge,
  379. strictMessage,
  380. escapeHtml,
  381. exportESM: esm,
  382. useClassComponent
  383. }
  384. );
  385. return {
  386. code,
  387. map: { mappings: "" }
  388. };
  389. }
  390. },
  391. transformInclude(id) {
  392. debug("transformInclude", id);
  393. if (meta.framework === "vite") {
  394. return true;
  395. } else {
  396. const { filename } = parseVueRequest(id);
  397. return filename.endsWith("vue") || filename.endsWith(INTLIFY_BUNDLE_IMPORT_ID) || filename.endsWith(INTLIFY_BUNDLE_IMPORT_DEPRECTED_ID) || /\.(json5?|ya?ml)$/.test(filename) && filter(filename);
  398. }
  399. },
  400. async transform(code, id) {
  401. const { filename, query } = parseVueRequest(id);
  402. debug("transform", id, JSON.stringify(query), filename);
  403. let langInfo = defaultSFCLang;
  404. let inSourceMap;
  405. if (!query.vue) {
  406. if (/\.(json5?|ya?ml|[c|m]?js)$/.test(id) && filter(id)) {
  407. langInfo = pathe.parse(filename).ext;
  408. const generate = getGenerator(langInfo);
  409. const parseOptions = getOptions(
  410. filename,
  411. isProduction,
  412. query,
  413. sourceMap,
  414. {
  415. inSourceMap,
  416. isGlobal: globalSFCScope,
  417. useClassComponent,
  418. allowDynamic,
  419. strictMessage,
  420. escapeHtml,
  421. bridge,
  422. exportESM: esm,
  423. forceStringify
  424. }
  425. );
  426. debug("parseOptions", parseOptions);
  427. const { code: generatedCode, map } = generate(
  428. code,
  429. parseOptions,
  430. bridge ? createBridgeCodeGenerator(code, query) : void 0
  431. );
  432. debug("generated code", generatedCode);
  433. debug("sourcemap", map, sourceMap);
  434. if (code === generatedCode)
  435. return;
  436. return {
  437. code: generatedCode,
  438. map: sourceMap ? map : { mappings: "" }
  439. };
  440. }
  441. } else {
  442. if (isCustomBlock(query)) {
  443. if (shared.isString(query.lang)) {
  444. langInfo = query.src ? query.lang === "i18n" ? "json" : query.lang : query.lang;
  445. } else if (defaultSFCLang) {
  446. langInfo = defaultSFCLang;
  447. }
  448. debug("langInfo", langInfo);
  449. const generate = /\.?json5?/.test(langInfo) ? bundleUtils.generateJSON : bundleUtils.generateYAML;
  450. const parseOptions = getOptions(
  451. filename,
  452. isProduction,
  453. query,
  454. sourceMap,
  455. {
  456. inSourceMap,
  457. isGlobal: globalSFCScope,
  458. useClassComponent,
  459. bridge,
  460. strictMessage,
  461. escapeHtml,
  462. exportESM: esm,
  463. forceStringify
  464. }
  465. );
  466. debug("parseOptions", parseOptions);
  467. const source = await getCode(
  468. code,
  469. filename,
  470. sourceMap,
  471. query,
  472. meta.framework
  473. );
  474. const { code: generatedCode, map } = generate(
  475. source,
  476. parseOptions,
  477. bridge ? createBridgeCodeGenerator(source, query) : void 0
  478. );
  479. debug("generated code", generatedCode);
  480. debug("sourcemap", map, sourceMap);
  481. if (code === generatedCode)
  482. return;
  483. return {
  484. code: generatedCode,
  485. map: sourceMap ? map : { mappings: "" }
  486. };
  487. }
  488. }
  489. }
  490. };
  491. });
  492. function getGenerator(ext, defaultGen = bundleUtils.generateJSON) {
  493. return /\.?json5?$/.test(ext) ? bundleUtils.generateJSON : /\.ya?ml$/.test(ext) ? bundleUtils.generateYAML : /\.([c|m]?js|[c|m]?ts)$/.test(ext) ? bundleUtils.generateJavaScript : defaultGen;
  494. }
  495. function normalizeConfigResolveAlias(resolve, framework) {
  496. if (resolve && resolve.alias) {
  497. return resolve;
  498. }
  499. if (!resolve) {
  500. if (framework === "vite") {
  501. return { alias: [] };
  502. } else if (framework === "webpack") {
  503. return { alias: {} };
  504. }
  505. } else if (!resolve.alias) {
  506. if (framework === "vite") {
  507. resolve.alias = [];
  508. return resolve;
  509. } else if (framework === "webpack") {
  510. resolve.alias = {};
  511. return resolve;
  512. }
  513. }
  514. }
  515. async function loadWebpack() {
  516. let webpack = null;
  517. try {
  518. webpack = await import('webpack').then((m) => m.default || m);
  519. } catch (e) {
  520. warn(`webpack not found, please install webpack.`);
  521. }
  522. return webpack;
  523. }
  524. async function generateBundleResources(resources, query, isProduction, {
  525. forceStringify = false,
  526. isGlobal = false,
  527. bridge = false,
  528. exportESM = true,
  529. strictMessage = true,
  530. escapeHtml = false,
  531. useClassComponent = false
  532. }) {
  533. const codes = [];
  534. for (const res of resources) {
  535. debug(`${res} bundle loading ...`);
  536. if (/\.(json5?|ya?ml)$/.test(res)) {
  537. const { ext, name } = pathe.parse(res);
  538. const source = await getRaw(res);
  539. const generate = /json5?/.test(ext) ? bundleUtils.generateJSON : bundleUtils.generateYAML;
  540. const parseOptions = getOptions(res, isProduction, {}, false, {
  541. isGlobal,
  542. useClassComponent,
  543. bridge,
  544. exportESM,
  545. strictMessage,
  546. escapeHtml,
  547. forceStringify
  548. });
  549. parseOptions.type = "bare";
  550. const { code } = generate(
  551. source,
  552. parseOptions,
  553. bridge ? createBridgeCodeGenerator(source, query) : void 0
  554. );
  555. debug("generated code", code);
  556. codes.push(`${JSON.stringify(name)}: ${code}`);
  557. }
  558. }
  559. return `export default {
  560. ${codes.join(`,
  561. `)}
  562. }`;
  563. }
  564. async function getCode(source, filename, sourceMap, query, framework = "vite") {
  565. const { index, issuerPath } = query;
  566. if (!shared.isNumber(index)) {
  567. raiseError(`unexpected index: ${index}`);
  568. }
  569. if (framework === "webpack") {
  570. if (issuerPath) {
  571. debug(`getCode (webpack) ${index} via issuerPath`, issuerPath);
  572. return await getRaw(filename);
  573. } else {
  574. const result = compilerSfc.parse(await getRaw(filename), {
  575. sourceMap,
  576. filename
  577. });
  578. const block = result.descriptor.customBlocks[index];
  579. if (block) {
  580. const code = block.src ? await getRaw(block.src) : block.content;
  581. debug(`getCode (webpack) ${index} from SFC`, code);
  582. return code;
  583. } else {
  584. return source;
  585. }
  586. }
  587. } else {
  588. return source;
  589. }
  590. }
  591. function isCustomBlock(query) {
  592. return !shared.isEmptyObject(query) && "vue" in query && (query["type"] === "custom" || query["type"] === "i18n" || query["blockType"] === "i18n");
  593. }
  594. function getOptions(filename, isProduction, query, sourceMap, {
  595. inSourceMap = void 0,
  596. forceStringify = false,
  597. isGlobal = false,
  598. bridge = false,
  599. exportESM = true,
  600. useClassComponent = false,
  601. allowDynamic = false,
  602. strictMessage = true,
  603. escapeHtml = false
  604. }) {
  605. const mode = isProduction ? "production" : "development";
  606. const baseOptions = {
  607. filename,
  608. sourceMap,
  609. inSourceMap,
  610. forceStringify,
  611. useClassComponent,
  612. allowDynamic,
  613. strictMessage,
  614. escapeHtml,
  615. bridge,
  616. exportESM,
  617. env: mode,
  618. onWarn: (msg) => {
  619. warn(`${filename} ${msg}`);
  620. },
  621. onError: (msg, extra) => {
  622. const codeFrame = shared.generateCodeFrame(
  623. extra?.source || extra?.location?.source || "",
  624. extra?.location?.start.column,
  625. extra?.location?.end.column
  626. );
  627. const errMssage = `${msg} (error code: ${extra?.code}) in ${filename}
  628. target message: ${extra?.source}
  629. target message path: ${extra?.path}
  630. ${codeFrame}
  631. `;
  632. error(errMssage);
  633. throw new Error(errMssage);
  634. }
  635. };
  636. if (isCustomBlock(query)) {
  637. return shared.assign(baseOptions, {
  638. type: "sfc",
  639. locale: shared.isString(query.locale) ? query.locale : "",
  640. isGlobal: isGlobal || !!query.global
  641. });
  642. } else {
  643. return shared.assign(baseOptions, {
  644. type: "plain",
  645. isGlobal: false
  646. });
  647. }
  648. }
  649. function getVirtualId(id, framework = "vite") {
  650. return framework === "vite" ? id.startsWith(VIRTUAL_PREFIX) ? id.slice(VIRTUAL_PREFIX.length) : "" : id;
  651. }
  652. function asVirtualId(id, framework = "vite") {
  653. return framework === "vite" ? VIRTUAL_PREFIX + id : id;
  654. }
  655. exports.default = unplugin;
  656. exports.unplugin = unplugin;