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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/lib/dependencies/AppEntryDependency.js +56 -0
  2. package/lib/dependencies/CommonJsVariableDependency.js +74 -0
  3. package/lib/dependencies/DynamicEntryDependency.js +127 -0
  4. package/lib/dependencies/FlagPluginDependency.js +23 -0
  5. package/lib/dependencies/InjectDependency.js +43 -0
  6. package/lib/dependencies/RecordGlobalComponentsDependency.js +50 -0
  7. package/lib/dependencies/RecordStaticResourceDependency.js +47 -0
  8. package/lib/{dependency → dependencies}/ReplaceDependency.js +19 -2
  9. package/lib/dependencies/ResolveDependency.js +83 -0
  10. package/lib/extractor.js +72 -179
  11. package/lib/file-loader.js +7 -19
  12. package/lib/helpers.js +41 -330
  13. package/lib/index.js +472 -356
  14. package/lib/json-compiler/helper.js +152 -0
  15. package/lib/json-compiler/index.js +148 -407
  16. package/lib/json-compiler/plugin.js +134 -0
  17. package/lib/json-compiler/{theme-loader.js → theme.js} +5 -3
  18. package/lib/loader.js +76 -171
  19. package/lib/native-loader.js +40 -70
  20. package/lib/record-loader.js +11 -0
  21. package/lib/{path-loader.js → resolve-loader.js} +0 -0
  22. package/lib/runtime/i18n.wxs +3 -3
  23. package/lib/selector.js +10 -7
  24. package/lib/style-compiler/index.js +20 -12
  25. package/lib/style-compiler/plugins/trans-special.js +21 -0
  26. package/lib/template-compiler/compiler.js +44 -176
  27. package/lib/template-compiler/index.js +47 -128
  28. package/lib/url-loader.js +11 -29
  29. package/lib/utils/add-query.js +1 -1
  30. package/lib/utils/const.js +5 -0
  31. package/lib/utils/emit-file.js +10 -0
  32. package/lib/utils/get-entry-name.js +13 -0
  33. package/lib/utils/is-url-request.js +10 -1
  34. package/lib/utils/normalize.js +0 -13
  35. package/lib/utils/parse-request.js +3 -3
  36. package/lib/utils/set.js +47 -0
  37. package/lib/utils/stringify-loaders-resource.js +25 -0
  38. package/lib/utils/stringify-query.js +4 -0
  39. package/lib/web/processScript.js +3 -3
  40. package/lib/web/processTemplate.js +2 -0
  41. package/lib/wxml/{wxml-loader.js → loader.js} +24 -60
  42. package/lib/wxs/WxsModuleIdsPlugin.js +32 -0
  43. package/lib/wxs/WxsParserPlugin.js +2 -2
  44. package/lib/wxs/WxsPlugin.js +4 -8
  45. package/lib/wxs/WxsTemplatePlugin.js +46 -92
  46. package/lib/wxs/{wxs-i18n-loader.js → i18n-loader.js} +0 -0
  47. package/lib/wxs/{wxs-loader.js → loader.js} +33 -38
  48. package/lib/wxs/{wxs-pre-loader.js → pre-loader.js} +0 -0
  49. package/lib/wxss/loader.js +31 -43
  50. package/lib/wxss/localsLoader.js +1 -5
  51. package/package.json +4 -8
  52. package/lib/content-loader.js +0 -13
  53. package/lib/dependency/ChildCompileDependency.js +0 -24
  54. package/lib/dependency/InjectDependency.js +0 -26
  55. package/lib/dependency/RemovedModuleDependency.js +0 -23
  56. package/lib/dependency/ResolveDependency.js +0 -49
  57. package/lib/plugin-loader.js +0 -287
  58. package/lib/utils/try-require.js +0 -16
  59. package/lib/wxss/getImportPrefix.js +0 -30
