features.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.initFeatures = void 0;
  7. const fs_1 = __importDefault(require("fs"));
  8. const path_1 = __importDefault(require("path"));
  9. const shared_1 = require("@vue/shared");
  10. function initProjectFeature({ inputDir }) {
  11. const features = {
  12. i18nLocale: false,
  13. i18nEn: true,
  14. i18nEs: true,
  15. i18nFr: true,
  16. i18nZhHans: true,
  17. i18nZhHant: true,
  18. uniCloud: false,
  19. };
  20. const localesDir = path_1.default.resolve(inputDir, 'locale');
  21. if (fs_1.default.existsSync(localesDir)) {
  22. if (fs_1.default.readdirSync(localesDir).find((file) => path_1.default.extname(file) === '.json')) {
  23. features.i18nLocale = true;
  24. }
  25. }
  26. if (process.env.UNI_CLOUD_PROVIDER) {
  27. try {
  28. const providers = JSON.parse(process.env.UNI_CLOUD_PROVIDER);
  29. if (providers.length) {
  30. features.uniCloud = true;
  31. }
  32. }
  33. catch (e) { }
  34. }
  35. return features;
  36. }
  37. function initPagesFeature({ pagesJson, command, inputDir, ssr, }) {
  38. const features = {
  39. nvue: true,
  40. pages: true,
  41. tabBar: true,
  42. tabBarMidButton: true,
  43. topWindow: false,
  44. leftWindow: false,
  45. rightWindow: false,
  46. navigationBar: true,
  47. pullDownRefresh: false,
  48. navigationBarButtons: true,
  49. navigationBarSearchInput: true,
  50. navigationBarTransparent: true,
  51. };
  52. const { tabBar, pages, topWindow, leftWindow, rightWindow, globalStyle } = pagesJson;
  53. // ssr 时强制启用多页面(需要用到router)
  54. if (!ssr && pages && pages.length === 1) {
  55. features.pages = false;
  56. }
  57. if (!(tabBar && tabBar.list && tabBar.list.length)) {
  58. features.tabBar = false;
  59. features.tabBarMidButton = false;
  60. }
  61. if (features.tabBar && !tabBar.midButton) {
  62. features.tabBarMidButton = false;
  63. }
  64. if (topWindow && topWindow.path) {
  65. features.topWindow = true;
  66. }
  67. if (leftWindow && leftWindow.path) {
  68. features.leftWindow = true;
  69. }
  70. if (rightWindow && rightWindow.path) {
  71. features.rightWindow = true;
  72. }
  73. if (globalStyle.enablePullDownRefresh) {
  74. features.pullDownRefresh = true;
  75. }
  76. else {
  77. if (pages.find((page) => page.style && page.style.enablePullDownRefresh)) {
  78. features.pullDownRefresh = true;
  79. }
  80. }
  81. if (command === 'build') {
  82. if (!pages.find((page) => fs_1.default.existsSync(path_1.default.resolve(inputDir, page.path + '.nvue')))) {
  83. features.nvue = false;
  84. }
  85. let isNavigationCustom = false;
  86. if (globalStyle.navigationBar.style === 'custom') {
  87. isNavigationCustom = true; // 全局custom
  88. if (pages.find((page) => page.style.navigationBar.style === 'default')) {
  89. isNavigationCustom = false;
  90. }
  91. }
  92. else {
  93. // 所有页面均custom
  94. if (pages.every((page) => page.style.navigationBar.style === 'custom')) {
  95. isNavigationCustom = true;
  96. }
  97. }
  98. if (isNavigationCustom) {
  99. features.navigationBar = false;
  100. features.navigationBarButtons = false;
  101. features.navigationBarSearchInput = false;
  102. features.navigationBarTransparent = false;
  103. }
  104. else {
  105. if (!pages.find((page) => (0, shared_1.isArray)(page.style.navigationBar.buttons) &&
  106. page.style.navigationBar.buttons.length)) {
  107. features.navigationBarButtons = false;
  108. }
  109. if (!globalStyle.navigationBar.searchInput &&
  110. !pages.find((page) => page.style.navigationBar.searchInput)) {
  111. features.navigationBarSearchInput = false;
  112. }
  113. if (globalStyle.navigationBar.type !== 'transparent' &&
  114. !pages.find((page) => page.style.navigationBar.type === 'transparent')) {
  115. features.navigationBarTransparent = false;
  116. }
  117. }
  118. }
  119. return features;
  120. }
  121. function initManifestFeature({ manifestJson, command, platform, }) {
  122. const features = {
  123. wx: false,
  124. wxs: true,
  125. rpx: true,
  126. promise: false,
  127. longpress: true,
  128. routerMode: '"hash"',
  129. vueOptionsApi: true,
  130. vueProdDevTools: false,
  131. };
  132. if (command === 'build') {
  133. // TODO 需要预编译一遍?
  134. // features.wxs = false
  135. // features.longpress = false
  136. }
  137. if (manifestJson.h5 &&
  138. manifestJson.h5.router &&
  139. manifestJson.h5.router.mode === 'history') {
  140. features.routerMode = '"history"';
  141. }
  142. // TODO other features
  143. return features;
  144. }
  145. function initFeatures(options) {
  146. const { wx, wxs, rpx, nvue, uniCloud, i18nEn, i18nEs, i18nFr, i18nZhHans, i18nZhHant, i18nLocale, vueOptionsApi, vueProdDevTools, pages, tabBar, tabBarMidButton, promise, longpress, routerMode, topWindow, leftWindow, rightWindow, navigationBar, pullDownRefresh, navigationBarButtons, navigationBarSearchInput, navigationBarTransparent, } = (0, shared_1.extend)(initManifestFeature(options), initPagesFeature(options), initProjectFeature(options));
  147. const features = {
  148. // vue
  149. __VUE_OPTIONS_API__: vueOptionsApi,
  150. __VUE_PROD_DEVTOOLS__: vueProdDevTools,
  151. // uni
  152. __UNI_FEATURE_WX__: wx,
  153. __UNI_FEATURE_WXS__: wxs,
  154. __UNI_FEATURE_RPX__: rpx,
  155. __UNI_FEATURE_PROMISE__: promise,
  156. __UNI_FEATURE_LONGPRESS__: longpress,
  157. __UNI_FEATURE_I18N_EN__: i18nEn,
  158. __UNI_FEATURE_I18N_ES__: i18nEs,
  159. __UNI_FEATURE_I18N_FR__: i18nFr,
  160. __UNI_FEATURE_I18N_ZH_HANS__: i18nZhHans,
  161. __UNI_FEATURE_I18N_ZH_HANT__: i18nZhHant,
  162. // 以下特性,编译器已自动识别是否需要启用
  163. __UNI_FEATURE_UNI_CLOUD__: uniCloud,
  164. __UNI_FEATURE_I18N_LOCALE__: i18nLocale,
  165. __UNI_FEATURE_NVUE__: nvue,
  166. __UNI_FEATURE_ROUTER_MODE__: routerMode,
  167. __UNI_FEATURE_PAGES__: pages,
  168. __UNI_FEATURE_TABBAR__: tabBar,
  169. __UNI_FEATURE_TABBAR_MIDBUTTON__: tabBarMidButton,
  170. __UNI_FEATURE_TOPWINDOW__: topWindow,
  171. __UNI_FEATURE_LEFTWINDOW__: leftWindow,
  172. __UNI_FEATURE_RIGHTWINDOW__: rightWindow,
  173. __UNI_FEATURE_RESPONSIVE__: topWindow || leftWindow || rightWindow,
  174. __UNI_FEATURE_NAVIGATIONBAR__: navigationBar,
  175. __UNI_FEATURE_PULL_DOWN_REFRESH__: pullDownRefresh,
  176. __UNI_FEATURE_NAVIGATIONBAR_BUTTONS__: navigationBarButtons,
  177. __UNI_FEATURE_NAVIGATIONBAR_SEARCHINPUT__: navigationBarSearchInput,
  178. __UNI_FEATURE_NAVIGATIONBAR_TRANSPARENT__: navigationBarTransparent, // 是否启用透明标题栏
  179. };
  180. // ssr nodejs features
  181. if (options.ssr) {
  182. Object.keys(features).forEach((name) => {
  183. const value = features[name];
  184. (0, shared_1.extend)(globalThis, {
  185. [name]: (0, shared_1.isString)(value) ? JSON.parse(value) : value,
  186. });
  187. });
  188. }
  189. return features;
  190. }
  191. exports.initFeatures = initFeatures;