pagesJson.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.uniPagesJsonPlugin = exports.nvuePagesCache = void 0;
  7. const path_1 = __importDefault(require("path"));
  8. const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
  9. exports.nvuePagesCache = new Map();
  10. // 在 @vue/compiler-sfc@3.2.47 执行前重写 @vue/compiler-dom compile 方法
  11. const nvuePages = {};
  12. rewriteBindingMetadata(nvuePages);
  13. function uniPagesJsonPlugin({ renderer, appService, }) {
  14. return (0, uni_cli_shared_1.defineUniPagesJsonPlugin)((opts) => {
  15. return {
  16. name: 'uni:app-nvue-pages-json',
  17. enforce: 'pre',
  18. configResolved(config) {
  19. exports.nvuePagesCache.set(config, nvuePages);
  20. },
  21. transform(code, id) {
  22. if (!opts.filter(id)) {
  23. return;
  24. }
  25. this.addWatchFile(path_1.default.resolve(process.env.UNI_INPUT_DIR, 'pages.json'));
  26. (0, uni_cli_shared_1.getLocaleFiles)(path_1.default.resolve(process.env.UNI_INPUT_DIR, 'locale')).forEach((filepath) => {
  27. this.addWatchFile(filepath);
  28. });
  29. const pagesJson = (0, uni_cli_shared_1.normalizePagesJson)(code, process.env.UNI_PLATFORM);
  30. Object.keys(nvuePages).forEach((name) => {
  31. delete nvuePages[name];
  32. });
  33. pagesJson.pages.forEach((page) => {
  34. if (page.style.isNVue) {
  35. const filename = (0, uni_cli_shared_1.normalizePath)(path_1.default.resolve(process.env.UNI_INPUT_DIR, page.path + '.nvue'));
  36. nvuePages[filename] = {
  37. disableScroll: page.style.disableScroll,
  38. scrollIndicator: page.style.scrollIndicator,
  39. };
  40. // fix: question/164673
  41. // this.addWatchFile(filename)
  42. }
  43. });
  44. if (renderer === 'native' && appService) {
  45. this.emitFile({
  46. fileName: uni_cli_shared_1.APP_CONFIG_SERVICE,
  47. type: 'asset',
  48. source: (0, uni_cli_shared_1.normalizeAppConfigService)(pagesJson, (0, uni_cli_shared_1.parseManifestJsonOnce)(process.env.UNI_INPUT_DIR)),
  49. });
  50. return {
  51. code: '',
  52. map: { mappings: '' },
  53. };
  54. }
  55. return {
  56. code: (0, uni_cli_shared_1.normalizeAppNVuePagesJson)(pagesJson),
  57. map: { mappings: '' },
  58. };
  59. },
  60. };
  61. });
  62. }
  63. exports.uniPagesJsonPlugin = uniPagesJsonPlugin;
  64. /**
  65. * 在 BindingMetadata 中补充页面标记
  66. */
  67. function rewriteBindingMetadata(nvuePages) {
  68. const compilerDom = require((0, uni_cli_shared_1.resolveBuiltIn)('@vue/compiler-dom'));
  69. const { compile } = compilerDom;
  70. compilerDom.compile = (template, options = {}) => {
  71. if (options.filename) {
  72. if (nvuePages[options.filename]) {
  73. ;
  74. (options.bindingMetadata || (options.bindingMetadata = {})).__pageOptions = nvuePages[options.filename];
  75. }
  76. }
  77. return compile(template, options);
  78. };
  79. }