@@ -0,0 +1,134 @@
1
+ const async = require('async')
2
+ const JSON5 = require('json5')
3
+ const getEntryName = require('../utils/get-entry-name')
4
+ const FlagPluginDependency = require('../dependencies/FlagPluginDependency')
5
+ const createJSONHelper = require('./helper')
6
+
7
+ module.exports = function (source) {
8
+ // 该loader中会在每次编译中动态添加entry,不能缓存,否则watch不好使
9
+ const nativeCallback = this.async()
10
+
11
+ const mpx = this.getMpx()
12
+
13
+ if (!mpx) {
14
+ return nativeCallback(null, source)
15
+ }
16
+
17
+ this._module.addPresentationalDependency(new FlagPluginDependency())
18
+
19
+ const emitWarning = (msg) => {
20
+ this.emitWarning(
21
+ new Error('[plugin loader][' + this.resource + ']: ' + msg)
22
+ )
23
+ }
24
+
25
+ const emitError = (msg) => {
26
+ this.emitError(
27
+ new Error('[plugin loader][' + this.resource + ']: ' + msg)
28
+ )
29
+ }
30
+
31
+ const {
32
+ processPage,
33
+ processDynamicEntry,
34
+ processComponent,
35
+ processJsExport
36
+ } = createJSONHelper({
37
+ loaderContext: this,
38
+ emitWarning,
39
+ emitError
40
+ })
41
+
42
+ const context = this.context
43
+ const relativePath = this._compilation.outputOptions.publicPath || ''
44
+ const mode = mpx.mode
45
+ const srcMode = mpx.srcMode
46
+ const entryName = getEntryName(this)
47
+ // 最终输出中不需要为plugin.json产生chunk,而是使用extractor输出,删除plugin.json对应的entrypoint
48
+ if (entryName) this._compilation.entries.delete(entryName)
49
+
50
+ // 新模式下plugin.json输出依赖于extractor
51
+ const callback = (err, processOutput) => {
52
+ if (err) return nativeCallback(err)
53
+ let output = `var pluginEntry = ${JSON.stringify(pluginEntry, null, 2)};\n`
54
+ if (processOutput) output = processOutput(output)
55
+ output += `module.exports = JSON.stringify(pluginEntry, null, 2);\n`
56
+ nativeCallback(null, output)
57
+ }
58
+
59
+ let pluginEntry
60
+ try {
61
+ pluginEntry = JSON5.parse(source)
62
+ } catch (err) {
63
+ return callback(err)
64
+ }
65
+
66
+ const processMain = (main, callback) => {
67
+ if (!main) return callback()
68
+ processJsExport(main, context, '', (err, entry) => {
69
+ if (err) return callback(err)
70
+ pluginEntry.main = entry
71
+ callback()
72
+ })
73
+ }
74
+
75
+ const processComponents = (components, callback) => {
76
+ if (!components) return callback()
77
+ async.eachOf(components, (component, name, callback) => {
78
+ processComponent(component, context, { relativePath }, (err, entry) => {
79
+ if (err) return callback(err)
80
+ pluginEntry.publicComponents[name] = entry
81
+ callback()
82
+ })
83
+ }, callback)
84
+ }
85
+
86
+ const processPages = (pages, callback) => {
87
+ if (!pages) return callback()
88
+ if (srcMode === 'ali') {
89
+ const reversedMap = {}
90
+ const publicPages = pluginEntry.publicPages || {}
91
+ Object.keys(publicPages).forEach((key) => {
92
+ const item = publicPages[key]
93
+ reversedMap[item] = key
94
+ })
95
+ pages = pages.reduce((page, target, index) => {
96
+ const key = reversedMap[page] || `__private_page_${index}__`
97
+ target[key] = page
98
+ }, {})
99
+ }
100
+
101
+ if (mode === 'ali') {
102
+ pluginEntry.publicPages = {}
103
+ pluginEntry.pages = []
104
+ }
105
+
106
+ async.eachOf(pages, (page, key) => {
107
+ processPage(page, context, '', (err, entry) => {
108
+ if (err) return callback(err)
109
+ pages[key] = entry
110
+ if (mode === 'ali') {
111
+ pluginEntry.pages.push(entry)
112
+ if (!/^__private_page_\d+__$/.test(key)) {
113
+ pluginEntry.publicPages[key] = entry
114
+ }
115
+ } else {
116
+ pluginEntry.pages[key] = entry
117
+ }
118
+ callback()
119
+ })
120
+ })
121
+ }
122
+
123
+ async.parallel([
124
+ (callback) => {
125
+ return processMain(pluginEntry.main, callback)
126
+ }, (callback) => {
127
+ return processComponents(pluginEntry.publicComponents, callback)
128
+ }, (callback) => {
129
+ return processPages(pluginEntry.publicPages, callback)
130
+ }
131
+ ], (err) => {
132
+ return callback(err, processDynamicEntry)
133
+ })
134
+ }
@@ -8,9 +8,11 @@ function isFile (request) {
8
8
  }
