@mpxjs/webpack-plugin 2.6.114-alpha.4 → 2.6.114-alpha.7

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 (104) hide show
  1. package/lib/{dependency → dependencies}/AddEntryDependency.js +0 -0
  2. package/lib/dependencies/AppEntryDependency.js +58 -0
  3. package/lib/dependencies/CommonJsAsyncDependency.js +51 -0
  4. package/lib/dependencies/CommonJsVariableDependency.js +81 -0
  5. package/lib/dependencies/DynamicEntryDependency.js +171 -0
  6. package/lib/dependencies/FlagPluginDependency.js +24 -0
  7. package/lib/dependencies/InjectDependency.js +43 -0
  8. package/lib/dependencies/RecordGlobalComponentsDependency.js +50 -0
  9. package/lib/dependencies/RecordIndependentDependency.js +44 -0
  10. package/lib/dependencies/RecordResourceMapDependency.js +62 -0
  11. package/lib/dependencies/RemoveEntryDependency.js +40 -0
  12. package/lib/{dependency → dependencies}/ReplaceDependency.js +19 -2
  13. package/lib/dependencies/ResolveDependency.js +88 -0
  14. package/lib/extractor.js +82 -178
  15. package/lib/file-loader.js +7 -19
  16. package/lib/helpers.js +39 -334
  17. package/lib/independent-loader.js +52 -0
  18. package/lib/index.js +864 -541
  19. package/lib/json-compiler/helper.js +156 -0
  20. package/lib/json-compiler/index.js +242 -451
  21. package/lib/json-compiler/plugin.js +150 -0
  22. package/lib/json-compiler/{theme-loader.js → theme.js} +5 -3
  23. package/lib/loader.js +137 -240
  24. package/lib/native-loader.js +71 -133
  25. package/lib/parser.js +1 -2
  26. package/lib/partial-compile/index.js +35 -0
  27. package/lib/platform/json/wx/index.js +1 -1
  28. package/lib/platform/template/normalize-component-rules.js +2 -3
  29. package/lib/platform/template/wx/index.js +30 -4
  30. package/lib/record-loader.js +11 -0
  31. package/lib/{path-loader.js → resolve-loader.js} +0 -0
  32. package/lib/resolver/AddEnvPlugin.js +4 -3
  33. package/lib/resolver/AddModePlugin.js +4 -3
  34. package/lib/resolver/FixDescriptionInfoPlugin.js +28 -0
  35. package/lib/resolver/PackageEntryPlugin.js +23 -36
  36. package/lib/runtime/base.styl +5 -0
  37. package/lib/runtime/components/tenon/tenon-button.vue +1 -1
  38. package/lib/runtime/components/tenon/tenon-image.vue +1 -1
  39. package/lib/runtime/components/tenon/tenon-input.vue +1 -1
  40. package/lib/runtime/components/tenon/tenon-switch.vue +1 -1
  41. package/lib/runtime/components/tenon/tenon-text-area.vue +1 -1
  42. package/lib/runtime/components/tenon/tenon-text.vue +1 -1
  43. package/lib/runtime/components/web/mpx-image.vue +20 -5
  44. package/lib/runtime/components/web/mpx-movable-view.vue +6 -2
  45. package/lib/runtime/components/web/mpx-swiper.vue +18 -3
  46. package/lib/runtime/i18n.wxs +31 -11
  47. package/lib/runtime/optionProcessor.js +48 -3
  48. package/lib/selector.js +29 -10
  49. package/lib/style-compiler/index.js +15 -23
  50. package/lib/style-compiler/load-postcss-config.js +3 -1
  51. package/lib/style-compiler/plugins/conditional-strip.js +68 -65
  52. package/lib/style-compiler/plugins/rpx.js +43 -37
  53. package/lib/style-compiler/plugins/scope-id.js +79 -72
  54. package/lib/style-compiler/plugins/trans-special.js +25 -18
  55. package/lib/style-compiler/plugins/trim.js +13 -7
  56. package/lib/style-compiler/plugins/vw.js +22 -16
  57. package/lib/template-compiler/compiler.js +95 -197
  58. package/lib/template-compiler/index.js +52 -139
  59. package/lib/template-compiler/trans-dynamic-class-expr.js +18 -13
  60. package/lib/tenon/index.js +1 -3
  61. package/lib/tenon/processScript.js +52 -40
  62. package/lib/url-loader.js +11 -29
  63. package/lib/utils/add-query.js +1 -1
  64. package/lib/utils/const.js +10 -0
  65. package/lib/utils/emit-file.js +10 -0
  66. package/lib/utils/eval-json-js.js +31 -0
  67. package/lib/utils/get-entry-name.js +13 -0
  68. package/lib/utils/get-json-content.js +42 -0
  69. package/lib/utils/is-url-request.js +10 -1
  70. package/lib/utils/match-condition.js +4 -1
  71. package/lib/utils/normalize.js +4 -15
  72. package/lib/utils/parse-request.js +3 -3
  73. package/lib/utils/resolve.js +13 -0
  74. package/lib/utils/set.js +47 -0
  75. package/lib/utils/stringify-loaders-resource.js +25 -0
  76. package/lib/utils/stringify-query.js +4 -0
  77. package/lib/web/processJSON.js +113 -144
  78. package/lib/web/processScript.js +47 -34
  79. package/lib/web/processTemplate.js +57 -40
  80. package/lib/wxml/{wxml-loader.js → loader.js} +21 -62
  81. package/lib/wxs/WxsModuleIdsPlugin.js +29 -0
  82. package/lib/wxs/WxsParserPlugin.js +2 -2
  83. package/lib/wxs/WxsPlugin.js +4 -8
  84. package/lib/wxs/WxsTemplatePlugin.js +46 -92
  85. package/lib/wxs/{wxs-i18n-loader.js → i18n-loader.js} +5 -4
  86. package/lib/wxs/loader.js +142 -0
  87. package/lib/wxs/{wxs-pre-loader.js → pre-loader.js} +20 -5
  88. package/lib/wxss/loader.js +31 -43
  89. package/lib/wxss/localsLoader.js +1 -5
  90. package/lib/wxss/processCss.js +107 -103
  91. package/package.json +18 -20
  92. package/lib/built-in-loader.js +0 -49
  93. package/lib/content-loader.js +0 -13
  94. package/lib/dependency/ChildCompileDependency.js +0 -24
  95. package/lib/dependency/InjectDependency.js +0 -26
  96. package/lib/dependency/RemovedModuleDependency.js +0 -23
  97. package/lib/dependency/ResolveDependency.js +0 -53
  98. package/lib/plugin-loader.js +0 -287
  99. package/lib/staticConfig.js +0 -4
  100. package/lib/utils/get-main-compilation.js +0 -6
  101. package/lib/utils/read-json-for-src.js +0 -34
  102. package/lib/utils/try-require.js +0 -16
  103. package/lib/wxs/wxs-loader.js +0 -117
  104. package/lib/wxss/getImportPrefix.js +0 -30
