@mpxjs/webpack-plugin 2.6.110 → 2.7.0-beta.10

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 (72) hide show
  1. package/lib/dependencies/AppEntryDependency.js +56 -0
  2. package/lib/dependencies/CommonJsVariableDependency.js +69 -0
  3. package/lib/dependencies/DynamicEntryDependency.js +131 -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/RecordResourceMapDependency.js +52 -0
  8. package/lib/dependencies/RemoveEntryDependency.js +40 -0
  9. package/lib/{dependency → dependencies}/ReplaceDependency.js +19 -2
  10. package/lib/dependencies/ResolveDependency.js +84 -0
  11. package/lib/extractor.js +73 -178
  12. package/lib/file-loader.js +7 -19
  13. package/lib/helpers.js +42 -330
  14. package/lib/index.js +497 -376
  15. package/lib/json-compiler/helper.js +148 -0
  16. package/lib/json-compiler/index.js +195 -439
  17. package/lib/json-compiler/plugin.js +150 -0
  18. package/lib/json-compiler/{theme-loader.js → theme.js} +5 -3
  19. package/lib/loader.js +107 -226
  20. package/lib/native-loader.js +65 -132
  21. package/lib/parser.js +1 -2
  22. package/lib/record-loader.js +11 -0
  23. package/lib/{path-loader.js → resolve-loader.js} +0 -0
  24. package/lib/resolver/AddEnvPlugin.js +3 -2
  25. package/lib/resolver/AddModePlugin.js +3 -2
  26. package/lib/runtime/base.styl +5 -0
  27. package/lib/runtime/i18n.wxs +3 -3
  28. package/lib/runtime/optionProcessor.js +3 -3
  29. package/lib/selector.js +9 -11
  30. package/lib/style-compiler/index.js +12 -19
  31. package/lib/template-compiler/compiler.js +21 -162
  32. package/lib/template-compiler/index.js +47 -136
  33. package/lib/url-loader.js +11 -29
  34. package/lib/utils/add-query.js +1 -1
  35. package/lib/utils/const.js +9 -0
  36. package/lib/utils/emit-file.js +10 -0
  37. package/lib/utils/eval-json-js.js +31 -0
  38. package/lib/utils/get-entry-name.js +13 -0
  39. package/lib/utils/get-json-content.js +41 -0
  40. package/lib/utils/is-url-request.js +10 -1
  41. package/lib/utils/normalize.js +0 -13
  42. package/lib/utils/parse-request.js +3 -3
  43. package/lib/utils/resolve.js +13 -0
  44. package/lib/utils/set.js +47 -0
  45. package/lib/utils/stringify-loaders-resource.js +25 -0
  46. package/lib/utils/stringify-query.js +4 -0
  47. package/lib/web/processJSON.js +113 -142
  48. package/lib/web/processScript.js +32 -26
  49. package/lib/web/processTemplate.js +57 -39
  50. package/lib/wxml/{wxml-loader.js → loader.js} +24 -60
  51. package/lib/wxs/WxsModuleIdsPlugin.js +32 -0
  52. package/lib/wxs/WxsParserPlugin.js +2 -2
  53. package/lib/wxs/WxsPlugin.js +4 -8
  54. package/lib/wxs/WxsTemplatePlugin.js +46 -92
  55. package/lib/wxs/{wxs-i18n-loader.js → i18n-loader.js} +1 -3
  56. package/lib/wxs/{wxs-loader.js → loader.js} +41 -50
  57. package/lib/wxs/{wxs-pre-loader.js → pre-loader.js} +3 -4
  58. package/lib/wxss/loader.js +31 -43
  59. package/lib/wxss/localsLoader.js +1 -5
  60. package/package.json +5 -9
  61. package/lib/built-in-loader.js +0 -49
  62. package/lib/content-loader.js +0 -13
  63. package/lib/dependency/ChildCompileDependency.js +0 -24
  64. package/lib/dependency/InjectDependency.js +0 -26
  65. package/lib/dependency/RemovedModuleDependency.js +0 -23
  66. package/lib/dependency/ResolveDependency.js +0 -49
  67. package/lib/plugin-loader.js +0 -287
  68. package/lib/staticConfig.js +0 -4
  69. package/lib/utils/get-main-compilation.js +0 -6
  70. package/lib/utils/read-json-for-src.js +0 -34
  71. package/lib/utils/try-require.js +0 -16
  72. package/lib/wxss/getImportPrefix.js +0 -30