9
9
 
10
10
  module.exports = function (raw) {
11
- const options = loaderUtils.getOptions(this) || {}
12
- const isUrlRequest = r => isUrlRequestRaw(r, options.root)
13
- const urlToRequest = r => loaderUtils.urlToRequest(r, options.root)
11
+ const mpx = this.getMpx()
12
+ const root = mpx.projectRoot
13
+ const externals = mpx.externals
14
+ const isUrlRequest = r => isUrlRequestRaw(r, root, externals)
15
+ const urlToRequest = r => loaderUtils.urlToRequest(r, root)
14
16
 
15
17
  const json = JSON5.parse(raw)
16
18
  let output = `var json = ${JSON.stringify(json, null, 2)};\n`
package/lib/loader.js CHANGED
@@ -2,7 +2,6 @@ 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
6
  const matchCondition = require('./utils/match-condition')
8
7
  const fixUsingComponent = require('./utils/fix-using-component')
@@ -14,13 +13,13 @@ const processStyles = require('./web/processStyles')
14
13
  const processTemplate = require('./web/processTemplate')
15
14
  const readJsonForSrc = require('./utils/read-json-for-src')
16
15
  const normalize = require('./utils/normalize')
17
- const getMainCompilation = require('./utils/get-main-compilation')
16
+ const getEntryName = require('./utils/get-entry-name')
17
+ const AppEntryDependency = require('./dependencies/AppEntryDependency')
18
18
 
19
19
  module.exports = function (content) {
20
20
  this.cacheable()
21
21
 
22
- const mainCompilation = getMainCompilation(this._compilation)
23
- const mpx = mainCompilation.__mpx__
22
+ const mpx = this.getMpx()
24
23
  if (!mpx) {
25
24
  return content
26
25
  }
@@ -42,19 +41,11 @@ module.exports = function (content) {
42
41
 
43
42
  // 支持资源query传入page或component支持页面/组件单独编译
44
43
  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
- }
44
+ const entryName = getEntryName(this)
54
45
  if (queryObj.component) {
55
- componentsMap[resourcePath] = entryChunkName || 'noEntryComponent'
46
+ componentsMap[resourcePath] = entryName || 'noEntryComponent'
56
47
  } else {
57
- pagesMap[resourcePath] = entryChunkName || 'noEntryPage'
48
+ pagesMap[resourcePath] = entryName || 'noEntryPage'
58
49
  }
59
50
  }
60
51
 
@@ -67,24 +58,14 @@ module.exports = function (content) {
67
58
  ctorType = 'component'
68
59
  }
69
60
 
61
+ if (ctorType === 'app') {
62
+ const appName = getEntryName(this)
63
+ this._module.addPresentationalDependency(new AppEntryDependency(resourcePath, appName))
64
+ }
65
+
70
66
  const loaderContext = this
71
67
  const stringifyRequest = r => loaderUtils.stringifyRequest(loaderContext, r)
72
68
  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
69
 
89
70
  const filePath = resourcePath
90
71
 
@@ -119,8 +100,7 @@ module.exports = function (content) {
119
100
  if (vueContentCache.has(filePath)) {
120
101
  return callback(null, vueContentCache.get(filePath))
121
102
  }
122
- // 只有ali才可能需要scoped
123
- const hasScoped = (parts.styles.some(({ scoped }) => scoped) || autoScope) && mode === 'ali'
103
+ const hasScoped = parts.styles.some(({ scoped }) => scoped) || autoScope
124
104
  const templateAttrs = parts.template && parts.template.attrs
125
105
  const hasComment = templateAttrs && templateAttrs.comments
126
106
  const isNative = false
@@ -144,23 +124,6 @@ module.exports = function (content) {
144
124
  }
145
125
  }
146
126
 
147
- const {
148
- getRequire,
149
- getRequireForSrc,
150
- getRequestString,
151
- getSrcRequestString
152
- } = createHelpers({
153
- loaderContext,
154
- options,
155
- moduleId,
156
- hasScoped,
157
- hasComment,
158
- usingComponents,
159
- srcMode,
160
- isNative,
161
- projectRoot
162
- })
163
-
164
127
  // 处理mode为web时输出vue格式文件
165
128
  if (mode === 'web') {
166
129
  if (ctorType === 'app' && !queryObj.app) {
@@ -185,6 +148,7 @@ module.exports = function (content) {
185
148
  async.parallel([
186
149
  (callback) => {
187
150
  processTemplate(parts.template, {
151
+ hasScoped,
188
152
  hasComment,
189
153
  isNative,
190
154
  mode,
@@ -238,7 +202,6 @@ module.exports = function (content) {
238
202
  srcMode,
239
203
  loaderContext,
240
204
  isProduction,
241
- getRequireForSrc,
242
205
  i18n,
243
206
  componentGenerics,
244
207
  projectRoot,
@@ -262,36 +225,23 @@ module.exports = function (content) {
262
225
  })
263
226
  }
264
227
 
265
- // 触发webpack global var 注入
266
- output += 'global.currentModuleId\n'
228
+ const {
229
+ getRequire
230
+ } = createHelpers(loaderContext)
267
231
 
268
- // todo loader中inject dep比较危险,watch模式下不一定靠谱,可考虑将import改为require然后通过修改loader内容注入
269
232
  // 注入模块id及资源路径
270
- let globalInjectCode = `global.currentModuleId = ${JSON.stringify(moduleId)}\n`
233
+ output += `global.currentModuleId = ${JSON.stringify(moduleId)}\n`
271
234
  if (!isProduction) {
272
- globalInjectCode += `global.currentResource = ${JSON.stringify(filePath)}\n`
235
+ output += `global.currentResource = ${JSON.stringify(filePath)}\n`
273
236
  }
274
- if (ctorType === 'app' && i18n && !mpx.forceDisableInject) {
275
- globalInjectCode += `global.i18n = ${JSON.stringify({ locale: i18n.locale, version: 0 })}\n`
237
+ if (ctorType === 'app' && i18n) {
238
+ output += `global.i18n = ${JSON.stringify({ locale: i18n.locale, version: 0 })}\n`
276
239
 
277
- const i18nMethodsVar = 'i18nMethods'
278
240
  const i18nWxsPath = normalize.lib('runtime/i18n.wxs')
279
- const i18nWxsLoaderPath = normalize.lib('wxs/wxs-i18n-loader.js')
241
+ const i18nWxsLoaderPath = normalize.lib('wxs/i18n-loader.js')
280
242
  const i18nWxsRequest = i18nWxsLoaderPath + '!' + i18nWxsPath
281
- const expression = `require(${loaderUtils.stringifyRequest(loaderContext, i18nWxsRequest)})`
282
- const deps = []
283
- this._module.parser.parse(expression, {
284
- current: {
285
- addDependency: dep => {
286
- dep.userRequest = i18nMethodsVar
287
- deps.push(dep)
288
- }
289
- },
290
- module: this._module
291
- })
292
- this._module.addVariable(i18nMethodsVar, expression, deps)
293
243
 
294
- globalInjectCode += `global.i18nMethods = ${i18nMethodsVar}\n`
244
+ output += `global.i18nMethods = require(${loaderUtils.stringifyRequest(loaderContext, i18nWxsRequest)})\n`
295
245
  }
296
246
  // 注入构造函数
297
247
  let ctor = 'App'
@@ -305,29 +255,68 @@ module.exports = function (content) {
305
255
  } else if (ctorType === 'component') {
306
256
  ctor = 'Component'
307
257
  }
308
- globalInjectCode += `global.currentCtor = ${ctor}\n`
309
- globalInjectCode += `global.currentCtorType = ${JSON.stringify(ctor.replace(/^./, (match) => {
258
+ output += `global.currentCtor = ${ctor}\n`
259
+ output += `global.currentCtorType = ${JSON.stringify(ctor.replace(/^./, (match) => {
310
260
  return match.toLowerCase()
311
261
  }))}\n`
312
262
 
313
- // <script>
263
+ // template
264
+ output += '/* template */\n'
265
+ const template = parts.template
266
+
267
+ if (template) {
268
+ const extraOptions = {
269
+ hasScoped,
270
+ hasComment,
271
+ isNative,
272
+ moduleId,
273
+ usingComponents
274
+ // 添加babel处理渲染函数中可能包含的...展开运算符
275
+ // 由于...运算符应用范围极小以及babel成本极高,先关闭此特性后续看情况打开
276
+ // needBabel: true
277
+ }
278
+ if (template.src) extraOptions.resourcePath = resourcePath
279
+ // 基于global.currentInject来注入模板渲染函数和refs等信息
280
+ output += getRequire('template', template, extraOptions) + '\n'
281
+ }
282
+
283
+ // styles
284
+ output += '/* styles */\n'
285
+ if (parts.styles.length) {
286
+ parts.styles.forEach((style, i) => {
287
+ const scoped = style.scoped || autoScope
288
+ const extraOptions = {
289
+ moduleId,
290
+ scoped
291
+ }
292
+ // require style
293
+ if (style.src) {
294
+ // style src会被特殊处理为全局复用样式,不添加resourcePath,添加isStatic及issuerResource
295
+ extraOptions.isStatic = true
296
+ extraOptions.issuerFile = mpx.getExtractedFile(this.resource)
297
+ }
298
+ output += getRequire('styles', style, extraOptions, i) + '\n'
299
+ })
300
+ } else if (ctorType === 'app' && mode === 'ali') {
301
+ output += getRequire('styles', {}) + '\n'
302
+ }
303
+
304
+ // json
305
+ output += '/* json */\n'
306
+ // 给予json默认值, 确保生成json request以自动补全json
307
+ const json = parts.json || {}
308
+ output += getRequire('json', json, json.src && { resourcePath }) + '\n'
309
+
310
+ // script
314
311
  output += '/* script */\n'
315
312
  let scriptSrcMode = srcMode
316
313
  const script = parts.script
317
314
  if (script) {
318
315
  scriptSrcMode = script.mode || scriptSrcMode
319
- let scriptRequestString
320
- if (script.src) {
321
- // 传入resourcePath以确保后续处理中能够识别src引入的资源为组件主资源
322
- script.src = processSrcQuery(script.src, 'script')
323
- scriptRequestString = getSrcRequestString('script', script)
324
- } else {
325
- scriptRequestString = getRequestString('script', script)
326
- }
327
- if (scriptRequestString) {
328
- output += 'export * from ' + scriptRequestString + '\n\n'
329
- if (ctorType === 'app') mpx.appScriptRawRequest = JSON.parse(scriptRequestString)
330
- }
316
+ if (scriptSrcMode) output += `global.currentSrcMode = ${JSON.stringify(scriptSrcMode)}\n`
317
+ const extraOptions = {}
318
+ if (script.src) extraOptions.resourcePath = resourcePath
319
+ output += getRequire('script', script, extraOptions) + '\n'
331
320
  } else {
332
321
  switch (ctorType) {
333
322
  case 'app':
@@ -344,90 +333,6 @@ module.exports = function (content) {
344
333
  }
345
334
  output += '\n'
346
335
  }
347
-
348
- if (scriptSrcMode) {
349
- globalInjectCode += `global.currentSrcMode = ${JSON.stringify(scriptSrcMode)}\n`
350
- }
351
-
352
- // styles
353
- output += '/* styles */\n'
354
- let cssModules
355
- if (parts.styles.length) {
356
- let styleInjectionCode = ''
357
- parts.styles.forEach((style, i) => {
358
- let scoped = hasScoped ? (style.scoped || autoScope) : false
359
- let requireString
360
- // require style
361
- if (style.src) {
362
- style.src = processSrcQuery(style.src, 'styles')
363
- requireString = getRequireForSrc('styles', style, -1, scoped)
364
- } else {
365
- requireString = getRequire('styles', style, i, scoped)
366
- }
367
- const hasStyleLoader = requireString.indexOf('style-loader') > -1
368
- const invokeStyle = code => `${code}\n`
369
-
370
- const moduleName = style.module === true ? '$style' : style.module
371
- // setCssModule
372
- if (moduleName) {
373
- if (!cssModules) {
374
- cssModules = {}
375
- }
376
- if (moduleName in cssModules) {
377
- loaderContext.emitError(
378
- 'CSS module name "' + moduleName + '" is not unique!'
379
- )
380
- styleInjectionCode += invokeStyle(requireString)
381
- } else {
382
- cssModules[moduleName] = true
383
-
384
- if (!hasStyleLoader) {
385
- requireString += '.locals'
386
- }
387
-
388
- styleInjectionCode += invokeStyle(
389
- 'this["' + moduleName + '"] = ' + requireString
390
- )
391
- }
392
- } else {
393
- styleInjectionCode += invokeStyle(requireString)
394
- }
395
- })
396
- output += styleInjectionCode + '\n'
397
- }
398
-
399
- // json
400
- output += '/* json */\n'
401
- // 给予json默认值, 确保生成json request以自动补全json
402
- const json = parts.json || {}
403
- if (json.src) {
404
- json.src = processSrcQuery(json.src, 'json')
405
- output += getRequireForSrc('json', json) + '\n\n'
406
- } else {
407
- output += getRequire('json', json) + '\n\n'
408
- }
409
-
410
- // template
411
- output += '/* template */\n'
412
- const template = parts.template
413
-
414
- if (template) {
415
- if (template.src) {
416
- template.src = processSrcQuery(template.src, 'template')
417
- output += getRequireForSrc('template', template) + '\n\n'
418
- } else {
419
- output += getRequire('template', template) + '\n\n'
420
- }
421
- }
422
-
423
- if (!mpx.forceDisableInject) {
424
- const dep = new InjectDependency({
425
- content: globalInjectCode,
426
- index: -3
427
- })
428
- this._module.addDependency(dep)
429
- }
430
-
431
336
  callback(null, output)
432
337
  }
433
338
  ], callback)
@@ -1,37 +1,27 @@
1
1
  const path = require('path')
2
2
  const JSON5 = require('json5')
3
3
  const parseRequest = require('./utils/parse-request')
4
- const loaderUtils = require('loader-utils')
5
4
  const config = require('./config')
6
5
  const createHelpers = require('./helpers')
7
- const InjectDependency = require('./dependency/InjectDependency')
8
- const addQuery = require('./utils/add-query')
9
6
  const mpxJSON = require('./utils/mpx-json')
10
7
  const async = require('async')
11
8
  const matchCondition = require('./utils/match-condition')
12
9
  const fixUsingComponent = require('./utils/fix-using-component')
13
- const getMainCompilation = require('./utils/get-main-compilation')
14
10
 
15
11
  module.exports = function (content) {
16
12
  this.cacheable()
17
13
 
18
- const mainCompilation = getMainCompilation(this._compilation)
19
- const mpx = mainCompilation.__mpx__
14
+ const mpx = this.getMpx()
20
15
  if (!mpx) {
21
16
  return content
22
17
  }
23
18
 
24
19
  const nativeCallback = this.async()
25
-
26
20
  const loaderContext = this
27
21
  const isProduction = this.minimize || process.env.NODE_ENV === 'production'
28
- const options = Object.assign({}, mpx.loaderOptions, loaderUtils.getOptions(this))
29
-
30
22
  const filePath = this.resourcePath
31
-
32
23
  const moduleId = 'm' + mpx.pathHash(filePath)
33
24
  const { resourcePath, queryObj } = parseRequest(this.resource)
34
- const projectRoot = mpx.projectRoot
35
25
  const mode = mpx.mode
36
26
  const defs = mpx.defs
37
27
  const globalSrcMode = mpx.srcMode
@@ -41,7 +31,7 @@ module.exports = function (content) {
41
31
  const componentsMap = mpx.componentsMap[packageName]
42
32
  const parsed = path.parse(resourcePath)
43
33
  const resourceName = path.join(parsed.dir, parsed.name)
44
- const isApp = !pagesMap[resourcePath] && !componentsMap[resourcePath]
34
+ const isApp = !(pagesMap[resourcePath] || componentsMap[resourcePath])
45
35
  const srcMode = localSrcMode || globalSrcMode
46
36
  const fs = this._compiler.inputFileSystem
47
37
  const typeExtMap = config[srcMode].typeExtMap
@@ -177,61 +167,52 @@ module.exports = function (content) {
177
167
  }
178
168
  } catch (e) {
179
169
  }
180
- const {
181
- getRequireForSrc,
182
- getNamedExportsForSrc
183
- } = createHelpers({
184
- loaderContext,
185
- options,
186
- moduleId,
187
- hasScoped,
188
- hasComment,
189
- usingComponents,
190
- srcMode,
191
- isNative,
192
- projectRoot
193
- })
194
170
 
195
- const getRequire = (type) => {
196
- const localQuery = Object.assign({}, queryObj)
197
- let src = typeResourceMap[type]
198
- localQuery.resourcePath = resourcePath
199
- if (type !== 'script') {
200
- this.addDependency(src)
201
- }
202
- if (type === 'template' && isApp) {
203
- return ''
204
- }
205
- if (type === 'json' && !useMPXJSON) {
206
- localQuery.__component = true
207
- }
208
- src = addQuery(src, localQuery, true)
209
- const partsOpts = { src }
171
+ const {
172
+ getRequire
173
+ } = createHelpers(loaderContext)
174
+
175
+ const getRequireByType = (type) => {
176
+ const src = typeResourceMap[type]
177
+ const part = { src }
178
+ const extraOptions = Object.assign({}, queryObj, {
179
+ resourcePath
180
+ })
210
181
 
211
- if (type === 'script') {
212
- return getNamedExportsForSrc(type, partsOpts)
213
- }
214
- if (type === 'styles') {
215
- if (cssLang) {
216
- partsOpts.lang = cssLang
217
- }
218
- if (hasScoped) {
219
- return getRequireForSrc(type, partsOpts, 0, true)
220
- }
182
+ if (type !== 'script') this.addDependency(src)
183
+
184
+ switch (type) {
185
+ case 'template':
186
+ if (isApp) return ''
187
+ Object.assign(extraOptions, {
188
+ hasScoped,
189
+ hasComment,
190
+ isNative,
191
+ moduleId,
192
+ usingComponents
193
+ })
194
+ break
195
+ case 'styles':
196
+ if (cssLang) part.lang = cssLang
197
+ Object.assign(extraOptions, {
198
+ moduleId,
199
+ scoped: hasScoped
200
+ })
201
+ break
221
202
  }
222
- return getRequireForSrc(type, partsOpts)
203
+ return getRequire(type, part, extraOptions)
223
204
  }
224
205
 
225
206
  // 注入模块id及资源路径
226
- let globalInjectCode = `global.currentModuleId = ${JSON.stringify(moduleId)}\n`
207
+ let output = `global.currentModuleId = ${JSON.stringify(moduleId)}\n`
227
208
  if (!isProduction) {
228
- globalInjectCode += `global.currentResource = ${JSON.stringify(filePath)}\n`
209
+ output += `global.currentResource = ${JSON.stringify(filePath)}\n`
229
210
  }
230
211
 
231
212
  // 注入构造函数
232
213
  let ctor = 'App'
233
214
  if (pagesMap[resourcePath]) {
234
- if (mpx.forceUsePageCtor || mode === 'ali') {
215
+ if (mpx.forceUsePageCtor || mode === 'ali' || mode === 'swan') {
235
216
  ctor = 'Page'
236
217
  } else {
237
218
  ctor = 'Component'
@@ -239,28 +220,17 @@ module.exports = function (content) {
239
220
  } else if (componentsMap[resourcePath]) {
240
221
  ctor = 'Component'
241
222
  }
242
- globalInjectCode += `global.currentCtor = ${ctor}\n`
243
- globalInjectCode += `global.currentCtorType = ${JSON.stringify(ctor.replace(/^./, (match) => {
223
+ output += `global.currentCtor = ${ctor}\n`
224
+ output += `global.currentCtorType = ${JSON.stringify(ctor.replace(/^./, (match) => {
244
225
  return match.toLowerCase()
245
226
  }))}\n`
246
227
 
247
228
  if (srcMode) {
248
- globalInjectCode += `global.currentSrcMode = ${JSON.stringify(srcMode)}\n`
229
+ output += `global.currentSrcMode = ${JSON.stringify(srcMode)}\n`
249
230
  }
250
231
 
251
- if (!mpx.forceDisableInject) {
252
- const dep = new InjectDependency({
253
- content: globalInjectCode,
254
- index: -3
255
- })
256
- this._module.addDependency(dep)
257
- }
258
-
259
- // 触发webpack global var 注入
260
- let output = 'global.currentModuleId;\n'
261
-
262
232
  for (let type in typeResourceMap) {
263
- output += `/* ${type} */\n${getRequire(type)}\n\n`
233
+ output += `/* ${type} */\n${getRequireByType(type)}\n\n`
264
234
  }
265
235
 
266
236
  callback(null, output)
@@ -0,0 +1,11 @@
1
+ const parseRequest = require('./utils/parse-request')
2
+ const RecordStaticResourceDependency = require('./dependencies/RecordStaticResourceDependency')
3
+
4
+ module.exports = function (source) {
5
+ const mpx = this.getMpx()
6
+ const { resourcePath, queryObj } = parseRequest(this.resource)
7
+ const file = mpx.getExtractedFile(this.resource)
8
+ const packageRoot = queryObj.packageRoot || ''
9
+ this._module.addPresentationalDependency(new RecordStaticResourceDependency(resourcePath, file, packageRoot))
10
+ return source
11
+ }
File without changes