@mpxjs/webpack-plugin 2.7.0-alpha.0 → 2.7.0-alpha.2

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 (108) hide show
  1. package/README.md +1 -1
  2. package/lib/config.js +14 -0
  3. package/lib/dependencies/AddEntryDependency.js +24 -0
  4. package/lib/dependencies/AppEntryDependency.js +2 -0
  5. package/lib/dependencies/CommonJsAsyncDependency.js +51 -0
  6. package/lib/dependencies/CommonJsVariableDependency.js +13 -6
  7. package/lib/dependencies/DynamicEntryDependency.js +85 -41
  8. package/lib/dependencies/FlagPluginDependency.js +1 -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/dependencies/ResolveDependency.js +11 -6
  13. package/lib/extractor.js +17 -7
  14. package/lib/file-loader.js +2 -2
  15. package/lib/helpers.js +6 -12
  16. package/lib/independent-loader.js +52 -0
  17. package/lib/index.js +595 -304
  18. package/lib/json-compiler/helper.js +36 -32
  19. package/lib/json-compiler/index.js +119 -66
  20. package/lib/json-compiler/plugin.js +23 -7
  21. package/lib/loader.js +135 -90
  22. package/lib/native-loader.js +37 -69
  23. package/lib/parser.js +1 -2
  24. package/lib/partial-compile/index.js +35 -0
  25. package/lib/platform/json/wx/index.js +8 -3
  26. package/lib/platform/template/normalize-component-rules.js +2 -3
  27. package/lib/platform/template/wx/component-config/button.js +17 -5
  28. package/lib/platform/template/wx/component-config/image.js +4 -0
  29. package/lib/platform/template/wx/component-config/input.js +4 -0
  30. package/lib/platform/template/wx/component-config/navigator.js +1 -1
  31. package/lib/platform/template/wx/component-config/rich-text.js +4 -0
  32. package/lib/platform/template/wx/component-config/scroll-view.js +4 -0
  33. package/lib/platform/template/wx/component-config/switch.js +4 -0
  34. package/lib/platform/template/wx/component-config/text.js +4 -0
  35. package/lib/platform/template/wx/component-config/textarea.js +5 -0
  36. package/lib/platform/template/wx/component-config/view.js +4 -0
  37. package/lib/platform/template/wx/index.js +149 -3
  38. package/lib/record-loader.js +2 -2
  39. package/lib/resolve-loader.js +4 -1
  40. package/lib/resolver/AddEnvPlugin.js +4 -3
  41. package/lib/resolver/AddModePlugin.js +4 -3
  42. package/lib/resolver/FixDescriptionInfoPlugin.js +28 -0
  43. package/lib/resolver/PackageEntryPlugin.js +23 -36
  44. package/lib/runtime/base.styl +5 -0
  45. package/lib/runtime/components/tenon/getInnerListeners.js +317 -0
  46. package/lib/runtime/components/tenon/tenon-button.vue +305 -0
  47. package/lib/runtime/components/tenon/tenon-image.vue +61 -0
  48. package/lib/runtime/components/tenon/tenon-input.vue +99 -0
  49. package/lib/runtime/components/tenon/tenon-rich-text.vue +21 -0
  50. package/lib/runtime/components/tenon/tenon-scroll-view.vue +124 -0
  51. package/lib/runtime/components/tenon/tenon-switch.vue +91 -0
  52. package/lib/runtime/components/tenon/tenon-text-area.vue +64 -0
  53. package/lib/runtime/components/tenon/tenon-text.vue +64 -0
  54. package/lib/runtime/components/tenon/tenon-view.vue +93 -0
  55. package/lib/runtime/components/tenon/util.js +44 -0
  56. package/lib/runtime/components/web/getInnerListeners.js +51 -45
  57. package/lib/runtime/components/web/mpx-image.vue +20 -5
  58. package/lib/runtime/components/web/mpx-keep-alive.vue +4 -1
  59. package/lib/runtime/components/web/mpx-movable-view.vue +6 -2
  60. package/lib/runtime/components/web/mpx-swiper.vue +37 -8
  61. package/lib/runtime/components/web/mpx-tab-bar-container.vue +2 -2
  62. package/lib/runtime/components/web/mpx-textarea.vue +1 -1
  63. package/lib/runtime/i18n.wxs +28 -8
  64. package/lib/runtime/optionProcessor.js +50 -20
  65. package/lib/runtime/optionProcessor.tenon.js +386 -0
  66. package/lib/runtime/stringify.wxs +6 -4
  67. package/lib/selector.js +23 -5
  68. package/lib/style-compiler/index.js +12 -13
  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 +19 -12
  77. package/lib/template-compiler/bind-this.js +4 -4
  78. package/lib/template-compiler/compiler.js +172 -62
  79. package/lib/template-compiler/index.js +8 -9
  80. package/lib/template-compiler/trans-dynamic-class-expr.js +32 -22
  81. package/lib/tenon/index.js +105 -0
  82. package/lib/tenon/processJSON.js +356 -0
  83. package/lib/tenon/processScript.js +261 -0
  84. package/lib/tenon/processStyles.js +21 -0
  85. package/lib/tenon/processTemplate.js +133 -0
  86. package/lib/utils/const.js +6 -1
  87. package/lib/utils/eval-json-js.js +31 -0
  88. package/lib/utils/get-entry-name.js +3 -3
  89. package/lib/utils/get-json-content.js +42 -0
  90. package/lib/utils/get-relative-path.js +25 -0
  91. package/lib/utils/match-condition.js +4 -1
  92. package/lib/utils/normalize.js +4 -2
  93. package/lib/utils/resolve.js +13 -0
  94. package/lib/web/processJSON.js +113 -144
  95. package/lib/web/processScript.js +45 -38
  96. package/lib/web/processTemplate.js +56 -41
  97. package/lib/wxml/loader.js +1 -6
  98. package/lib/wxs/WxsModuleIdsPlugin.js +11 -14
  99. package/lib/wxs/i18n-loader.js +5 -4
  100. package/lib/wxs/loader.js +87 -56
  101. package/lib/wxs/pre-loader.js +30 -10
  102. package/lib/wxss/loader.js +3 -3
  103. package/lib/wxss/processCss.js +135 -131
  104. package/package.json +23 -17
  105. package/lib/built-in-loader.js +0 -49
  106. package/lib/dependencies/RecordStaticResourceDependency.js +0 -47
  107. package/lib/utils/get-main-compilation.js +0 -6
  108. package/lib/utils/read-json-for-src.js +0 -34