@@ -1,11 +1,10 @@
1
1
  const JSON5 = require('json5')
2
2
  const he = require('he')
3
3
  const config = require('../config')
4
- const { MPX_ROOT_VIEW, MPX_APP_MODULE_ID } = require('../staticConfig')
4
+ const { MPX_ROOT_VIEW, MPX_APP_MODULE_ID } = require('../utils/const')
5
5
  const normalize = require('../utils/normalize')
6
6
  const isValidIdentifierStr = require('../utils/is-valid-identifier-str')
7
7
  const isEmptyObject = require('../utils/is-empty-object')
8
- const mpxJSON = require('../utils/mpx-json')
9
8
  const getRulesRunner = require('../platform/index')
10
9
  const addQuery = require('../utils/add-query')
11
10
  const transDynamicClassExpr = require('./trans-dynamic-class-expr')
@@ -130,107 +129,6 @@ function isForbiddenTag (el) {
130
129
  )
131
130
  }
132
131
 
133
- // mpx special comments
134
- // example
135
- /*
136
- {
137
- 'tt,swan': {
138
- remove: [
139
- 'open-type',
140
- // src mode attr
141
- 'wx:if'
142
- ],
143
- add: {
144
- type: 'primary',
145
- // attr name only
146
- foo: null,
147
- }
148
- }
149
- }
150
- */
151
- let curMpxComment = null
152
-
153
- function evalMpxCommentExp (exp) {
154
- /* eslint-disable no-new-func */
155
- const f = new Function(`return ${exp};`)
156
- return f()
157
- }
158
-
159
- function isMpxCommentAttrs (content) {
160
- return /@mpx-attrs/.test(content)
161
- }
162
-
163
- function normalizePlatformMpxAttrsOpts (opts) {
164
- const ret = {}
165
- // Array to map for removing attributes
166
- ret.remove = (opts.remove || []).reduce((acc, val) => {
167
- acc[val] = true
168
- return acc
169
- }, {})
170
- // Default adding map
171
- ret.add = opts.add || {}
172
- return ret
173
- }
174
-
175
- function produceMpxCommentAttrs (content) {
176
- const exp = /@mpx-attrs[^(]*?\(([\s\S]*)\)/.exec(content)[1].trim()
177
- const tmpOpts = evalMpxCommentExp(exp)
178
- // normalize
179
- Object.keys(tmpOpts).forEach(k => {
180
- Object.assign(tmpOpts[k], normalizePlatformMpxAttrsOpts(tmpOpts[k]))
181
-
182
- if (k.indexOf(',') > -1) {
183
- const modes = k.split(',')
184
- modes.forEach(mode => {
185
- tmpOpts[mode] = tmpOpts[k]
186
- })
187
- delete tmpOpts[k]
188
- }
189
- })
190
- curMpxComment = tmpOpts
191
- }
192
-
193
- function modifyAttrsFromCurMpxAttrOptions (attrs, curModeMpxComment) {
194
- const removeMap = curModeMpxComment.remove
195
- const addMap = curModeMpxComment.add
196
-
197
- const newAttrs = []
198
- attrs.forEach(attr => {
199
- if (!removeMap[attr.name]) {
200
- newAttrs.push(attr)
201
- }
202
- })
203
-
204
- Object.keys(addMap).forEach(name => {
205
- newAttrs.push({
206
- name,
207
- value: addMap[name]
208
- })
209
- })
210
-
211
- return newAttrs
212
- }
213
-
214
- function consumeMpxCommentAttrs (attrs, mode) {
215
- let ret = attrs
216
- if (curMpxComment) {
217
- const curModeMpxComment = curMpxComment[mode]
218
- if (curModeMpxComment) {
219
- ret = modifyAttrsFromCurMpxAttrOptions(attrs, curModeMpxComment)
220
- }
221
-
222
- // reset
223
- curMpxComment = null
224
- }
225
- return ret
226
- }
227
-
228
- function assertMpxCommentAttrsEnd () {
229
- if (curMpxComment) {
230
- error$1('No target for @mpx-attrs!')
231
- }
232
- }
233
-
234
132
  // Browser environment sniffing
235
133
  const UA = inBrowser && window.navigator.userAgent.toLowerCase()
236
134
  const isIE = UA && /msie|trident/.test(UA)
@@ -254,6 +152,9 @@ let forScopesMap = {}
254
152
  let hasI18n = false
255
153
  let i18nInjectableComputed = []
256
154
  let env
155
+ let platformGetTagNamespace
156
+ let filePath
157
+ let refId
257
158
 
258
159
  function updateForScopesMap () {
259
160
  forScopes.forEach((scope) => {
@@ -280,9 +181,6 @@ const deleteErrorInResultMap = (node) => {
280
181
  rulesResultMap.delete(node)
281
182
  Array.isArray(node.children) && node.children.forEach(item => deleteErrorInResultMap(item))
282
183
  }
283
- let platformGetTagNamespace
284
- let basename
285
- let refId
286
184
 
287
185
  function baseWarn (msg) {
288
186
  console.warn(('[template compiler]: ' + msg))
@@ -311,7 +209,7 @@ function decode (value) {
311
209
 
312
210
  const i18nFuncNames = ['\\$(t)', '\\$(tc)', '\\$(te)', '\\$(d)', '\\$(n)']
313
211
  const i18nWxsPath = normalize.lib('runtime/i18n.wxs')
314
- const i18nWxsLoaderPath = normalize.lib('wxs/wxs-i18n-loader.js')
212
+ const i18nWxsLoaderPath = normalize.lib('wxs/i18n-loader.js')
315
213
  // 添加~前缀避免wxs绝对路径在存在projectRoot时被拼接为错误路径
316
214
  const i18nWxsRequest = '~' + i18nWxsLoaderPath + '!' + i18nWxsPath
317
215
  const i18nModuleName = '__i18n__'
@@ -598,8 +496,8 @@ function parseHTML (html, options) {
598
496
 
599
497
  function parseComponent (content, options) {
600
498
  mode = options.mode || 'wx'
601
- defs = options.defs || {}
602
499
  env = options.env
500
+ filePath = options.filePath
603
501
 
604
502
  let sfc = {
605
503
  template: null,
@@ -649,6 +547,9 @@ function parseComponent (content, options) {
649
547
  if (/^application\/json/.test(currentBlock.type) || currentBlock.name === 'json') {
650
548
  tag = 'json'
651
549
  }
550
+ if (currentBlock.name === 'json') {
551
+ currentBlock.useJSONJS = true
552
+ }
652
553
  }
653
554
  if (currentBlock.mode && currentBlock.env) {
654
555
  if (currentBlock.mode === mode && currentBlock.env === env) {
@@ -692,9 +593,6 @@ function parseComponent (content, options) {
692
593
  if (attr.name === 'scoped') {
693
594
  block.scoped = true
694
595
  }
695
- if (attr.name === 'module') {
696
- block.module = attr.value || true
697
- }
698
596
  if (attr.name === 'src') {
699
597
  block.src = attr.value
700
598
  }
@@ -710,20 +608,15 @@ function parseComponent (content, options) {
710
608
  }
711
609
  }
712
610
 
713
- function end (tag, start, end) {
611
+ function end (tag, start) {
714
612
  if (depth === 1 && currentBlock) {
715
613
  currentBlock.end = start
716
614
  let text = content.slice(currentBlock.start, currentBlock.end)
717
615
  // pad content so that linters and pre-processors can output correct
718
616
  // line numbers in errors and warnings
719
- if (currentBlock.tag !== 'template' && options.pad) {
617
+ if (options.pad) {
720
618
  text = padContent(currentBlock, options.pad) + text
721
619
  }
722
-
723
- // 对于<script name="json">的标签,传参调用函数,其返回结果作为json的内容
724
- if (currentBlock.tag === 'script' && !/^application\/json/.test(currentBlock.type) && currentBlock.name === 'json') {
725
- text = mpxJSON.compileMPXJSONText({ source: text, defs, filePath: options.filePath })
726
- }
727
620
  currentBlock.content = text
728
621
  currentBlock = null
729
622
  }
@@ -774,7 +667,7 @@ function parse (template, options) {
774
667
  defs = options.defs || {}
775
668
  srcMode = options.srcMode || mode
776
669
  isNative = options.isNative
777
- basename = options.basename
670
+ filePath = options.filePath
778
671
  i18n = options.i18n
779
672
  refId = 0
780
673
 
@@ -825,16 +718,6 @@ function parse (template, options) {
825
718
  attrs = guardIESVGBug(attrs)
826
719
  }
827
720
 
828
- if (options.globalMpxAttrsFilter) {
829
- attrs = modifyAttrsFromCurMpxAttrOptions(attrs, normalizePlatformMpxAttrsOpts(options.globalMpxAttrsFilter({
830
- tagName: tag,
831
- attrs,
832
- __mpx_mode__: mode,
833
- filePath: options.filePath
834
- }) || {}))
835
- }
836
- attrs = consumeMpxCommentAttrs(attrs, mode)
837
-
838
721
  let element = createASTElement(tag, attrs, currentParent)
839
722
  if (ns) {
840
723
  element.ns = ns
@@ -849,21 +732,6 @@ function parse (template, options) {
849
732
  )
850
733
  }
851
734
 
852
- // single root
853
- // // gen root
854
- // if (!root) {
855
- // root = element
856
- // } else {
857
- // // mount element
858
- // if (currentParent) {
859
- // currentParent.children.push(element)
860
- // element.parent = currentParent
861
- // } else {
862
- // multiRootError = true
863
- // return
864
- // }
865
- // }
866
-
867
735
  // multi root
868
736
  if (!currentParent) genTempRoot()
869
737
 
@@ -932,10 +800,7 @@ function parse (template, options) {
932
800
  },
933
801
  comment: function comment (text) {
934
802
  if (!currentParent) genTempRoot()
935
- // special comments should not be output
936
- if (isMpxCommentAttrs(text)) {
937
- produceMpxCommentAttrs(text)
938
- } else if (options.hasComment) {
803
+ if (options.hasComment) {
939
804
  currentParent.children.push({
940
805
  type: 3,
941
806
  text: text,
@@ -946,8 +811,6 @@ function parse (template, options) {
946
811
  }
947
812
  })
948
813
 
949
- assertMpxCommentAttrsEnd()
950
-
951
814
  if (multiRootError) {
952
815
  error$1('Template fields should has one single root, considering wrapping your template content with <view> or <text> tag!')
953
816
  }
@@ -1137,15 +1000,6 @@ function processComponentIs (el, options) {
1137
1000
  }
1138
1001
  }
1139
1002
 
1140
- // function processComponentDepth (el, options) {
1141
- // if (isComponentNode(el,options)) {
1142
- // addAttrs(el, [{
1143
- // name: 'mpxDepth',
1144
- // value: '{{mpxDepth + 1}}'
1145
- // }])
1146
- // }
1147
- // }
1148
-
1149
1003
  const eventIdentifier = '__mpx_event__'
1150
1004
 
1151
1005
  function parseFuncStr2 (str) {
@@ -1553,9 +1407,13 @@ function postProcessWxs (el, meta) {
1553
1407
  content = el.children.filter((child) => {
1554
1408
  return child.type === 3 && !child.isComment
1555
1409
  }).map(child => child.text).join('\n')
1556
- src = addQuery('./' + basename, {
1410
+
1411
+ const fakeRequest = filePath + config[mode].wxs.ext
1412
+
1413
+ src = addQuery(`~${fakeRequest}!=!${filePath}`, {
1557
1414
  wxsModule: module
1558
1415
  })
1416
+
1559
1417
  addAttrs(el, [{
1560
1418
  name: config[mode].wxs.src,
1561
1419
  value: src
@@ -1636,9 +1494,9 @@ function postProcessFor (el) {
1636
1494
  }
1637
1495
 
1638
1496
  function evalExp (exp) {
1639
- // eslint-disable-next-line no-new-func
1640
1497
  let result = { success: false }
1641
1498
  try {
1499
+ // eslint-disable-next-line no-new-func
1642
1500
  const fn = new Function(`return ${exp};`)
1643
1501
  result = {
1644
1502
  success: true,
@@ -2484,5 +2342,6 @@ module.exports = {
2484
2342
  makeAttrsMap,
2485
2343
  stringifyAttr,
2486
2344
  parseMustache,
2487
- stringifyWithResolveComputed
2345
+ stringifyWithResolveComputed,
2346
+ addAttrs
2488
2347
  }
@@ -1,19 +1,14 @@
1
1
  const compiler = require('./compiler')
2
- const loaderUtils = require('loader-utils')
3
2
  const bindThis = require('./bind-this').transform
4
- const InjectDependency = require('../dependency/InjectDependency')
5
3
  const parseRequest = require('../utils/parse-request')
6
- const getMainCompilation = require('../utils/get-main-compilation')
7
4
  const matchCondition = require('../utils/match-condition')
8
- const path = require('path')
5
+ const loaderUtils = require('loader-utils')
9
6
 
10
7
  module.exports = function (raw) {
11
8
  this.cacheable()
12
- const options = loaderUtils.getOptions(this) || {}
13
9
  const { resourcePath, queryObj } = parseRequest(this.resource)
14
- const compilation = this._compilation
15
- const mainCompilation = getMainCompilation(compilation)
16
- const mpx = mainCompilation.__mpx__
10
+ const mpx = this.getMpx()
11
+ const root = mpx.projectRoot
17
12
  const mode = mpx.mode
18
13
  const env = mpx.env
19
14
  const defs = mpx.defs
@@ -25,6 +20,11 @@ module.exports = function (raw) {
25
20
  const packageName = queryObj.packageRoot || mpx.currentPackageRoot || 'main'
26
21
  const componentsMap = mpx.componentsMap[packageName]
27
22
  const wxsContentMap = mpx.wxsContentMap
23
+ const usingComponents = queryObj.usingComponents || []
24
+ const hasComment = queryObj.hasComment
25
+ const isNative = queryObj.isNative
26
+ const hasScoped = queryObj.hasScoped
27
+ const moduleId = queryObj.moduleId
28
28
 
29
29
  const warn = (msg) => {
30
30
  this.emitWarning(
@@ -38,13 +38,12 @@ module.exports = function (raw) {
38
38
  )
39
39
  }
40
40
 
41
- const parsed = compiler.parse(raw, {
41
+ const { root: ast, meta } = compiler.parse(raw, {
42
42
  warn,
43
43
  error,
44
- usingComponents: options.usingComponents,
45
- hasComment: options.hasComment,
46
- isNative: options.isNative,
47
- basename: path.basename(resourcePath),
44
+ usingComponents,
45
+ hasComment,
46
+ isNative,
48
47
  isComponent: !!componentsMap[resourcePath],
49
48
  mode,
50
49
  env,
@@ -52,49 +51,54 @@ module.exports = function (raw) {
52
51
  defs,
53
52
  decodeHTMLText,
54
53
  externalClasses,
55
- hasScoped: options.hasScoped,
56
- moduleId: options.moduleId,
54
+ hasScoped,
55
+ moduleId,
57
56
  filePath: this.resourcePath,
58
57
  i18n,
59
58
  checkUsingComponents: mpx.checkUsingComponents,
60
59
  globalComponents: Object.keys(mpx.usingComponents),
61
- // deprecated option
62
- globalMpxAttrsFilter: mpx.globalMpxAttrsFilter,
63
- forceProxyEvent: matchCondition(this.resourcePath, mpx.forceProxyEventRules),
60
+ forceProxyEvent: matchCondition(resourcePath, mpx.forceProxyEventRules),
64
61
  hasVirtualHost: matchCondition(resourcePath, mpx.autoVirtualHostRules)
65
62
  })
66
63
 
67
- let ast = parsed.root
68
- let meta = parsed.meta
69
-
70
64
  if (meta.wxsContentMap) {
71
65
  for (let module in meta.wxsContentMap) {
72
66
  wxsContentMap[`${resourcePath}~${module}`] = meta.wxsContentMap[module]
73
67
  }
74
68
  }
75
69
 
70
+ let resultSource = ''
71
+
72
+ for (let module in meta.wxsModuleMap) {
73
+ const src = loaderUtils.urlToRequest(meta.wxsModuleMap[module], root)
74
+ resultSource += `var ${module} = require(${loaderUtils.stringifyRequest(this, src)});\n`
75
+ }
76
+
76
77
  let result = compiler.serialize(ast)
77
78
 
78
- if (options.isNative || mpx.forceDisableInject) {
79
+ if (isNative) {
79
80
  return result
80
81
  }
81
82
 
82
- const rawCode = `global.currentInject = {
83
- moduleId: ${JSON.stringify(options.moduleId)},
84
- render: function () {
85
- ${compiler.genNode(ast)}this._r();
86
- }
83
+ const rawCode = `
84
+ global.currentInject = {
85
+ moduleId: ${JSON.stringify(moduleId)},
86
+ render: function () {
87
+ ${compiler.genNode(ast)}
88
+ this._r();
89
+ }
87
90
  };\n`
88
91
 
89
- let renderResult
92
+ let bindResult
90
93
 
91
94
  try {
92
- renderResult = bindThis(rawCode, {
95
+ bindResult = bindThis(rawCode, {
93
96
  needCollect: true,
94
97
  ignoreMap: meta.wxsModuleMap
95
98
  })
96
99
  } catch (e) {
97
- error(`Invalid render function generated by the template, please check!\n
100
+ error(`
101
+ Invalid render function generated by the template, please check!\n
98
102
  Template result:
99
103
  ${result}\n
100
104
  Error code:
@@ -104,123 +108,30 @@ ${e.stack}`)
104
108
  return result
105
109
  }
106
110
 
107
- // todo 此处在loader中往其他模块addDep更加危险,考虑修改为通过抽取后的空模块的module.exports来传递信息
108
- let globalInjectCode = renderResult.code + '\n'
111
+ resultSource += bindResult.code + '\n'
109
112
 
110
- if ((mode === 'tt' || mode === 'swan') && renderResult.propKeys) {
111
- globalInjectCode += `global.currentInject.propKeys = ${JSON.stringify(renderResult.propKeys)};\n`
113
+ if ((mode === 'tt' || mode === 'swan') && bindResult.propKeys) {
114
+ resultSource += `global.currentInject.propKeys = ${JSON.stringify(bindResult.propKeys)};\n`
112
115
  }
113
116
 
114
117
  if (meta.computed) {
115
- globalInjectCode += bindThis(`global.currentInject.injectComputed = {
118
+ resultSource += bindThis(`
119
+ global.currentInject.injectComputed = {
116
120
  ${meta.computed.join(',')}
117
- };`).code + '\n'
121
+ };`).code + '\n'
118
122
  }
119
123
 
120
124
  if (meta.refs) {
121
- globalInjectCode += `global.currentInject.getRefsData = function () {
125
+ resultSource += `
126
+ global.currentInject.getRefsData = function () {
122
127
  return ${JSON.stringify(meta.refs)};
123
- };\n`
124
- }
125
-
126
- if (meta.options) {
127
- globalInjectCode += bindThis(`global.currentInject.injectOptions = ${JSON.stringify(meta.options)};`).code + '\n'
128
+ };\n`
128
129
  }
129
130
 
130
- const issuer = this._module.issuer
131
- const parser = issuer.parser
132
-
133
- // 同步issuer的dependencies,确保watch中issuer rebuild时template也进行rebuild,使该loader中往issuer中注入的依赖持续有效
134
- issuer.buildInfo.fileDependencies.forEach((dep) => {
135
- this.addDependency(dep)
136
- })
137
- issuer.buildInfo.contextDependencies.forEach((dep) => {
138
- this.addContextDependency(dep)
139
- })
140
-
141
- // 删除issuer中上次注入的dependencies,避免issuer本身不需要更新时上次的注入代码残留
142
- issuer.dependencies = issuer.dependencies.filter((dep) => {
143
- return !dep.templateInject
144
- })
145
-
146
- const dep = new InjectDependency({
147
- content: globalInjectCode,
148
- index: -2
131
+ this.emitFile(resourcePath, '', undefined, {
132
+ skipEmit: true,
133
+ extractedResultSource: resultSource
149
134
  })
150
135
 
151
- dep.templateInject = true
152
- issuer.addDependency(dep)
153
-
154
- let isSync = true
155
-
156
- const iterationOfArrayCallback = (arr, fn) => {
157
- for (let index = 0; index < arr.length; index++) {
158
- fn(arr[index])
159
- }
160
- }
161
-
162
- const dependencies = new Map()
163
-
164
- const addDependency = dep => {
165
- const resourceIdent = dep.getResourceIdentifier()
166
- if (resourceIdent) {
167
- const factory = compilation.dependencyFactories.get(dep.constructor)
168
- if (factory === undefined) {
169
- throw new Error(`No module factory available for dependency type: ${dep.constructor.name}`)
170
- }
171
- let innerMap = dependencies.get(factory)
172
- if (innerMap === undefined) {
173
- dependencies.set(factory, (innerMap = new Map()))
174
- }
175
- let list = innerMap.get(resourceIdent)
176
- if (list === undefined) innerMap.set(resourceIdent, (list = []))
177
- list.push(dep)
178
- }
179
- }
180
-
181
- for (let module in meta.wxsModuleMap) {
182
- isSync = false
183
- const src = loaderUtils.urlToRequest(meta.wxsModuleMap[module], options.root)
184
- // 编译render函数只在mpx文件中运行,此处issuer的context一定等同于当前loader的context
185
- const expression = `require(${loaderUtils.stringifyRequest(this, src)})`
186
- const deps = []
187
- parser.parse(expression, {
188
- current: {
189
- addDependency: dep => {
190
- dep.userRequest = module
191
- deps.push(dep)
192
- }
193
- },
194
- module: issuer
195
- })
196
- issuer.addVariable(module, expression, deps)
197
- iterationOfArrayCallback(deps, addDependency)
198
- }
199
-
200
- if (isSync) {
201
- return result
202
- } else {
203
- const callback = this.async()
204
-
205
- const sortedDependencies = []
206
- for (const pair1 of dependencies) {
207
- for (const pair2 of pair1[1]) {
208
- sortedDependencies.push({
209
- factory: pair1[0],
210
- dependencies: pair2[1]
211
- })
212
- }
213
- }
214
-
215
- compilation.addModuleDependencies(
216
- issuer,
217
- sortedDependencies,
218
- compilation.bail,
219
- null,
220
- true,
221
- () => {
222
- callback(null, result)
223
- }
224
- )
225
- }
136
+ return result
226
137
  }
package/lib/url-loader.js CHANGED
@@ -1,51 +1,33 @@
1
1
  const loaderUtils = require('loader-utils')
2
2
  const mime = require('mime')
3
+ const parseRequest = require('./utils/parse-request')
3
4
  const getOptions = loaderUtils.getOptions
4
- const parseQuery = loaderUtils.parseQuery
5
5
 
6
6
  function isStyleRequest (request) {
7
- let elements = request.replace(/^-?!+/, '').replace(/!!+/g, '!').split('!')
8
- elements.pop()
9
- for (let i = 0; i < elements.length; i++) {
10
- let element = elements[i]
11
- let queryString = '?'
12
- let loaderString = element
13
- let idx = element.indexOf('?')
14
- if (idx >= 0) {
15
- queryString = element.substr(idx)
16
- loaderString = element.substr(0, idx)
17
- }
18
- if (/css-loader/.test(loaderString)) {
19
- return true
20
- }
21
- if (/content-loader/.test(loaderString)) {
22
- let query = parseQuery(queryString)
23
- if (query.type === 'styles') {
24
- return true
25
- }
26
- }
27
- }
7
+ const { loaderString, queryObj } = parseRequest(request)
8
+ if (queryObj.type === 'styles') return true
9
+ if (/(css-loader|wxss\/loader)/.test(loaderString)) return true
28
10
  return false
29
11
  }
30
12
 
31
13
  module.exports = function (src) {
32
14
  let transBase64 = false
33
15
  const options = Object.assign({}, getOptions(this))
34
- const filePath = this.resourcePath
35
- const mimetype = options.mimetype || mime.getType(filePath)
36
- const issuer = this._module.issuer
16
+ const { resourcePath, queryObj } = parseRequest(this.resource)
17
+ const mimetype = options.mimetype || mime.getType(resourcePath)
18
+ const moduleGraph = this._compilation.moduleGraph
19
+ const issuer = moduleGraph.getIssuer(this._module)
37
20
  const publicPathScope = options.publicPathScope === 'all' ? 'all' : 'styleOnly'
38
- const queryOption = parseQuery(this.resourceQuery || '?')
39
21
  const limit = options.limit
40
- const useLocal = !limit || src.length < limit || queryOption.useLocal
41
- const isStyle = (issuer && issuer.request && isStyleRequest(issuer.request)) || queryOption.isStyle
22
+ const useLocal = !limit || src.length < limit || queryObj.useLocal
23
+ const isStyle = (issuer && issuer.request && isStyleRequest(issuer.request)) || queryObj.isStyle
42
24
 
43
25
  if (isStyle) {
44
26
  if (options.publicPath) {
45
27
  if (useLocal) {
46
28
  transBase64 = true
47
29
  }
48
- if (queryOption.fallback) {
30
+ if (queryObj.fallback) {
49
31
  transBase64 = false
50
32
  }
51
33
  } else {
@@ -3,7 +3,7 @@ const stringifyQuery = require('./stringify-query')
3
3
  const type = require('./type')
4
4
 
5
5
  // 默认为非强行覆盖原query,如需强行覆盖传递force为true
6
- module.exports = function (request, data = {}, force, removeKeys) {
6
+ module.exports = function addQuery (request, data = {}, force, removeKeys) {
7
7
  const { rawResourcePath: resourcePath, loaderString, queryObj: queryObjRaw } = parseRequest(request)
8
8
  const queryObj = Object.assign({}, queryObjRaw)
9
9
  if (force) {
@@ -0,0 +1,9 @@
1
+ module.exports = {
2
+ MPX_PROCESSED_FLAG: 'mpx_processed',
3
+ MPX_DISABLE_EXTRACTOR_CACHE: 'mpx_disable_extractor_cache',
4
+ DEFAULT_RESULT_SOURCE: '',
5
+ RESOLVE_IGNORED_ERR: new Error('Resolve ignored!'),
6
+ JSON_JS_EXT: '.json.js',
7
+ MPX_ROOT_VIEW: 'mpx-root-view', // 根节点类名
8
+ MPX_APP_MODULE_ID: 'mpx-app-scope' // app文件moduleId
9
+ }
@@ -0,0 +1,10 @@
1
+ const { RawSource } = require('webpack').sources
2
+
3
+ module.exports = (module, name, content, sourceMap, assetInfo) => {
4
+ if (!module.buildInfo.assets) {
5
+ module.buildInfo.assets = Object.create(null)
6
+ module.buildInfo.assetsInfo = new Map()
7
+ }
8
+ module.buildInfo.assets[name] = new RawSource(content)
9
+ module.buildInfo.assetsInfo.set(name, assetInfo)
10
+ }