uni.compiler.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. 'use strict';
  2. var initMiniProgramPlugin = require('@dcloudio/uni-mp-vite');
  3. var path = require('path');
  4. var uniCliShared = require('@dcloudio/uni-cli-shared');
  5. var uniMpCompiler = require('@dcloudio/uni-mp-compiler');
  6. function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
  7. var initMiniProgramPlugin__default = /*#__PURE__*/_interopDefault(initMiniProgramPlugin);
  8. var path__default = /*#__PURE__*/_interopDefault(path);
  9. var appid = "";
  10. var host = "baiduboxapp";
  11. var projectname = "";
  12. var setting = {
  13. autoAudits: false,
  14. urlCheck: false
  15. };
  16. var source = {
  17. appid: appid,
  18. "compilation-args": {
  19. common: {
  20. babelSetting: {
  21. ignore: [
  22. ]
  23. },
  24. enhance: true,
  25. ignorePrefixCss: false
  26. },
  27. selected: -3
  28. },
  29. host: host,
  30. projectname: projectname,
  31. setting: setting
  32. };
  33. const transformFor = (node, context) => {
  34. if (!uniMpCompiler.isForElementNode(node)) {
  35. return;
  36. }
  37. const { vFor, props } = node;
  38. let sourceCode = vFor.valueAlias + ' in ' + vFor.sourceAlias;
  39. const keyProp = uniMpCompiler.findProp(node, 'key', true);
  40. if (keyProp) {
  41. const { exp } = keyProp;
  42. if (exp) {
  43. const key = uniMpCompiler.rewriteExpression(exp, context).content;
  44. sourceCode = sourceCode + ' trackBy ' + key;
  45. props.splice(props.indexOf(keyProp), 1);
  46. }
  47. }
  48. vFor.valueAlias = '';
  49. vFor.sourceCode = sourceCode;
  50. };
  51. /**
  52. * 百度小程序的自定义组件,不支持动态事件绑定
  53. */
  54. const transformOn = uniCliShared.createTransformOn(uniMpCompiler.transformOn);
  55. /**
  56. * 百度小程序的自定义组件,不支持动态事件绑定,故 v-model 也需要调整
  57. */
  58. const transformModel = uniCliShared.createTransformModel(uniMpCompiler.transformModel);
  59. const customElements = [
  60. 'animation-video',
  61. 'animation-view',
  62. 'ar-camera',
  63. 'rtc-room',
  64. 'rtc-room-item',
  65. 'tabs',
  66. 'tab-item',
  67. 'follow-swan',
  68. 'login',
  69. 'inline-payment-panel',
  70. 'talos-linear-gradient',
  71. 'talos-rc-view',
  72. 'talos-nested-scroll-view',
  73. 'talos-nested-scroll-top-container',
  74. 'talos-nested-scroll-bottom-container',
  75. 'talos-waterfall-view',
  76. 'talos-waterfall-item',
  77. 'talos-waterfall-header',
  78. 'talos-waterfall-footer',
  79. 'talos-pull-refresh',
  80. 'talos-control-container',
  81. 'talos-na-refresh-control',
  82. 'talos-modal',
  83. 'talos-svg',
  84. ];
  85. const nodeTransforms = [uniCliShared.transformRef, transformFor, uniCliShared.transformMatchMedia];
  86. const directiveTransforms = {
  87. on: transformOn,
  88. model: transformModel,
  89. };
  90. const COMPONENTS_DIR = 'swancomponents';
  91. const miniProgram = {
  92. class: {
  93. array: true,
  94. },
  95. slot: {
  96. fallbackContent: true,
  97. // https://github.com/baidu/san/discussions/601
  98. dynamicSlotNames: false,
  99. },
  100. directive: 's-',
  101. lazyElement: {
  102. editor: [
  103. {
  104. name: 'on',
  105. arg: ['ready'],
  106. },
  107. ],
  108. 'animation-view': true,
  109. },
  110. component: {
  111. dir: COMPONENTS_DIR,
  112. },
  113. };
  114. const compilerOptions = {
  115. nodeTransforms,
  116. directiveTransforms,
  117. };
  118. const projectConfigFilename = 'project.swan.json';
  119. const options = {
  120. cdn: 3,
  121. vite: {
  122. inject: {
  123. uni: [path__default.default.resolve(__dirname, 'uni.api.esm.js'), 'default'],
  124. },
  125. alias: {
  126. 'uni-mp-runtime': path__default.default.resolve(__dirname, 'uni.mp.esm.js'),
  127. },
  128. copyOptions: {
  129. assets: [COMPONENTS_DIR],
  130. targets: [
  131. {
  132. src: ['ext.json'],
  133. get dest() {
  134. return process.env.UNI_OUTPUT_DIR;
  135. },
  136. },
  137. ],
  138. },
  139. },
  140. global: 'swan',
  141. app: {
  142. darkmode: false,
  143. subpackages: true,
  144. usingComponents: true,
  145. },
  146. project: {
  147. filename: projectConfigFilename,
  148. config: ['project.swan.json'],
  149. source,
  150. normalize(projectJson) {
  151. var _a;
  152. const miniprogram = (_a = projectJson.condition) === null || _a === void 0 ? void 0 : _a.miniprogram;
  153. if (miniprogram &&
  154. Array.isArray(miniprogram.list) &&
  155. miniprogram.list.length) {
  156. projectJson['compilation-args'].options =
  157. miniprogram.list.map((item) => {
  158. return {
  159. id: item.id,
  160. text: item.name,
  161. extra: {
  162. index: item.pathName,
  163. query: item.query,
  164. },
  165. };
  166. });
  167. delete projectJson.condition;
  168. }
  169. return projectJson;
  170. },
  171. },
  172. template: Object.assign(Object.assign({}, miniProgram), { customElements, filter: {
  173. extname: '.sjs',
  174. lang: 'sjs',
  175. generate(filter, filename) {
  176. if (filename) {
  177. return `<import-sjs src="${filename}.sjs" module="${filter.name}"/>`;
  178. }
  179. return `<import-sjs module="${filter.name}">
  180. ${filter.code}
  181. </import-sjs>`;
  182. },
  183. }, extname: '.swan', compilerOptions }),
  184. style: {
  185. extname: '.css',
  186. },
  187. };
  188. const uniMiniProgramBaiduPlugin = {
  189. name: 'uni:mp-baidu',
  190. config() {
  191. return {
  192. define: {
  193. __VUE_CREATED_DEFERRED__: false,
  194. },
  195. };
  196. },
  197. };
  198. var index = [uniMiniProgramBaiduPlugin, ...initMiniProgramPlugin__default.default(options)];
  199. module.exports = index;