@@ -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
- const matchCondition = require('../utils/match-condition')
8
- const path = require('path')
4
+ const { matchCondition } = require('../utils/match-condition')
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 || 'm' + mpx.pathHash(resourcePath)
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,56 @@ module.exports = function (raw) {
52
51
  defs,
53
52
  decodeHTMLText,
54
53
  externalClasses,
55
- hasScoped: options.hasScoped,
56
- moduleId: options.moduleId,
57
- filePath: this.resourcePath,
54
+ hasScoped,
55
+ moduleId,
56
+ // 这里需传递resourcePath和wxsContentMap保持一致
57
+ filePath: resourcePath,
58
58
  i18n,
59
59
  checkUsingComponents: mpx.checkUsingComponents,
60
60
  globalComponents: Object.keys(mpx.usingComponents),
61
- // deprecated option
62
- globalMpxAttrsFilter: mpx.globalMpxAttrsFilter,
63
- forceProxyEvent: matchCondition(this.resourcePath, mpx.forceProxyEventRules),
64
- hasVirtualHost: matchCondition(resourcePath, mpx.autoVirtualHostRules)
61
+ forceProxyEvent: matchCondition(resourcePath, mpx.forceProxyEventRules),
62
+ hasVirtualHost: matchCondition(resourcePath, mpx.autoVirtualHostRules),
63
+ proxyComponentEventsRules: mpx.proxyComponentEventsRules
65
64
  })
66
65
 
