@mpxjs/webpack-plugin 2.8.49 → 2.8.51

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.
@@ -0,0 +1,51 @@
1
+ const ModuleDependency = require('webpack/lib/dependencies/ModuleDependency')
2
+ const makeSerializable = require('webpack/lib/util/makeSerializable')
3
+
4
+ class CommonJsExtractDependency extends ModuleDependency {
5
+ constructor (request, range) {
6
+ super(request)
7
+ this.range = range
8
+ }
9
+
10
+ get type () {
11
+ return 'mpx cjs extract'
12
+ }
13
+
14
+ get category () {
15
+ return 'commonjs'
16
+ }
17
+ }
18
+
19
+ CommonJsExtractDependency.Template = class CommonJsExtractDependencyTemplate extends (
20
+ ModuleDependency.Template
21
+ ) {
22
+ apply (
23
+ dep,
24
+ source,
25
+ {
26
+ runtimeTemplate,
27
+ moduleGraph,
28
+ chunkGraph,
29
+ runtimeRequirements
30
+ }
31
+ ) {
32
+ let content = ''
33
+ if (!dep.weak) {
34
+ content = runtimeTemplate.moduleExports({
35
+ module: moduleGraph.getModule(dep),
36
+ chunkGraph,
37
+ request: dep.request,
38
+ weak: dep.weak,
39
+ runtimeRequirements
40
+ })
41
+ }
42
+ source.replace(dep.range[0], dep.range[1] - 1, content)
43
+ }
44
+ }
45
+
46
+ makeSerializable(
47
+ CommonJsExtractDependency,
48
+ '@mpxjs/webpack-plugin/lib/dependencies/CommonJsExtractDependency'
49
+ )
50
+
51
+ module.exports = CommonJsExtractDependency
package/lib/extractor.js CHANGED
@@ -116,5 +116,6 @@ module.exports.pitch = async function (remainingRequest) {
116
116
  }
117
117
  }
118
118
 
119
+ if (!resultSource) buildInfo.isEmpty = true
119
120
  return resultSource
120
121
  }
