@mpxjs/webpack-plugin 2.7.20 → 2.7.27

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/index.js CHANGED
@@ -22,6 +22,7 @@ const ExternalsPlugin = require('webpack/lib/ExternalsPlugin')
22
22
  const AddModePlugin = require('./resolver/AddModePlugin')
23
23
  const AddEnvPlugin = require('./resolver/AddEnvPlugin')
24
24
  const PackageEntryPlugin = require('./resolver/PackageEntryPlugin')
25
+ const FixDescriptionInfoPlugin = require('./resolver/FixDescriptionInfoPlugin')
25
26
  // const CommonJsRequireDependency = require('webpack/lib/dependencies/CommonJsRequireDependency')
26
27
  // const HarmonyImportSideEffectDependency = require('webpack/lib/dependencies/HarmonyImportSideEffectDependency')
27
28
  // const RequireHeaderDependency = require('webpack/lib/dependencies/RequireHeaderDependency')
@@ -302,6 +303,7 @@ class MpxWebpackPlugin {
302
303
  compiler.options.resolve.plugins.push(addEnvPlugin)
303
304
  }
304
305
  compiler.options.resolve.plugins.push(packageEntryPlugin)
306
+ compiler.options.resolve.plugins.push(new FixDescriptionInfoPlugin())
305
307
 
306
308
  let splitChunksPlugin
307
309
  let splitChunksOptions
@@ -789,14 +791,25 @@ class MpxWebpackPlugin {
789
791
  }
790
792
  }
