@mpxjs/webpack-plugin 2.7.2 → 2.7.6

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.
@@ -117,8 +117,11 @@ class DynamicEntryDependency extends NullDependency {
117
117
 
118
118
  DynamicEntryDependency.Template = class DynamicEntryDependencyTemplate {
119
119
  apply (dep, source) {
120
- const { resultPath, range, key } = dep
121
- if (resultPath) {
120
+ const { resultPath, range, key, outputPath } = dep
121
+ if (outputPath === 'custom-tab-bar/index') {
122
+ // replace with true for custom-tab-bar
123
+ source.replace(range[0], range[1] - 1, 'true')
124
+ } else if (resultPath) {
122
125
  source.replace(range[0], range[1] - 1, JSON.stringify(resultPath))
123
126
  } else {
124
127
  const replaceRange = `mpx_replace_path_${key}`
package/lib/extractor.js CHANGED
@@ -16,7 +16,7 @@ module.exports.pitch = async function (remainingRequest) {
16
16
  const type = queryObj.type
17
17
  const index = queryObj.index || 0
18
18
  const isStatic = queryObj.isStatic
19
- const issuerFile = queryObj.issuerFile
19
+ const issuerResource = queryObj.issuerResource
20
20
  const fromImport = queryObj.fromImport
21
21
  const needBabel = queryObj.needBabel
22
22
 
@@ -82,7 +82,8 @@ module.exports.pitch = async function (remainingRequest) {
82
82
  // styles为static就两种情况,一种是.mpx中使用src引用样式,第二种为css-loader中处理@import
83
83
  // 为了支持持久化缓存,.mpx中使用src引用样式对issueFile asset产生的副作用迁移到ExtractDependency中处理
84
84
  case 'styles':
85
- if (issuerFile) {
85
+ if (issuerResource) {
86
+ const issuerFile = mpx.getExtractedFile(issuerResource)
86
87
  let relativePath = toPosix(path.relative(path.dirname(issuerFile), file))
87
88
  relativePath = fixRelative(relativePath, mode)
88
89
  if (fromImport) {
@@ -92,7 +93,8 @@ module.exports.pitch = async function (remainingRequest) {
92
93
  skipEmit: true,
93
94
  extractedInfo: {
94
95
  content: `@import "${relativePath}";\n`,
95
- index: -1
96
+ index,
97
+ pre: true
96
98
  }
97
99
  })
98
100
  }
package/lib/index.js CHANGED
@@ -279,7 +279,7 @@ class MpxWebpackPlugin {
279
279
 
280
280
  const addModePlugin = new AddModePlugin('before-file', this.options.mode, this.options.fileConditionRules, 'file')
281
281
  const addEnvPlugin = new AddEnvPlugin('before-file', this.options.env, this.options.fileConditionRules, 'file')
282
- const packageEntryPlugin = new PackageEntryPlugin('before-described-relative', this.options.miniNpmPackages, 'resolve')
282
+ const packageEntryPlugin = new PackageEntryPlugin('before-file', this.options.miniNpmPackages, 'file')
283
283
  if (Array.isArray(compiler.options.resolve.plugins)) {
284
284
  compiler.options.resolve.plugins.push(addModePlugin)
285
285
  } else {
@@ -838,6 +838,23 @@ class MpxWebpackPlugin {
838
838
  mpx.assetsModulesMap.set(filename, modules)
839
839
  })
840
840
 
841
+ const fillExtractedAssetsMap = (assetsMap, { index, content }, filename) => {
842
+ if (assetsMap.has(index)) {
843
+ if (assetsMap.get(index) !== content) {
844
+ compilation.errors.push(new Error(`The extracted file [${filename}] is filled with same index [${index}] and different content:
845
+ old content: ${assetsMap.get(index)}
846
+ new content: ${content}
847
+ please check!`))
848
+ }
849
+ } else {
850
+ assetsMap.set(index, content)
851
+ }
852
+ }
853
+
854
+ const sortExtractedAssetsMap = (assetsMap) => {
855
+ return [...assetsMap.entries()].sort((a, b) => a[0] - b[0]).map(item => item[1])
856
+ }
857
+
841
858
  compilation.hooks.beforeModuleAssets.tap('MpxWebpackPlugin', () => {
842
859
  const extractedAssetsMap = new Map()
843
860
  for (const module of compilation.modules) {
@@ -846,19 +863,19 @@ class MpxWebpackPlugin {
846
863
  if (extractedInfo) {
847
864
  let extractedAssets = extractedAssetsMap.get(filename)
848
865
  if (!extractedAssets) {
849
- extractedAssets = []
866
+ extractedAssets = [new Map(), new Map()]
850
867
  extractedAssetsMap.set(filename, extractedAssets)
851
868
  }
852
- extractedAssets.push(extractedInfo)
869
+ fillExtractedAssetsMap(extractedInfo.pre ? extractedAssets[0] : extractedAssets[1], extractedInfo, filename)
853
870
  compilation.hooks.moduleAsset.call(module, filename)
854
871
  }
855
872
  }
856
873
  }
857
874
 
858
- for (const [filename, extractedAssets] of extractedAssetsMap) {
859
- const sortedExtractedAssets = extractedAssets.sort((a, b) => a.index - b.index)
875
+ for (const [filename, [pre, normal]] of extractedAssetsMap) {
876
+ const sortedExtractedAssets = [...sortExtractedAssetsMap(pre), ...sortExtractedAssetsMap(normal)]
860
877
  const source = new ConcatSource()
861
- sortedExtractedAssets.forEach(({ content }) => {
878
+ sortedExtractedAssets.forEach((content) => {
862
879
  if (content) {
863
880
  // 处理replace path
864
881
  if (/"mpx_replace_path_.*?"/.test(content)) {
@@ -1344,6 +1361,7 @@ try {
1344
1361
  const fs = compiler.intermediateFileSystem
1345
1362
  const cacheLocation = compiler.options.cache.cacheLocation
1346
1363
  return new Promise((resolve) => {
1364
+ if (!cacheLocation) return resolve()
1347
1365
  if (typeof fs.rm === 'function') {
1348
1366
  fs.rm(cacheLocation, {
1349
1367
  recursive: true,
@@ -474,11 +474,12 @@ module.exports = function (content) {
474
474
 
475
475
  const processCustomTabBar = (tabBar, context, callback) => {
476
476
  if (tabBar && tabBar.custom) {
477
- processComponent('./custom-tab-bar/index', context, { outputPath: 'custom-tab-bar/index' }, (err) => {
477
+ processComponent('./custom-tab-bar/index', context, { outputPath: 'custom-tab-bar/index' }, (err, entry) => {
478
478
  if (err === RESOLVE_IGNORED_ERR) {
479
479
  delete tabBar.custom
480
480
  return callback()
481
481
  }
482
+ tabBar.custom = entry // hack for javascript parser call hook.
482
483
  callback(err)
483
484
  })
484
485
  } else {
package/lib/loader.js CHANGED
@@ -290,7 +290,7 @@ module.exports = function (content) {
290
290
  ...style.src ? {
291
291
  ...queryObj,
292
292
  isStatic: true,
293
- issuerFile: mpx.getExtractedFile(addQuery(this.resource, { type: 'styles' }, true))
293
+ issuerResource: addQuery(this.resource, { type: 'styles' }, true)
294
294
  } : null,
295
295
  moduleId,
296
296
  scoped
@@ -1,55 +1,42 @@
1
1
  const path = require('path')
2
- /**
3
- * @desc 获取小程序npm包资源入口目录
4
- */
5
- const getEntry = (name, miniprogram) => {
6
- return path.join(name, miniprogram)
7
- }
2
+ const toPosix = require('../utils/to-posix')
8
3
 
9
4
  module.exports = class PackageEntryPlugin {
10
- constructor (source, miniNpmPackage, target) {
5
+ constructor (source, miniNpmPackages, target) {
11
6
  this.source = source
12
7
  this.target = target
13
- this.miniNpmPackage = miniNpmPackage
8
+ this.miniNpmPackages = miniNpmPackages
14
9
  }
15
10
 
16
11
  /**
17
12
  * 判断是否需要更改innerRequest
18
13
  * 小程序发布npm包约束: package.json配置miniprogram 或默认 miniprogram_dist目录
19
- * 0. 前提: request中含有package.json中name字段
20
14
  * 1. package.json中配置了miniprogram, 且request中不含miniprogram,尝试拼接
21
- * 2. 用户配置miniNpmPackage说明是小程序npm包,如果package.json中没配置miniprogram字段,则尝试拼接默认miniprogram_dist目录
15
+ * 2. 用户配置miniNpmPackages说明是小程序npm包,如果package.json中没配置miniprogram字段,则尝试拼接默认miniprogram_dist目录
22
16
  */
23
17
  apply (resolver) {
24
18
  const target = resolver.ensureHook(this.target)
25
19
  resolver.getHook(this.source).tapAsync('PackagePlugin', (request, resolveContext, callback) => {
26
- const innerRequest = request.request || request.path
27
- if (!innerRequest || request.miniprogram) return callback()
20
+ if (request.miniprogram) return callback()
21
+ let { path: resourcePath, descriptionFileData, descriptionFileRoot } = request
22
+ if (request.miniprogram || !descriptionFileData) return callback()
23
+
24
+ let { name, miniprogram } = descriptionFileData
25
+ if (!miniprogram && this.miniNpmPackages.includes(name)) miniprogram = 'miniprogram_dist'
26
+ if (!miniprogram) return callback()
27
+
28
+ let relativePath = path.relative(descriptionFileRoot, resourcePath)
29
+ if (relativePath.startsWith(miniprogram)) return callback()
30
+
31
+ relativePath = path.join(miniprogram, relativePath)
28
32
 
29
- const descriptionFileData = request.descriptionFileData || {}
30
- const { name = '', miniprogram } = descriptionFileData
31
- let newEntry = ''
32
- // request.path和系统环境有关:windows和linux
33
- let normalizedName = path.normalize(name)
34
- if (innerRequest.indexOf(normalizedName) === -1) {
35
- return callback()
36
- }
37
- if (miniprogram) {
38
- newEntry = getEntry(normalizedName, miniprogram)
39
- } else if (this.miniNpmPackage.includes(name)) {
40
- newEntry = getEntry(normalizedName, 'miniprogram_dist')
41
- }
33
+ const obj = Object.assign({}, request, {
34
+ path: path.join(descriptionFileRoot, relativePath),
35
+ relativePath: './' + toPosix(relativePath),
36
+ miniprogram: true
37
+ })
42
38
 
43
- if (newEntry) {
44
- const newRequest = innerRequest.replace(normalizedName, newEntry)
45
- const obj = Object.assign({}, request, {
46
- request: newRequest,
47
- miniprogram: true
48
- })
49
- resolver.doResolve(target, obj, `change request ${innerRequest} to :` + newRequest, resolveContext, callback)
50
- } else {
51
- callback()
52
- }
39
+ resolver.doResolve(target, obj, 'add miniprogram dist: ' + miniprogram, resolveContext, callback)
53
40
  })
54
41
  }
55
42
  }
@@ -85,6 +85,10 @@
85
85
  animation: {
86
86
  type: Boolean,
87
87
  default: true
88
+ },
89
+ speed: {
90
+ type: Number,
91
+ default: 1000
88
92
  }
89
93
  },
90
94
  watch: {
@@ -96,7 +100,7 @@
96
100
  if (newVal < this.bs.maxScrollX) {
97
101
  newVal = this.bs.maxScrollX
98
102
  }
99
- this.bs.scrollTo(newVal, this.bs.y)
103
+ this.bs.scrollTo(newVal, this.bs.y, this.speed)
100
104
  },
101
105
  y (newVal) {
102
106
  this.source = ''
@@ -106,7 +110,7 @@
106
110
  if (newVal < this.bs.maxScrollY) {
107
111
  newVal = this.bs.maxScrollY
108
112
  }
109
- this.bs.scrollTo(this.bs.x, newVal)
113
+ this.bs.scrollTo(this.bs.x, newVal, this.speed)
110
114
  },
111
115
  scaleValue (newVal) {
112
116
  this.isZooming = true
@@ -1,29 +1,32 @@
1
1
  const babylon = require('@babel/parser')
2
2
  const t = require('@babel/types')
3
+ const traverse = require('@babel/traverse').default
3
4
  const generate = require('@babel/generator').default
4
5
 
5
6
  module.exports = function transDynamicClassExpr (expr, { error } = {}) {
6
7
  try {
7
- const ast = babylon.parseExpression(expr, {
8
+ const ast = babylon.parse(expr, {
8
9
  plugins: [
9
10
  'objectRestSpread'
10
11
  ]
11
12
  })
12
- if (t.isObjectExpression(ast)) {
13
- ast.properties.forEach((property) => {
14
- if (t.isObjectProperty(property) && !property.computed) {
15
- const propertyName = property.key.name || property.key.value
16
- if (/-/.test(propertyName)) {
17
- if (/\$/.test(propertyName)) {
18
- error && error(`Dynamic classname [${propertyName}] is not supported, which includes [-] char and [$] char at the same time.`)
19
- } else {
20
- property.key = t.identifier(propertyName.replace(/-/g, '$$') + 'MpxDash')
13
+ traverse(ast, {
14
+ ObjectExpression (path) {
15
+ path.node.properties.forEach((property) => {
16
+ if (t.isObjectProperty(property) && !property.computed) {
17
+ const propertyName = property.key.name || property.key.value
18
+ if (/-/.test(propertyName)) {
19
+ if (/\$/.test(propertyName)) {
20
+ error && error(`Dynamic classname [${propertyName}] is not supported, which includes [-] char and [$] char at the same time.`)
21
+ } else {
22
+ property.key = t.identifier(propertyName.replace(/-/g, '$$') + 'MpxDash')
23
+ }
21
24
  }
22
25
  }
23
- }
24
- })
25
- }
26
- return generate(ast, {
26
+ })
27
+ }
28
+ })
29
+ return generate(ast.program.body[0].expression, {
27
30
  compact: true
28
31
  }).code
29
32
  } catch (e) {
@@ -99,7 +99,7 @@ module.exports = function (content) {
99
99
  case config[mode].wxs.tag:
100
100
  // 显式传递issuerResource避免模块缓存以及提供给wxs-loader计算相对路径
101
101
  extraOptions = {
102
- issuerFile: mpx.getExtractedFile(this.resource),
102
+ issuerResource: this.resource,
103
103
  isStatic: true
104
104
  }
105
105
  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
  }
@@ -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 + ') +' +
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/webpack-plugin",
3
- "version": "2.7.2",
3
+ "version": "2.7.6",
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": "eef4ac033679bdc5cb51ed1ddca08bd1d54bfdd2"
83
+ "gitHead": "2005cbdd935b0f7a193d41e7bb3d12c4a5f62e96"
84
84
  }