@mpxjs/webpack-plugin 2.6.115 → 2.7.0-alpha

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 (129) hide show
  1. package/LICENSE +433 -0
  2. package/README.md +1 -1
  3. package/lib/config.js +14 -0
  4. package/lib/dependencies/AddEntryDependency.js +24 -0
  5. package/lib/dependencies/AppEntryDependency.js +58 -0
  6. package/lib/dependencies/CommonJsAsyncDependency.js +51 -0
  7. package/lib/dependencies/CommonJsVariableDependency.js +81 -0
  8. package/lib/dependencies/DynamicEntryDependency.js +171 -0
  9. package/lib/dependencies/FlagPluginDependency.js +24 -0
  10. package/lib/dependencies/InjectDependency.js +43 -0
  11. package/lib/dependencies/RecordGlobalComponentsDependency.js +50 -0
  12. package/lib/dependencies/RecordIndependentDependency.js +44 -0
  13. package/lib/dependencies/RecordResourceMapDependency.js +62 -0
  14. package/lib/dependencies/RemoveEntryDependency.js +40 -0
  15. package/lib/{dependency → dependencies}/ReplaceDependency.js +19 -2
  16. package/lib/dependencies/ResolveDependency.js +88 -0
  17. package/lib/extractor.js +82 -178
  18. package/lib/file-loader.js +7 -19
  19. package/lib/helpers.js +39 -334
  20. package/lib/independent-loader.js +52 -0
  21. package/lib/index.js +889 -525
  22. package/lib/json-compiler/helper.js +156 -0
  23. package/lib/json-compiler/index.js +245 -451
  24. package/lib/json-compiler/plugin.js +150 -0
  25. package/lib/json-compiler/{theme-loader.js → theme.js} +5 -3
  26. package/lib/loader.js +178 -241
  27. package/lib/native-loader.js +71 -133
  28. package/lib/parser.js +1 -2
  29. package/lib/partial-compile/index.js +35 -0
  30. package/lib/platform/json/wx/index.js +1 -1
  31. package/lib/platform/template/normalize-component-rules.js +2 -3
  32. package/lib/platform/template/wx/component-config/button.js +14 -2
  33. package/lib/platform/template/wx/component-config/image.js +4 -0
  34. package/lib/platform/template/wx/component-config/input.js +4 -0
  35. package/lib/platform/template/wx/component-config/rich-text.js +4 -0
  36. package/lib/platform/template/wx/component-config/scroll-view.js +4 -0
  37. package/lib/platform/template/wx/component-config/switch.js +4 -0
  38. package/lib/platform/template/wx/component-config/text.js +4 -0
  39. package/lib/platform/template/wx/component-config/textarea.js +5 -0
  40. package/lib/platform/template/wx/component-config/view.js +4 -0
  41. package/lib/platform/template/wx/index.js +149 -3
  42. package/lib/record-loader.js +11 -0
  43. package/lib/resolve-loader.js +6 -0
  44. package/lib/resolver/AddEnvPlugin.js +4 -3
  45. package/lib/resolver/AddModePlugin.js +4 -3
  46. package/lib/resolver/FixDescriptionInfoPlugin.js +28 -0
  47. package/lib/resolver/PackageEntryPlugin.js +23 -36
  48. package/lib/runtime/base.styl +5 -0
  49. package/lib/runtime/components/tenon/getInnerListeners.js +317 -0
  50. package/lib/runtime/components/tenon/tenon-button.vue +305 -0
  51. package/lib/runtime/components/tenon/tenon-image.vue +61 -0
  52. package/lib/runtime/components/tenon/tenon-input.vue +99 -0
  53. package/lib/runtime/components/tenon/tenon-rich-text.vue +21 -0
  54. package/lib/runtime/components/tenon/tenon-scroll-view.vue +124 -0
  55. package/lib/runtime/components/tenon/tenon-switch.vue +91 -0
  56. package/lib/runtime/components/tenon/tenon-text-area.vue +64 -0
  57. package/lib/runtime/components/tenon/tenon-text.vue +64 -0
  58. package/lib/runtime/components/tenon/tenon-view.vue +93 -0
  59. package/lib/runtime/components/tenon/util.js +44 -0
  60. package/lib/runtime/components/web/getInnerListeners.js +1 -3
  61. package/lib/runtime/components/web/mpx-image.vue +20 -5
  62. package/lib/runtime/components/web/mpx-movable-view.vue +6 -2
  63. package/lib/runtime/components/web/mpx-swiper.vue +18 -3
  64. package/lib/runtime/i18n.wxs +31 -11
  65. package/lib/runtime/optionProcessor.js +48 -3
  66. package/lib/runtime/optionProcessor.tenon.js +386 -0
  67. package/lib/selector.js +29 -10
  68. package/lib/style-compiler/index.js +16 -24
  69. package/lib/style-compiler/load-postcss-config.js +3 -1
  70. package/lib/style-compiler/plugins/conditional-strip.js +68 -65
  71. package/lib/style-compiler/plugins/hm.js +20 -0
  72. package/lib/style-compiler/plugins/rpx.js +43 -37
  73. package/lib/style-compiler/plugins/scope-id.js +79 -72
  74. package/lib/style-compiler/plugins/trans-special.js +25 -18
  75. package/lib/style-compiler/plugins/trim.js +13 -7
  76. package/lib/style-compiler/plugins/vw.js +22 -16
  77. package/lib/template-compiler/compiler.js +106 -199
  78. package/lib/template-compiler/index.js +52 -139
  79. package/lib/template-compiler/trans-dynamic-class-expr.js +18 -13
  80. package/lib/tenon/index.js +105 -0
  81. package/lib/tenon/processJSON.js +356 -0
  82. package/lib/tenon/processScript.js +261 -0
  83. package/lib/tenon/processStyles.js +21 -0
  84. package/lib/tenon/processTemplate.js +133 -0
  85. package/lib/url-loader.js +11 -29
  86. package/lib/utils/add-query.js +1 -1
  87. package/lib/utils/const.js +10 -0
  88. package/lib/utils/emit-file.js +10 -0
  89. package/lib/utils/eval-json-js.js +31 -0
  90. package/lib/utils/get-entry-name.js +13 -0
  91. package/lib/utils/get-json-content.js +42 -0
  92. package/lib/utils/get-relative-path.js +25 -0
  93. package/lib/utils/is-url-request.js +10 -1
  94. package/lib/utils/match-condition.js +4 -1
  95. package/lib/utils/normalize.js +4 -15
  96. package/lib/utils/parse-request.js +3 -3
  97. package/lib/utils/resolve.js +13 -0
  98. package/lib/utils/set.js +47 -0
  99. package/lib/utils/stringify-loaders-resource.js +25 -0
  100. package/lib/utils/stringify-query.js +4 -0
  101. package/lib/web/processJSON.js +113 -144
  102. package/lib/web/processScript.js +47 -34
  103. package/lib/web/processTemplate.js +57 -40
  104. package/lib/wxml/{wxml-loader.js → loader.js} +21 -62
  105. package/lib/wxs/WxsModuleIdsPlugin.js +29 -0
  106. package/lib/wxs/WxsParserPlugin.js +2 -2
  107. package/lib/wxs/WxsPlugin.js +4 -8
  108. package/lib/wxs/WxsTemplatePlugin.js +46 -92
  109. package/lib/wxs/{wxs-i18n-loader.js → i18n-loader.js} +5 -4
  110. package/lib/wxs/loader.js +142 -0
  111. package/lib/wxs/{wxs-pre-loader.js → pre-loader.js} +20 -5
  112. package/lib/wxss/loader.js +31 -43
  113. package/lib/wxss/localsLoader.js +1 -5
  114. package/lib/wxss/processCss.js +107 -103
  115. package/package.json +21 -18
  116. package/lib/built-in-loader.js +0 -49
  117. package/lib/content-loader.js +0 -13
  118. package/lib/dependency/ChildCompileDependency.js +0 -24
  119. package/lib/dependency/InjectDependency.js +0 -26
  120. package/lib/dependency/RemovedModuleDependency.js +0 -23
  121. package/lib/dependency/ResolveDependency.js +0 -49
  122. package/lib/path-loader.js +0 -3
  123. package/lib/plugin-loader.js +0 -287
  124. package/lib/staticConfig.js +0 -4
  125. package/lib/utils/get-main-compilation.js +0 -6
  126. package/lib/utils/read-json-for-src.js +0 -34
  127. package/lib/utils/try-require.js +0 -16
  128. package/lib/wxs/wxs-loader.js +0 -117
  129. package/lib/wxss/getImportPrefix.js +0 -30
