@mpxjs/webpack-plugin 2.7.1-beta.1 → 2.7.1-beta.2

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.
@@ -40,16 +40,7 @@ module.exports = function (content) {
40
40
  const i18nWxsPath = normalize.lib('runtime/i18n.wxs')
41
41
  const i18nWxsLoaderPath = normalize.lib('wxs/i18n-loader.js')
42
42
  const i18nWxsRequest = i18nWxsLoaderPath + '!' + i18nWxsPath
43
- const i18nMethodsVar = 'i18nMethods'
44
- this._module.addDependency(new CommonJsVariableDependency(i18nWxsRequest, i18nMethodsVar))
45
-
46
- output += `if (!global.i18n) {
47
- global.i18n = ${JSON.stringify({
48
- locale: i18n.locale,
49
- version: 0
50
- })}
51
- global.i18nMethods = ${i18nMethodsVar}
52
- }\n`
43
+ this._module.addDependency(new CommonJsVariableDependency(i18nWxsRequest))
53
44
  }
54
45
  output += content
55
46
  output += '\n'
package/lib/index.js CHANGED
@@ -487,13 +487,13 @@ class MpxWebpackPlugin {
487
487
  exportModules: new Set(),
488
488
  // 记录entryModule与entryNode的对应关系,用于体积分析
489
489
  entryNodeModulesMap: new Map(),
490
- extractedMap: {},
490
+ // 记录与asset相关联的modules,用于体积分析
491
+ assetModulesMap: new Map(),
491
492
  usingComponents: {},
492
493
  // todo es6 map读写性能高于object,之后会逐步替换
493
494
  vueContentCache: new Map(),
494
495
  currentPackageRoot: '',
495
496
  wxsContentMap: {},
496
- assetsInfo: new Map(),
497
497
  forceUsePageCtor: this.options.forceUsePageCtor,
498
498
  resolveMode: this.options.resolveMode,
499
499
  mode: this.options.mode,
@@ -792,6 +792,12 @@ class MpxWebpackPlugin {
792
792
  return source
793
793
  })
794
794
 
795
+ compilation.hooks.moduleAsset.tap('MpxWebpackPlugin', (module, filename) => {
796
+ const modules = mpx.assetModulesMap.get(filename) || new Set()
797
+ modules.add(module)
798
+ mpx.assetModulesMap.set(filename, modules)
799
+ })
800
+
795
801
  compilation.hooks.beforeModuleAssets.tap('MpxWebpackPlugin', () => {
796
802
  const extractedAssetsMap = new Map()
797
803
  for (const module of compilation.modules) {
@@ -804,8 +810,7 @@ class MpxWebpackPlugin {
804
810
  extractedAssetsMap.set(filename, extractedAssets)
805
811
  }
806
812
  extractedAssets.push(extractedInfo)
807
- // todo 后续计算体积时可以通过这个钩子关联静态assets和module
808
- // compilation.hooks.moduleAsset.call(module, filename)
813
+ compilation.hooks.moduleAsset.call(module, filename)
809
814
  }
810
815
  }
811
816
  }
@@ -1032,7 +1037,6 @@ class MpxWebpackPlugin {
1032
1037
  }
1033
1038
 
1034
1039
  const processedChunk = new Set()
1035
- // const rootName = compilation.entries.keys().next().value
1036
1040
  const appName = mpx.appInfo.name
1037
1041
 
1038
1042
  function processChunk (chunk, isRuntime, relativeChunks) {
@@ -1300,13 +1304,45 @@ try {
1300
1304
  const fs = compiler.intermediateFileSystem
1301
1305
  const cacheLocation = compiler.options.cache.cacheLocation
1302
1306
  return new Promise((resolve, reject) => {
1303
- fs.rm(cacheLocation, {
1304
- recursive: true,
1305
- force: true
1306
- }, (err) => {
1307
- if (err) return reject(err)
1308
- resolve()
1309
- })
1307
+ if (fs.rm) {
1308
+ fs.rm(cacheLocation, {
1309
+ recursive: true,
1310
+ force: true
1311
+ }, (err) => {
1312
+ if (err) return reject(err)
1313
+ resolve()
1314
+ })
1315
+ } else {
1316
+ // polyfill fs.rm
1317
+ const rmdir = (dir, callback) => {
1318
+ fs.readdir(dir, (err, files) => {
1319
+ if (err) return callback(err)
1320
+ async.each(files, (file, callback) => {
1321
+ file = path.join(dir, file)
1322
+ async.waterfall([
1323
+ (callback) => {
1324
+ fs.stat(file, callback)
1325
+ },
1326
+ (stats, callback) => {
1327
+ if (stats.isDirectory()) {
1328
+ rmdir(file, callback)
1329
+ } else {
1330
+ fs.unlink(file, callback)
1331
+ }
1332
+ }
1333
+ ], callback)
1334
+ }, (err) => {
1335
+ if (err) return callback(err)
1336
+ fs.rmdir(dir, callback)
1337
+ })
1338
+ })
1339
+ }
1340
+
1341
+ rmdir(cacheLocation, (err) => {
1342
+ if (err) return reject(err)
1343
+ resolve()
1344
+ })
1345
+ }
1310
1346
  })
1311
1347
  }
1312
1348
 
@@ -272,9 +272,7 @@ module.exports = function (content) {
272
272
  env
273
273
  })
274
274
  // 对于通过.mpx文件声明的独立分包,默认将其自身的script block视为init module
275
- if (parts.script && queryObj.independent === true) {
276
- queryObj.independent = result
277
- }
275
+ if (queryObj.independent === true) queryObj.independent = result
278
276
  getJSONContent(parts.json || {}, this, (err, content) => {
279
277
  callback(err, result, content)
280
278
  })
