jsonJs.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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.defineUniManifestJsonPlugin = exports.defineUniPagesJsonPlugin = void 0;
  7. const fs_1 = __importDefault(require("fs"));
  8. const path_1 = __importDefault(require("path"));
  9. const constants_1 = require("../../constants");
  10. const utils_1 = require("../../utils");
  11. exports.defineUniPagesJsonPlugin = createDefineJsonJsPlugin('pages.json');
  12. exports.defineUniManifestJsonPlugin = createDefineJsonJsPlugin('manifest.json');
  13. function createDefineJsonJsPlugin(name) {
  14. const JSON_JS = constants_1.JSON_JS_MAP[name];
  15. return function (createVitePlugin) {
  16. const opts = {
  17. resolvedConfig: {},
  18. filter(id) {
  19. return id.endsWith(JSON_JS);
  20. },
  21. };
  22. const plugin = createVitePlugin(opts);
  23. const origLoad = plugin.load;
  24. const origResolveId = plugin.resolveId;
  25. const origConfigResolved = plugin.configResolved;
  26. let jsonPath = '';
  27. let jsonJsPath = '';
  28. plugin.resolveId = function (id, importer, options) {
  29. const res = origResolveId && origResolveId.call(this, id, importer, options);
  30. if (res) {
  31. return res;
  32. }
  33. if (id.endsWith(JSON_JS)) {
  34. return jsonJsPath;
  35. }
  36. };
  37. plugin.configResolved = function (config) {
  38. opts.resolvedConfig = config;
  39. jsonPath = (0, utils_1.normalizePath)(path_1.default.join(process.env.UNI_INPUT_DIR, name));
  40. jsonJsPath = (0, utils_1.normalizePath)(path_1.default.join(process.env.UNI_INPUT_DIR, JSON_JS));
  41. return origConfigResolved && origConfigResolved(config);
  42. };
  43. plugin.load = function (id, ssr) {
  44. const res = origLoad && origLoad.call(this, id, ssr);
  45. if (res) {
  46. return res;
  47. }
  48. if (!opts.filter(id)) {
  49. return;
  50. }
  51. return fs_1.default.readFileSync(jsonPath, 'utf8');
  52. };
  53. return plugin;
  54. };
  55. }