67
- let ast = parsed.root
68
- let meta = parsed.meta
69
-
70
66
  if (meta.wxsContentMap) {
71
67
  for (let module in meta.wxsContentMap) {
72
68
  wxsContentMap[`${resourcePath}~${module}`] = meta.wxsContentMap[module]
73
69
  }
74
70
  }
75
71
 
72
+ let resultSource = ''
73
+
74
+ for (let module in meta.wxsModuleMap) {
75
+ const src = loaderUtils.urlToRequest(meta.wxsModuleMap[module], root)
76
+ resultSource += `var ${module} = require(${loaderUtils.stringifyRequest(this, src)});\n`
77
+ }
78
+
76
79
  let result = compiler.serialize(ast)
77
80
 
78
- if (options.isNative || mpx.forceDisableInject) {
81
+ if (isNative) {
79
82
  return result
80
83
  }
81
84
 
82
- const rawCode = `global.currentInject = {
83
- moduleId: ${JSON.stringify(options.moduleId)},
84
- render: function () {
85
- ${compiler.genNode(ast)}this._r();
86
- }
85
+ const rawCode = `
86
+ global.currentInject = {
87
+ moduleId: ${JSON.stringify(moduleId)},
88
+ render: function () {
89
+ ${compiler.genNode(ast)}
90
+ this._r();
91
+ }
87
92
  };\n`
88
93
 
89
- let renderResult
94
+ let bindResult
90
95
 
91
96
  try {
92
- renderResult = bindThis(rawCode, {
97
+ bindResult = bindThis(rawCode, {
93
98
  needCollect: true,
94
99
  ignoreMap: meta.wxsModuleMap
95
100
  })
96
101
  } catch (e) {
97
- error(`Invalid render function generated by the template, please check!\n
102
+ error(`
103
+ Invalid render function generated by the template, please check!\n
98
104
  Template result:
99
105
  ${result}\n
100
106
  Error code:
@@ -104,123 +110,30 @@ ${e.stack}`)
104
110
  return result
105
111
  }
106
112
 
107
- // todo 此处在loader中往其他模块addDep更加危险,考虑修改为通过抽取后的空模块的module.exports来传递信息
108
- let globalInjectCode = renderResult.code + '\n'
113
+ resultSource += bindResult.code + '\n'
109
114
 
