@mpxjs/webpack-plugin 2.10.17 → 2.10.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (238) hide show
  1. package/lib/config.js +60 -0
  2. package/lib/file-loader.js +4 -1
  3. package/lib/global.d.ts +231 -0
  4. package/lib/index.js +58 -73
  5. package/lib/init.js +3 -0
  6. package/lib/json-compiler/index.js +13 -4
  7. package/lib/loader.js +4 -0
  8. package/lib/platform/json/wx/index.js +6 -0
  9. package/lib/platform/style/wx/index.js +102 -72
  10. package/lib/platform/template/wx/component-config/ad.js +5 -0
  11. package/lib/platform/template/wx/component-config/button.js +10 -3
  12. package/lib/platform/template/wx/component-config/camera.js +13 -3
  13. package/lib/platform/template/wx/component-config/canvas.js +8 -1
  14. package/lib/platform/template/wx/component-config/cover-image.js +7 -2
  15. package/lib/platform/template/wx/component-config/cover-view.js +3 -1
  16. package/lib/platform/template/wx/component-config/form.js +27 -2
  17. package/lib/platform/template/wx/component-config/image.js +5 -0
  18. package/lib/platform/template/wx/component-config/input.js +10 -0
  19. package/lib/platform/template/wx/component-config/label.js +10 -2
  20. package/lib/platform/template/wx/component-config/map.js +11 -0
  21. package/lib/platform/template/wx/component-config/movable-area.js +4 -1
  22. package/lib/platform/template/wx/component-config/movable-view.js +17 -2
  23. package/lib/platform/template/wx/component-config/navigator.js +26 -0
  24. package/lib/platform/template/wx/component-config/picker-view.js +12 -0
  25. package/lib/platform/template/wx/component-config/picker.js +3 -1
  26. package/lib/platform/template/wx/component-config/progress.js +11 -1
  27. package/lib/platform/template/wx/component-config/rich-text.js +5 -0
  28. package/lib/platform/template/wx/component-config/scroll-view.js +12 -1
  29. package/lib/platform/template/wx/component-config/slider.js +8 -0
  30. package/lib/platform/template/wx/component-config/swiper-item.js +5 -2
  31. package/lib/platform/template/wx/component-config/swiper.js +10 -0
  32. package/lib/platform/template/wx/component-config/text.js +5 -0
  33. package/lib/platform/template/wx/component-config/textarea.js +19 -2
  34. package/lib/platform/template/wx/component-config/unsupported.js +9 -0
  35. package/lib/platform/template/wx/component-config/video.js +10 -0
  36. package/lib/platform/template/wx/index.js +21 -1
  37. package/lib/react/LoadAsyncChunkModule.js +1 -1
  38. package/lib/react/processStyles.js +21 -9
  39. package/lib/react/script-helper.js +2 -2
  40. package/lib/react/style-helper.js +76 -13
  41. package/lib/resolver/AddModePlugin.js +17 -7
  42. package/lib/runtime/components/react/animationHooks/index.ts +75 -0
  43. package/lib/runtime/components/react/animationHooks/useAnimationAPIHooks.ts +197 -0
  44. package/lib/runtime/components/react/animationHooks/useTransitionHooks.ts +301 -0
  45. package/lib/runtime/components/react/animationHooks/utils.ts +197 -0
  46. package/lib/runtime/components/react/context.ts +12 -3
  47. package/lib/runtime/components/react/dist/animationHooks/index.d.ts +15 -0
  48. package/lib/runtime/components/react/dist/animationHooks/index.js +67 -0
  49. package/lib/runtime/components/react/dist/animationHooks/useAnimationAPIHooks.d.ts +3 -0
  50. package/lib/runtime/components/react/dist/animationHooks/useAnimationAPIHooks.js +181 -0
  51. package/lib/runtime/components/react/dist/animationHooks/useTransitionHooks.d.ts +3 -0
  52. package/lib/runtime/components/react/dist/animationHooks/useTransitionHooks.js +279 -0
  53. package/lib/runtime/components/react/dist/animationHooks/utils.d.ts +109 -0
  54. package/lib/runtime/components/react/dist/animationHooks/utils.js +151 -0
  55. package/lib/runtime/components/react/dist/context.d.ts +10 -3
  56. package/lib/runtime/components/react/dist/context.js +1 -2
  57. package/lib/runtime/components/react/dist/event.config.d.ts +0 -1
  58. package/lib/runtime/components/react/dist/getInnerListeners.d.ts +0 -1
  59. package/lib/runtime/components/react/dist/mpx-async-suspense.d.ts +0 -1
  60. package/lib/runtime/components/react/dist/mpx-async-suspense.jsx +3 -1
  61. package/lib/runtime/components/react/dist/mpx-button.d.ts +0 -1
  62. package/lib/runtime/components/react/dist/mpx-canvas/Bus.d.ts +0 -1
  63. package/lib/runtime/components/react/dist/mpx-canvas/CanvasGradient.d.ts +0 -1
  64. package/lib/runtime/components/react/dist/mpx-canvas/CanvasRenderingContext2D.d.ts +0 -1
  65. package/lib/runtime/components/react/dist/mpx-canvas/Image.d.ts +0 -1
  66. package/lib/runtime/components/react/dist/mpx-canvas/ImageData.d.ts +0 -1
  67. package/lib/runtime/components/react/dist/mpx-canvas/constructorsRegistry.d.ts +0 -1
  68. package/lib/runtime/components/react/dist/mpx-canvas/html.d.ts +0 -1
  69. package/lib/runtime/components/react/dist/mpx-canvas/index.d.ts +0 -1
  70. package/lib/runtime/components/react/dist/mpx-canvas/utils.d.ts +0 -1
  71. package/lib/runtime/components/react/dist/mpx-checkbox-group.d.ts +0 -1
  72. package/lib/runtime/components/react/dist/mpx-checkbox.d.ts +0 -1
  73. package/lib/runtime/components/react/dist/mpx-form.d.ts +0 -1
  74. package/lib/runtime/components/react/dist/mpx-icon/index.d.ts +0 -1
  75. package/lib/runtime/components/react/dist/mpx-image.d.ts +0 -1
  76. package/lib/runtime/components/react/dist/mpx-image.jsx +2 -2
  77. package/lib/runtime/components/react/dist/mpx-inline-text.d.ts +0 -1
  78. package/lib/runtime/components/react/dist/mpx-input.d.ts +2 -1
  79. package/lib/runtime/components/react/dist/mpx-input.jsx +66 -50
  80. package/lib/runtime/components/react/dist/mpx-keyboard-avoiding-view.d.ts +0 -1
  81. package/lib/runtime/components/react/dist/mpx-keyboard-avoiding-view.jsx +35 -6
  82. package/lib/runtime/components/react/dist/mpx-label.d.ts +0 -1
  83. package/lib/runtime/components/react/dist/mpx-movable-area.d.ts +0 -1
  84. package/lib/runtime/components/react/dist/mpx-movable-view.d.ts +0 -1
  85. package/lib/runtime/components/react/dist/mpx-nav.d.ts +0 -1
  86. package/lib/runtime/components/react/dist/mpx-navigator.d.ts +0 -1
  87. package/lib/runtime/components/react/dist/mpx-picker/date.d.ts +0 -1
  88. package/lib/runtime/components/react/dist/mpx-picker/dateData.d.ts +0 -1
  89. package/lib/runtime/components/react/dist/mpx-picker/index.d.ts +0 -1
  90. package/lib/runtime/components/react/dist/mpx-picker/multiSelector.d.ts +0 -1
  91. package/lib/runtime/components/react/dist/mpx-picker/region.d.ts +0 -1
  92. package/lib/runtime/components/react/dist/mpx-picker/regionData.d.ts +0 -1
  93. package/lib/runtime/components/react/dist/mpx-picker/selector.d.ts +0 -1
  94. package/lib/runtime/components/react/dist/mpx-picker/time.d.ts +0 -1
  95. package/lib/runtime/components/react/dist/mpx-picker/type.d.ts +0 -1
  96. package/lib/runtime/components/react/dist/mpx-picker-view/index.d.ts +0 -1
  97. package/lib/runtime/components/react/dist/mpx-picker-view/pickerVIewContext.d.ts +0 -1
  98. package/lib/runtime/components/react/dist/mpx-picker-view-column/index.d.ts +0 -1
  99. package/lib/runtime/components/react/dist/mpx-picker-view-column/pickerViewColumnItem.d.ts +0 -1
  100. package/lib/runtime/components/react/dist/mpx-picker-view-column/pickerViewColumnItemLite.d.ts +0 -1
  101. package/lib/runtime/components/react/dist/mpx-picker-view-column/pickerViewFaces.d.ts +0 -1
  102. package/lib/runtime/components/react/dist/mpx-picker-view-column/pickerViewIndicator.d.ts +0 -1
  103. package/lib/runtime/components/react/dist/mpx-picker-view-column/pickerViewMask.d.ts +0 -1
  104. package/lib/runtime/components/react/dist/mpx-popup/index.d.ts +0 -1
  105. package/lib/runtime/components/react/dist/mpx-popup/popupBase.d.ts +0 -1
  106. package/lib/runtime/components/react/dist/mpx-portal/index.d.ts +0 -1
  107. package/lib/runtime/components/react/dist/mpx-portal/portal-host.d.ts +0 -1
  108. package/lib/runtime/components/react/dist/mpx-portal/portal-manager.d.ts +0 -1
  109. package/lib/runtime/components/react/dist/mpx-portal/portal-manager.jsx +2 -2
  110. package/lib/runtime/components/react/dist/mpx-progress.d.ts +0 -1
  111. package/lib/runtime/components/react/dist/mpx-radio-group.d.ts +0 -1
  112. package/lib/runtime/components/react/dist/mpx-radio.d.ts +0 -1
  113. package/lib/runtime/components/react/dist/mpx-rich-text/html.d.ts +0 -1
  114. package/lib/runtime/components/react/dist/mpx-rich-text/index.d.ts +0 -1
  115. package/lib/runtime/components/react/dist/mpx-root-portal.d.ts +0 -1
  116. package/lib/runtime/components/react/dist/mpx-scroll-view.d.ts +0 -1
  117. package/lib/runtime/components/react/dist/mpx-scroll-view.jsx +84 -78
  118. package/lib/runtime/components/react/dist/mpx-simple-text.d.ts +0 -1
  119. package/lib/runtime/components/react/dist/mpx-simple-view.d.ts +0 -1
  120. package/lib/runtime/components/react/dist/mpx-slider.d.ts +0 -1
  121. package/lib/runtime/components/react/dist/mpx-sticky-header.d.ts +0 -1
  122. package/lib/runtime/components/react/dist/mpx-sticky-header.jsx +20 -20
  123. package/lib/runtime/components/react/dist/mpx-sticky-section.d.ts +0 -1
  124. package/lib/runtime/components/react/dist/mpx-swiper-item.d.ts +0 -1
  125. package/lib/runtime/components/react/dist/mpx-swiper.d.ts +10 -1
  126. package/lib/runtime/components/react/dist/mpx-swiper.jsx +95 -61
  127. package/lib/runtime/components/react/dist/mpx-switch.d.ts +0 -1
  128. package/lib/runtime/components/react/dist/mpx-text.d.ts +0 -1
  129. package/lib/runtime/components/react/dist/mpx-textarea.d.ts +0 -1
  130. package/lib/runtime/components/react/dist/mpx-textarea.jsx +1 -0
  131. package/lib/runtime/components/react/dist/mpx-video.d.ts +0 -1
  132. package/lib/runtime/components/react/dist/mpx-view.d.ts +3 -3
  133. package/lib/runtime/components/react/dist/mpx-view.jsx +22 -8
  134. package/lib/runtime/components/react/dist/mpx-web-view.d.ts +0 -1
  135. package/lib/runtime/components/react/dist/parser.d.ts +0 -1
  136. package/lib/runtime/components/react/dist/useNodesRef.d.ts +0 -1
  137. package/lib/runtime/components/react/dist/utils.d.ts +10 -9
  138. package/lib/runtime/components/react/dist/utils.jsx +47 -24
  139. package/lib/runtime/components/react/mpx-async-suspense.tsx +3 -1
  140. package/lib/runtime/components/react/mpx-image.tsx +2 -2
  141. package/lib/runtime/components/react/mpx-input.tsx +77 -54
  142. package/lib/runtime/components/react/mpx-keyboard-avoiding-view.tsx +35 -6
  143. package/lib/runtime/components/react/mpx-portal/portal-manager.tsx +2 -2
  144. package/lib/runtime/components/react/mpx-scroll-view.tsx +110 -114
  145. package/lib/runtime/components/react/mpx-sticky-header.tsx +24 -24
  146. package/lib/runtime/components/react/mpx-swiper.tsx +115 -60
  147. package/lib/runtime/components/react/mpx-textarea.tsx +1 -0
  148. package/lib/runtime/components/react/mpx-view.tsx +27 -12
  149. package/lib/runtime/components/react/tsconfig.json +26 -0
  150. package/lib/runtime/components/react/types/global.d.ts +1 -0
  151. package/lib/runtime/components/react/utils.tsx +51 -27
  152. package/lib/runtime/optionProcessor.js +5 -0
  153. package/lib/runtime/optionProcessorReact.js +7 -0
  154. package/lib/runtime/stringify.wxs +2 -2
  155. package/lib/script-setup-compiler/index.js +1 -2
  156. package/lib/style-compiler/strip-conditional.js +243 -0
  157. package/lib/template-compiler/compiler.js +2 -5
  158. package/lib/utils/string.js +25 -1
  159. package/lib/wxss/loader.js +4 -1
  160. package/lib/wxss/utils.js +7 -2
  161. package/package.json +7 -14
  162. package/lib/runtime/components/react/dist/context.d.ts.map +0 -1
  163. package/lib/runtime/components/react/dist/event.config.d.ts.map +0 -1
  164. package/lib/runtime/components/react/dist/getInnerListeners.d.ts.map +0 -1
  165. package/lib/runtime/components/react/dist/mpx-async-suspense.d.ts.map +0 -1
  166. package/lib/runtime/components/react/dist/mpx-button.d.ts.map +0 -1
  167. package/lib/runtime/components/react/dist/mpx-canvas/Bus.d.ts.map +0 -1
  168. package/lib/runtime/components/react/dist/mpx-canvas/CanvasGradient.d.ts.map +0 -1
  169. package/lib/runtime/components/react/dist/mpx-canvas/CanvasRenderingContext2D.d.ts.map +0 -1
  170. package/lib/runtime/components/react/dist/mpx-canvas/Image.d.ts.map +0 -1
  171. package/lib/runtime/components/react/dist/mpx-canvas/ImageData.d.ts.map +0 -1
  172. package/lib/runtime/components/react/dist/mpx-canvas/constructorsRegistry.d.ts.map +0 -1
  173. package/lib/runtime/components/react/dist/mpx-canvas/html.d.ts.map +0 -1
  174. package/lib/runtime/components/react/dist/mpx-canvas/index.d.ts.map +0 -1
  175. package/lib/runtime/components/react/dist/mpx-canvas/utils.d.ts.map +0 -1
  176. package/lib/runtime/components/react/dist/mpx-checkbox-group.d.ts.map +0 -1
  177. package/lib/runtime/components/react/dist/mpx-checkbox.d.ts.map +0 -1
  178. package/lib/runtime/components/react/dist/mpx-form.d.ts.map +0 -1
  179. package/lib/runtime/components/react/dist/mpx-icon/index.d.ts.map +0 -1
  180. package/lib/runtime/components/react/dist/mpx-image.d.ts.map +0 -1
  181. package/lib/runtime/components/react/dist/mpx-inline-text.d.ts.map +0 -1
  182. package/lib/runtime/components/react/dist/mpx-input.d.ts.map +0 -1
  183. package/lib/runtime/components/react/dist/mpx-keyboard-avoiding-view.d.ts.map +0 -1
  184. package/lib/runtime/components/react/dist/mpx-label.d.ts.map +0 -1
  185. package/lib/runtime/components/react/dist/mpx-movable-area.d.ts.map +0 -1
  186. package/lib/runtime/components/react/dist/mpx-movable-view.d.ts.map +0 -1
  187. package/lib/runtime/components/react/dist/mpx-nav.d.ts.map +0 -1
  188. package/lib/runtime/components/react/dist/mpx-navigator.d.ts.map +0 -1
  189. package/lib/runtime/components/react/dist/mpx-picker/date.d.ts.map +0 -1
  190. package/lib/runtime/components/react/dist/mpx-picker/dateData.d.ts.map +0 -1
  191. package/lib/runtime/components/react/dist/mpx-picker/index.d.ts.map +0 -1
  192. package/lib/runtime/components/react/dist/mpx-picker/multiSelector.d.ts.map +0 -1
  193. package/lib/runtime/components/react/dist/mpx-picker/region.d.ts.map +0 -1
  194. package/lib/runtime/components/react/dist/mpx-picker/regionData.d.ts.map +0 -1
  195. package/lib/runtime/components/react/dist/mpx-picker/selector.d.ts.map +0 -1
  196. package/lib/runtime/components/react/dist/mpx-picker/time.d.ts.map +0 -1
  197. package/lib/runtime/components/react/dist/mpx-picker/type.d.ts.map +0 -1
  198. package/lib/runtime/components/react/dist/mpx-picker-view/index.d.ts.map +0 -1
  199. package/lib/runtime/components/react/dist/mpx-picker-view/pickerVIewContext.d.ts.map +0 -1
  200. package/lib/runtime/components/react/dist/mpx-picker-view-column/index.d.ts.map +0 -1
  201. package/lib/runtime/components/react/dist/mpx-picker-view-column/pickerViewColumnItem.d.ts.map +0 -1
  202. package/lib/runtime/components/react/dist/mpx-picker-view-column/pickerViewColumnItemLite.d.ts.map +0 -1
  203. package/lib/runtime/components/react/dist/mpx-picker-view-column/pickerViewFaces.d.ts.map +0 -1
  204. package/lib/runtime/components/react/dist/mpx-picker-view-column/pickerViewIndicator.d.ts.map +0 -1
  205. package/lib/runtime/components/react/dist/mpx-picker-view-column/pickerViewMask.d.ts.map +0 -1
  206. package/lib/runtime/components/react/dist/mpx-popup/index.d.ts.map +0 -1
  207. package/lib/runtime/components/react/dist/mpx-popup/popupBase.d.ts.map +0 -1
  208. package/lib/runtime/components/react/dist/mpx-portal/index.d.ts.map +0 -1
  209. package/lib/runtime/components/react/dist/mpx-portal/portal-host.d.ts.map +0 -1
  210. package/lib/runtime/components/react/dist/mpx-portal/portal-manager.d.ts.map +0 -1
  211. package/lib/runtime/components/react/dist/mpx-progress.d.ts.map +0 -1
  212. package/lib/runtime/components/react/dist/mpx-radio-group.d.ts.map +0 -1
  213. package/lib/runtime/components/react/dist/mpx-radio.d.ts.map +0 -1
  214. package/lib/runtime/components/react/dist/mpx-rich-text/html.d.ts.map +0 -1
  215. package/lib/runtime/components/react/dist/mpx-rich-text/index.d.ts.map +0 -1
  216. package/lib/runtime/components/react/dist/mpx-root-portal.d.ts.map +0 -1
  217. package/lib/runtime/components/react/dist/mpx-scroll-view.d.ts.map +0 -1
  218. package/lib/runtime/components/react/dist/mpx-simple-text.d.ts.map +0 -1
  219. package/lib/runtime/components/react/dist/mpx-simple-view.d.ts.map +0 -1
  220. package/lib/runtime/components/react/dist/mpx-slider.d.ts.map +0 -1
  221. package/lib/runtime/components/react/dist/mpx-sticky-header.d.ts.map +0 -1
  222. package/lib/runtime/components/react/dist/mpx-sticky-section.d.ts.map +0 -1
  223. package/lib/runtime/components/react/dist/mpx-swiper-item.d.ts.map +0 -1
  224. package/lib/runtime/components/react/dist/mpx-swiper.d.ts.map +0 -1
  225. package/lib/runtime/components/react/dist/mpx-switch.d.ts.map +0 -1
  226. package/lib/runtime/components/react/dist/mpx-text.d.ts.map +0 -1
  227. package/lib/runtime/components/react/dist/mpx-textarea.d.ts.map +0 -1
  228. package/lib/runtime/components/react/dist/mpx-video.d.ts.map +0 -1
  229. package/lib/runtime/components/react/dist/mpx-view.d.ts.map +0 -1
  230. package/lib/runtime/components/react/dist/mpx-web-view.d.ts.map +0 -1
  231. package/lib/runtime/components/react/dist/parser.d.ts.map +0 -1
  232. package/lib/runtime/components/react/dist/useAnimationHooks.d.ts +0 -33
  233. package/lib/runtime/components/react/dist/useAnimationHooks.d.ts.map +0 -1
  234. package/lib/runtime/components/react/dist/useAnimationHooks.js +0 -289
  235. package/lib/runtime/components/react/dist/useNodesRef.d.ts.map +0 -1
  236. package/lib/runtime/components/react/dist/utils.d.ts.map +0 -1
  237. package/lib/runtime/components/react/useAnimationHooks.ts +0 -320
  238. package/lib/style-compiler/strip-conditional-loader.js +0 -289
