@mpxjs/webpack-plugin 2.6.102 → 2.7.0-beta.1

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 (59) hide show
  1. package/lib/dependencies/AppEntryDependency.js +56 -0
  2. package/lib/dependencies/CommonJsVariableDependency.js +74 -0
  3. package/lib/dependencies/DynamicEntryDependency.js +127 -0
  4. package/lib/dependencies/FlagPluginDependency.js +23 -0
  5. package/lib/dependencies/InjectDependency.js +43 -0
  6. package/lib/dependencies/RecordGlobalComponentsDependency.js +50 -0
  7. package/lib/dependencies/RecordStaticResourceDependency.js +47 -0
  8. package/lib/{dependency → dependencies}/ReplaceDependency.js +19 -2
  9. package/lib/dependencies/ResolveDependency.js +83 -0
  10. package/lib/extractor.js +72 -179
  11. package/lib/file-loader.js +7 -19
  12. package/lib/helpers.js +41 -330
  13. package/lib/index.js +472 -356
  14. package/lib/json-compiler/helper.js +152 -0
  15. package/lib/json-compiler/index.js +148 -407
  16. package/lib/json-compiler/plugin.js +134 -0
  17. package/lib/json-compiler/{theme-loader.js → theme.js} +5 -3
  18. package/lib/loader.js +76 -171
  19. package/lib/native-loader.js +40 -70
  20. package/lib/record-loader.js +11 -0
  21. package/lib/{path-loader.js → resolve-loader.js} +0 -0
  22. package/lib/runtime/i18n.wxs +3 -3
  23. package/lib/selector.js +10 -7
  24. package/lib/style-compiler/index.js +20 -12
  25. package/lib/style-compiler/plugins/trans-special.js +21 -0
  26. package/lib/template-compiler/compiler.js +44 -176
  27. package/lib/template-compiler/index.js +47 -128
  28. package/lib/url-loader.js +11 -29
  29. package/lib/utils/add-query.js +1 -1
  30. package/lib/utils/const.js +5 -0
  31. package/lib/utils/emit-file.js +10 -0
  32. package/lib/utils/get-entry-name.js +13 -0
  33. package/lib/utils/is-url-request.js +10 -1
  34. package/lib/utils/normalize.js +0 -13
  35. package/lib/utils/parse-request.js +3 -3
  36. package/lib/utils/set.js +47 -0
  37. package/lib/utils/stringify-loaders-resource.js +25 -0
  38. package/lib/utils/stringify-query.js +4 -0
  39. package/lib/web/processScript.js +3 -3
  40. package/lib/web/processTemplate.js +2 -0
  41. package/lib/wxml/{wxml-loader.js → loader.js} +24 -60
  42. package/lib/wxs/WxsModuleIdsPlugin.js +32 -0
  43. package/lib/wxs/WxsParserPlugin.js +2 -2
  44. package/lib/wxs/WxsPlugin.js +4 -8
  45. package/lib/wxs/WxsTemplatePlugin.js +46 -92
  46. package/lib/wxs/{wxs-i18n-loader.js → i18n-loader.js} +0 -0
  47. package/lib/wxs/{wxs-loader.js → loader.js} +33 -38
  48. package/lib/wxs/{wxs-pre-loader.js → pre-loader.js} +0 -0
  49. package/lib/wxss/loader.js +31 -43
  50. package/lib/wxss/localsLoader.js +1 -5
  51. package/package.json +4 -8
  52. package/lib/content-loader.js +0 -13
  53. package/lib/dependency/ChildCompileDependency.js +0 -24
  54. package/lib/dependency/InjectDependency.js +0 -26
  55. package/lib/dependency/RemovedModuleDependency.js +0 -23
  56. package/lib/dependency/ResolveDependency.js +0 -49
  57. package/lib/plugin-loader.js +0 -287
  58. package/lib/utils/try-require.js +0 -16
  59. package/lib/wxss/getImportPrefix.js +0 -30
@@ -265,17 +265,17 @@ var dateTimeFormats = {}
265
265
  var numberFormats = {}
266
266
 