110
- if ((mode === 'tt' || mode === 'swan') && renderResult.propKeys) {
111
- globalInjectCode += `global.currentInject.propKeys = ${JSON.stringify(renderResult.propKeys)};\n`
115
+ if ((mode === 'tt' || mode === 'swan') && bindResult.propKeys) {
116
+ resultSource += `global.currentInject.propKeys = ${JSON.stringify(bindResult.propKeys)};\n`
112
117
  }
113
118
 
114
119
  if (meta.computed) {
115
- globalInjectCode += bindThis(`global.currentInject.injectComputed = {
120
+ resultSource += bindThis(`
121
+ global.currentInject.injectComputed = {
116
122
  ${meta.computed.join(',')}
117
- };`).code + '\n'
123
+ };`).code + '\n'
118
124
  }
119
125
 
120
126
  if (meta.refs) {
121
- globalInjectCode += `global.currentInject.getRefsData = function () {
127
+ resultSource += `
128
+ global.currentInject.getRefsData = function () {
122
129
  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'
130
+ };\n`
128
131
  }
129
132
 
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
133
+ this.emitFile(resourcePath, '', undefined, {
134
+ skipEmit: true,
135
+ extractedResultSource: resultSource
149
136
  })
150
137
 
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
- }
138
+ return result
226
139
  }
@@ -1,29 +1,34 @@
1
1
  const babylon = require('@babel/parser')
2
2
  const t = require('@babel/types')
3
+ const traverse = require('@babel/traverse').default
3
4
  const generate = require('@babel/generator').default
4
5
 
5
6
  module.exports = function transDynamicClassExpr (expr, { error } = {}) {
6
7
  try {
7
- const ast = babylon.parseExpression(expr, {
8
+ const ast = babylon.parse(expr, {
8
9
  plugins: [
9
10
  'objectRestSpread'
10
11
  ]
11
12
  })
12
- if (t.isObjectExpression(ast)) {
13
- ast.properties.forEach((property) => {
14
- if (t.isObjectProperty(property) && !property.computed) {
15
- const propertyName = property.key.name || property.key.value
16
- if (/-/.test(propertyName)) {
17
- if (/\$/.test(propertyName)) {
18
- error && error(`Dynamic classname [${propertyName}] is not supported, which includes [-] char and [$] char at the same time.`)
13
+ traverse(ast, {
14
+ ObjectExpression (path) {
15
+ path.node.properties.forEach((property) => {
16
+ if (t.isObjectProperty(property) && !property.computed) {
17
+ const propertyName = property.key.name || property.key.value
18
+ if (/-/.test(propertyName)) {
19
+ if (/\$/.test(propertyName)) {
20
+ error && error(`Dynamic classname [${propertyName}] is not supported, which includes [-] char and [$] char at the same time.`)
21
+ } else {
22
+ property.key = t.identifier(propertyName.replace(/-/g, '$$') + 'MpxDash')
23
+ }
19
24
  } else {
20
- property.key = t.identifier(propertyName.replace(/-/g, '$$') + 'MpxDash')
25
+ property.key = t.identifier(propertyName)
21
26
  }
22
27
  }
23
- }
24
- })
25
- }
26
- return generate(ast, {
28
+ })
29
+ }
30
+ })
31
+ return generate(ast.program.body[0].expression, {
27
32
  compact: true
28
33
  }).code
29
34
  } catch (e) {
@@ -15,8 +15,6 @@ module.exports = function ({
15
15
  queryObj,
16
16
  autoScope,
17
17
  componentsMap,
18
- projectRoot,
19
- getRequireForSrc,
20
18
  vueContentCache,
21
19
  moduleId,
22
20
  callback
@@ -29,6 +27,7 @@ module.exports = function ({
29
27
  const defs = mpx.defs
30
28
  const resolveMode = mpx.resolveMode
31
29
  const pagesMap = mpx.pagesMap
30
+ const projectRoot = mpx.projectRoot
32
31
 
33
32
  let output = ''
34
33
  let usingComponents = [].concat(Object.keys(mpx.usingComponents))
@@ -89,7 +88,6 @@ module.exports = function ({
89
88
  srcMode,
90
89
  loaderContext,
91
90
  isProduction,
92
- getRequireForSrc,
93
91
  projectRoot,
94
92
  jsonConfig: jsonRes.jsonObj,
95
93
  componentId: queryObj.componentId || '',
@@ -2,10 +2,11 @@ const genComponentTag = require('../utils/gen-component-tag')
2
2
  const loaderUtils = require('loader-utils')
3
3
  const addQuery = require('../utils/add-query')
4
4
  const normalize = require('../utils/normalize')
5
- const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin')
6
- const AddEntryDependency = require('../dependency/AddEntryDependency')
5
+ // const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin')
6
+ // const AddEntryDependency = require('../dependency/AddEntryDependency')
7
7
  const builtInLoaderPath = normalize.lib('built-in-loader')
8
8
  const optionProcessorPath = normalize.lib('runtime/optionProcessor')
9
+ const createJSONHelper = require('../json-compiler/helper')
9
10
 
10
11
  function shallowStringify (obj) {
11
12
  let arr = []
@@ -21,10 +22,10 @@ function shallowStringify (obj) {
21
22
  return `{${arr.join(',')}}`
22
23
  }
23
24
 
24
- let entryDeps = new Set()
25
+ // let entryDeps = new Set()
25
26
 
26
- let callbacked = false
27
- let cacheCallback
27
+ // let callbacked = false
28
+ // let cacheCallback
28
29
 
29
30
  module.exports = function (script, options, callback) {
30
31
  const ctorType = options.ctorType
@@ -35,7 +36,6 @@ module.exports = function (script, options, callback) {
35
36
  const loaderContext = options.loaderContext
36
37
  const isProduction = options.isProduction
37
38
  const componentId = options.componentId
38
- const getRequireForSrc = options.getRequireForSrc
39
39
  // const i18n = options.i18n
40
40
  const jsonConfig = options.jsonConfig
41
41
  // const tabBar = jsonConfig.tabBar
@@ -46,37 +46,51 @@ module.exports = function (script, options, callback) {
46
46
  const forceDisableBuiltInLoader = options.forceDisableBuiltInLoader
47
47
 
48
48
  // add entry
49
- const checkEntryDeps = (callback) => {
50
- callback = callback || cacheCallback
51
- if (callback && entryDeps.size === 0) {
52
- callback()
53
- } else {
54
- cacheCallback = callback
55
- }
49
+ // const checkEntryDeps = (callback) => {
50
+ // callback = callback || cacheCallback
51
+ // if (callback && entryDeps.size === 0) {
52
+ // callback()
53
+ // } else {
54
+ // cacheCallback = callback
55
+ // }
56
+ // }
57
+
58
+ // const addEntryDep = (context, resource, name) => {
59
+ // // 如果loader已经回调,就不再添加entry
60
+ // if (callbacked) return
61
+ // const dep = SingleEntryPlugin.createDependency(resource, name)
62
+ // entryDeps.add(dep)
63
+ // const virtualModule = new AddEntryDependency({
64
+ // context: context._compiler.context,
65
+ // dep,
66
+ // name
67
+ // })
68
+ // /* eslint-disable camelcase */
69
+ // context._module.__has_tenon_entry = true
70
+ // context._module.addDependency(virtualModule)
71
+ // entryDeps.delete(dep)
72
+ // checkEntryDeps()
73
+ // }
74
+
75
+ const emitWarning = (msg) => {
76
+ loaderContext.emitWarning(
77
+ new Error('[tenon script processor][' + loaderContext.resource + ']: ' + msg)
78
+ )
56
79
  }
57
80
 
58
- const addEntryDep = (context, resource, name) => {
59
- // 如果loader已经回调,就不再添加entry
60
- if (callbacked) return
61
- const dep = SingleEntryPlugin.createDependency(resource, name)
62
- entryDeps.add(dep)
63
- const virtualModule = new AddEntryDependency({
64
- context: context._compiler.context,
65
- dep,
66
- name
67
- })
68
- /* eslint-disable camelcase */
69
- context._module.__has_tenon_entry = true
70
- context._module.addDependency(virtualModule)
71
- entryDeps.delete(dep)
72
- checkEntryDeps()
81
+ const emitError = (msg) => {
82
+ loaderContext.emitError(
83
+ new Error('[tenon script processor][' + loaderContext.resource + ']: ' + msg)
84
+ )
73
85
  }
74
86
 
75
- // const emitWarning = (msg) => {
76
- // loaderContext.emitWarning(
77
- // new Error('[script processor][' + loaderContext.resource + ']: ' + msg)
78
- // )
79
- // }
87
+ const {
88
+ processJsExport
89
+ } = createJSONHelper({
90
+ loaderContext,
91
+ emitWarning,
92
+ emitError
93
+ })
80
94
 
81
95
  const stringifyRequest = r => loaderUtils.stringifyRequest(loaderContext, r)
82
96
  // let tabBarPagesMap = {}
@@ -159,7 +173,8 @@ module.exports = function (script, options, callback) {
159
173
  Object.keys(localPagesMap).forEach((pagePath) => {
160
174
  const pageCfg = localPagesMap[pagePath]
161
175
  // const pageRequest = stringifyRequest(pageCfg.resource)
162
- addEntryDep(loaderContext, addQuery(pageCfg.resource, { tenon: true }), pagePath)
176
+ processJsExport(addQuery(pageCfg.resource, { tenon: true }), loaderContext.context, '', () => {})
177
+ // addEntryDep(loaderContext, addQuery(pageCfg.resource, { tenon: true }), pagePath)
163
178
  // addEntrySafely(loaderContext, addQuery(pageCfg.resource, { tenon: true }), pagePath)
164
179
  // loaderContext.resolve(loaderContext._compiler.context, addQuery(pageCfg.resource, { tenon: true }), (err, resource) => {
165
180
  // if(err) return callback(err)
@@ -199,7 +214,7 @@ module.exports = function (script, options, callback) {
199
214
  }
200
215
  // 为了正确获取currentSrcMode便于运行时进行转换,对于src引入的组件script采用require方式引入(由于webpack会将import的执行顺序上升至最顶),这意味着对于src引入脚本中的named export将不会生效,不过鉴于mpx和小程序中本身也没有在组件script中声明export的用法,所以应该没有影响
201
216
  content += script.src
202
- ? (getRequireForSrc('script', script) + '\n')
217
+ ? `require(${stringifyRequest(script.src)})\n`
203
218
  : (script.content + '\n') + '\n'
204
219
  // createApp/Page/Component执行完成后立刻获取当前的option并暂存
205
220
  content += ` const currentOption = global.currentOption\n`
@@ -251,10 +266,7 @@ module.exports = function (script, options, callback) {
251
266
  }
252
267
  })
253
268
  output += '\n'
254
- checkEntryDeps(() => {
255
- callbacked = true
256
- callback(null, {
257
- output
258
- })
269
+ callback(null, {
270
+ output
259
271
  })
260
272
  }
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,10 @@
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
+ MPX_CURRENT_CHUNK: 'mpx_current_chunk'
10
+ }
@@ -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
+ }
@@ -0,0 +1,31 @@
1
+ const path = require('path')
2
+
3
+ module.exports = function evalJSONJS (source, filename, loaderContext) {
4
+ if (!loaderContext._compiler) return {}
5
+ const fs = loaderContext._compiler.inputFileSystem
6
+ const defs = loaderContext.getMpx().defs
7
+ const defKeys = Object.keys(defs)
8
+ const defValues = defKeys.map((key) => {
9
+ return defs[key]
10
+ })
11
+ const dirname = path.dirname(filename)
12
+ // eslint-disable-next-line no-new-func
13
+ const func = new Function('module', 'exports', 'require', '__filename', '__dirname', ...defKeys, source)
14
+ const module = {
15
+ exports: {}
16
+ }
17
+ // 此处采用readFileSync+evalJSONJS而不直接使用require获取依赖内容有两个原因:
18
+ // 1. 支持依赖中正常访问defs变量
19
+ // 2. 避免对应的依赖文件被作为buildDependencies
20
+ func(module, module.exports, function (request) {
21
+ if (request.startsWith('.')) {
22
+ request = path.join(dirname, request)
23
+ }
24
+ const filename = require.resolve(request)
25
+ loaderContext.addDependency(filename)
26
+ const source = fs.readFileSync(filename).toString('utf-8')
27
+ return evalJSONJS(source, filename, loaderContext)
28
+ }, filename, dirname, ...defValues)
29
+
30
+ return module.exports
31
+ }
@@ -0,0 +1,13 @@
1
+ module.exports = function (loaderContext) {
2
+ if (!loaderContext._compilation) return ''
3
+ const moduleGraph = loaderContext._compilation.moduleGraph
4
+ let entryName = ''
5
+ for (const [name, { dependencies }] of loaderContext._compilation.entries) {
6
+ const entryModule = moduleGraph.getModule(dependencies[0])
7
+ if (entryModule.resource === loaderContext.resource) {
8
+ entryName = name
9
+ break
10
+ }
11
+ }
12
+ return entryName
13
+ }
@@ -0,0 +1,42 @@
1
+ const parseRequest = require('./parse-request')
2
+ const evalJSONJS = require('./eval-json-js')
3
+ const resolve = require('./resolve')
4
+ const async = require('async')
5
+ const { JSON_JS_EXT } = require('./const')
6
+
7
+ module.exports = function getJSONContent (json, loaderContext, callback) {
8
+ if (!loaderContext._compiler) return callback(null, '{}')
9
+ const fs = loaderContext._compiler.inputFileSystem
10
+ async.waterfall([
11
+ (callback) => {
12
+ if (json.src) {
13
+ resolve(loaderContext.context, json.src, loaderContext, (err, result) => {
14
+ if (err) return callback(err)
15
+ const { rawResourcePath: resourcePath } = parseRequest(result)
16
+ fs.readFile(resourcePath, (err, content) => {
17
+ if (err) return callback(err)
18
+ callback(null, {
19
+ content: content.toString('utf-8'),
20
+ useJSONJS: json.useJSONJS || resourcePath.endsWith(JSON_JS_EXT),
21
+ filename: resourcePath
22
+
23
+ })
24
+ })
25
+ })
26
+ } else {
27
+ callback(null, {
28
+ content: json.content,
29
+ useJSONJS: json.useJSONJS,
30
+ filename: loaderContext.resourcePath
31
+ })
32
+ }
33
+ },
34
+ ({ content, useJSONJS, filename }, callback) => {
35
+ if (!content) return callback(null, '{}')
36
+ if (useJSONJS) {
37
+ content = JSON.stringify(evalJSONJS(content, filename, loaderContext))
38
+ }
39
+ callback(null, content)
40
+ }
41
+ ], callback)
42
+ }