@@ -1,289 +0,0 @@
1
- const fs = require('fs/promises')
2
- const parseRequest = require('../utils/parse-request')
3
- const atImport = require('postcss-import')
4
- const { default: postcss } = require('postcss')
5
-
6
- class Node {
7
- constructor(type, condition = null) {
8
- this.type = type // 'If', 'ElseIf', 'Else' 或 'Text'
9
- this.condition = condition // If 或 Elif 的条件
10
- this.children = []
11
- this.value = ''
12
- }
13
- }
14
-
15
- // 提取 css string 为 token
16
- function tokenize(cssString) {
17
- const regex = /\/\*\s*@mpx-(if|elif|else|endif)(?:\s*\((.*?)\))?\s*\*\//g
18
- const tokens = []
19
- let lastIndex = 0
20
- let match
21
-
22
- while ((match = regex.exec(cssString)) !== null) {
23
- // 如果 token 前有普通文本,生成文本 token
24
- if (match.index > lastIndex) {
25
- const text = cssString.substring(lastIndex, match.index)
26
- tokens.push({ type: 'text', content: text })
27
- }
28
- // match[1] 为关键字:if, elif, else, endif
29
- // match[2] 为条件(如果存在)
30
- tokens.push({
31
- type: match[1], // 'if'、'elif'、'else' 或 'endif'
32
- condition: match[2] ? match[2].trim() : null
33
- })
34
- lastIndex = regex.lastIndex
35
- }
36
- // 处理结尾剩余的文本
37
- if (lastIndex < cssString.length) {
38
- const text = cssString.substring(lastIndex)
39
- tokens.push({ type: 'text', content: text })
40
- }
41
- return tokens
42
- }
43
-
44
- // parse:将生成的 token 数组构造成嵌套的 AST
45
- function parse(cssString) {
46
- const tokens = tokenize(cssString)
47
- const ast = []
48
- const nodeStack = []
49
- let currentChildren = ast
50
- tokens.forEach((token) => {
51
- if (token.type === 'text') {
52
- const node = new Node('Text')
53
- node.value = token.content
54
- currentChildren.push(node)
55
- } else if (token.type === 'if') {
56
- const node = new Node('If', token.condition)
57
- currentChildren.push(node)
58
- nodeStack.push(currentChildren)
59
- currentChildren = node.children
60
- } else if (token.type === 'elif') {
61
- if (nodeStack.length === 0) {
62
- throw new Error('[Mpx style error]: elif without a preceding if')
63
- }
64
- currentChildren = nodeStack[nodeStack.length - 1]
65
- const node = new Node('ElseIf', token.condition)
66
- currentChildren.push(node)
67
- currentChildren = node.children
68
- } else if (token.type === 'else') {
69
- if (nodeStack.length === 0) {
70
- throw new Error('[Mpx style error]: else without a preceding if')
71
- }
72
- currentChildren = nodeStack[nodeStack.length - 1]
73
- const node = new Node('Else')
74
- currentChildren.push(node)
75
- currentChildren = node.children
76
- } else if (token.type === 'endif') {
77
- if (nodeStack.length > 0) {
78
- currentChildren = nodeStack.pop()
79
- }
80
- }
81
- })
82
- return ast
83
- }
84
-
85
- function evaluateCondition(condition, defs) {
86
- try {
87
- const keys = Object.keys(defs)
88
- const values = keys.map((key) => defs[key])
89
- /* eslint-disable no-new-func */
90
- const func = new Function(...keys, `return (${condition});`)
91
- return func(...values)
92
- } catch (e) {
93
- console.error(`[Mpx style error]:Error evaluating condition: ${condition}`, e)
94
- return false
95
- }
96
- }
97
-
98
- function traverseAndEvaluate(ast, defs) {
99
- let output = ''
100
- let batchedIf = false
101
- function traverse(nodes) {
102
- for (const node of nodes) {
103
- if (node.type === 'Text') {
104
- output += node.value
105
- } else if (node.type === 'If') {
106
- // 直接判断 If 节点
107
- batchedIf = false
108
- if (evaluateCondition(node.condition, defs)) {
109
- traverse(node.children)
110
- batchedIf = true
111
- }
112
- } else if (node.type === 'ElseIf' && !batchedIf) {
113
- if (evaluateCondition(node.condition, defs)) {
114
- traverse(node.children)
115
- batchedIf = true
116
- }
117
- } else if (node.type === 'Else' && !batchedIf) {
118
- traverse(node.children)
119
- }
120
- }
121
- }
122
- traverse(ast)
123
- return output
124
- }
125
-
126
- /**
127
- *
128
- * @param {string} content
129
- * @param {Record<string, any>} defs
130
- * @returns
131
- */
132
- function stripCondition(content, defs) {
133
- const ast = parse(content)
134
- const result = traverseAndEvaluate(ast, defs)
135
- return result
136
- }
137
-
138
- /**
139
- * @typedef {Object} StripByPostcssOption
140
- * @property {string} lang 样式语法格式
141
- * @property {string} resourcePath 文件路径
142
- * @property {string} css 源文件
143
- * @property {Record<string, any>} defs 条件定义
144
- * @property {import('webpack').LoaderContext<any>['resolve']} resolve webpack resolve 方法
145
- */
146
-
147
- /**
148
- * @typedef {Object} AtImportConfig
149
- * @property {string} from 当前文件路径
150
- * @property {(filename: string) => Promise<string> | string;} load 加载文件内容的函数
151
- * @property {(id: string, base: string) => Promise<string | null> | string | null;} resolve 解析文件路径的函数
152
- */
153
-
154
- /**
155
- *
156
- * @param {Function} callback
157
- * @param {string} name
158
- * @returns
159
- */
160
- const shouldInstallWarning = (callback, name) => {
161
- return () => {
162
- try {
163
- return callback()
164
- } catch (error) {
165
- throw new Error(
166
- `[mpx-strip-conditional-loader]: ${name} is not installed, please install it first.\norginal Error: ${
167
- error?.message ?? error.toString()
168
- }`,
169
- {
170
- cause: error
171
- }
172
- )
173
- }
174
- }
175
- }
176
- /**
177
- *
178
- * @typedef {import('postcss').ProcessOptions} ProcessOptions
179
- * @typedef {import('postcss').Root} Root
180
- *
181
- * @type {Record<string, ProcessOptions['syntax']>}
182
- */
183
- const styleSyntaxProcesserMap = {
184
- stylus: shouldInstallWarning(() => require('postcss-styl'), 'postcss-styl'),
185
- less: shouldInstallWarning(() => require('postcss-less'), 'postcss-less'),
186
- scss: shouldInstallWarning(() => require('postcss-scss'), 'postcss-scss')
187
- }
188
-
189
- /**
190
- * @param {StripByPostcssOption} options
191
- */
192
- async function stripByPostcss(options) {
193
- const syntax = styleSyntaxProcesserMap[options.lang]?.()
194
- const defs = options.defs ?? {}
195
-
196
- function stripContentCondition(content) {
197
- content = stripCondition(content, defs)
198
-
199
- if (options.lang === 'stylus') {
200
- content = content.replace(/\t/g, ' ')
201
- }
202
-
203
- return content
204
- }
205
-
206
- const afterConditionStrip = stripContentCondition(options.css, defs)
207
-
208
- /**
209
- * @type {import('postcss').AcceptedPlugin[]}
210
- */
211
- const plugins = [
212
- atImport({
213
- async load(filename) {
214
- let content = await fs.readFile(filename, 'utf-8')
215
- const processer = postcss(plugins)
216
-
217
- content = stripContentCondition(content, defs)
218
-
219
- const { css } = await processer.process(content, {
220
- syntax,
221
- from: filename,
222
- to: options.resourcePath
223
- })
224
- return css
225
- },
226
- resolve: (id, base) => {
227
- return new Promise((resolve, reject) => {
228
- options.resolve(base, id, (err, res) => {
229
- if (err) return reject(err)
230
- if (typeof res !== 'string') {
231
- return reject(
232
- new Error(
233
- `[mpx-strip-conditional-loader]: Cannot resolve ${id} from ${base}`
234
- )
235
- )
236
- }
237
- resolve(res)
238
- })
239
- })
240
- }
241
- }),
242
- {
243
- // less/scss syntax 在 postcss 重新生成 css 后,`//` 注释后面不会保留换行,会和后续的 css 语句和注释连在一起,导致后续语法错误
244
- postcssPlugin: 'mpx-strip-conditional-loader-append-command',
245
- CommentExit(comment) {
246
- comment.raws.right ??= ''
247
-
248
- if (comment.raws.right.endsWith('\n')) {
249
- return
250
- }
251
-
252
- comment.raws.right += '\n'
253
- }
254
- }
255
- ]
256
-
257
- const processer = postcss(plugins)
258
- return processer.process(afterConditionStrip, {
259
- from: options.resourcePath,
260
- syntax
261
- })
262
- }
263
-
264
- /**
265
- *
266
- * @this {import('webpack').LoaderContext<any>}
267
- * @param {string} css
268
- */
269
- module.exports = async function (css) {
270
- this.cacheable()
271
-
272
- const callback = this.async()
273
-
274
- const mpx = this.getMpx()
275
- const { resourcePath, queryObj } = parseRequest(this.resource)
276
-
277
- const result = await stripByPostcss({
278
- lang: queryObj.lang,
279
- resourcePath,
280
- css,
281
- defs: mpx.defs,
282
- resolve: this.resolve.bind(this)
283
- })
284
-
285
- callback(null, result.css, result.map)
286
- }
287
-
288
- module.exports.stripByPostcss = stripByPostcss
289
- module.exports.stripCondition = stripCondition