theme.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637
  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.initTheme = exports.normalizeThemeConfigOnce = exports.parseThemeJson = exports.hasThemeJson = void 0;
  7. const fs_1 = __importDefault(require("fs"));
  8. const path_1 = __importDefault(require("path"));
  9. const json_1 = require("./json");
  10. const uni_shared_1 = require("@dcloudio/uni-shared");
  11. function hasThemeJson(themeLocation) {
  12. if (!fs_1.default.existsSync(themeLocation)) {
  13. return false;
  14. }
  15. return true;
  16. }
  17. exports.hasThemeJson = hasThemeJson;
  18. const parseThemeJson = (themeLocation = 'theme.json') => {
  19. if (!themeLocation || !process.env.UNI_INPUT_DIR) {
  20. return {};
  21. }
  22. themeLocation = path_1.default.join(process.env.UNI_INPUT_DIR, themeLocation);
  23. if (!hasThemeJson(themeLocation)) {
  24. return {};
  25. }
  26. const jsonStr = fs_1.default.readFileSync(themeLocation, 'utf8');
  27. return (0, json_1.parseJson)(jsonStr, true);
  28. };
  29. exports.parseThemeJson = parseThemeJson;
  30. exports.normalizeThemeConfigOnce = (0, uni_shared_1.once)((manifestJsonPlatform = {}) => (0, exports.parseThemeJson)(manifestJsonPlatform.themeLocation));
  31. function initTheme(manifestJson, pagesJson) {
  32. const platform = process.env.UNI_PLATFORM === 'app' ? 'app-plus' : process.env.UNI_PLATFORM;
  33. const manifestPlatform = manifestJson['plus'] || manifestJson[platform] || {};
  34. const themeConfig = (0, exports.normalizeThemeConfigOnce)(manifestPlatform);
  35. return (0, uni_shared_1.normalizeStyles)(pagesJson, themeConfig);
  36. }
  37. exports.initTheme = initTheme;