@mpxjs/webpack-plugin 2.7.44 → 2.7.47

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.
package/lib/helpers.js CHANGED
@@ -16,6 +16,8 @@ module.exports = function createHelpers (loaderContext) {
16
16
  const rawRequest = loaderUtils.getRemainingRequest(loaderContext)
17
17
  const { resourcePath, queryObj } = parseRequest(loaderContext.resource)
18
18
 
19
+ const { mode, env } = loaderContext.getMpx() || {}
20
+
19
21
  function getRequire (type, part, extraOptions, index) {
20
22
  return 'require(' + getRequestString(type, part, extraOptions, index) + ')'
21
23
  }
@@ -66,7 +68,7 @@ module.exports = function createHelpers (loaderContext) {
66
68
  return loaderUtils.stringifyRequest(loaderContext, addQuery(src, options, true))
67
69
  } else {
68
70
  const fakeRequest = getFakeRequest(type, part)
69
- const request = `${selectorPath}!${addQuery(rawRequest, options, true)}`
71
+ const request = `${selectorPath}?mode=${mode}&env=${env}!${addQuery(rawRequest, options, true)}`
70
72
  return loaderUtils.stringifyRequest(loaderContext, `${fakeRequest}!=!${request}`)
71
73
  }
72
74
  }
package/lib/index.js CHANGED
@@ -537,7 +537,6 @@ class MpxWebpackPlugin {
537
537
  assetsASTsMap: new Map(),
538
538
  usingComponents: {},
539
539
  // todo es6 map读写性能高于object,之后会逐步替换
540
- vueContentCache: new Map(),
541
540
  wxsAssetsCache: new Map(),
542
541
  currentPackageRoot: '',
543
542
  wxsContentMap: {},
@@ -941,7 +940,7 @@ class MpxWebpackPlugin {
941
940
  }
942
941
  })
943
942
 