267
267
  function getMessages () {
268
- // __mpx_messages__会在编译时通过lib/wxs/wxs-i18n-loader注入
268
+ // __mpx_messages__会在编译时通过lib/wxs/i18n-loader注入
269
269
  return __mpx_messages__ || messages
270
270
  }
271
271
 
272
272
  function getDateTimeFormats () {
273
- // __mpx_datetime_formats__会在编译时通过lib/wxs/wxs-i18n-loader注入
273
+ // __mpx_datetime_formats__会在编译时通过lib/wxs/i18n-loader注入
274
274
  return __mpx_datetime_formats__ || dateTimeFormats
275
275
  }
276
276
 
277
277
  function getNumberFormats () {
278
- // __mpx_number_formats__会在编译时通过lib/wxs/wxs-i18n-loader注入
278
+ // __mpx_number_formats__会在编译时通过lib/wxs/i18n-loader注入
279
279
  return __mpx_number_formats__ || numberFormats
280
280
  }
281
281
 
package/lib/selector.js CHANGED
@@ -1,18 +1,19 @@
1
1
  const parseComponent = require('./parser')
2
- const loaderUtils = require('loader-utils')
3
- const getMainCompilation = require('./utils/get-main-compilation')
2
+ const parseRequest = require('./utils/parse-request')
4
3
 