791
793
  } else if (independent) {
792
- // ContextModule/RawModuleExternalModule等只在独立分包的情况下添加分包标记,其余默认不添加
793
- const postfix = `|independent=${independent}|${currentPackageRoot}`
794
- const rawIdentifier = module.identifier
795
- if (rawIdentifier) {
796
- module.identifier = () => {
797
- return rawIdentifier.call(module) + postfix
794
+ // ContextModule/RawModule/ExternalModule等只在独立分包的情况下添加分包标记,其余默认不添加
795
+ const hackModuleIdentifier = (module) => {
796
+ const postfix = `|independent=${independent}|${currentPackageRoot}`
797
+ const rawIdentifier = module.identifier
798
+ if (rawIdentifier && !rawIdentifier.__mpxHacked) {
799
+ module.identifier = () => {
800
+ return rawIdentifier.call(module) + postfix
801
+ }
802
+ module.identifier.__mpxHacked = true
798
803
  }
799
804
  }
805
+ hackModuleIdentifier(module)
806
+ const rawCallback = callback
807
+ callback = (err, module) => {
808
+ // 因为文件缓存的存在,前面hack identifier的行为对于从文件缓存中创建得到的module并不生效,因此需要在回调中进行二次hack处理
809
+ if (err) return rawCallback(err)
810
+ hackModuleIdentifier(module)
811
+ rawCallback(null, module)
812
+ }
800
813
  }
801
814
  return rawAddModule.call(compilation, module, callback)
802
815
  }
@@ -12,7 +12,7 @@ module.exports = function createJSONHelper ({ loaderContext, emitWarning, custom
12
12
  const resolveMode = mpx.resolveMode
13
13
  const externals = mpx.externals
14
14
  const root = mpx.projectRoot
15
- const publicPath = loaderContext._compilation?.outputOptions.publicPath || ''
15
+ const publicPath = (loaderContext._compilation && loaderContext._compilation.outputOptions.publicPath) || ''
16
16
  const pathHash = mpx.pathHash
17
17
  const getOutputPath = mpx.getOutputPath
18
18
  const mode = mpx.mode
@@ -0,0 +1,28 @@
1
+ const path = require('path')
2
+
3
+ module.exports = class FixDescriptionInfoPlugin {
4
+ apply (resolver) {
5
+ resolver.hooks.result.tap('FixDescriptionInfoPlugin', (request) => {
6
+ const { path: resourcePath } = request
7
+ const segments = resourcePath.split(path.sep)
8
+ let rootIndex = -1
9
+ for (let i = segments.length - 1; i >= 0; i--) {
10
+ const segment = segments[i]
11
+ if (segment === 'node_modules') {
12
+ rootIndex = segments[i + 1].startsWith('@') ? i + 2 : i + 1
13
+ break
14
+ }
15
+ }
16
+ if (rootIndex !== -1) {
17
+ const descriptionFileRoot = segments.slice(0, rootIndex + 1).join(path.sep)
18
+ const descriptionFilePath = path.join(descriptionFileRoot, 'package.json')
19
+ if (descriptionFilePath !== request.descriptionFilePath) {
20
+ Object.assign(request, {
21
+ descriptionFileRoot,
22
+ descriptionFilePath
23
+ })
24
+ }
25
+ }
26
+ })
27
+ }
28
+ }
@@ -16,7 +16,7 @@ module.exports = class PackageEntryPlugin {
16
16
  */
17
17
  apply (resolver) {
18
18
  const target = resolver.ensureHook(this.target)
19
- resolver.getHook(this.source).tapAsync('PackagePlugin', (request, resolveContext, callback) => {
19
+ resolver.getHook(this.source).tapAsync('PackageEntryPlugin', (request, resolveContext, callback) => {
20
20
  if (request.miniprogram) return callback()
21
21
  let { path: resourcePath, descriptionFileData, descriptionFileRoot } = request
22
22
  if (request.miniprogram || !descriptionFileData) return callback()
@@ -22,6 +22,7 @@ module.exports = function evalJSONJS (source, filename, loaderContext) {
22
22
  request = path.join(dirname, request)
23
23
  }
24
24
  const filename = require.resolve(request)
25
+ loaderContext.addDependency(filename)
25
26
  const source = fs.readFileSync(filename).toString('utf-8')
26
27
  return evalJSONJS(source, filename, loaderContext)
27
28
  }, filename, dirname, ...defValues)
@@ -222,7 +222,7 @@ module.exports = function (json, {
222
222
  loaderContext._module && loaderContext._module.addPresentationalDependency(new RecordResourceMapDependency(resourcePath, 'page', outputPath))
223
223
  localPagesMap[outputPath] = {
224
224
  resource: addQuery(resource, { isPage: true }),
225
- async: tarRoot || queryObj.async,
225
+ async: queryObj.async || tarRoot,
226
226
  isFirst
227
227
  }
228
228
  callback()
@@ -20,6 +20,13 @@ function shallowStringify (obj) {
20
20
  return `{${arr.join(',')}}`
21
21
  }
22
22
 
23
+ function getAsyncChunkName (chunkName) {
24
+ if (chunkName && typeof chunkName !== 'boolean') {
25
+ return `/* webpackChunkName: "${chunkName}" */`
26
+ }
27
+ return ''
28
+ }
29
+
23
30
  module.exports = function (script, {
24
31
  loaderContext,
25
32
  ctorType,
@@ -62,7 +69,7 @@ module.exports = function (script, {
62
69
  if (pageCfg) {
63
70
  const pageRequest = stringifyRequest(pageCfg.resource)
64
71
  if (pageCfg.async) {
65
- tabBarPagesMap[pagePath] = `()=>import(${pageRequest}).then(res => getComponent(res, { __mpxPageRoute: ${JSON.stringify(pagePath)} }))`
72
+ tabBarPagesMap[pagePath] = `()=>import(${getAsyncChunkName(pageCfg.async)}${pageRequest}).then(res => getComponent(res, { __mpxPageRoute: ${JSON.stringify(pagePath)} }))`
66
73
  } else {
67
74
  tabBarPagesMap[pagePath] = `getComponent(require(${pageRequest}), { __mpxPageRoute: ${JSON.stringify(pagePath)} })`
68
75
  }
@@ -177,7 +184,7 @@ module.exports = function (script, {
177
184
  pagesMap[pagePath] = `getComponent(require(${stringifyRequest(tabBarContainerPath)}), { __mpxBuiltIn: true })`
178
185
  } else {
179
186
  if (pageCfg.async) {
180
- pagesMap[pagePath] = `()=>import(${pageRequest}).then(res => getComponent(res, { __mpxPageRoute: ${JSON.stringify(pagePath)} }))`
187
+ pagesMap[pagePath] = `()=>import(${getAsyncChunkName(pageCfg.async)} ${pageRequest}).then(res => getComponent(res, { __mpxPageRoute: ${JSON.stringify(pagePath)} }))`
181
188
  } else {
182
189
  // 为了保持小程序中app->page->component的js执行顺序,所有的page和component都改为require引入
183
190
  pagesMap[pagePath] = `getComponent(require(${pageRequest}), { __mpxPageRoute: ${JSON.stringify(pagePath)} })`
@@ -193,7 +200,7 @@ module.exports = function (script, {
193
200
  const componentCfg = localComponentsMap[componentName]
194
201
  const componentRequest = stringifyRequest(componentCfg.resource)
195
202
  if (componentCfg.async) {
196
- componentsMap[componentName] = `()=>import(${componentRequest}).then(res => getComponent(res))`
203
+ componentsMap[componentName] = `()=>import(${getAsyncChunkName(componentCfg.async)}${componentRequest}).then(res => getComponent(res))`
197
204
  } else {
198
205
  componentsMap[componentName] = `getComponent(require(${componentRequest}))`
199
206
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/webpack-plugin",
3
- "version": "2.7.20",
3
+ "version": "2.7.27",
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": "b4a3fea64b6a0135989ce9b3b26ccb24318b8027"
83
+ "gitHead": "8404ec35695a6260eee41bfa80f4655dc1ce948b"
84
84
  }