@mpxjs/webpack-plugin 2.7.2 → 2.7.3

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/extractor.js CHANGED
@@ -92,7 +92,8 @@ module.exports.pitch = async function (remainingRequest) {
92
92
  skipEmit: true,
93
93
  extractedInfo: {
94
94
  content: `@import "${relativePath}";\n`,
95
- index: -1
95
+ index,
96
+ pre: true
96
97
  }
97
98
  })
98
99
  }
package/lib/index.js CHANGED
@@ -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,
@@ -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 + ');') + ', ' +
@@ -75,7 +75,7 @@ module.exports = function (content, map) {
75
75
  isStatic: true,
76
76
  issuerFile: mpx.getExtractedFile(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.3",
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": "0cb7ce760316aa22f7c96ba8561a96f3eab2b8d6"
84
84
  }