@mpxjs/webpack-plugin 2.6.114-alpha.6 → 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 (98) 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 +854 -542
  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/web/mpx-image.vue +20 -5
  38. package/lib/runtime/components/web/mpx-movable-view.vue +6 -2
  39. package/lib/runtime/components/web/mpx-swiper.vue +18 -3
  40. package/lib/runtime/i18n.wxs +31 -11
  41. package/lib/runtime/optionProcessor.js +48 -3
  42. package/lib/selector.js +29 -10
  43. package/lib/style-compiler/index.js +15 -23
  44. package/lib/style-compiler/load-postcss-config.js +3 -1
  45. package/lib/style-compiler/plugins/conditional-strip.js +68 -65
  46. package/lib/style-compiler/plugins/rpx.js +43 -37
  47. package/lib/style-compiler/plugins/scope-id.js +79 -72
  48. package/lib/style-compiler/plugins/trans-special.js +25 -18
  49. package/lib/style-compiler/plugins/trim.js +13 -7
  50. package/lib/style-compiler/plugins/vw.js +22 -16
  51. package/lib/template-compiler/compiler.js +95 -197
  52. package/lib/template-compiler/index.js +52 -139
  53. package/lib/template-compiler/trans-dynamic-class-expr.js +18 -13
  54. package/lib/tenon/index.js +1 -3
  55. package/lib/tenon/processScript.js +52 -40
  56. package/lib/url-loader.js +11 -29
  57. package/lib/utils/add-query.js +1 -1
  58. package/lib/utils/const.js +10 -0
  59. package/lib/utils/emit-file.js +10 -0
  60. package/lib/utils/eval-json-js.js +31 -0
  61. package/lib/utils/get-entry-name.js +13 -0
  62. package/lib/utils/get-json-content.js +42 -0
  63. package/lib/utils/is-url-request.js +10 -1
  64. package/lib/utils/match-condition.js +4 -1
  65. package/lib/utils/normalize.js +4 -15
  66. package/lib/utils/parse-request.js +3 -3
  67. package/lib/utils/resolve.js +13 -0
  68. package/lib/utils/set.js +47 -0
  69. package/lib/utils/stringify-loaders-resource.js +25 -0
  70. package/lib/utils/stringify-query.js +4 -0
  71. package/lib/web/processJSON.js +113 -144
  72. package/lib/web/processScript.js +47 -34
  73. package/lib/web/processTemplate.js +57 -40
  74. package/lib/wxml/{wxml-loader.js → loader.js} +21 -62
  75. package/lib/wxs/WxsModuleIdsPlugin.js +29 -0
  76. package/lib/wxs/WxsParserPlugin.js +2 -2
  77. package/lib/wxs/WxsPlugin.js +4 -8
  78. package/lib/wxs/WxsTemplatePlugin.js +46 -92
  79. package/lib/wxs/{wxs-i18n-loader.js → i18n-loader.js} +5 -4
  80. package/lib/wxs/loader.js +142 -0
  81. package/lib/wxs/{wxs-pre-loader.js → pre-loader.js} +20 -5
  82. package/lib/wxss/loader.js +31 -43
  83. package/lib/wxss/localsLoader.js +1 -5
  84. package/lib/wxss/processCss.js +107 -103
  85. package/package.json +18 -20
  86. package/lib/built-in-loader.js +0 -49
  87. package/lib/content-loader.js +0 -13
  88. package/lib/dependency/ChildCompileDependency.js +0 -24
  89. package/lib/dependency/InjectDependency.js +0 -26
  90. package/lib/dependency/RemovedModuleDependency.js +0 -23
  91. package/lib/dependency/ResolveDependency.js +0 -53
  92. package/lib/plugin-loader.js +0 -287
  93. package/lib/staticConfig.js +0 -4
  94. package/lib/utils/get-main-compilation.js +0 -6
  95. package/lib/utils/read-json-for-src.js +0 -34
  96. package/lib/utils/try-require.js +0 -16
  97. package/lib/wxs/wxs-loader.js +0 -117
  98. 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')
@@ -13,27 +12,37 @@ const processScript = require('./web/processScript')
13
12
  const processStyles = require('./web/processStyles')
14
13
  const processTemplate = require('./web/processTemplate')
15
14
  const processForTenon = require('./tenon/index')
16
- const readJsonForSrc = require('./utils/read-json-for-src')
15
+ const getJSONContent = require('./utils/get-json-content')
17
16
  const normalize = require('./utils/normalize')
