@mpxjs/webpack-plugin 2.7.19 → 2.7.25

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
  }
@@ -951,6 +964,9 @@ class MpxWebpackPlugin {
951
964
  parser.state.current.addDependency(dep)
952
965
  }
953
966
  return true
967
+ } else {
968
+ compilation.errors.push(new Error(`The require async JS [${request}] need to declare subpackage name by root`))
969
+ return true
954
970
  }
955
971
  }
956
972
  }
@@ -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
@@ -96,6 +96,7 @@ module.exports = function (content) {
96
96
  } else {
97
97
  fs.readFile(file, (err, content) => {
98
98
  if (err) return callback(err)
99
+ if (!this._compilation) return callback()
99
100
  let targetPath = path.relative(context, file)
100
101
  this._compilation.assets[targetPath] = {
101
102
  size: function size () {
@@ -350,7 +351,7 @@ module.exports = function (content) {
350
351
  }
351
352
 
352
353
  const recordIndependent = (root, request) => {
353
- this._module.addPresentationalDependency(new RecordIndependentDependency(root, request))
354
+ this._module && this._module.addPresentationalDependency(new RecordIndependentDependency(root, request))
354
355
  }
355
356
 
356
357
  const processIndependent = (otherConfig, context, tarRoot, callback) => {
@@ -181,9 +181,36 @@ module.exports = function getSpec ({ warn, error }) {
181
181
  }
182
182
  }
183
183
  },
184
+ {
185
+ // style样式绑定
186
+ test: /^(style|wx:style)$/,
187
+ web ({ value }, { el }) {
188
+ if (el.isStyleParsed) {
189
+ return false
190
+ }
191
+ let styleBinding = []
192
+ el.isStyleParsed = true
193
+ el.attrsList.map((item, index) => {
194
+ const parsed = parseMustache(item.value)
195
+ if (item.name === 'style') {
196
+ if (parsed.hasBinding || parsed.result.indexOf('rpx') > -1) {
197
+ styleBinding.push(parseMustache(item.value).result)
198
+ } else {
199
+ styleBinding.push(JSON.stringify(item.value))
200
+ }
201
+ } else if (item.name === 'wx:style') {
202
+ styleBinding.push(parseMustache(item.value).result)
203
+ }
204
+ })
205
+ return {
206
+ name: ':style',
207
+ value: `[${styleBinding}] | transRpxStyle`
208
+ }
209
+ }
210
+ },
184
211
  {
185
212
  // 样式类名绑定
186
- test: /^wx:(class|style)$/,
213
+ test: /^wx:class$/,
187
214
  web ({ name, value }) {
188
215
  const dir = this.test.exec(name)[1]
189
216
  const parsed = parseMustache(value)
@@ -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()
@@ -85,6 +85,51 @@ export default function processOption (
85
85
  }
86
86
  })
87
87
 
88
+ Vue.filter('transRpxStyle', style => {
89
+ const defaultTransRpxFn = function (match, $1) {
90
+ const rpx2vwRatio = +(100 / 750).toFixed(8)
91
+ return '' + ($1 * rpx2vwRatio) + 'vw'
92
+ }
93
+ const transRpxFn = global.__mpxTransRpxFn || defaultTransRpxFn
94
+ const parsedStyleObj = {}
95
+ const rpxRegExpG = /\b(\d+(\.\d+)?)rpx\b/g
96
+ const parseStyleText = (cssText) => {
97
+ const listDelimiter = /;(?![^(]*\))/g
98
+ const propertyDelimiter = /:(.+)/
99
+ if (typeof cssText === 'string') {
100
+ cssText.split(listDelimiter).forEach((item) => {
101
+ if (item) {
102
+ var tmp = item.split(propertyDelimiter)
103
+ tmp.length > 1 && (parsedStyleObj[tmp[0].trim()] = tmp[1].trim())
104
+ }
105
+ })
106
+ } else if (typeof cssText === 'object') {
107
+ if (Array.isArray(cssText)) {
108
+ cssText.forEach(cssItem => {
109
+ parseStyleText(cssItem)
110
+ })
111
+ } else {
112
+ Object.assign(parsedStyleObj, cssText)
113
+ }
114
+ }
115
+ }
116
+ const transRpxStyleFn = (val) => {
117
+ if (typeof val === 'string' && val.indexOf('rpx') > 0) {
118
+ return val.replace(rpxRegExpG, transRpxFn).replace(/"/g, '')
119
+ }
120
+ return val
121
+ }
122
+ if (style) {
123
+ style.forEach(item => {
124
+ parseStyleText(item)
125
+ for (let key in parsedStyleObj) {
126
+ parsedStyleObj[key] = transRpxStyleFn(parsedStyleObj[key])
127
+ }
128
+ })
129
+ }
130
+ return parsedStyleObj
131
+ })
132
+
88
133
  const routes = []
89
134
 
90
135
  for (const pagePath in pagesMap) {
@@ -1,6 +1,7 @@
1
1
  const path = require('path')
2
2
 
3
3
  module.exports = function evalJSONJS (source, filename, loaderContext) {
4
+ if (!loaderContext._compiler) return {}
4
5
  const fs = loaderContext._compiler.inputFileSystem
5
6
  const defs = loaderContext.getMpx().defs
6
7
  const defKeys = Object.keys(defs)
@@ -1,8 +1,8 @@
1
1
  module.exports = function (loaderContext) {
2
- const compilation = loaderContext._compilation
3
- const moduleGraph = compilation.moduleGraph
2
+ if (!loaderContext._compilation) return ''
3
+ const moduleGraph = loaderContext._compilation.moduleGraph
4
4
  let entryName = ''
5
- for (const [name, { dependencies }] of compilation.entries) {
5
+ for (const [name, { dependencies }] of loaderContext._compilation.entries) {
6
6
  const entryModule = moduleGraph.getModule(dependencies[0])
7
7
  if (entryModule.resource === loaderContext.resource) {
8
8
  entryName = name
@@ -5,7 +5,6 @@ const async = require('async')
5
5
  const { JSON_JS_EXT } = require('./const')
6
6
 
7
7
  module.exports = function getJSONContent (json, loaderContext, callback) {
8
- // error process
9
8
  if (!loaderContext._compiler) return callback(null, '{}')
10
9
  const fs = loaderContext._compiler.inputFileSystem
11
10
  async.waterfall([
@@ -219,11 +219,10 @@ module.exports = function (json, {
219
219
  }
220
220
 
221
221
  pagesMap[resourcePath] = outputPath
222
- loaderContext._module.addPresentationalDependency(new RecordResourceMapDependency(resourcePath, 'page', outputPath))
223
-
222
+ loaderContext._module && loaderContext._module.addPresentationalDependency(new RecordResourceMapDependency(resourcePath, 'page', outputPath))
224
223
  localPagesMap[outputPath] = {
225
224
  resource: addQuery(resource, { isPage: true }),
226
- async: tarRoot || queryObj.async,
225
+ async: queryObj.async || tarRoot,
227
226
  isFirst
228
227
  }
229
228
  callback()
@@ -269,8 +268,7 @@ module.exports = function (json, {
269
268
  }
270
269
  const { resourcePath, queryObj } = parseRequest(resource)
271
270
  componentsMap[resourcePath] = outputPath
272
- loaderContext._module.addPresentationalDependency(new RecordResourceMapDependency(resourcePath, 'component', outputPath))
273
-
271
+ loaderContext._module && loaderContext._module.addPresentationalDependency(new RecordResourceMapDependency(resourcePath, 'component', outputPath))
274
272
  localComponentsMap[name] = {
275
273
  resource: addQuery(resource, {
276
274
  isComponent: true,
@@ -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
  }
@@ -129,8 +136,8 @@ module.exports = function (script, {
129
136
  global.__networkTimeout = ${JSON.stringify(jsonConfig.networkTimeout)}
130
137
  global.__mpxGenericsMap = {}
131
138
  global.__style = ${JSON.stringify(jsonConfig.style || 'v1')}
132
- global.__mpxPageConfig = ${JSON.stringify(jsonConfig.window)}\n`
133
-
139
+ global.__mpxPageConfig = ${JSON.stringify(jsonConfig.window)}
140
+ global.__mpxTransRpxFn = ${mpx.webConfig.transRpxFn}\n`
134
141
  if (i18n) {
135
142
  const i18nObj = Object.assign({}, i18n)
136
143
  content += ` import VueI18n from 'vue-i18n'
@@ -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.19",
3
+ "version": "2.7.25",
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": "adadff7f3e1c658678def33059e222ef053af4d6"
83
+ "gitHead": "0bcd275c2d8a3fe33ba383b5c2b53e1988217f9e"
84
84
  }