uvue.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.uniUTSUVueJavaScriptPlugin = void 0;
  4. const vue_1 = require("../../../vue");
  5. function uniUTSUVueJavaScriptPlugin(options = {}) {
  6. process.env.UNI_UTS_USING_ROLLUP = 'true';
  7. return {
  8. name: 'uni:uts-uvue',
  9. enforce: 'pre',
  10. configResolved(config) {
  11. // 移除自带的 esbuild 处理 ts 文件
  12. const index = config.plugins.findIndex((p) => p.name === 'vite:esbuild');
  13. if (index > -1) {
  14. // @ts-expect-error
  15. config.plugins.splice(index, 1);
  16. }
  17. },
  18. transform(code, id) {
  19. if (!(0, vue_1.isVueSfcFile)(id)) {
  20. return;
  21. }
  22. return {
  23. code: code.replace(/<script([^>]*)>/gi, (match, attributes) => {
  24. // 如果 <script> 标签中没有 lang 属性,添加 lang="uts"
  25. if (!/lang=["']?[^"']*["']?/.test(attributes)) {
  26. return `<script${attributes} lang="uts">`;
  27. }
  28. // 否则,将现有的 lang 属性替换为 lang="uts"
  29. return match.replace(/lang=["']?ts["']?/, 'lang="uts"');
  30. }),
  31. map: null,
  32. };
  33. },
  34. };
  35. }
  36. exports.uniUTSUVueJavaScriptPlugin = uniUTSUVueJavaScriptPlugin;