package/lib/loader.js CHANGED
@@ -224,16 +224,7 @@ module.exports = function (content) {
224
224
  const i18nWxsPath = normalize.lib('runtime/i18n.wxs')
225
225
  const i18nWxsLoaderPath = normalize.lib('wxs/i18n-loader.js')
226
226
  const i18nWxsRequest = i18nWxsLoaderPath + '!' + i18nWxsPath
227
- const i18nMethodsVar = 'i18nMethods'
228
- this._module.addDependency(new CommonJsVariableDependency(i18nWxsRequest, i18nMethodsVar))
229
-
230
- output += `if (!global.i18n) {
231
- global.i18n = ${JSON.stringify({
232
- locale: i18n.locale,
233
- version: 0
234
- })}
235
- global.i18nMethods = ${i18nMethodsVar}
236
- }\n`
227
+ this._module.addDependency(new CommonJsVariableDependency(i18nWxsRequest))
237
228
  }
238
229
 
239
230
  // 为独立分包注入init module
@@ -6,7 +6,6 @@ function genRegExp (str, flags) {
6
6
  }
7
7
  }
8
8
 
9
-
10
9
  function likeArray (arr) {
11
10
  if (!__mpx_wxs__) {
12
11
  return Array.isArray(arr)
@@ -23,7 +22,6 @@ function isDef (v) {
23
22
  return v !== undefined && v !== null
24
23
  }
25
24
 
26
-
27
25
  var RE_TOKEN_LIST_VALUE = genRegExp('^[0-9]+')
28
26
  var RE_TOKEN_NAMED_VALUE = genRegExp('^[A-Za-z0-9_]+')
29
27
 
@@ -40,7 +38,10 @@ function parseMessage (format) {
40
38
  var char = format[position++]
41
39
  if (char === '{') {
42
40
  if (text) {
43
- tokens.push({ type: 'text', value: text })
41
+ tokens.push({
42
+ type: 'text',
43
+ value: text
44
+ })
44
45
  }
45
46
 
46
47
  text = ''
@@ -56,7 +57,10 @@ function parseMessage (format) {
56
57
  : isClosed && RE_TOKEN_NAMED_VALUE.test(sub)
57
58
  ? 'named'
58
59
  : 'unknown'
59
- tokens.push({ value: sub, type: type })
60
+ tokens.push({
61
+ value: sub,
62
+ type: type
63
+ })
60
64
  } else if (char === '%') {
61
65
  // when found rails i18n syntax, skip text capture
62
66
  if (format[(position)] !== '{') {
@@ -67,7 +71,10 @@ function parseMessage (format) {
67
71
  }
68
72
  }
69
73
 
70
- text && tokens.push({ type: 'text', value: text })
74
+ text && tokens.push({
75
+ type: 'text',
76
+ value: text
77
+ })
71
78
 
72
79
  return tokens
73
80
  }
@@ -263,6 +270,11 @@ function exist (messages, locale, key) {
263
270
  var messages = {}
264
271
  var dateTimeFormats = {}
265
272
  var numberFormats = {}
273
+ var locale = 'zh-CN'
274
+
275
+ function getLocale () {
276
+ return __mpx_locale__ || locale
277
+ }
266
278
 
267
279
  function getMessages () {
268
280
  // __mpx_messages__会在编译时通过lib/wxs/i18n-loader注入
@@ -339,7 +351,15 @@ module.exports = {
339
351
  }
340
352
 
341
353
  if (!__mpx_wxs__) {
342
- module.exports.__getMessages = getMessages
343
- module.exports.__getDateTimeFormats = getDateTimeFormats
344
- module.exports.__getNumberFormats = getNumberFormats
354
+ if (!global.i18n) {
355
+ global.i18n = {
356
+ locale: getLocale(),
357
+ version: 0
358
+ }
359
+ global.i18nMethods = Object.assign(module.exports, {
360
+ __getMessages: getMessages,
361
+ __getDateTimeFormats: getDateTimeFormats,
362
+ __getNumberFormats: getNumberFormats
363
+ })
364
+ }
345
365
  }
@@ -3,7 +3,7 @@ const loaderUtils = require('loader-utils')
3
3
 
4
4
  module.exports = function (content) {
5
5
  const i18n = this.getMpx().i18n
6
- let prefix = 'var __mpx_messages__, __mpx_datetime_formats__, __mpx_number_formats__\n'
6
+ let prefix = 'var __mpx_messages__, __mpx_datetime_formats__, __mpx_number_formats__, __mpx_locale__\n'
7
7
  if (i18n) {
8
8
  if (i18n.messages) {
9
9
  prefix += `__mpx_messages__ = ${JSON.stringify(i18n.messages)}\n`
@@ -20,6 +20,9 @@ module.exports = function (content) {
20
20
  } else if (i18n.numberFormatsPath) {
21
21
  prefix += `__mpx_number_formats__ = require(${loaderUtils.stringifyRequest(this, i18n.numberFormatsPath)})\n`
22
22
  }
23
+ if (i18n.locale) {
24
+ prefix += `__mpx_locale__ = ${JSON.stringify(i18n.locale)}\n`
25
+ }
23
26
  }
24
27
  content = prefix + content
25
28
  return content
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/webpack-plugin",
3
- "version": "2.7.1-beta.1",
3
+ "version": "2.7.1-beta.2",
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": "0ea8bcfff2ccfc433cb2ce43275b7e2af2bc8a61"
83
+ "gitHead": "63d16469fdb25698f7d5fb6585300530f0ae5381"
84
84
  }