18
- const getMainCompilation = require('./utils/get-main-compilation')
19
- 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
+
20
24
  module.exports = function (content) {
21
25
  this.cacheable()
22
26
 
23
- const mainCompilation = getMainCompilation(this._compilation)
24
- 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()
25
34
  if (!mpx) {
26
35
  return content
27
36
  }
28
37
  const { resourcePath, queryObj } = parseRequest(this.resource)
29
- 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
30
42
  const pagesMap = mpx.pagesMap
31
43
  const componentsMap = mpx.componentsMap[packageName]
32
- const resolveMode = mpx.resolveMode
33
- const projectRoot = mpx.projectRoot
34
44
  const mode = mpx.mode
35
45
  const env = mpx.env
36
- const defs = mpx.defs
37
46
  const i18n = mpx.i18n
38
47
  const globalSrcMode = mpx.srcMode
39
48
  const localSrcMode = queryObj.mode
@@ -41,24 +50,6 @@ module.exports = function (content) {
41
50
  const vueContentCache = mpx.vueContentCache
42
51
  const autoScope = matchCondition(resourcePath, mpx.autoScopeRules)
43
52
 
44
- // 支持资源query传入page或component支持页面/组件单独编译
45
- if ((queryObj.component && !componentsMap[resourcePath]) || (queryObj.page && !pagesMap[resourcePath])) {
46
- let entryChunkName
47
- const rawRequest = this._module.rawRequest
48
- const _preparedEntrypoints = this._compilation._preparedEntrypoints
49
- for (let i = 0; i < _preparedEntrypoints.length; i++) {
50
- if (rawRequest === _preparedEntrypoints[i].request) {
51
- entryChunkName = _preparedEntrypoints[i].name
52
- break
53
- }
54
- }
55
- if (queryObj.component) {
56
- componentsMap[resourcePath] = entryChunkName || 'noEntryComponent'
57
- } else {
58
- pagesMap[resourcePath] = entryChunkName || 'noEntryPage'
59
- }
60
- }
61
-
62
53
  let ctorType = 'app'
63
54
  if (pagesMap[resourcePath]) {
64
55
  // page
@@ -67,63 +58,48 @@ module.exports = function (content) {
67
58
  // component
68
59
  ctorType = 'component'
69
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
+
70
69
  const loaderContext = this
71
70
  const stringifyRequest = r => loaderUtils.stringifyRequest(loaderContext, r)
72
71
  const isProduction = this.minimize || process.env.NODE_ENV === 'production'
73
- const options = loaderUtils.getOptions(this) || {}
74
- const processSrcQuery = (src, type) => {
75
- const localQuery = Object.assign({}, queryObj)
76
- // style src会被特殊处理为全局复用样式,不添加resourcePath,添加isStatic及issuerResource
77
- if (type === 'styles') {
78
- localQuery.isStatic = true
79
- localQuery.issuerResource = this.resource
80
- } else {
81
- localQuery.resourcePath = resourcePath
82
- }
83
- if (type === 'json') {
84
- localQuery.__component = true
85
- }
86
- return addQuery(src, localQuery)
87
- }
88
-
89
- const filePath = resourcePath
90
-
91
- let moduleId = 'm' + mpx.pathHash(filePath)
92
- if (ctorType === 'app') {
93
- moduleId = MPX_APP_MODULE_ID
94
- }
72
+ const filePath = this.resourcePath
73
+ const moduleId = ctorType === 'app' ? MPX_APP_MODULE_ID : 'm' + mpx.pathHash(filePath)
95
74
 
96
75
  const parts = parseComponent(content, {
97
76
  filePath,
98
77
  needMap: this.sourceMap,
99
78
  mode,
100
- defs,
101
79
  env
102
80
  })
103
81
 
82
+ const {
83
+ getRequire
84
+ } = createHelpers(loaderContext)
85
+
104
86
  let output = ''
105
87
  const callback = this.async()
106
88
 
107
89
  async.waterfall([
108
90
  (callback) => {
109
- const json = parts.json || {}
110
- if (json.src) {
111
- readJsonForSrc(json.src, loaderContext, (err, result) => {
112
- if (err) return callback(err)
113
- json.content = result
114
- callback()
115
- })
116
- } else {
91
+ getJSONContent(parts.json || {}, loaderContext, (err, content) => {
92
+ if (err) return callback(err)
93
+ if (parts.json) parts.json.content = content
117
94
  callback()
118
- }
95
+ })
119
96
  },
120
97
  (callback) => {
121
98
  // web输出模式下没有任何inject,可以通过cache直接返回,由于读取src json可能会新增模块依赖,需要在之后返回缓存内容
122
99
  if (vueContentCache.has(filePath)) {
123
100
  return callback(null, vueContentCache.get(filePath))
124
101
  }
125
- // 只有ali才可能需要scoped
126
- const hasScoped = (parts.styles.some(({ scoped }) => scoped) || autoScope) && mode === 'ali'
102
+ const hasScoped = parts.styles.some(({ scoped }) => scoped) || autoScope
127
103
  const templateAttrs = parts.template && parts.template.attrs
128
104
  const hasComment = templateAttrs && templateAttrs.comments
129
105
  const isNative = false
@@ -147,23 +123,6 @@ module.exports = function (content) {
147
123
  }
148
124
  }
149
125
 
150
- const {
151
- getRequire,
152
- getRequireForSrc,
153
- getRequestString,
154
- getSrcRequestString
155
- } = createHelpers({
156
- loaderContext,
157
- options,
158
- moduleId,
159
- hasScoped,
160
- hasComment,
161
- usingComponents,
162
- srcMode,
163
- isNative,
164
- projectRoot
165
- })
166
-
167
126
  if (mode === 'tenon') {
168
127
  if (ctorType === 'app' && !queryObj.app) {
169
128
  const request = addQuery(this.resource, { app: true })
@@ -197,8 +156,6 @@ module.exports = function (content) {
197
156
  ctorType,
198
157
  autoScope,
199
158
  componentsMap,
200
- projectRoot,
201
- getRequireForSrc,
202
159
  vueContentCache,
203
160
  moduleId,
204
161
  callback
@@ -207,8 +164,8 @@ module.exports = function (content) {
207
164
 
208
165
  // 处理mode为web时输出vue格式文件
209
166
  if (mode === 'web') {
210
- if (ctorType === 'app' && !queryObj.app) {
211
- const request = addQuery(this.resource, { app: true })
167
+ if (ctorType === 'app' && !queryObj.isApp) {
168
+ const request = addQuery(this.resource, { isApp: true })
212
169
  output += `
213
170
  import App from ${stringifyRequest(request)}
214
171
  import Vue from 'vue'
@@ -229,19 +186,15 @@ module.exports = function (content) {
229
186
  async.parallel([
230
187
  (callback) => {
231
188
  processTemplate(parts.template, {
189
+ loaderContext,
190
+ hasScoped,
232
191
  hasComment,
233
192
  isNative,
234
- mode,
235
193
  srcMode,
236
- defs,
237
- loaderContext,
238
194
  moduleId,
239
195
  ctorType,
240
196
  usingComponents,
241
- componentGenerics,
242
- decodeHTMLText: mpx.decodeHTMLText,
243
- externalClasses: mpx.externalClasses,
244
- checkUsingComponents: mpx.checkUsingComponents
197
+ componentGenerics
245
198
  }, callback)
246
199
  },
247
200
  (callback) => {
@@ -253,16 +206,9 @@ module.exports = function (content) {
253
206
  },
254
207
  (callback) => {
255
208
  processJSON(parts.json, {
256
- mode,
257
- env,
258
- defs,
259
- resolveMode,
260
209
  loaderContext,
261
210
  pagesMap,
262
- pagesEntryMap: mpx.pagesEntryMap,
263
- pathHash: mpx.pathHash,
264
- componentsMap,
265
- projectRoot
211
+ componentsMap
266
212
  }, callback)
267
213
  }
268
214
  ], (err, res) => {
@@ -278,24 +224,20 @@ module.exports = function (content) {
278
224
  }
279
225
 
280
226
  processScript(parts.script, {
227
+ loaderContext,
281
228
  ctorType,
282
229
  srcMode,
283
- loaderContext,
284
230
  isProduction,
285
- getRequireForSrc,
286
- i18n,
287
231
  componentGenerics,
288
- projectRoot,
289
232
  jsonConfig: jsonRes.jsonObj,
290
- componentId: queryObj.componentId || '',
233
+ outputPath: queryObj.outputPath || '',
291
234
  tabBarMap: jsonRes.tabBarMap,
292
235
  tabBarStr: jsonRes.tabBarStr,
293
236
  builtInComponentsMap: templateRes.builtInComponentsMap,
294
237
  genericsInfo: templateRes.genericsInfo,
295
238
  wxsModuleMap: templateRes.wxsModuleMap,
296
239
  localComponentsMap: jsonRes.localComponentsMap,
297
- localPagesMap: jsonRes.localPagesMap,
298
- forceDisableBuiltInLoader: mpx.forceDisableBuiltInLoader
240
+ localPagesMap: jsonRes.localPagesMap
299
241
  }, callback)
300
242
  }
301
243
  ], (err, scriptRes) => {
@@ -306,39 +248,44 @@ module.exports = function (content) {
306
248
  })
307
249
  }
308
250
 
309
- // 触发webpack global var 注入
310
- 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
+ }
311
263
 
312
- // todo loader中inject dep比较危险,watch模式下不一定靠谱,可考虑将import改为require然后通过修改loader内容注入
313
264
  // 注入模块id及资源路径
314
- let globalInjectCode = `global.currentModuleId = ${JSON.stringify(moduleId)}\n`
265
+ output += `global.currentModuleId = ${JSON.stringify(moduleId)}\n`
315
266
  if (!isProduction) {
316
- globalInjectCode += `global.currentResource = ${JSON.stringify(filePath)}\n`
267
+ output += `global.currentResource = ${JSON.stringify(filePath)}\n`
317
268
  }
318
269
 
319
- if (i18n && (ctorType === 'app' || (ctorType === 'page' && queryObj.isIndependent)) && !mpx.forceDisableInject) {
320
- const i18nMethodsVar = 'i18nMethods'
270
+ // 为app注入i18n
271
+ if (i18n && ctorType === 'app') {
321
272
  const i18nWxsPath = normalize.lib('runtime/i18n.wxs')
322
- const i18nWxsLoaderPath = normalize.lib('wxs/wxs-i18n-loader.js')
273
+ const i18nWxsLoaderPath = normalize.lib('wxs/i18n-loader.js')
323
274
  const i18nWxsRequest = i18nWxsLoaderPath + '!' + i18nWxsPath
324
- const expression = `require(${loaderUtils.stringifyRequest(loaderContext, i18nWxsRequest)})`
325
- const deps = []
326
- this._module.parser.parse(expression, {
327
- current: {
328
- addDependency: dep => {
329
- dep.userRequest = i18nMethodsVar
330
- deps.push(dep)
331
- }
332
- },
333
- module: this._module
334
- })
335
- this._module.addVariable(i18nMethodsVar, expression, deps)
275
+ this._module.addDependency(new CommonJsVariableDependency(i18nWxsRequest))
276
+ // 避免该模块被concatenate导致注入的i18n没有最先执行
277
+ this._module.buildInfo.moduleConcatenationBailout = 'i18n'
278
+ }
336
279
 
337
- globalInjectCode += `if (!global.i18n) {
338
- global.i18n = ${JSON.stringify({ locale: i18n.locale, version: 0 })}
339
- global.i18nMethods = ${i18nMethodsVar}
340
- }\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'
341
287
  }
288
+
342
289
  // 注入构造函数
343
290
  let ctor = 'App'
344
291
  if (ctorType === 'page') {
@@ -350,137 +297,87 @@ module.exports = function (content) {
350
297
  } else if (ctorType === 'component') {
351
298
  ctor = 'Component'
352
299
  }
353
- globalInjectCode += `global.currentCtor = ${ctor}\n`
354
- globalInjectCode += `global.currentResourceType = '${ctorType}'\n`
355
- globalInjectCode += `global.currentCtorType = ${JSON.stringify(ctor.replace(/^./, (match) => {
300
+ output += `global.currentCtor = ${ctor}\n`
301
+ output += `global.currentCtorType = ${JSON.stringify(ctor.replace(/^./, (match) => {
356
302
  return match.toLowerCase()
357
303
  }))}\n`
304
+ output += `global.currentResourceType = ${JSON.stringify(ctorType)}\n`
358
305
 
359
- // <script>
360
- output += '/* script */\n'
361
- let scriptSrcMode = srcMode
362
- const script = parts.script
363
- if (script) {
364
- scriptSrcMode = script.mode || scriptSrcMode
365
- let scriptRequestString
366
- if (script.src) {
367
- // 传入resourcePath以确保后续处理中能够识别src引入的资源为组件主资源
368
- script.src = processSrcQuery(script.src, 'script')
369
- scriptRequestString = getSrcRequestString('script', script)
370
- } else {
371
- scriptRequestString = getRequestString('script', script)
372
- }
373
- if (scriptRequestString) {
374
- output += 'export * from ' + scriptRequestString + '\n\n'
375
- if (ctorType === 'app') {
376
- mpx.appScriptRawRequest = JSON.parse(scriptRequestString)
377
- mpx.appScriptPromise = new Promise((resolve) => {
378
- mpx.appScriptPromiseResolve = resolve
379
- })
380
- }
381
- }
382
- } else {
383
- switch (ctorType) {
384
- case 'app':
385
- output += 'import {createApp} from "@mpxjs/core"\n' +
386
- 'createApp({})\n'
387
- break
388
- case 'page':
389
- output += 'import {createPage} from "@mpxjs/core"\n' +
390
- 'createPage({})\n'
391
- break
392
- case 'component':
393
- output += 'import {createComponent} from "@mpxjs/core"\n' +
394
- 'createComponent({})\n'
395
- }
396
- output += '\n'
397
- }
306
+ // template
307
+ output += '/* template */\n'
308
+ const template = parts.template
398
309
 
399
- if (scriptSrcMode) {
400
- 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'
401
328
  }
402
329
 
403
330
  // styles
404
331
  output += '/* styles */\n'
405
- let cssModules
406
332
  if (parts.styles.length) {
407
- let styleInjectionCode = ''
408
333
  parts.styles.forEach((style, i) => {
409
- let scoped = hasScoped ? (style.scoped || autoScope) : false
410
- let requireString
411
- // require style
412
- if (style.src) {
413
- style.src = processSrcQuery(style.src, 'styles')
414
- requireString = getRequireForSrc('styles', style, -1, scoped)
415
- } else {
416
- requireString = getRequire('styles', style, i, scoped)
417
- }
418
- const hasStyleLoader = requireString.indexOf('style-loader') > -1
419
- const invokeStyle = code => `${code}\n`
420
-
421
- const moduleName = style.module === true ? '$style' : style.module
422
- // setCssModule
423
- if (moduleName) {
424
- if (!cssModules) {
425
- cssModules = {}
426
- }
427
- if (moduleName in cssModules) {
428
- loaderContext.emitError(
429
- 'CSS module name "' + moduleName + '" is not unique!'
430
- )
431
- styleInjectionCode += invokeStyle(requireString)
432
- } else {
433
- cssModules[moduleName] = true
434
-
435
- if (!hasStyleLoader) {
436
- requireString += '.locals'
437
- }
438
-
439
- styleInjectionCode += invokeStyle(
440
- 'this["' + moduleName + '"] = ' + requireString
441
- )
442
- }
443
- } else {
444
- 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
445
344
  }
345
+ // require style
346
+ output += getRequire('styles', style, extraOptions, i) + '\n'
446
347
  })
447
- output += styleInjectionCode + '\n'
448
- } else if (ctorType === 'app' && mode === 'ali') {
449
- 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'
450
352
  }
451
353
 
452
354
  // json
453
355
  output += '/* json */\n'
454
356
  // 给予json默认值, 确保生成json request以自动补全json
455
357
  const json = parts.json || {}
456
- if (json.src) {
457
- json.src = processSrcQuery(json.src, 'json')
458
- output += getRequireForSrc('json', json) + '\n\n'
459
- } else {
460
- output += getRequire('json', json) + '\n\n'
461
- }
462
-
463
- // template
464
- output += '/* template */\n'
465
- const template = parts.template
358
+ output += getRequire('json', json, json.src && {
359
+ ...queryObj,
360
+ resourcePath
361
+ }) + '\n'
466
362
 
467
- if (template) {
468
- if (template.src) {
469
- template.src = processSrcQuery(template.src, 'template')
470
- output += getRequireForSrc('template', template) + '\n\n'
471
- } else {
472
- 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
473
378
  }
379
+ output += getRequire('script', script, extraOptions) + '\n'
474
380
  }
475
-
476
- if (!mpx.forceDisableInject) {
477
- const dep = new InjectDependency({
478
- content: globalInjectCode,
479
- index: -3
480
- })
481
- this._module.addDependency(dep)
482
- }
483
-
484
381
  callback(null, output)
485
382
  }
486
383
  ], callback)