polyfill.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.rewriteCompilerSfcParseOnce = exports.rewriteCompileScriptOnce = void 0;
  4. const shared_1 = require("@vue/shared");
  5. const uni_shared_1 = require("@dcloudio/uni-shared");
  6. const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
  7. exports.rewriteCompileScriptOnce = (0, uni_shared_1.once)(rewriteCompileScript);
  8. exports.rewriteCompilerSfcParseOnce = (0, uni_shared_1.once)(rewriteCompilerSfcParse);
  9. function rewriteCompileScript() {
  10. const compiler = require((0, uni_cli_shared_1.resolveBuiltIn)('@vue/compiler-sfc'));
  11. const { compileScript, compileTemplate, compileStyle, compileStyleAsync } = compiler;
  12. compiler.compileStyle = (options) => {
  13. // https://github.com/dcloudio/uni-app/issues/4076
  14. options.isProd = true;
  15. return compileStyle(options);
  16. };
  17. compiler.compileStyleAsync = (options) => {
  18. // https://github.com/dcloudio/uni-app/issues/4076
  19. options.isProd = true;
  20. return compileStyleAsync(options);
  21. };
  22. // script-setup + v-bind
  23. compiler.compileScript = (sfc, options) => {
  24. if (options?.templateOptions?.compilerOptions) {
  25. ;
  26. options.templateOptions.compilerOptions.bindingCssVars =
  27. sfc.cssVars || [];
  28. }
  29. // 强制生产模式,确保 cssVar 的生成使用 hash
  30. // https://github.com/dcloudio/uni-app/issues/4076
  31. // dev模式下,会生成:{ "83a5a03c-style.color": style.color}
  32. options.isProd = true;
  33. return compileScript(sfc, options);
  34. };
  35. // script + v-bind
  36. compiler.compileTemplate = (options) => {
  37. if (options?.compilerOptions) {
  38. ;
  39. options.compilerOptions.bindingCssVars =
  40. options.ssrCssVars || [];
  41. }
  42. // 同上
  43. options.isProd = true;
  44. return compileTemplate(options);
  45. };
  46. }
  47. /**
  48. * 重写 parse,解决相同内容被缓存,未触发 template 编译的问题
  49. */
  50. function rewriteCompilerSfcParse() {
  51. // @ts-ignore
  52. const compilerSfc = require((0, uni_cli_shared_1.resolveBuiltIn)('@vue/compiler-sfc'));
  53. const { parse } = compilerSfc;
  54. compilerSfc.parse = (source, options) => {
  55. const res = parse(source, options);
  56. // template 中,先<view>hello</view>,然后修改为<view></view>,再恢复为<view>hello</view>,
  57. // 此时因为 descriptor 被缓存,不会触发 compileTemplate,故 parse 时,每次生成一个全新的 descriptor
  58. // https://github.com/vitejs/vite/blob/v2.9.13/packages/plugin-vue/src/script.ts#L44
  59. // https://github.com/dcloudio/uni-app/issues/3685
  60. res.descriptor = (0, shared_1.extend)({}, res.descriptor);
  61. return res;
  62. };
  63. }