5
4
  module.exports = function (content) {
6
5
  this.cacheable()
7
- const mainCompilation = getMainCompilation(this._compilation)
8
- const mpx = mainCompilation.__mpx__
6
+ // todo 移除mpx访问依赖,支持thread-loader
7
+ const mpx = this.getMpx()
9
8
  if (!mpx) {
10
9
  return content
11
10
  }
11
+ const { queryObj } = parseRequest(this.resource)
12
+ const type = queryObj.type
13
+ const index = queryObj.index || 0
12
14
  const mode = mpx.mode
13
15
  const env = mpx.env
14
16
  const defs = mpx.defs
15
- const query = loaderUtils.getOptions(this) || {}
16
17
  const filePath = this.resourcePath
17
18
  const parts = parseComponent(content, {
18
19
  filePath,
@@ -21,9 +22,11 @@ module.exports = function (content) {
21
22
  defs,
22
23
  env
23
24
  })
24
- let part = parts[query.type] || {}
25
+ let part = parts[type] || {}
25
26
  if (Array.isArray(part)) {
26
- part = part[query.index]
27
+ part = part[index] || {
28
+ content: ''
29
+ }
27
30
  }
28
31
  this.callback(null, part.content, part.map)
29
32
  }
@@ -1,26 +1,27 @@
1
- const getMainCompilation = require('../utils/get-main-compilation')
2
1
  const postcss = require('postcss')
3
- const loaderUtils = require('loader-utils')
4
2
  const loadPostcssConfig = require('./load-postcss-config')
5
-
6
3
  const trim = require('./plugins/trim')
7
4
  const rpx = require('./plugins/rpx')
8
5
  const vw = require('./plugins/vw')
9
6
  const pluginCondStrip = require('./plugins/conditional-strip')
10
7
  const scopeId = require('./plugins/scope-id')
8
+ const transSpecial = require('./plugins/trans-special')
11
9
  const matchCondition = require('../utils/match-condition')
10
+ const parseRequest = require('../utils/parse-request')
12
11
 
13
12
  module.exports = function (css, map) {
14
13
  this.cacheable()
15
14
  const cb = this.async()
16
- const loaderOptions = loaderUtils.getOptions(this) || {}
17
-
18
- const mainCompilation = getMainCompilation(this._compilation)
19
- const mpx = mainCompilation.__mpx__
15
+ const { resourcePath, queryObj } = parseRequest(this.resource)
16
+ const id = queryObj.moduleId || queryObj.mid
17
+ const mpx = this.getMpx()
20
18
  const defs = mpx.defs
21
-
19
+ const mode = mpx.mode
20
+ const packageName = queryObj.packageRoot || mpx.currentPackageRoot || 'main'
21
+ const componentsMap = mpx.componentsMap[packageName]
22
+ const pagesMap = mpx.pagesMap
23
+ const isApp = !(pagesMap[resourcePath] || componentsMap[resourcePath])
22
24
  const transRpxRulesRaw = mpx.transRpxRules
23
-
24
25
  const transRpxRules = transRpxRulesRaw ? (Array.isArray(transRpxRulesRaw) ? transRpxRulesRaw : [transRpxRulesRaw]) : []
25
26
 
26
27
  const testResolveRange = (include = () => true, exclude) => {
@@ -38,10 +39,13 @@ module.exports = function (css, map) {
38
39
  },
39
40
  config.options
40
41
  )
42
+ // ali环境处理host选择器
43
+ if (mode === 'ali') {
44
+ plugins.push(transSpecial({ id }))
45
+ }
41
46
 
42
- if (loaderOptions.scoped) {
43
- const moduleId = loaderOptions.moduleId || loaderOptions.mid
44
- plugins.push(scopeId({ id: moduleId }))
47
+ if (queryObj.scoped) {
48
+ plugins.push(scopeId({ id }))
45
49
  }
46
50
 
47
51
  plugins.push(pluginCondStrip({
@@ -79,6 +83,10 @@ module.exports = function (css, map) {
79
83
  return postcss(plugins)
80
84
  .process(css, options)
81
85
  .then(result => {
86
+ // ali环境添加全局样式抹平root差异
87
+ if (mode === 'ali' && isApp) {
88
+ result.css += '\n.mpx-root-view { display: inline; line-height: normal; }\n'
89
+ }
82
90
  if (result.messages) {
83
91
  result.messages.forEach(({ type, file }) => {
84
92
  if (type === 'dependency') {
@@ -0,0 +1,21 @@
1
+ const postcss = require('postcss')
2
+ const selectorParser = require('postcss-selector-parser')
3
+
4
+ module.exports = postcss.plugin('trans-special', ({ id }) => root => {
5
+ root.each(function rewriteSelector (node) {
6
+ if (!node.selector) return
7
+ node.selector = selectorParser(selectors => {
8
+ selectors.each(selector => {
9
+ selector.each(n => {
10
+ if (/^:host$/.test(n.value)) {
11
+ const compoundSelectors = n.nodes
12
+ n.replaceWith(selectorParser.className({
13
+ value: 'host-' + id
14
+ }))
15
+ selector.insertAfter(n, compoundSelectors)
16
+ }
17
+ })
18
+ })
19
+ }).process(node.selector).result
20
+ })
21
+ })
@@ -129,107 +129,6 @@ function isForbiddenTag (el) {
129
129
  )
130
130
  }
131
131
 
132
- // mpx special comments
133
- // example
134
- /*
135
- {
136
- 'tt,swan': {
137
- remove: [
138
- 'open-type',
139
- // src mode attr
140
- 'wx:if'
141
- ],
142
- add: {
143
- type: 'primary',
144
- // attr name only
145
- foo: null,
146
- }
147
- }
148
- }
149
- */
150
- let curMpxComment = null
151
-
152
- function evalMpxCommentExp (exp) {
153
- /* eslint-disable no-new-func */
154
- const f = new Function(`return ${exp};`)
155
- return f()
156
- }
157
-
158
- function isMpxCommentAttrs (content) {
159
- return /@mpx-attrs/.test(content)
160
- }
161
-
162
- function normalizePlatformMpxAttrsOpts (opts) {
163
- const ret = {}
164
- // Array to map for removing attributes
165
- ret.remove = (opts.remove || []).reduce((acc, val) => {
166
- acc[val] = true
167
- return acc
168
- }, {})
169
- // Default adding map
170
- ret.add = opts.add || {}
171
- return ret
172
- }
173
-
174
- function produceMpxCommentAttrs (content) {
175
- const exp = /@mpx-attrs[^(]*?\(([\s\S]*)\)/.exec(content)[1].trim()
176
- const tmpOpts = evalMpxCommentExp(exp)
177
- // normalize
178
- Object.keys(tmpOpts).forEach(k => {
179
- Object.assign(tmpOpts[k], normalizePlatformMpxAttrsOpts(tmpOpts[k]))
180
-
181
- if (k.indexOf(',') > -1) {
182
- const modes = k.split(',')
183
- modes.forEach(mode => {
184
- tmpOpts[mode] = tmpOpts[k]
185
- })
186
- delete tmpOpts[k]
187
- }
188
- })
189
- curMpxComment = tmpOpts
190
- }
191
-
192
- function modifyAttrsFromCurMpxAttrOptions (attrs, curModeMpxComment) {
193
- const removeMap = curModeMpxComment.remove
194
- const addMap = curModeMpxComment.add
195
-
196
- const newAttrs = []
197
- attrs.forEach(attr => {
198
- if (!removeMap[attr.name]) {
199
- newAttrs.push(attr)
200
- }
201
- })
202
-
203
- Object.keys(addMap).forEach(name => {
204
- newAttrs.push({
205
- name,
206
- value: addMap[name]
207
- })
208
- })
209
-
210
- return newAttrs
211
- }
212
-
213
- function consumeMpxCommentAttrs (attrs, mode) {
214
- let ret = attrs
215
- if (curMpxComment) {
216
- const curModeMpxComment = curMpxComment[mode]
217
- if (curModeMpxComment) {
218
- ret = modifyAttrsFromCurMpxAttrOptions(attrs, curModeMpxComment)
219
- }
220
-
221
- // reset
222
- curMpxComment = null
223
- }
224
- return ret
225
- }
226
-
227
- function assertMpxCommentAttrsEnd () {
228
- if (curMpxComment) {
229
- error$1('No target for @mpx-attrs!')
230
- }
231
- }
232
-
233
132
  // Browser environment sniffing
234
133
  const UA = inBrowser && window.navigator.userAgent.toLowerCase()
235
134
  const isIE = UA && /msie|trident/.test(UA)
@@ -253,6 +152,9 @@ let forScopesMap = {}
253
152
  let hasI18n = false
254
153
  let i18nInjectableComputed = []
255
154
  let env
155
+ let platformGetTagNamespace
156
+ let filePath
157
+ let refId
256
158
 
257
159
  function updateForScopesMap () {
258
160
  forScopes.forEach((scope) => {
@@ -279,9 +181,6 @@ const deleteErrorInResultMap = (node) => {
279
181
  rulesResultMap.delete(node)
280
182
  Array.isArray(node.children) && node.children.forEach(item => deleteErrorInResultMap(item))
281
183
  }
282
- let platformGetTagNamespace
283
- let basename
284
- let refId
285
184
 
286
185
  function baseWarn (msg) {
287
186
  console.warn(('[template compiler]: ' + msg))
@@ -310,7 +209,7 @@ function decode (value) {
310
209
 
311
210
  const i18nFuncNames = ['\\$(t)', '\\$(tc)', '\\$(te)', '\\$(d)', '\\$(n)']
312
211
  const i18nWxsPath = normalize.lib('runtime/i18n.wxs')
313
- const i18nWxsLoaderPath = normalize.lib('wxs/wxs-i18n-loader.js')
212
+ const i18nWxsLoaderPath = normalize.lib('wxs/i18n-loader.js')
314
213
  // 添加~前缀避免wxs绝对路径在存在projectRoot时被拼接为错误路径
315
214
  const i18nWxsRequest = '~' + i18nWxsLoaderPath + '!' + i18nWxsPath
316
215
  const i18nModuleName = '__i18n__'
@@ -599,6 +498,7 @@ function parseComponent (content, options) {
599
498
  mode = options.mode || 'wx'
600
499
  defs = options.defs || {}
601
500
  env = options.env
501
+ filePath = options.filePath
602
502
 
603
503
  let sfc = {
604
504
  template: null,
@@ -691,9 +591,6 @@ function parseComponent (content, options) {
691
591
  if (attr.name === 'scoped') {
692
592
  block.scoped = true
693
593
  }
694
- if (attr.name === 'module') {
695
- block.module = attr.value || true
696
- }
697
594
  if (attr.name === 'src') {
698
595
  block.src = attr.value
699
596
  }
@@ -721,7 +618,7 @@ function parseComponent (content, options) {
721
618
 
722
619
  // 对于<script name="json">的标签,传参调用函数,其返回结果作为json的内容
723
620
  if (currentBlock.tag === 'script' && !/^application\/json/.test(currentBlock.type) && currentBlock.name === 'json') {
724
- text = mpxJSON.compileMPXJSONText({ source: text, defs, filePath: options.filePath })
621
+ text = mpxJSON.compileMPXJSONText({ source: text, defs, filePath })
725
622
  }
726
623
  currentBlock.content = text
727
624
  currentBlock = null
@@ -773,7 +670,7 @@ function parse (template, options) {
773
670
  defs = options.defs || {}
774
671
  srcMode = options.srcMode || mode
775
672
  isNative = options.isNative
776
- basename = options.basename
673
+ filePath = options.filePath
777
674
  i18n = options.i18n
778
675
  refId = 0
779
676
 
@@ -803,7 +700,7 @@ function parse (template, options) {
803
700
 
804
701
  function genTempRoot () {
805
702
  // 使用临时节点作为root,处理multi root的情况
806
- root = currentParent = getTempNode()
703
+ root = currentParent = getVirtualHostRoot(options, meta)
807
704
  stack.push(root)
808
705
  }
809
706
 
@@ -824,16 +721,6 @@ function parse (template, options) {
824
721
  attrs = guardIESVGBug(attrs)
825
722
  }
826
723
 
827
- if (options.globalMpxAttrsFilter) {
828
- attrs = modifyAttrsFromCurMpxAttrOptions(attrs, normalizePlatformMpxAttrsOpts(options.globalMpxAttrsFilter({
829
- tagName: tag,
830
- attrs,
831
- __mpx_mode__: mode,
832
- filePath: options.filePath
833
- }) || {}))
834
- }
835
- attrs = consumeMpxCommentAttrs(attrs, mode)
836
-
837
724
  let element = createASTElement(tag, attrs, currentParent)
838
725
  if (ns) {
839
726
  element.ns = ns
@@ -848,21 +735,6 @@ function parse (template, options) {
848
735
  )
849
736
  }
850
737
 
851
- // single root
852
- // // gen root
853
- // if (!root) {
854
- // root = element
855
- // } else {
856
- // // mount element
857
- // if (currentParent) {
858
- // currentParent.children.push(element)
859
- // element.parent = currentParent
860
- // } else {
861
- // multiRootError = true
862
- // return
863
- // }
864
- // }
865
-
866
738
  // multi root
867
739
  if (!currentParent) genTempRoot()
868
740
 
@@ -931,10 +803,7 @@ function parse (template, options) {
931
803
  },
932
804
  comment: function comment (text) {
933
805
  if (!currentParent) genTempRoot()
934
- // special comments should not be output
935
- if (isMpxCommentAttrs(text)) {
936
- produceMpxCommentAttrs(text)
937
- } else if (options.hasComment) {
806
+ if (options.hasComment) {
938
807
  currentParent.children.push({
939
808
  type: 3,
940
809
  text: text,
@@ -945,8 +814,6 @@ function parse (template, options) {
945
814
  }
946
815
  })
947
816
 
948
- assertMpxCommentAttrsEnd()
949
-
950
817
  if (multiRootError) {
951
818
  error$1('Template fields should has one single root, considering wrapping your template content with <view> or <text> tag!')
952
819
  }
@@ -1136,15 +1003,6 @@ function processComponentIs (el, options) {
1136
1003
  }
1137
1004
  }
1138
1005
 
1139
- // function processComponentDepth (el, options) {
1140
- // if (isComponentNode(el,options)) {
1141
- // addAttrs(el, [{
1142
- // name: 'mpxDepth',
1143
- // value: '{{mpxDepth + 1}}'
1144
- // }])
1145
- // }
1146
- // }
1147
-
1148
1006
  const eventIdentifier = '__mpx_event__'
1149
1007
 
1150
1008
  function parseFuncStr2 (str) {
@@ -1552,9 +1410,13 @@ function postProcessWxs (el, meta) {
1552
1410
  content = el.children.filter((child) => {
1553
1411
  return child.type === 3 && !child.isComment
1554
1412
  }).map(child => child.text).join('\n')
1555
- src = addQuery('./' + basename, {
1413
+
1414
+ const fakeRequest = filePath + config[mode].wxs.ext
1415
+
1416
+ src = addQuery(`~${fakeRequest}!=!${filePath}`, {
1556
1417
  wxsModule: module
1557
1418
  })
1419
+
1558
1420
  addAttrs(el, [{
1559
1421
  name: config[mode].wxs.src,
1560
1422
  value: src
@@ -1635,9 +1497,9 @@ function postProcessFor (el) {
1635
1497
  }
1636
1498
 
1637
1499
  function evalExp (exp) {
1638
- // eslint-disable-next-line no-new-func
1639
1500
  let result = { success: false }
1640
1501
  try {
1502
+ // eslint-disable-next-line no-new-func
1641
1503
  const fn = new Function(`return ${exp};`)
1642
1504
  result = {
1643
1505
  success: true,
@@ -1876,10 +1738,11 @@ function processWebExternalClassesHack (el, options) {
1876
1738
  function processScoped (el, options) {
1877
1739
  if (options.hasScoped && isRealNode(el)) {
1878
1740
  const moduleId = options.moduleId
1741
+ const rootModuleId = options.isComponent ? '' : 'mpx-app-scope' // 处理app全局样式对页面的影响
1879
1742
  const staticClass = getAndRemoveAttr(el, 'class').val
1880
1743
  addAttrs(el, [{
1881
1744
  name: 'class',
1882
- value: staticClass ? `${staticClass} ${moduleId}` : moduleId
1745
+ value: `${staticClass || ''} ${moduleId} ${rootModuleId}`
1883
1746
  }])
1884
1747
  }
1885
1748
  }
@@ -1898,38 +1761,43 @@ function processBuiltInComponents (el, meta) {
1898
1761
  }
1899
1762
  }
1900
1763
 
1901
- function processAliStyleClassHack (el, options, root) {
1764
+ function processAliStyleClassHack (el, options) {
1765
+ if (!isComponentNode(el, options)) return
1902
1766
  ['style', 'class'].forEach((type) => {
1903
1767
  let exp = getAndRemoveAttr(el, type).val
1904
- let sep = type === 'style' ? ';' : ' '
1905
-
1906
1768
  let typeName = 'mpx' + type.replace(/^./, (matched) => {
1907
1769
  return matched.toUpperCase()
1908
1770
  })
1909
-
1910
- if (options.isComponent && el.parent === root && isRealNode(el)) {
1911
- if (exp !== undefined) {
1912
- exp = `{{${typeName}||''}}` + sep + exp
1913
- } else {
1914
- exp = `{{${typeName}||''}}`
1915
- }
1916
- }
1917
1771
  if (exp !== undefined) {
1918
- if (isComponentNode(el, options)) {
1919
- addAttrs(el, [{
1920
- name: typeName,
1921
- value: exp
1922
- }])
1923
- } else {
1924
- addAttrs(el, [{
1925
- name: type,
1926
- value: exp
1927
- }])
1928
- }
1772
+ addAttrs(el, [{
1773
+ name: typeName,
1774
+ value: exp
1775
+ }])
1929
1776
  }
1930
1777
  })
1931
1778
  }
1932
1779
 
1780
+ // 有virtualHost情况wx组件注入virtualHost。无virtualHost阿里组件注入root-view。其他跳过。
1781
+ function getVirtualHostRoot (options, meta) {
1782
+ if (mode === 'wx' && options.hasVirtualHost && options.isComponent) {
1783
+ !meta.options && (meta.options = {})
1784
+ meta.options.virtualHost = true
1785
+ }
1786
+ if (mode === 'ali' && !options.hasVirtualHost && options.isComponent) {
1787
+ return createASTElement('view', [
1788
+ {
1789
+ name: 'class',
1790
+ value: `mpx-root-view host-${options.moduleId} ${options.hasScoped ? options.moduleId : ''} {{mpxClass||''}}`
1791
+ },
1792
+ {
1793
+ name: 'style',
1794
+ value: `{{mpxStyle||''}}`
1795
+ }
1796
+ ])
1797
+ }
1798
+ return getTempNode()
1799
+ }
1800
+
1933
1801
  function processShow (el, options, root) {
1934
1802
  let show = getAndRemoveAttr(el, config[mode].directive.show).val
1935
1803
  if (mode === 'swan') show = wrapMustache(show)