944
- normalModuleFactory.hooks.parser.for('javascript/auto').tap('MpxWebpackPlugin', (parser) => {
943
+ const normalModuleFactoryParserCallback = (parser) => {
945
944
  parser.hooks.call.for('__mpx_resolve_path__').tap('MpxWebpackPlugin', (expr) => {
946
945
  if (expr.arguments[0]) {
947
946
  const resource = expr.arguments[0].value
@@ -1152,7 +1151,10 @@ class MpxWebpackPlugin {
1152
1151
  parser.hooks.callMemberChain.for('mpx').tap('MpxWebpackPlugin', injectSrcModeForTransApi)
1153
1152
  parser.hooks.callMemberChain.for('wx').tap('MpxWebpackPlugin', injectSrcModeForTransApi)
1154
1153
  }
1155
- })
1154
+ }
1155
+ normalModuleFactory.hooks.parser.for('javascript/auto').tap('MpxWebpackPlugin', normalModuleFactoryParserCallback)
1156
+ normalModuleFactory.hooks.parser.for('javascript/dynamic').tap('MpxWebpackPlugin', normalModuleFactoryParserCallback)
1157
+ normalModuleFactory.hooks.parser.for('javascript/esm').tap('MpxWebpackPlugin', normalModuleFactoryParserCallback)
1156
1158
 
1157
1159
  // 为了正确生成sourceMap,将该步骤由原来的compile.hooks.emit迁移到compilation.hooks.processAssets
1158
1160
  compilation.hooks.processAssets.tap({
package/lib/loader.js CHANGED
@@ -46,7 +46,6 @@ module.exports = function (content) {
46
46
  const globalSrcMode = mpx.srcMode
47
47
  const localSrcMode = queryObj.mode
48
48
  const srcMode = localSrcMode || globalSrcMode
49
- const vueContentCache = mpx.vueContentCache
50
49
  const autoScope = matchCondition(resourcePath, mpx.autoScopeRules)
51
50
 
52
51
  let ctorType = 'app'
@@ -60,7 +59,7 @@ module.exports = function (content) {
60
59
 
61
60
  // 支持资源query传入isPage或isComponent支持页面/组件单独编译
62
61
  if (ctorType === 'app' && (queryObj.isComponent || queryObj.isPage)) {
63
- const entryName = getEntryName(this) || (queryObj.isComponent ? 'noEntryComponent' : 'noEntryPage')
62
+ const entryName = getEntryName(this) || mpx.getOutputPath(resourcePath, queryObj.isComponent ? 'component' : 'page')
64
63
  ctorType = queryObj.isComponent ? 'component' : 'page'
65
64
  this._module.addPresentationalDependency(new RecordResourceMapDependency(resourcePath, ctorType, entryName, packageRoot))
66
65
  }
@@ -94,10 +93,6 @@ module.exports = function (content) {
94
93
  })
95
94
  },
96
95
  (callback) => {
97
- // web输出模式下没有任何inject,可以通过cache直接返回,由于读取src json可能会新增模块依赖,需要在之后返回缓存内容
98
- if (vueContentCache.has(filePath)) {
99
- return callback(null, vueContentCache.get(filePath))
100
- }
101
96
  const hasScoped = parts.styles.some(({ scoped }) => scoped) || autoScope
102
97
  const templateAttrs = parts.template && parts.template.attrs
103
98
  const hasComment = templateAttrs && templateAttrs.comments
@@ -204,7 +199,6 @@ module.exports = function (content) {
204
199
  ], (err, scriptRes) => {
205
200
  if (err) return callback(err)
206
201
  output += scriptRes.output
207
- vueContentCache.set(filePath, output)
208
202
  callback(null, output)
209
203
  })
210
204
  }
package/lib/selector.js CHANGED
@@ -3,17 +3,17 @@ const parseRequest = require('./utils/parse-request')
3
3
 
4
4
  module.exports = function (content) {
5
5
  this.cacheable()
6
- // todo 移除mpx访问依赖,支持thread-loader
7
- const mpx = this.getMpx()
8
- if (!mpx) {
6
+
7
+ // 移除mpx访问依赖,支持 thread-loader
8
+ const { mode, env } = this.getOptions() || {}
9
+ if (!mode && !env) {
9
10
  return content
10
11
  }
12
+
11
13
  const { queryObj } = parseRequest(this.resource)
12
14
  const ctorType = queryObj.ctorType
13
15
  const type = queryObj.type
14
16
  const index = queryObj.index || 0
15
- const mode = mpx.mode
16
- const env = mpx.env
17
17
  const filePath = this.resourcePath
18
18
  const parts = parseComponent(content, {
19
19
  filePath,
@@ -2,6 +2,7 @@ const genComponentTag = require('../utils/gen-component-tag')
2
2
  const loaderUtils = require('loader-utils')
3
3
  const addQuery = require('../utils/add-query')
4
4
  const normalize = require('../utils/normalize')
5
+ const parseRequest = require('../utils/parse-request')
5
6
  const optionProcessorPath = normalize.lib('runtime/optionProcessor')
6
7
  const tabBarContainerPath = normalize.lib('runtime/components/web/mpx-tab-bar-container.vue')
7
8
  const tabBarPath = normalize.lib('runtime/components/web/mpx-tab-bar.vue')
@@ -49,6 +50,7 @@ module.exports = function (script, {
49
50
  projectRoot
50
51
  } = mpx
51
52
 
53
+ const { queryObj } = parseRequest(loaderContext.resource)
52
54
  const tabBar = jsonConfig.tabBar
53
55
 
54
56
  const emitWarning = (msg) => {
@@ -219,7 +221,8 @@ module.exports = function (script, {
219
221
  // 为了正确获取currentSrcMode便于运行时进行转换,对于src引入的组件script采用require方式引入(由于webpack会将import的执行顺序上升至最顶),这意味着对于src引入脚本中的named export将不会生效,不过鉴于mpx和小程序中本身也没有在组件script中声明export的用法,所以应该没有影响
220
222
  content += '\n\n\n/** Source start **/\n'
221
223
  content += script.src
222
- ? `require(${stringifyRequest(script.src)})\n`
224
+ // 继承单文件组件query避免多个单文件模块实例引用一个src模块,因模块缓存导致createComponent不执行的问题
225
+ ? `require(${stringifyRequest(addQuery(script.src, queryObj))})\n`
223
226
  : script.content
224
227
  content += '\n/** Source end **/\n\n\n'
225
228
  // createApp/Page/Component执行完成后立刻获取当前的option并暂存
@@ -23,6 +23,14 @@ class WxsPlugin {
23
23
  normalModuleFactory.hooks.parser
24
24
  .for('javascript/auto')
25
25
  .tap('WxsPlugin', handler)
26
+
27
+ normalModuleFactory.hooks.parser
28
+ .for('javascript/dynamic')
29
+ .tap('WxsPlugin', handler)
30
+
31
+ normalModuleFactory.hooks.parser
32
+ .for('javascript/esm')
33
+ .tap('WxsPlugin', handler)
26
34
  })
27
35
  }
28
36
  }
@@ -1,6 +1,49 @@
1
1
  const config = require('../config')
2
2
  const { ConcatSource } = require('webpack').sources
3
3
  const JavascriptModulesPlugin = require('webpack/lib/javascript/JavascriptModulesPlugin')
4
+ const RuntimeGlobals = require('webpack/lib/RuntimeGlobals')
5
+ const HelperRuntimeModule = require('webpack/lib/runtime/HelperRuntimeModule')
6
+ const Template = require('webpack/lib/Template')
7
+
8
+ class MakeNamespaceObjectRuntimeModule extends HelperRuntimeModule {
9
+ constructor () {
10
+ super('make namespace object')
11
+ }
12
+
13
+ generate () {
14
+ const { runtimeTemplate } = this.compilation
15
+ const fn = RuntimeGlobals.makeNamespaceObject
16
+ return Template.asString([
17
+ '// define __esModule on exports',
18
+ `${fn} = ${runtimeTemplate.basicFunction('exports', [
19
+ 'exports.__esModule = true;'
20
+ ])};`
21
+ ])
22
+ }
23
+ }
24
+
25
+ class CompatGetDefaultExportRuntimeModule extends HelperRuntimeModule {
26
+ constructor () {
27
+ super('compat get default export')
28
+ }
29
+
30
+ generate () {
31
+ const { runtimeTemplate } = this.compilation
32
+ const fn = RuntimeGlobals.compatGetDefaultExport
33
+ return Template.asString([
34
+ '// getDefaultExport function for compatibility with non-harmony modules',
35
+ `${fn} = ${runtimeTemplate.basicFunction('module', [
36
+ 'var getter = module && module.__esModule ?',
37
+ Template.indent([
38
+ `${runtimeTemplate.returningFunction('module["default"]')} :`,
39
+ `${runtimeTemplate.returningFunction('module')};`
40
+ ]),
41
+ 'getter.a = getter();',
42
+ 'return getter;'
43
+ ])};`
44
+ ])
45
+ }
46
+ }
4
47
 
5
48
  module.exports = class WxsTemplatePlugin {
6
49
  constructor (options = { mode: 'wx' }) {
@@ -20,7 +63,34 @@ module.exports = class WxsTemplatePlugin {
20
63
  return new ConcatSource(prefix, source)
21
64
  })
22
65
 
23
- // todo webpack5的新的代码生成模式下完美支持.d.r.n的成本较高,暂不处理,wxs暂时只支持wx源码形式
66
+ // __webpack_require__.r
67
+ compilation.hooks.runtimeRequirementInTree
68
+ .for(RuntimeGlobals.makeNamespaceObject)
69
+ .tap({
70
+ name: 'WxsTemplatePlugin',
71
+ stage: -1000
72
+ }, chunk => {
73
+ compilation.addRuntimeModule(
74
+ chunk,
75
+ new MakeNamespaceObjectRuntimeModule()
76
+ )
77
+ return true
78
+ })
79
+
80
+ // __webpack_require__.n
81
+ compilation.hooks.runtimeRequirementInTree
82
+ .for(RuntimeGlobals.compatGetDefaultExport)
83
+ .tap({
84
+ name: 'WxsTemplatePlugin',
85
+ stage: -1000
86
+ }, chunk => {
87
+ compilation.addRuntimeModule(
88
+ chunk,
89
+ new CompatGetDefaultExportRuntimeModule()
90
+ )
91
+ return true
92
+ })
93
+
24
94
  // mainTemplate.hooks.requireExtensions.tap(
25
95
  // 'WxsMainTemplatePlugin',
26
96
  // () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/webpack-plugin",
3
- "version": "2.7.44",
3
+ "version": "2.7.47",
4
4
  "description": "mpx compile core",
5
5
  "keywords": [
6
6
  "mpx"
@@ -80,5 +80,5 @@
80
80
  "engines": {
81
81
  "node": ">=14.14.0"
82
82
  },
83
- "gitHead": "306aee23bc55c7f1af58b8de0c6e6394370c88d4"
83
+ "gitHead": "05354d5750021d76f4bf0ae2a4ab73fb2f8ef83b"
84
84
  }