scripts.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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.parseScripts = void 0;
  7. const shared_1 = require("@vue/shared");
  8. const fs_1 = __importDefault(require("fs"));
  9. function parseScripts(name, pkgPath) {
  10. if (!fs_1.default.existsSync(pkgPath)) {
  11. return;
  12. }
  13. const pkg = JSON.parse(fs_1.default.readFileSync(pkgPath, 'utf8'));
  14. const scripts = pkg['uni-app']?.scripts || {};
  15. const options = scripts[name];
  16. if (!options) {
  17. return;
  18. }
  19. if (!options.env?.UNI_PLATFORM) {
  20. console.error(`package.json->uni-app->scripts->${name}->env->UNI_PLATFORM is required`);
  21. process.exit(0);
  22. }
  23. const { UNI_PLATFORM, ...define } = options.env;
  24. const context = options.define || {};
  25. // 补充当前编译环境未定义的其他编译环境 define
  26. Object.keys(scripts).forEach((scriptName) => {
  27. if (scriptName !== name) {
  28. const scriptDefine = scripts[scriptName].define || {};
  29. Object.keys(scriptDefine).forEach((key) => {
  30. if (!(0, shared_1.hasOwn)(context, key)) {
  31. context[key] = false;
  32. }
  33. });
  34. }
  35. });
  36. return {
  37. name: name,
  38. platform: UNI_PLATFORM,
  39. define,
  40. context,
  41. };
  42. }
  43. exports.parseScripts = parseScripts;