package/lib/helpers.js CHANGED
@@ -20,7 +20,15 @@ module.exports = function createHelpers (loaderContext) {
20
20
  const { mode, env } = loaderContext.getMpx() || {}
21
21
 
22
22
  function getRequire (type, part, extraOptions, index) {
23
- return 'require(' + getRequestString(type, part, extraOptions, index) + ')'
23
+ let extract = false
24
+ switch (type) {
25
+ // eslint-disable-next-line no-fallthrough
26
+ case 'json':
27
+ case 'styles':
28
+ case 'template':
29
+ extract = true
30
+ }
31
+ return (extract ? 'require.extract(' : 'require(') + getRequestString(type, part, extraOptions, index) + ')'
24
32
  }
25
33
 
26
34
  function getImport (type, part, extraOptions, index) {
package/lib/index.js CHANGED
@@ -8,6 +8,7 @@ const ReplaceDependency = require('./dependencies/ReplaceDependency')
8
8
  const NullFactory = require('webpack/lib/NullFactory')
9
9
  const CommonJsVariableDependency = require('./dependencies/CommonJsVariableDependency')
10
10
  const CommonJsAsyncDependency = require('./dependencies/CommonJsAsyncDependency')
11
+ const CommonJsExtractDependency = require('./dependencies/CommonJsExtractDependency')
11
12
  const harmonySpecifierTag = require('webpack/lib/dependencies/HarmonyImportDependencyParserPlugin').harmonySpecifierTag
12
13
  const NormalModule = require('webpack/lib/NormalModule')
13
14
  const EntryPlugin = require('webpack/lib/EntryPlugin')
@@ -124,6 +125,7 @@ class MpxWebpackPlugin {
124
125
  options.resolveMode = options.resolveMode || 'webpack'
125
126
  options.writeMode = options.writeMode || 'changed'
126
127
  options.autoScopeRules = options.autoScopeRules || {}
128
+ options.renderOptimizeRules = options.renderOptimizeRules || {}
127
129
  options.autoVirtualHostRules = options.autoVirtualHostRules || {}
128
130
  options.forceDisableProxyCtor = options.forceDisableProxyCtor || false
129
131
  options.transMpxRules = options.transMpxRules || {
@@ -171,6 +173,12 @@ class MpxWebpackPlugin {
171
173
  options.retryRequireAsync = options.retryRequireAsync || false
172
174
  options.enableAliRequireAsync = options.enableAliRequireAsync || false
173
175
  options.optimizeSize = options.optimizeSize || false
176
+ let proxyComponentEventsRules = []
177
+ const proxyComponentEventsRulesRaw = options.proxyComponentEventsRules
178
+ if (proxyComponentEventsRulesRaw) {
179
+ proxyComponentEventsRules = Array.isArray(proxyComponentEventsRulesRaw) ? proxyComponentEventsRulesRaw : [proxyComponentEventsRulesRaw]
180
+ }
181
+ options.proxyComponentEventsRules = proxyComponentEventsRules
174
182
  this.options = options
175
183
  // Hack for buildDependencies
176
184
  const rawResolveBuildDependencies = FileSystemInfo.prototype.resolveBuildDependencies
@@ -405,13 +413,13 @@ class MpxWebpackPlugin {
405
413
  name: 'MpxPartialCompilePlugin',
406
414
  stage: -100
407
415
  }, (obj, resolverContext, callback) => {
408
- if (obj.path.startsWith(require.resolve('./json-compiler/default-page.mpx'))) {
416
+ if (obj.path.startsWith(require.resolve('./runtime/components/wx/default-page.mpx'))) {
409
417
  return callback(null, obj)
410
418
  }
411
419
  if (isResolvingPage(obj) && !matchCondition(obj.path, this.options.partialCompile)) {
412
420
  const infix = obj.query ? '&' : '?'
413
421
  obj.query += `${infix}resourcePath=${obj.path}`
414
- obj.path = require.resolve('./json-compiler/default-page.mpx')
422
+ obj.path = require.resolve('./runtime/components/wx/default-page.mpx')
415
423
  }
416
424
  callback(null, obj)
417
425
  })
@@ -554,6 +562,9 @@ class MpxWebpackPlugin {
554
562
  compilation.dependencyFactories.set(CommonJsAsyncDependency, normalModuleFactory)
555
563
  compilation.dependencyTemplates.set(CommonJsAsyncDependency, new CommonJsAsyncDependency.Template())
556
564
 
565
+ compilation.dependencyFactories.set(CommonJsExtractDependency, normalModuleFactory)
566
+ compilation.dependencyTemplates.set(CommonJsExtractDependency, new CommonJsExtractDependency.Template())
567
+
557
568
  compilation.dependencyFactories.set(RecordVueContentDependency, new NullFactory())
558
569
  compilation.dependencyTemplates.set(RecordVueContentDependency, new RecordVueContentDependency.Template())
559
570
  })
@@ -628,12 +639,14 @@ class MpxWebpackPlugin {
628
639
  appTitle: 'Mpx homepage',
629
640
  attributes: this.options.attributes,
630
641
  externals: this.options.externals,
642
+ renderOptimizeRules: this.options.renderOptimizeRules,
631
643
  useRelativePath: this.options.useRelativePath,
632
644
  removedChunks: [],
633
645
  forceProxyEventRules: this.options.forceProxyEventRules,
634
646
  enableRequireAsync: this.options.mode === 'wx' || (this.options.mode === 'ali' && this.options.enableAliRequireAsync),
635
647
  partialCompile: this.options.partialCompile,
636
648
  asyncSubpackageRules: this.options.asyncSubpackageRules,
649
+ proxyComponentEventsRules: this.options.proxyComponentEventsRules,
637
650
  pathHash: (resourcePath) => {
638
651
  if (this.options.pathHashMode === 'relative' && this.options.projectRoot) {
639
652
  return hash(path.relative(this.options.projectRoot, resourcePath))
@@ -923,6 +936,17 @@ class MpxWebpackPlugin {
923
936
  })
924
937
 
925
938
  compilation.hooks.finishModules.tap('MpxWebpackPlugin', (modules) => {
939
+ // 移除extractor抽取后的空模块
940
+ for (const module of modules) {
941
+ if (module.buildInfo.isEmpty) {
942
+ for (const connection of moduleGraph.getIncomingConnections(module)) {
943
+ if (connection.dependency.type === 'mpx cjs extract') {
944
+ connection.weak = true
945
+ connection.dependency.weak = true
946
+ }
947
+ }
948
+ }
949
+ }
926
950
  // 自动跟进分包配置修改splitChunksPlugin配置
927
951
  if (splitChunksPlugin) {
928
952
  let needInit = false
@@ -1096,6 +1120,31 @@ class MpxWebpackPlugin {
1096
1120
  stage: -1000
1097
1121
  }, (expr, calleeMembers, callExpr) => requireAsyncHandler(callExpr, calleeMembers, expr.arguments))
1098
1122
 
1123
+ const requireExtractHandler = (expr, members, args) => {
1124
+ if (members[0] === 'extract') {
1125
+ const request = expr.arguments[0].value
1126
+ const range = expr.range
1127
+ const dep = new CommonJsExtractDependency(request, range)
1128
+ parser.state.current.addDependency(dep)
1129
+ if (args) parser.walkExpressions(args)
1130
+ return true
1131
+ }
1132
+ }
1133
+
1134
+ parser.hooks.callMemberChain
1135
+ .for('require')
1136
+ .tap({
1137
+ name: 'MpxWebpackPlugin',
1138
+ stage: -2000
1139
+ }, (expr, members) => requireExtractHandler(expr, members))
1140
+
1141
+ parser.hooks.callMemberChainOfCallMemberChain
1142
+ .for('require')
1143
+ .tap({
1144
+ name: 'MpxWebpackPlugin',
1145
+ stage: -2000
1146
+ }, (expr, calleeMembers, callExpr) => requireExtractHandler(callExpr, calleeMembers, expr.arguments))
1147
+
1099
1148
  // hack babel polyfill global
1100
1149
  parser.hooks.statementIf.tap('MpxWebpackPlugin', (expr) => {
1101
1150
  if (/core-js.+microtask/.test(parser.state.module.resource)) {
@@ -250,7 +250,7 @@ module.exports = function (content) {
250
250
  const localPages = []
251
251
  const subPackagesCfg = {}
252
252
  const pageKeySet = new Set()
253
- const defaultPagePath = require.resolve('./default-page.mpx')
253
+ const defaultPagePath = require.resolve('../runtime/components/wx/default-page.mpx')
254
254
  const processPages = (pages, context, tarRoot = '', callback) => {
255
255
  if (pages) {
256
256
  const pagesCache = []
package/lib/loader.js CHANGED
@@ -280,7 +280,7 @@ module.exports = function (content) {
280
280
  let ctor = 'App'
281
281
  if (ctorType === 'page') {
282
282
  // swan也默认使用Page构造器
283
- if (mpx.forceUsePageCtor || mode === 'ali' || mode === 'swan') {
283
+ if (mpx.forceUsePageCtor || mode === 'ali') {
284
284
  ctor = 'Page'
285
285
  } else {
286
286
  ctor = 'Component'
@@ -214,7 +214,7 @@ module.exports = function (content) {
214
214
  let ctorType = 'app'
215
215
  if (pagesMap[resourcePath]) {
216
216
  ctorType = 'page'
217
- if (mpx.forceUsePageCtor || mode === 'ali' || mode === 'swan') {
217
+ if (mpx.forceUsePageCtor || mode === 'ali') {
218
218
  ctor = 'Page'
219
219
  } else {
220
220
  ctor = 'Component'
@@ -3,6 +3,7 @@ const normalizeTest = require('../normalize-test')
3
3
  const changeKey = require('../change-key')
4
4
  const normalize = require('../../../utils/normalize')
5
5
  const { capitalToHyphen } = require('../../../utils/string')
6
+ const { isOriginTag } = require('../../../utils/dom-tag-config')
6
7
 
7
8
  const mpxViewPath = normalize.lib('runtime/components/ali/mpx-view.mpx')
8
9
  const mpxTextPath = normalize.lib('runtime/components/ali/mpx-text.mpx')
@@ -125,6 +126,26 @@ module.exports = function getSpec ({ warn, error }) {
125
126
  }
126
127
  }
127
128
 
129
+ /**
130
+ * 将小程序代码中使用的与原生 HTML tag 同名组件进行转化,以解决与原生tag命名冲突问题。
131
+ * @param {string} type usingComponents
132
+ * @returns input
133
+ */
134
+ function webHTMLTagProcesser (type) {
135
+ return function (input) {
136
+ const usingComponents = input[type]
137
+ if (usingComponents) {
138
+ Object.keys(usingComponents).forEach(tag => {
139
+ if (isOriginTag(tag)) {
140
+ usingComponents[`mpx-com-${tag}`] = usingComponents[tag]
141
+ delete usingComponents[tag]
142
+ }
143
+ })
144
+ }
145
+ return input
146
+ }
147
+ }
148
+
128
149
  const componentRules = [
129
150
  {
130
151
  test: 'componentGenerics',
@@ -137,6 +158,10 @@ module.exports = function getSpec ({ warn, error }) {
137
158
  tt: deletePath(),
138
159
  jd: deletePath()
139
160
  },
161
+ {
162
+ test: 'usingComponents',
163
+ web: webHTMLTagProcesser('usingComponents')
164
+ },
140
165
  {
141
166
  test: 'usingComponents',
142
167
  ali: componentNameCapitalToHyphen('usingComponents'),
@@ -237,7 +262,7 @@ module.exports = function getSpec ({ warn, error }) {
237
262
  }
238
263
 
239
264
  const spec = {
240
- supportedModes: ['ali', 'swan', 'qq', 'tt', 'jd', 'qa', 'dd'],
265
+ supportedModes: ['ali', 'swan', 'qq', 'tt', 'jd', 'qa', 'dd', 'web'],
241
266
  normalizeTest,
242
267
  page: [
243
268
  ...windowRules,
@@ -338,6 +363,10 @@ module.exports = function getSpec ({ warn, error }) {
338
363
  tt: deletePath(),
339
364
  jd: deletePath(true)
340
365
  },
366
+ {
367
+ test: 'usingComponents',
368
+ web: webHTMLTagProcesser('usingComponents')
369
+ },
341
370
  {
342
371
  test: 'usingComponents',
343
372
  ali: componentNameCapitalToHyphen('usingComponents'),
@@ -32,7 +32,8 @@ module.exports = function runRules (rules = [], input, options = {}) {
32
32
  if (result !== undefined) {
33
33
  input = result
34
34
  }
35
- if (!waterfall) break
35
+ // rule 内外 waterfall 均为 false 时跳过
36
+ if (!rule.waterfall && !waterfall) break
36
37
  }
37
38
  }
38
39
  return input
@@ -11,6 +11,8 @@ module.exports = function normalizeComponentRules (cfgs, spec) {
11
11
  if (cfg.test) {
12
12
  result.test = cfg.test
13
13
  }
14
+ // 透传 waterfall 信息
15
+ if (cfg.waterfall) result.waterfall = cfg.waterfall
14
16
  const supportedModes = cfg.supportedModes || spec.supportedModes
15
17
  // 合并component-config中组件的event 与index中公共的event规则
16
18
  const eventRules = (cfg.event || []).concat(spec.event.rules)
@@ -0,0 +1,17 @@
1
+ const { isOriginTag } = require('../../../../utils/dom-tag-config')
2
+
3
+ module.exports = function () {
4
+ return {
5
+ waterfall: true,
6
+ test: (input) => isOriginTag(input),
7
+ // 处理原生tag
8
+ web (tag, data = {}) {
9
+ // @see packages/webpack-plugin/lib/platform/json/wx/index.js webHTMLTagProcesser
10
+ const newTag = `mpx-com-${tag}`
11
+ const usingComponents = data.usingComponents || []
12
+ // 存在同名组件,则返回新tag
13
+ if (usingComponents.includes(newTag)) return newTag
14
+ return tag
15
+ }
16
+ }
17
+ }
@@ -39,6 +39,7 @@ const view = require('./view')
39
39
  const webView = require('./web-view')
40
40
  const wxs = require('./wxs')
41
41
  const component = require('./component')
42
+ const fixHTMLTag = require('./fix-html-tag')
42
43
 
43
44
  module.exports = function getComponentConfigs ({ warn, error }) {
44
45
  /**
@@ -79,6 +80,7 @@ module.exports = function getComponentConfigs ({ warn, error }) {
79
80
 
80
81
  // 转换规则只需以微信为基准配置微信和支付宝的差异部分,比如微信和支付宝都支持但是写法不一致,或者微信支持而支付宝不支持的部分(抛出错误或警告)
81
82
  return [
83
+ fixHTMLTag(),
82
84
  ...Nonsupport({ print }),
83
85
  ad({ print }),
84
86
  view({ print }),
@@ -1,3 +1,12 @@
1
+ const {
2
+ isRichTextTag,
3
+ isUnaryTag,
4
+ isSpace,
5
+ isContWidth,
6
+ isContHeight,
7
+ isContConRow
8
+ } = require('../../../utils/dom-tag-config')
9
+
1
10
  // eslint-disable-next-line
2
11
  const attribute = /^\s*([^\s"'<>\/=]+)(?:\s*(=)\s*(?:"([^"]*)"+|'([^']*)'+|([^\s"'=<>`]+)))?/ // useless escape
3
12
  const ncname = '[a-zA-Z_][\\w\\-\\.]*'
@@ -11,36 +20,6 @@ const comment = /^<!\--/
11
20
  const invalidAttributeRE = /[\s"'<>\/=]/
12
21
  let currentParent
13
22
 
14
- function makeMap (str, expectsLowerCase) {
15
- const map = Object.create(null)
16
- const list = str.split(',')
17
- for (let i = 0; i < list.length; i++) {
18
- map[list[i]] = true
19
- }
20
- return expectsLowerCase
21
- ? val => map[val.toLowerCase()]
22
- : val => map[val]
23
- }
24
-
25
- const isRichTextTag = makeMap(
26
- 'a,abbr,address,article,aside,b,bdi,bdo,big,blockquote,br,caption,' +
27
- 'center,cite,code,col,colgroup,dd,del,div,dl,dt,em,fieldset,' +
28
- 'font,footer,h1,h2,h3,h4,h5,h6,header,hr,i,img,ins,label,legend,' +
29
- 'li,mark,nav,ol,p,pre,q,rt,ruby,s,section,small,span,strong,sub,sup,' +
30
- 'table,tbody,td,tfoot,th,thead,tr,tt,u,ul'
31
- )
32
- const isUnaryTag = makeMap(
33
- 'area,base,br,col,embed,frame,hr,img,input,isindex,keygen,' +
34
- 'link,meta,param,source,track,wbr'
35
- )
36
- const isSpace = makeMap('ensp,emsp,nbsp')
37
-
38
- const isContWidth = makeMap('col,colgroup,img,table,td,th,tr')
39
-
40
- const isContHeight = makeMap('img,td,th,tr')
41
-
42
- const isContConRow = makeMap('td,th,tr')
43
-
44
23
  function makeAttrsMap (attrs) {
45
24
  const map = {}
46
25
  for (let i = 0, l = attrs.length; i < l; i++) {
@@ -345,7 +345,11 @@
345
345
  class: 'circle circle-c'
346
346
  }),
347
347
  ]
348
- ) : null
348
+ ) : this.$slots.refresher
349
+ ? createElement('div', {
350
+ class: 'mpx-pull-down-slot',
351
+ }, this.$slots.refresher)
352
+ : null
349
353
 
350
354
  const pullDownWrapper = this.refresherEnabled ? createElement('div', {
351
355
  class: 'mpx-pull-down-wrapper',
@@ -379,7 +383,11 @@
379
383
  bottom: 20px
380
384
  left: 50%
381
385
  transform: translateX(-50%)
382
-
386
+ .mpx-pull-down-slot
387
+ position: absolute
388
+ width: 100%
389
+ height: auto
390
+ bottom: 0
383
391
  .mpx-pull-down-content-black
384
392
  .circle
385
393
  display: inline-block;
@@ -0,0 +1,27 @@
1
+ <template>
2
+ <view>局部构建兜底页面</view>
3
+ <view>当前路由:{{currentRoute}}</view>
4
+ </template>
5
+
6
+ <script>
7
+ import { onLoad } from '@mpxjs/core'
8
+ import { createPage } from '@mpxjs/core'
9
+
10
+ createPage({
11
+ data() {
12
+ return {
13
+ currentRoute: '',
14
+ }
15
+ },
16
+ onLoad() {
17
+ this.getPagePath()
18
+ },
19
+ methods: {
20
+ getPagePath() {
21
+ const pages = getCurrentPages() || []
22
+ const currPage = pages[pages.length - 1]
23
+ this.currentRoute = currPage && currPage.route || ''
24
+ },
25
+ }
26
+ })
27
+ </script>
@@ -47,6 +47,10 @@ module.exports = function (css, map) {
47
47
  plugins.push(transSpecial({ id }))
48
48
  }
49
49
 
50
+ if (mode === 'web') {
51
+ plugins.push(transSpecial({ id }))
52
+ }
53
+
50
54
  plugins.push(pluginCondStrip({
51
55
  defs
52
56
  }))