@@ -1,9 +1,8 @@
1
1
  const templateCompiler = require('../template-compiler/compiler')
2
2
  const genComponentTag = require('../utils/gen-component-tag')
3
3
  const addQuery = require('../utils/add-query')
4
- const path = require('path')
5
4
  const parseRequest = require('../utils/parse-request')
6
- const getMainCompilation = require('../utils/get-main-compilation')
5
+ // const { matchCondition } = require('../utils/match-condition')
7
6
 
8
7
  function calculateRootEleChild (arr) {
9
8
  if (!arr) {
@@ -21,19 +20,30 @@ function calculateRootEleChild (arr) {
21
20
  }, 0)
22
21
  }
23
22
 
24
- module.exports = function (template, options, callback) {
25
- const mode = options.mode
26
- const srcMode = options.srcMode
27
- const defs = options.defs
28
- const moduleId = options.moduleId
29
- const loaderContext = options.loaderContext
30
- const ctorType = options.ctorType
31
- const resourcePath = parseRequest(loaderContext.resource).resourcePath
23
+ module.exports = function (template, {
24
+ loaderContext,
25
+ // hasScoped,
26
+ hasComment,
27
+ isNative,
28
+ srcMode,
29
+ moduleId,
30
+ ctorType,
31
+ usingComponents,
32
+ componentGenerics
33
+ }, callback) {
34
+ const mpx = loaderContext.getMpx()
35
+ const {
36
+ mode,
37
+ defs,
38
+ wxsContentMap,
39
+ decodeHTMLText,
40
+ externalClasses,
41
+ checkUsingComponents
42
+ // autoVirtualHostRules
43
+ } = mpx
44
+ const { resourcePath } = parseRequest(loaderContext.resource)
32
45
  const builtInComponentsMap = {}
33
- const compilation = loaderContext._compilation
34
- const mainCompilation = getMainCompilation(compilation)
35
- const mpx = mainCompilation.__mpx__
36
- const wxsContentMap = mpx.wxsContentMap
46
+
37
47
  let wxsModuleMap, genericsInfo
38
48
  let output = '/* template */\n'
39
49
 
@@ -43,7 +53,7 @@ module.exports = function (template, options, callback) {
43
53
  content: '<div class="app"><mpx-keep-alive><router-view class="page"></router-view></mpx-keep-alive></div>'
44
54
  }
45
55
  builtInComponentsMap['mpx-keep-alive'] = {
46
- resource: addQuery('@mpxjs/webpack-plugin/lib/runtime/components/web/mpx-keep-alive.vue', { component: true })
56
+ resource: addQuery('@mpxjs/webpack-plugin/lib/runtime/components/web/mpx-keep-alive.vue', { isComponent: true })
47
57
  }
48
58
  }
49
59
 
@@ -62,7 +72,7 @@ module.exports = function (template, options, callback) {
62
72
  }
63
73
  if (template.content) {
64
74
  const templateSrcMode = template.mode || srcMode
65
- const parsed = templateCompiler.parse(template.content, {
75
+ const { root, meta } = templateCompiler.parse(template.content, {
66
76
  warn: (msg) => {
67
77
  loaderContext.emitWarning(
68
78
  new Error('[template compiler][' + loaderContext.resource + ']: ' + msg)
@@ -73,54 +83,59 @@ module.exports = function (template, options, callback) {
73
83
  new Error('[template compiler][' + loaderContext.resource + ']: ' + msg)
74
84
  )
75
85
  },
76
- usingComponents: options.usingComponents,
77
- hasComment: options.hasComment,
78
- isNative: options.isNative,
79
- basename: path.basename(resourcePath),
86
+ usingComponents,
87
+ hasComment,
88
+ isNative,
80
89
  isComponent: ctorType === 'component',
81
90
  mode,
82
91
  srcMode: templateSrcMode,
83
92
  defs,
84
- decodeHTMLText: options.decodeHTMLText,
85
- externalClasses: options.externalClasses,
93
+ decodeHTMLText,
94
+ externalClasses,
86
95
  // todo 后续输出web也采用mpx的scoped处理
87
- // hasScoped:options.hasScoped,
88
96
  hasScoped: false,
89
97
  moduleId,
90
- filePath: loaderContext.resourcePath,
98
+ filePath: resourcePath,
91
99
  i18n: null,
92
- checkUsingComponents: options.checkUsingComponents,
100
+ checkUsingComponents,
93
101
  // web模式下全局组件不会被合入usingComponents中,故globalComponents可以传空
94
102
  globalComponents: [],
95
103
  // web模式下实现抽象组件
96
- componentGenerics: options.componentGenerics
104
+ componentGenerics
105
+ // todo 后续输出web也基于autoVirtualHostRules决定是否添加root wrapper
106
+ // hasVirtualHost: matchCondition(resourcePath, autoVirtualHostRules)
97
107
  })
98
- if (parsed.meta.wxsModuleMap) {
99
- wxsModuleMap = parsed.meta.wxsModuleMap
108
+ if (meta.wxsModuleMap) {
109
+ wxsModuleMap = meta.wxsModuleMap
100
110
  }
101
- if (parsed.meta.wxsContentMap) {
102
- for (let module in parsed.meta.wxsContentMap) {
103
- wxsContentMap[`${resourcePath}~${module}`] = parsed.meta.wxsContentMap[module]
111
+ if (meta.wxsContentMap) {
112
+ for (let module in meta.wxsContentMap) {
113
+ wxsContentMap[`${resourcePath}~${module}`] = meta.wxsContentMap[module]
104
114
  }
105
115
  }
106
- if (parsed.meta.builtInComponentsMap) {
107
- Object.keys(parsed.meta.builtInComponentsMap).forEach((name) => {
116
+ if (meta.builtInComponentsMap) {
117
+ Object.keys(meta.builtInComponentsMap).forEach((name) => {
108
118
  builtInComponentsMap[name] = {
109
- resource: addQuery(parsed.meta.builtInComponentsMap[name], { component: true })
119
+ resource: addQuery(meta.builtInComponentsMap[name], { isComponent: true })
110
120
  }
111
121
  })
112
122
  }
113
- if (parsed.meta.genericsInfo) {
114
- genericsInfo = parsed.meta.genericsInfo
123
+ if (meta.genericsInfo) {
124
+ genericsInfo = meta.genericsInfo
115
125
  }
116
- // 输出H5有多个root element时, 使用div标签包裹
117
- if (parsed.root.tag === 'temp-node') {
118
- const childLen = calculateRootEleChild(parsed.root.children)
126
+ // 输出H5有多个root element时, 使用mpx-root-view标签包裹
127
+ // todo 后续输出web也基于autoVirtualHostRules决定是否添加root wrapper
128
+ if (root.tag === 'temp-node') {
129
+ const childLen = calculateRootEleChild(root.children)
119
130
  if (childLen >= 2) {
120
- parsed.root.tag = 'div'
131
+ root.tag = 'div'
132
+ templateCompiler.addAttrs(root, [{
133
+ name: 'class',
134
+ value: 'mpx-root-view'
135
+ }])
121
136
  }
122
137
  }
123
- return templateCompiler.serialize(parsed.root)
138
+ return templateCompiler.serialize(root)
124
139
  }
125
140
  })
126
141
  output += '\n\n'
@@ -17,13 +17,10 @@ module.exports = function (content) {
17
17
  const externals = mpx.externals
18
18
 
19
19
  const { queryObj } = parseRequest(this.resource)
20
- const moduleId = queryObj.moduleId
21
20
  const hasScoped = false
22
21
  const hasComment = false
23
22
  const isNative = true
24
23
 
25
- const usingComponents = []
26
-
27
24
  const mode = mpx.mode
28
25
  const localSrcMode = queryObj.mode
29
26
  const customAttributes = options.attributes || mpx.attributes || []
@@ -90,8 +87,6 @@ module.exports = function (content) {
90
87
  hasScoped,
91
88
  hasComment,
92
89
  isNative,
93
- moduleId,
94
- usingComponents,
95
90
  isStatic: true
96
91
  }
97
92
  requestString = getRequestString('template', { src, mode: localSrcMode }, extraOptions)
@@ -99,7 +94,7 @@ module.exports = function (content) {
99
94
  case config[mode].wxs.tag:
100
95
  // 显式传递issuerResource避免模块缓存以及提供给wxs-loader计算相对路径
101
96
  extraOptions = {
102
- issuerFile: mpx.getExtractedFile(this.resource),
97
+ issuerResource: this.resource,
103
98
  isStatic: true
104
99
  }
105
100
  requestString = getRequestString('wxs', { src, mode: localSrcMode }, extraOptions)
@@ -1,7 +1,10 @@
1
1
  const {
2
2
  compareModulesByPreOrderIndexOrIdentifier
3
3
  } = require('webpack/lib/util/comparators')
4
- const { assignAscendingModuleIds } = require('webpack/lib/ids/IdHelpers')
4
+ const {
5
+ assignAscendingModuleIds,
6
+ getUsedModuleIdsAndModules
7
+ } = require('webpack/lib/ids/IdHelpers')
5
8
 
6
9
  /** @typedef {import("../Compiler")} Compiler */
7
10
  /** @typedef {import("../Module")} Module */
@@ -12,19 +15,13 @@ class WxsModuleIdsPlugin {
12
15
  name: 'WxsModuleIdsPlugin',
13
16
  // 放在最前面执行,确保生成的代码模块为数组形式,符合wxs规范
14
17
  stage: -1000
15
- }, modules => {
16
- const chunkGraph = compilation.chunkGraph
17
- const modulesInNaturalOrder = Array.from(modules)
18
- .filter(
19
- m =>
20
- m.needId &&
21
- chunkGraph.getNumberOfModuleChunks(m) > 0 &&
22
- chunkGraph.getModuleId(m) === null
23
- )
24
- .sort(
25
- compareModulesByPreOrderIndexOrIdentifier(compilation.moduleGraph)
26
- )
27
- assignAscendingModuleIds(modulesInNaturalOrder, compilation)
18
+ }, () => {
19
+ const [usedIds, modulesInNaturalOrder] =
20
+ getUsedModuleIdsAndModules(compilation)
21
+ modulesInNaturalOrder.sort(
22
+ compareModulesByPreOrderIndexOrIdentifier(compilation.moduleGraph)
23
+ )
24
+ assignAscendingModuleIds(usedIds, modulesInNaturalOrder, compilation)
28
25
  })
29
26
  }
30
27
  }
@@ -1,11 +1,9 @@
1
1
  // 该loader用于将用户定义的messages注入到i18n.wxs中
2
- const getMainCompilation = require('../utils/get-main-compilation')
3
2
  const loaderUtils = require('loader-utils')
4
3
 
5
4
  module.exports = function (content) {
6
- const mainCompilation = getMainCompilation(this._compilation)
7
- const i18n = mainCompilation.__mpx__.i18n
8
- let prefix = 'var __mpx_messages__, __mpx_datetime_formats__, __mpx_number_formats__\n'
5
+ const i18n = this.getMpx().i18n
6
+ let prefix = 'var __mpx_messages__, __mpx_datetime_formats__, __mpx_number_formats__, __mpx_locale__\n'
9
7
  if (i18n) {
10
8
  if (i18n.messages) {
11
9
  prefix += `__mpx_messages__ = ${JSON.stringify(i18n.messages)}\n`
@@ -22,6 +20,9 @@ module.exports = function (content) {
22
20
  } else if (i18n.numberFormatsPath) {
23
21
  prefix += `__mpx_number_formats__ = require(${loaderUtils.stringifyRequest(this, i18n.numberFormatsPath)})\n`
24
22
  }
23
+ if (i18n.locale) {
24
+ prefix += `__mpx_locale__ = ${JSON.stringify(i18n.locale)}\n`
25
+ }
25
26
  }
26
27
  content = prefix + content
27
28
  return content
package/lib/wxs/loader.js CHANGED
@@ -1,29 +1,33 @@
1
1
  const NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin')
2
2
  const EntryPlugin = require('webpack/lib/EntryPlugin')
3
+ const LazySet = require('webpack/lib/util/LazySet')
3
4
  const LimitChunkCountPlugin = require('webpack/lib/optimize/LimitChunkCountPlugin')
5
+ const FlagEntryExportAsUsedPlugin = require('webpack/lib/FlagEntryExportAsUsedPlugin')
4
6
  const path = require('path')
5
7
  const WxsPlugin = require('./WxsPlugin')
6
- const RecordStaticResourceDependency = require('../dependencies/RecordStaticResourceDependency')
8
+ const RecordResourceMapDependency = require('../dependencies/RecordResourceMapDependency')
7
9
  const parseRequest = require('../utils/parse-request')
8
10
  const toPosix = require('../utils/to-posix')
9
11
  const fixRelative = require('../utils/fix-relative')
12
+ const addQuery = require('../utils/add-query')
10
13
  const config = require('../config')
11
14
 
12
- module.exports = function () {
15
+ module.exports = content => content
16
+
17
+ module.exports.pitch = function (remainingRequest) {
13
18
  const nativeCallback = this.async()
14
19
  const moduleGraph = this._compilation.moduleGraph
15
20
  const mpx = this.getMpx()
16
21
  const mode = mpx.mode
17
- const wxsMap = mpx.wxsMap
18
- const appInfo = mpx.appInfo
22
+ const getOutputPath = mpx.getOutputPath
19
23
  let { resourcePath, queryObj } = parseRequest(this.resource)
20
24
  const issuer = moduleGraph.getIssuer(this._module)
21
25
  const { resourcePath: issuerResourcePath, queryObj: issuerQueryObj } = parseRequest(queryObj.issuerResource || issuer.resource)
22
- const issuerPackageName = issuerQueryObj.packageRoot || mpx.currentPackageRoot || 'main'
26
+ const issuerPackageName = issuerQueryObj.packageRoot || 'main'
23
27
  const pagesMap = mpx.pagesMap
24
28
  const componentsMap = mpx.componentsMap[issuerPackageName]
25
29
  const staticResourcesMap = mpx.staticResourcesMap[issuerPackageName]
26
- const issuerName = issuerResourcePath === appInfo.resourcePath ? appInfo.name : (pagesMap[issuerResourcePath] || componentsMap[issuerResourcePath] || staticResourcesMap[issuerResourcePath])
30
+ const issuerName = pagesMap[issuerResourcePath] || componentsMap[issuerResourcePath] || staticResourcesMap[issuerResourcePath]
27
31
  const issuerDir = path.dirname(issuerName)
28
32
 
29
33
  const getName = (raw) => {
@@ -36,9 +40,9 @@ module.exports = function () {
36
40
  resourcePath = `${resourcePath}~${wxsModule}`
37
41
  }
38
42
  const packageRoot = queryObj.packageRoot || ''
39
- const name = path.parse(resourcePath).name + mpx.pathHash(resourcePath)
40
- const filename = toPosix(path.join(packageRoot, /^\.([^.]+)/.exec(config[mode].wxs.ext)[1], `${name}${config[mode].wxs.ext}`))
41
- this._module.addPresentationalDependency(new RecordStaticResourceDependency(resourcePath, filename, packageRoot))
43
+ const ext = config[mode].wxs.ext
44
+ const filename = toPosix(path.join(packageRoot, getOutputPath(resourcePath, ext.slice(1), { ext })))
45
+ this._module.addPresentationalDependency(new RecordResourceMapDependency(resourcePath, 'staticResource', filename, packageRoot))
42
46
 
43
47
  const callback = (err) => {
44
48
  if (err) return nativeCallback(err)
@@ -47,65 +51,92 @@ module.exports = function () {
47
51
  nativeCallback(null, `module.exports = ${JSON.stringify(relativePath)};`)
48
52
  }
49
53
 
50
- if (wxsMap[filename]) {
51
- wxsMap[filename].modules.push(this._module)
52
- return callback()
53
- }
54
+ // 清空issuerResource query避免文件内容输出报错并进行子编译缓存优化
55
+ const request = '!!' + addQuery(remainingRequest, {}, false, ['issuerResource'])
54
56
 
55
- wxsMap[filename] = {
56
- dep: null,
57
- modules: [this._module]
58
- }
57
+ // request中已经包含全量构成filename的信息,故可以直接使用request作为key来进行缓存
58
+ if (!mpx.wxsAssetsCache.has(request)) {
59
+ mpx.wxsAssetsCache.set(request, new Promise((resolve, reject) => {
60
+ const outputOptions = {
61
+ filename,
62
+ // 避免输出的wxs中包含es语法
63
+ environment: {
64
+ // The environment supports arrow functions ('() => { ... }').
65
+ arrowFunction: false,
66
+ // The environment supports BigInt as literal (123n).
67
+ bigIntLiteral: false,
68
+ // The environment supports const and let for variable declarations.
69
+ const: false,
70
+ // The environment supports destructuring ('{ a, b } = obj').
71
+ destructuring: false,
72
+ // The environment supports an async import() function to import EcmaScript modules.
73
+ dynamicImport: false,
74
+ // The environment supports 'for of' iteration ('for (const x of array) { ... }').
75
+ forOf: false,
76
+ // The environment supports ECMAScript Module syntax to import ECMAScript modules (import ... from '...').
77
+ module: false
78
+ }
79
+ }
59
80
 
60
- const outputOptions = {
61
- filename
62
- }
63
- // wxs文件必须经过pre-loader
64
- const request = '!!' + this.remainingRequest
65
- const plugins = [
66
- new WxsPlugin({ mode }),
67
- new NodeTargetPlugin(),
68
- new EntryPlugin(this.context, request, { name: getName(filename) }),
69
- new LimitChunkCountPlugin({ maxChunks: 1 })
70
- ]
81
+ const plugins = [
82
+ new WxsPlugin({ mode }),
83
+ new NodeTargetPlugin(),
84
+ new EntryPlugin(this.context, request, { name: getName(filename) }),
85
+ new LimitChunkCountPlugin({ maxChunks: 1 }),
86
+ new FlagEntryExportAsUsedPlugin(true, 'entry')
87
+ ]
71
88
 
72
- const childCompiler = this._compilation.createChildCompiler(resourcePath, outputOptions, plugins)
89
+ const childCompiler = this._compilation.createChildCompiler(resourcePath, outputOptions, plugins)
73
90
 
74
- // let entryModule
75
- // childCompiler.hooks.thisCompilation.tap('MpxWebpackPlugin ', (compilation) => {
76
- // compilation.hooks.succeedEntry.tap('MpxWebpackPlugin', (entry, name, module) => {
77
- // entryModule = module
78
- // // const dep = new ChildCompileDependency(entryModule)
79
- // // wxsMap[filename].dep = dep
80
- // })
81
- // })
91
+ let assets = []
82
92
 
83
- childCompiler.hooks.afterCompile.tap('MpxWebpackPlugin', (compilation) => {
84
- // 持久化缓存,使用module.buildInfo.assets来输出文件
85
- compilation.getAssets().forEach(({ name, source, info }) => {
86
- this.emitFile(name, source.source(), undefined, info)
87
- })
88
- compilation.clearAssets()
89
- })
93
+ childCompiler.hooks.afterCompile.tap('MpxWebpackPlugin', (compilation) => {
94
+ // 持久化缓存,使用module.buildInfo.assets来输出文件
95
+ assets = compilation.getAssets()
96
+ compilation.clearAssets()
97
+ })
90
98
 
91
- childCompiler.runAsChild((err, entries, compilation) => {
92
- if (err) return callback(err)
93
- if (compilation.errors.length > 0) {
94
- return callback(compilation.errors[0])
95
- }
99
+ childCompiler.runAsChild((err, entries, compilation) => {
100
+ if (err) return reject(err)
101
+ const fileDependencies = new LazySet()
102
+ const contextDependencies = new LazySet()
103
+ const missingDependencies = new LazySet()
104
+ const buildDependencies = new LazySet()
105
+ compilation.modules.forEach((module) => {
106
+ module.addCacheDependencies(
107
+ fileDependencies,
108
+ contextDependencies,
109
+ missingDependencies,
110
+ buildDependencies
111
+ )
112
+ })
113
+ resolve({
114
+ assets,
115
+ fileDependencies,
116
+ contextDependencies,
117
+ missingDependencies,
118
+ buildDependencies
119
+ })
120
+ })
121
+ }))
122
+ }
96
123
 
97
- compilation.fileDependencies.forEach((dep) => {
124
+ mpx.wxsAssetsCache.get(request).then(({ assets, fileDependencies, contextDependencies, missingDependencies, buildDependencies }) => {
125
+ assets.forEach(({ name, source, info }) => {
126
+ this.emitFile(name, source.source(), undefined, info)
127
+ })
128
+ fileDependencies.forEach((dep) => {
98
129
  this.addDependency(dep)
99
- }, this)
100
- compilation.contextDependencies.forEach((dep) => {
130
+ })
131
+ contextDependencies.forEach((dep) => {
101
132
  this.addContextDependency(dep)
102
- }, this)
103
- compilation.missingDependencies.forEach((dep) => {
133
+ })
134
+ missingDependencies.forEach((dep) => {
104
135
  this.addMissingDependency(dep)
105
136
  })
106
- compilation.buildDependencies.forEach((dep) => {
137
+ buildDependencies.forEach((dep) => {
107
138
  this.addBuildDependency(dep)
108
139
  })
109
140
  callback()
110
- })
141
+ }).catch(callback)
111
142
  }
@@ -1,22 +1,21 @@
1
- const babylon = require('babylon')
2
- const traverse = require('babel-traverse').default
3
- const t = require('babel-types')
4
- const generate = require('babel-generator').default
5
- const getMainCompilation = require('../utils/get-main-compilation')
1
+ const babylon = require('@babel/parser')
2
+ const traverse = require('@babel/traverse').default
3
+ const t = require('@babel/types')
4
+ const generate = require('@babel/generator').default
6
5
  const parseRequest = require('../utils/parse-request')
7
6
  const isEmptyObject = require('../utils/is-empty-object')
8
7
  const parseQuery = require('loader-utils').parseQuery
9
8
 
10
9
  module.exports = function (content) {
11
10
  this.cacheable()
12
- const mainCompilation = getMainCompilation(this._compilation)
11
+ const mpx = this.getMpx()
13
12
  const module = this._module
14
- const mode = mainCompilation.__mpx__.mode
13
+ const mode = mpx.mode
15
14
  const wxsModule = parseQuery(this.resourceQuery || '?').wxsModule
16
15
 
17
16
  // 处理内联wxs
18
17
  if (wxsModule) {
19
- const wxsContentMap = mainCompilation.__mpx__.wxsContentMap
18
+ const wxsContentMap = mpx.wxsContentMap
20
19
  const resourcePath = parseRequest(this.resource).resourcePath
21
20
  content = wxsContentMap[`${resourcePath}~${wxsModule}`] || content
22
21
  }
@@ -40,11 +39,25 @@ module.exports = function (content) {
40
39
  let results = targetPath.unshiftContainer('body', insertNodes) || []
41
40
  targetPath.inserted = true
42
41
  results.forEach((item) => {
43
- item.stop()
42
+ item.shouldStopTraverse = true
44
43
  })
45
44
  }
46
45
  }
47
46
  },
47
+ CallExpression (path) {
48
+ const callee = path.node.callee
49
+ if (t.isIdentifier(callee) && callee.name === 'getRegExp') {
50
+ const argPath = path.get('arguments')[0]
51
+ if (argPath.isStringLiteral()) {
52
+ argPath.replaceWith(t.stringLiteral(argPath.node.extra.raw.slice(1, -1)))
53
+ }
54
+ }
55
+ },
56
+ ForStatement (path) {
57
+ if (path.shouldStopTraverse) {
58
+ path.stop()
59
+ }
60
+ },
48
61
  // 处理vant-aliapp中export var bem = bem;这种不被acorn支持的2b语法
49
62
  ExportNamedDeclaration (path) {
50
63
  if (
@@ -88,12 +101,19 @@ module.exports = function (content) {
88
101
  },
89
102
  CallExpression (path) {
90
103
  const callee = path.node.callee
104
+ const args = path.node.arguments
91
105
  const transMap = {
92
106
  getDate: 'Date',
93
107
  getRegExp: 'RegExp'
94
108
  }
95
109
  if (t.isIdentifier(callee) && transMap[callee.name]) {
96
- path.replaceWith(t.newExpression(t.identifier(transMap[callee.name]), path.node.arguments))
110
+ if (callee.name === 'getRegExp') {
111
+ const arg = args[0]
112
+ if (t.isStringLiteral(arg)) {
113
+ args[0] = t.stringLiteral(arg.extra.raw.slice(1, -1))
114
+ }
115
+ }
116
+ path.replaceWith(t.newExpression(t.identifier(transMap[callee.name]), args))
97
117
  }
98
118
  }
99
119
  })
@@ -65,7 +65,7 @@ module.exports = function (content, map) {
65
65
  alreadyImported[imp.url] = true
66
66
  }
67
67
  return true
68
- }).map((imp) => {
68
+ }).map((imp, i) => {
69
69
  if (!isUrlRequest(imp.url, root, externals)) {
70
70
  return 'exports.push([module.id, ' +
71
71
  JSON.stringify('@import url(' + imp.url + ');') + ', ' +
@@ -73,9 +73,9 @@ module.exports = function (content, map) {
73
73
  } else {
74
74
  const requestString = getRequestString('styles', { src: imp.url }, {
75
75
  isStatic: true,
76
- issuerFile: mpx.getExtractedFile(this.resource),
76
+ issuerResource: this.resource,
77
77
  fromImport: true
78
- })
78
+ }, i)
79
79
  return 'exports.push([module.id, ' +
80
80
  JSON.stringify('@import "') +
81
81
  '+ require(' + requestString + ') +' +