package/lib/loader.js CHANGED
@@ -2,9 +2,8 @@ const JSON5 = require('json5')
2
2
  const parseComponent = require('./parser')
3
3
  const createHelpers = require('./helpers')
4
4
  const loaderUtils = require('loader-utils')
5
- const InjectDependency = require('./dependency/InjectDependency')
6
5
  const parseRequest = require('./utils/parse-request')
7
- const matchCondition = require('./utils/match-condition')
6
+ const { matchCondition } = require('./utils/match-condition')
8
7
  const fixUsingComponent = require('./utils/fix-using-component')
9
8
  const addQuery = require('./utils/add-query')
10
9
  const async = require('async')
@@ -12,27 +11,38 @@ const processJSON = require('./web/processJSON')
12
11
  const processScript = require('./web/processScript')
13
12
  const processStyles = require('./web/processStyles')
14
13
  const processTemplate = require('./web/processTemplate')
15
- const readJsonForSrc = require('./utils/read-json-for-src')
14
+ const processForTenon = require('./tenon/index')
15
+ const getJSONContent = require('./utils/get-json-content')
16
16
  const normalize = require('./utils/normalize')
17
- const getMainCompilation = require('./utils/get-main-compilation')
18
- const { MPX_APP_MODULE_ID } = require('./staticConfig')
17
+ const getEntryName = require('./utils/get-entry-name')
18
+ const AppEntryDependency = require('./dependencies/AppEntryDependency')
19
+ const RecordResourceMapDependency = require('./dependencies/RecordResourceMapDependency')
20
+ const CommonJsVariableDependency = require('./dependencies/CommonJsVariableDependency')
21
+ const { MPX_APP_MODULE_ID } = require('./utils/const')
22
+ const path = require('path')
23
+
19
24
  module.exports = function (content) {
20
25
  this.cacheable()
21
26
 
22
- const mainCompilation = getMainCompilation(this._compilation)
23
- const mpx = mainCompilation.__mpx__
27
+ // 兼容处理处理ts-loader中watch-run/updateFile逻辑,直接跳过当前loader及后续的vue-loader返回内容
28
+ if (path.extname(this.resourcePath) === '.ts') {
29
+ this.loaderIndex -= 2
30
+ return content
31
+ }
32
+
33
+ const mpx = this.getMpx()
24
34
  if (!mpx) {
25
35
  return content
26
36
  }
27
37
  const { resourcePath, queryObj } = parseRequest(this.resource)
28
- const packageName = queryObj.packageRoot || mpx.currentPackageRoot || 'main'
38
+
39
+ const packageRoot = queryObj.packageRoot || mpx.currentPackageRoot
40
+ const packageName = packageRoot || 'main'
41
+ const independent = queryObj.independent
29
42
  const pagesMap = mpx.pagesMap
30
43
  const componentsMap = mpx.componentsMap[packageName]
31
- const resolveMode = mpx.resolveMode
32
- const projectRoot = mpx.projectRoot
33
44
  const mode = mpx.mode
34
45
  const env = mpx.env
35
- const defs = mpx.defs
36
46
  const i18n = mpx.i18n
37
47
  const globalSrcMode = mpx.srcMode
38
48
  const localSrcMode = queryObj.mode
@@ -40,24 +50,6 @@ module.exports = function (content) {
40
50
  const vueContentCache = mpx.vueContentCache
41
51
  const autoScope = matchCondition(resourcePath, mpx.autoScopeRules)
42
52
 
43
- // 支持资源query传入page或component支持页面/组件单独编译
44
- if ((queryObj.component && !componentsMap[resourcePath]) || (queryObj.page && !pagesMap[resourcePath])) {
45
- let entryChunkName
46
- const rawRequest = this._module.rawRequest
47
- const _preparedEntrypoints = this._compilation._preparedEntrypoints
48
- for (let i = 0; i < _preparedEntrypoints.length; i++) {
49
- if (rawRequest === _preparedEntrypoints[i].request) {
50
- entryChunkName = _preparedEntrypoints[i].name
51
- break
52
- }
53
- }
54
- if (queryObj.component) {
55
- componentsMap[resourcePath] = entryChunkName || 'noEntryComponent'
56
- } else {
57
- pagesMap[resourcePath] = entryChunkName || 'noEntryPage'
58
- }
59
- }
60
-
61
53
  let ctorType = 'app'
62
54
  if (pagesMap[resourcePath]) {
63
55
  // page
@@ -66,63 +58,48 @@ module.exports = function (content) {
66
58
  // component
67
59
  ctorType = 'component'
68
60
  }
61
+
62
+ // 支持资源query传入isPage或isComponent支持页面/组件单独编译
63
+ if (ctorType === 'app' && (queryObj.isComponent || queryObj.isPage)) {
64
+ const entryName = getEntryName(this) || (queryObj.isComponent ? 'noEntryComponent' : 'noEntryPage')
65
+ ctorType = queryObj.isComponent ? 'component' : 'page'
66
+ this._module.addPresentationalDependency(new RecordResourceMapDependency(resourcePath, ctorType, entryName, packageRoot))
67
+ }
68
+
69
69
  const loaderContext = this
70
70
  const stringifyRequest = r => loaderUtils.stringifyRequest(loaderContext, r)
71
71
  const isProduction = this.minimize || process.env.NODE_ENV === 'production'
72
- const options = loaderUtils.getOptions(this) || {}
73
- const processSrcQuery = (src, type) => {
74
- const localQuery = Object.assign({}, queryObj)
75
- // style src会被特殊处理为全局复用样式,不添加resourcePath,添加isStatic及issuerResource
76
- if (type === 'styles') {
77
- localQuery.isStatic = true
78
- localQuery.issuerResource = this.resource
79
- } else {
80
- localQuery.resourcePath = resourcePath
81
- }
82
- if (type === 'json') {
83
- localQuery.__component = true
84
- }
85
- return addQuery(src, localQuery)
86
- }
87
-
88
- const filePath = resourcePath
89
-
90
- let moduleId = 'm' + mpx.pathHash(filePath)
91
- if (ctorType === 'app') {
92
- moduleId = MPX_APP_MODULE_ID
93
- }
72
+ const filePath = this.resourcePath
73
+ const moduleId = ctorType === 'app' ? MPX_APP_MODULE_ID : 'm' + mpx.pathHash(filePath)
94
74
 
95
75
  const parts = parseComponent(content, {
96
76
  filePath,
97
77
  needMap: this.sourceMap,
98
78
  mode,
99
- defs,
100
79
  env
101
80
  })
102
81
 
82
+ const {
83
+ getRequire
84
+ } = createHelpers(loaderContext)
85
+
103
86
  let output = ''
104
87
  const callback = this.async()
105
88
 
106
89
  async.waterfall([
107
90
  (callback) => {
108
- const json = parts.json || {}
109
- if (json.src) {
110
- readJsonForSrc(json.src, loaderContext, (err, result) => {
111
- if (err) return callback(err)
112
- json.content = result
113
- callback()
114
- })
115
- } else {
91
+ getJSONContent(parts.json || {}, loaderContext, (err, content) => {
92
+ if (err) return callback(err)
93
+ if (parts.json) parts.json.content = content
116
94
  callback()
117
- }
95
+ })
118
96
  },
119
97
  (callback) => {
120
98
  // web输出模式下没有任何inject,可以通过cache直接返回,由于读取src json可能会新增模块依赖,需要在之后返回缓存内容
121
99
  if (vueContentCache.has(filePath)) {
122
100
  return callback(null, vueContentCache.get(filePath))
123
101
  }
124
- // 只有ali才可能需要scoped
125
- const hasScoped = (parts.styles.some(({ scoped }) => scoped) || autoScope) && mode === 'ali'
102
+ const hasScoped = parts.styles.some(({ scoped }) => scoped) || autoScope
126
103
  const templateAttrs = parts.template && parts.template.attrs
127
104
  const hasComment = templateAttrs && templateAttrs.comments
128
105
  const isNative = false
@@ -146,33 +123,54 @@ module.exports = function (content) {
146
123
  }
147
124
  }
148
125
 
149
- const {
150
- getRequire,
151
- getRequireForSrc,
152
- getRequestString,
153
- getSrcRequestString
154
- } = createHelpers({
155
- loaderContext,
156
- options,
157
- moduleId,
158
- hasScoped,
159
- hasComment,
160
- usingComponents,
161
- srcMode,
162
- isNative,
163
- projectRoot
164
- })
126
+ if (mode === 'tenon') {
127
+ if (ctorType === 'app' && !queryObj.app) {
128
+ const request = addQuery(this.resource, { app: true })
129
+ output += `
130
+ import App from ${stringifyRequest(request)}
131
+ import * as Tenon from '@hummer/tenon-vue'
132
+
133
+ Tenon.render(App)\n`
134
+ // 直接结束loader进入parse
135
+ this.loaderIndex = -1
136
+ return callback(null, output)
137
+ }
138
+ if (ctorType === 'page' && queryObj.tenon) {
139
+ console.log(resourcePath)
140
+ const request = addQuery(resourcePath, { page: true })
141
+ output += `
142
+ import page from ${stringifyRequest(request)}
143
+ import * as Tenon from '@hummer/tenon-vue'
144
+
145
+ Tenon.render(page)\n`
146
+ this.loaderIndex = -1
147
+ return callback(null, output)
148
+ }
149
+ return processForTenon({
150
+ mpx,
151
+ loaderContext,
152
+ isProduction,
153
+ queryObj,
154
+ filePath,
155
+ parts,
156
+ ctorType,
157
+ autoScope,
158
+ componentsMap,
159
+ vueContentCache,
160
+ moduleId,
161
+ callback
162
+ })
163
+ }
165
164
 
166
165
  // 处理mode为web时输出vue格式文件
167
166
  if (mode === 'web') {
168
- if (ctorType === 'app' && !queryObj.app) {
169
- const request = addQuery(this.resource, { app: true })
170
- const el = mpx.webConfig.el || '#app'
167
+ if (ctorType === 'app' && !queryObj.isApp) {
168
+ const request = addQuery(this.resource, { isApp: true })
171
169
  output += `
172
170
  import App from ${stringifyRequest(request)}
173
171
  import Vue from 'vue'
174
172
  new Vue({
175
- el: '${el}',
173
+ el: '#app',
176
174
  render: function(h){
177
175
  return h(App)
178
176
  }
@@ -188,19 +186,15 @@ module.exports = function (content) {
188
186
  async.parallel([
189
187
  (callback) => {
190
188
  processTemplate(parts.template, {
189
+ loaderContext,
190
+ hasScoped,
191
191
  hasComment,
192
192
  isNative,
193
- mode,
194
193
  srcMode,
195
- defs,
196
- loaderContext,
197
194
  moduleId,
198
195
  ctorType,
199
196
  usingComponents,
200
- componentGenerics,
201
- decodeHTMLText: mpx.decodeHTMLText,
202
- externalClasses: mpx.externalClasses,
203
- checkUsingComponents: mpx.checkUsingComponents
197
+ componentGenerics
204
198
  }, callback)
205
199
  },
206
200
  (callback) => {
@@ -212,16 +206,9 @@ module.exports = function (content) {
212
206
  },
213
207
  (callback) => {
214
208
  processJSON(parts.json, {
215
- mode,
216
- env,
217
- defs,
218
- resolveMode,
219
209
  loaderContext,
220
210
  pagesMap,
221
- pagesEntryMap: mpx.pagesEntryMap,
222
- pathHash: mpx.pathHash,
223
- componentsMap,
224
- projectRoot
211
+ componentsMap
225
212
  }, callback)
226
213
  }
227
214
  ], (err, res) => {
@@ -237,24 +224,20 @@ module.exports = function (content) {
237
224
  }
238
225
 
239
226
  processScript(parts.script, {
227
+ loaderContext,
240
228
  ctorType,
241
229
  srcMode,
242
- loaderContext,
243
230
  isProduction,
244
- getRequireForSrc,
245
- i18n,
246
231
  componentGenerics,
247
- projectRoot,
248
232
  jsonConfig: jsonRes.jsonObj,
249
- componentId: queryObj.componentId || '',
233
+ outputPath: queryObj.outputPath || '',
250
234
  tabBarMap: jsonRes.tabBarMap,
251
235
  tabBarStr: jsonRes.tabBarStr,
252
236
  builtInComponentsMap: templateRes.builtInComponentsMap,
253
237
  genericsInfo: templateRes.genericsInfo,
254
238
  wxsModuleMap: templateRes.wxsModuleMap,
255
239
  localComponentsMap: jsonRes.localComponentsMap,
256
- localPagesMap: jsonRes.localPagesMap,
257
- forceDisableBuiltInLoader: mpx.forceDisableBuiltInLoader
240
+ localPagesMap: jsonRes.localPagesMap
258
241
  }, callback)
259
242
  }
260
243
  ], (err, scriptRes) => {
@@ -265,44 +248,48 @@ module.exports = function (content) {
265
248
  })
266
249
  }
267
250
 
268
- // 触发webpack global var 注入
269
- output += 'global.currentModuleId\n'
251
+ const moduleGraph = this._compilation.moduleGraph
252
+
253
+ const issuer = moduleGraph.getIssuer(this._module)
254
+
255
+ if (issuer) {
256
+ return callback(new Error(`Current ${ctorType} [${this.resourcePath}] is issued by [${issuer.resource}], which is not allowed!`))
257
+ }
258
+
259
+ if (ctorType === 'app') {
260
+ const appName = getEntryName(this)
261
+ this._module.addPresentationalDependency(new AppEntryDependency(resourcePath, appName))
262
+ }
270
263
 
271
- // todo loader中inject dep比较危险,watch模式下不一定靠谱,可考虑将import改为require然后通过修改loader内容注入
272
264
  // 注入模块id及资源路径
273
- let globalInjectCode = `global.currentModuleId = ${JSON.stringify(moduleId)}\n`
265
+ output += `global.currentModuleId = ${JSON.stringify(moduleId)}\n`
274
266
  if (!isProduction) {
275
- globalInjectCode += `global.currentResource = ${JSON.stringify(filePath)}\n`
267
+ output += `global.currentResource = ${JSON.stringify(filePath)}\n`
276
268
  }
277
269
 
278
- if (i18n && (ctorType === 'app' || (ctorType === 'page' && queryObj.isIndependent)) && !mpx.forceDisableInject) {
279
- const i18nMethodsVar = 'i18nMethods'
270
+ // 为app注入i18n
271
+ if (i18n && ctorType === 'app') {
280
272
  const i18nWxsPath = normalize.lib('runtime/i18n.wxs')
281
- const i18nWxsLoaderPath = normalize.lib('wxs/wxs-i18n-loader.js')
273
+ const i18nWxsLoaderPath = normalize.lib('wxs/i18n-loader.js')
282
274
  const i18nWxsRequest = i18nWxsLoaderPath + '!' + i18nWxsPath
283
- const expression = `require(${loaderUtils.stringifyRequest(loaderContext, i18nWxsRequest)})`
284
- const deps = []
285
- this._module.parser.parse(expression, {
286
- current: {
287
- addDependency: dep => {
288
- dep.userRequest = i18nMethodsVar
289
- deps.push(dep)
290
- }
291
- },
292
- module: this._module
293
- })
294
- this._module.addVariable(i18nMethodsVar, expression, deps)
275
+ this._module.addDependency(new CommonJsVariableDependency(i18nWxsRequest))
276
+ // 避免该模块被concatenate导致注入的i18n没有最先执行
277
+ this._module.buildInfo.moduleConcatenationBailout = 'i18n'
278
+ }
295
279
 
296
- globalInjectCode += `if (!global.i18n) {
297
- global.i18n = ${JSON.stringify({ locale: i18n.locale, version: 0 })}
298
- global.i18nMethods = ${i18nMethodsVar}
299
- }\n`
280
+ // 为独立分包注入init module
281
+ if (independent && typeof independent === 'string') {
282
+ const independentLoader = normalize.lib('independent-loader.js')
283
+ const independentInitRequest = `!!${independentLoader}!${independent}`
284
+ this._module.addDependency(new CommonJsVariableDependency(independentInitRequest))
285
+ // 避免该模块被concatenate导致注入的independent init没有最先执行
286
+ this._module.buildInfo.moduleConcatenationBailout = 'independent init'
300
287
  }
288
+
301
289
  // 注入构造函数
302
290
  let ctor = 'App'
303
291
  if (ctorType === 'page') {
304
- // swan也默认使用Page构造器
305
- if (mpx.forceUsePageCtor || mode === 'ali' || mode === 'swan') {
292
+ if (mpx.forceUsePageCtor || mode === 'ali') {
306
293
  ctor = 'Page'
307
294
  } else {
308
295
  ctor = 'Component'
@@ -310,137 +297,87 @@ module.exports = function (content) {
310
297
  } else if (ctorType === 'component') {
311
298
  ctor = 'Component'
312
299
  }
313
- globalInjectCode += `global.currentCtor = ${ctor}\n`
314
- globalInjectCode += `global.currentResourceType = '${ctorType}'\n`
315
- globalInjectCode += `global.currentCtorType = ${JSON.stringify(ctor.replace(/^./, (match) => {
300
+ output += `global.currentCtor = ${ctor}\n`
301
+ output += `global.currentCtorType = ${JSON.stringify(ctor.replace(/^./, (match) => {
316
302
  return match.toLowerCase()
317
303
  }))}\n`
304
+ output += `global.currentResourceType = ${JSON.stringify(ctorType)}\n`
318
305
 
319
- // <script>
320
- output += '/* script */\n'
321
- let scriptSrcMode = srcMode
322
- const script = parts.script
323
- if (script) {
324
- scriptSrcMode = script.mode || scriptSrcMode
325
- let scriptRequestString
326
- if (script.src) {
327
- // 传入resourcePath以确保后续处理中能够识别src引入的资源为组件主资源
328
- script.src = processSrcQuery(script.src, 'script')
329
- scriptRequestString = getSrcRequestString('script', script)
330
- } else {
331
- scriptRequestString = getRequestString('script', script)
332
- }
333
- if (scriptRequestString) {
334
- output += 'export * from ' + scriptRequestString + '\n\n'
335
- if (ctorType === 'app') {
336
- mpx.appScriptRawRequest = JSON.parse(scriptRequestString)
337
- mpx.appScriptPromise = new Promise((resolve) => {
338
- mpx.appScriptPromiseResolve = resolve
339
- })
340
- }
341
- }
342
- } else {
343
- switch (ctorType) {
344
- case 'app':
345
- output += 'import {createApp} from "@mpxjs/core"\n' +
346
- 'createApp({})\n'
347
- break
348
- case 'page':
349
- output += 'import {createPage} from "@mpxjs/core"\n' +
350
- 'createPage({})\n'
351
- break
352
- case 'component':
353
- output += 'import {createComponent} from "@mpxjs/core"\n' +
354
- 'createComponent({})\n'
355
- }
356
- output += '\n'
357
- }
306
+ // template
307
+ output += '/* template */\n'
308
+ const template = parts.template
358
309
 
359
- if (scriptSrcMode) {
360
- globalInjectCode += `global.currentSrcMode = ${JSON.stringify(scriptSrcMode)}\n`
310
+ if (template) {
311
+ const extraOptions = {
312
+ ...template.src ? {
313
+ ...queryObj,
314
+ resourcePath
315
+ } : null,
316
+ hasScoped,
317
+ hasComment,
318
+ isNative,
319
+ moduleId,
320
+ usingComponents
321
+ // 添加babel处理渲染函数中可能包含的...展开运算符
322
+ // 由于...运算符应用范围极小以及babel成本极高,先关闭此特性后续看情况打开
323
+ // needBabel: true
324
+ }
325
+ if (template.src) extraOptions.resourcePath = resourcePath
326
+ // 基于global.currentInject来注入模板渲染函数和refs等信息
327
+ output += getRequire('template', template, extraOptions) + '\n'
361
328
  }
362
329
 
363
330
  // styles
364
331
  output += '/* styles */\n'
365
- let cssModules
366
332
  if (parts.styles.length) {
367
- let styleInjectionCode = ''
368
333
  parts.styles.forEach((style, i) => {
369
- let scoped = hasScoped ? (style.scoped || autoScope) : false
370
- let requireString
371
- // require style
372
- if (style.src) {
373
- style.src = processSrcQuery(style.src, 'styles')
374
- requireString = getRequireForSrc('styles', style, -1, scoped)
375
- } else {
376
- requireString = getRequire('styles', style, i, scoped)
377
- }
378
- const hasStyleLoader = requireString.indexOf('style-loader') > -1
379
- const invokeStyle = code => `${code}\n`
380
-
381
- const moduleName = style.module === true ? '$style' : style.module
382
- // setCssModule
383
- if (moduleName) {
384
- if (!cssModules) {
385
- cssModules = {}
386
- }
387
- if (moduleName in cssModules) {
388
- loaderContext.emitError(
389
- 'CSS module name "' + moduleName + '" is not unique!'
390
- )
391
- styleInjectionCode += invokeStyle(requireString)
392
- } else {
393
- cssModules[moduleName] = true
394
-
395
- if (!hasStyleLoader) {
396
- requireString += '.locals'
397
- }
398
-
399
- styleInjectionCode += invokeStyle(
400
- 'this["' + moduleName + '"] = ' + requireString
401
- )
402
- }
403
- } else {
404
- styleInjectionCode += invokeStyle(requireString)
334
+ const scoped = style.scoped || autoScope
335
+ const extraOptions = {
336
+ // style src会被特殊处理为全局复用样式,不添加resourcePath,添加isStatic及issuerFile
337
+ ...style.src ? {
338
+ ...queryObj,
339
+ isStatic: true,
340
+ issuerResource: addQuery(this.resource, { type: 'styles' }, true)
341
+ } : null,
342
+ moduleId,
343
+ scoped
405
344
  }
345
+ // require style
346
+ output += getRequire('styles', style, extraOptions, i) + '\n'
406
347
  })
407
- output += styleInjectionCode + '\n'
408
- } else if (ctorType === 'app' && mode === 'ali') {
409
- output += getRequire('styles', {}) + '\n'
348
+ }
349
+
350
+ if (parts.styles.filter(style => !style.src).length === 0 && ctorType === 'app' && mode === 'ali') {
351
+ output += getRequire('styles', {}, {}, parts.styles.length) + '\n'
410
352
  }
411
353
 
412
354
  // json
413
355
  output += '/* json */\n'
414
356
  // 给予json默认值, 确保生成json request以自动补全json
415
357
  const json = parts.json || {}
416
- if (json.src) {
417
- json.src = processSrcQuery(json.src, 'json')
418
- output += getRequireForSrc('json', json) + '\n\n'
419
- } else {
420
- output += getRequire('json', json) + '\n\n'
421
- }
422
-
423
- // template
424
- output += '/* template */\n'
425
- const template = parts.template
358
+ output += getRequire('json', json, json.src && {
359
+ ...queryObj,
360
+ resourcePath
361
+ }) + '\n'
426
362
 
427
- if (template) {
428
- if (template.src) {
429
- template.src = processSrcQuery(template.src, 'template')
430
- output += getRequireForSrc('template', template) + '\n\n'
431
- } else {
432
- output += getRequire('template', template) + '\n\n'
363
+ // script
364
+ output += '/* script */\n'
365
+ let scriptSrcMode = srcMode
366
+ // 给予script默认值, 确保生成js request以自动补全js
367
+ const script = parts.script || {}
368
+ if (script) {
369
+ scriptSrcMode = script.mode || scriptSrcMode
370
+ if (scriptSrcMode) output += `global.currentSrcMode = ${JSON.stringify(scriptSrcMode)}\n`
371
+ // 传递ctorType以补全js内容
372
+ const extraOptions = {
373
+ ...script.src ? {
374
+ ...queryObj,
375
+ resourcePath
376
+ } : null,
377
+ ctorType
433
378
  }
379
+ output += getRequire('script', script, extraOptions) + '\n'
434
380
  }
435
-
436
- if (!mpx.forceDisableInject) {
437
- const dep = new InjectDependency({
438
- content: globalInjectCode,
439
- index: -3
440
- })
441
- this._module.addDependency(dep)
442
- }
443
-
444
381
  callback(null, output)
445
382
  }
446
383
  ], callback)