@mpxjs/webpack-plugin 2.7.1 → 2.7.5

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
@@ -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
@@ -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,
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
@@ -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
@@ -49,7 +49,7 @@ module.exports = ({ id }) => {
49
49
  value: id
50
50
  }))
51
51
  })
52
- }).process(node.selector).result
52
+ }).processSync(node.selector)
53
53
  })
54
54
 
55
55
  // If keyframes are found in this <style>, find and rewrite animation names
@@ -19,7 +19,7 @@ module.exports = ({ id }) => {
19
19
  }
20
20
  })
21
21
  })
22
- }).process(node.selector).result
22
+ }).processSync(node.selector)
23
23
  })
24
24
  }
25
25
  }
@@ -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.1",
3
+ "version": "2.7.5",
4
4
  "description": "mpx compile core",
5
5
  "keywords": [
6
6
  "mpx"
@@ -32,7 +32,7 @@
32
32
  "consolidate": "^0.15.1",
33
33
  "css": "^2.2.1",
34
34
  "css-selector-tokenizer": "^0.7.0",
35
- "cssnano": "^3.10.0",
35
+ "cssnano": "^5.0.16",
36
36
  "de-indent": "^1.0.2",
37
37
  "fastparse": "^1.1.1",
38
38
  "hash-sum": "^1.0.2",
@@ -80,5 +80,5 @@
80
80
  "engines": {
81
81
  "node": ">=14.14.0"
82
82
  },
83
- "gitHead": "a672cdbf1689bb4d7f1a0fbcb1037d9f99e5d015"
83
+ "gitHead": "84d834c2ace18403bb1d4beb8585a1bfaca8e509"
84
84
  }