@mpxjs/webpack-plugin 2.8.62 → 2.8.64-bridgetest

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,198 @@
1
+ const hasOwn = require('../utils/has-own')
2
+ const loaderUtils = require('loader-utils')
3
+ const normalize = require('../utils/normalize')
4
+ const createHelpers = require('../helpers')
5
+ const tabBarContainerPath = normalize.lib('runtime/components/web/mpx-tab-bar-container.vue')
6
+ const tabBarPath = normalize.lib('runtime/components/web/mpx-tab-bar.vue')
7
+ const addQuery = require('../utils/add-query')
8
+
9
+ function stringifyRequest (loaderContext, request) {
10
+ return loaderUtils.stringifyRequest(loaderContext, request)
11
+ }
12
+
13
+ function shallowStringify (obj) {
14
+ const arr = []
15
+ for (const key in obj) {
16
+ if (hasOwn(obj, key)) {
17
+ let value = obj[key]
18
+ if (Array.isArray(value)) {
19
+ value = `[${value.join(',')}]`
20
+ }
21
+ arr.push(`'${key}':${value}`)
22
+ }
23
+ }
24
+ return `{${arr.join(',')}}`
25
+ }
26
+
27
+ function getAsyncChunkName (chunkName) {
28
+ if (chunkName && typeof chunkName !== 'boolean') {
29
+ return `/* webpackChunkName: "${chunkName}" */`
30
+ }
31
+ return ''
32
+ }
33
+
34
+ function buildComponentsMap ({ localComponentsMap, builtInComponentsMap, loaderContext }) {
35
+ const componentsMap = {}
36
+ if (localComponentsMap) {
37
+ Object.keys(localComponentsMap).forEach((componentName) => {
38
+ const componentCfg = localComponentsMap[componentName]
39
+ const componentRequest = stringifyRequest(loaderContext, componentCfg.resource)
40
+ if (componentCfg.async) {
41
+ componentsMap[componentName] = `()=>import(${getAsyncChunkName(componentCfg.async)}${componentRequest}).then(res => getComponent(res))`
42
+ } else {
43
+ componentsMap[componentName] = `getComponent(require(${componentRequest}))`
44
+ }
45
+ })
46
+ }
47
+ if (builtInComponentsMap) {
48
+ Object.keys(builtInComponentsMap).forEach((componentName) => {
49
+ const componentCfg = builtInComponentsMap[componentName]
50
+ const componentRequest = stringifyRequest(loaderContext, componentCfg.resource)
51
+ componentsMap[componentName] = `getComponent(require(${componentRequest}), { __mpxBuiltIn: true })`
52
+ })
53
+ }
54
+ return componentsMap
55
+ }
56
+
57
+ function buildPagesMap ({ localPagesMap, loaderContext, tabBar, tabBarMap, tabBarStr, jsonConfig }) {
58
+ let globalTabBar = ''
59
+ let firstPage = ''
60
+ const pagesMap = {}
61
+ const tabBarPagesMap = {}
62
+ if (tabBar && tabBarMap) {
63
+ // 挂载tabBar组件
64
+ const tabBarRequest = stringifyRequest(loaderContext, addQuery(tabBar.custom ? './custom-tab-bar/index' : tabBarPath, { isComponent: true }))
65
+ tabBarPagesMap['mpx-tab-bar'] = `getComponent(require(${tabBarRequest}))`
66
+ // 挂载tabBar页面
67
+ Object.keys(tabBarMap).forEach((pagePath) => {
68
+ const pageCfg = localPagesMap[pagePath]
69
+ if (pageCfg) {
70
+ const pageRequest = stringifyRequest(loaderContext, pageCfg.resource)
71
+ if (pageCfg.async) {
72
+ tabBarPagesMap[pagePath] = `()=>import(${getAsyncChunkName(pageCfg.async)}${pageRequest}).then(res => getComponent(res, { __mpxPageRoute: ${JSON.stringify(pagePath)} }))`
73
+ } else {
74
+ tabBarPagesMap[pagePath] = `getComponent(require(${pageRequest}), { __mpxPageRoute: ${JSON.stringify(pagePath)} })`
75
+ }
76
+ } else {
77
+ loaderContext.emitWarning(
78
+ new Error('[json processor][' + loaderContext.resource + ']: ' + `TabBar page path ${pagePath} is not exist in local page map, please check!`)
79
+ )
80
+ }
81
+ })
82
+ }
83
+ if (tabBarStr && tabBarPagesMap) {
84
+ globalTabBar += ` global.__tabBar = ${tabBarStr}
85
+ Vue.observable(global.__tabBar)
86
+ // @ts-ignore
87
+ global.__tabBarPagesMap = ${shallowStringify(tabBarPagesMap)}\n`
88
+ }
89
+ Object.keys(localPagesMap).forEach((pagePath) => {
90
+ const pageCfg = localPagesMap[pagePath]
91
+ const pageRequest = stringifyRequest(loaderContext, pageCfg.resource)
92
+ if (tabBarMap && tabBarMap[pagePath]) {
93
+ pagesMap[pagePath] = `getComponent(require(${stringifyRequest(loaderContext, tabBarContainerPath)}), { __mpxBuiltIn: true })`
94
+ } else {
95
+ if (pageCfg.async) {
96
+ pagesMap[pagePath] = `()=>import(${getAsyncChunkName(pageCfg.async)} ${pageRequest}).then(res => getComponent(res, { __mpxPageRoute: ${JSON.stringify(pagePath)} }))`
97
+ } else {
98
+ // 为了保持小程序中app->page->component的js执行顺序,所有的page和component都改为require引入
99
+ pagesMap[pagePath] = `getComponent(require(${pageRequest}), { __mpxPageRoute: ${JSON.stringify(pagePath)} })`
100
+ }
101
+ }
102
+
103
+ if (pagePath === jsonConfig.entryPagePath) {
104
+ firstPage = pagePath
105
+ }
106
+ if (!firstPage && pageCfg.isFirst) {
107
+ firstPage = pagePath
108
+ }
109
+ })
110
+ return {
111
+ pagesMap,
112
+ firstPage,
113
+ globalTabBar
114
+ }
115
+ }
116
+
117
+ function getRequireScript ({ ctorType, script, loaderContext }) {
118
+ let content = ' /** script content **/\n'
119
+ const extraOptions = { ctorType, lang: script.lang || 'js' }
120
+ const { getRequire } = createHelpers(loaderContext)
121
+ content += ` ${getRequire('script', script, extraOptions)}\n`
122
+ return content
123
+ }
124
+
125
+ function buildGlobalParams ({ moduleId, scriptSrcMode, loaderContext, isProduction, jsonConfig, webConfig, isMain, globalTabBar }) {
126
+ let content = ''
127
+ if (isMain) {
128
+ content += `global.getApp = function(){}
129
+ global.getCurrentPages = function () {
130
+ if (!(typeof window !== 'undefined')) {
131
+ console.error('[Mpx runtime error]: Dangerous API! global.getCurrentPages is running in non browser environment, It may cause some problems, please use this method with caution')
132
+ }
133
+ const router = global.__mpxRouter
134
+ if(!router) return []
135
+ // @ts-ignore
136
+ return (router.lastStack || router.stack).map(item => {
137
+ let page
138
+ const vnode = item.vnode
139
+ if (vnode && vnode.componentInstance) {
140
+ page = vnode.tag.endsWith('mpx-tab-bar-container') ? vnode.componentInstance.$refs.tabBarPage : vnode.componentInstance
141
+ }
142
+ return page || { route: item.path.slice(1) }
143
+ })
144
+ }
145
+ global.__networkTimeout = ${JSON.stringify(jsonConfig.networkTimeout)}
146
+ global.__mpxGenericsMap = {}
147
+ global.__mpxOptionsMap = {}
148
+ global.__style = ${JSON.stringify(jsonConfig.style || 'v1')}
149
+ global.__mpxPageConfig = ${JSON.stringify(jsonConfig.window)}
150
+ global.__mpxTransRpxFn = ${webConfig.transRpxFn}\n`
151
+ if (globalTabBar) {
152
+ content += globalTabBar
153
+ }
154
+ }
155
+ content += ` global.currentModuleId = ${JSON.stringify(moduleId)}\n`
156
+ content += ` global.currentSrcMode = ${JSON.stringify(scriptSrcMode)}\n`
157
+ content += ` global.currentInject = ${JSON.stringify({ moduleId })}\n`
158
+ if (!isProduction) {
159
+ content += ` global.currentResource = ${JSON.stringify(loaderContext.resourcePath)}\n`
160
+ }
161
+ return content
162
+ }
163
+
164
+ function buildI18n ({ i18n, loaderContext }) {
165
+ let i18nContent = ''
166
+ const i18nObj = Object.assign({}, i18n)
167
+ i18nContent += ` import VueI18n from 'vue-i18n'
168
+ import { createI18n } from 'vue-i18n-bridge'
169
+ Vue.use(VueI18n , { bridge: true })\n`
170
+ const requestObj = {}
171
+ const i18nKeys = ['messages', 'dateTimeFormats', 'numberFormats']
172
+ i18nKeys.forEach((key) => {
173
+ if (i18nObj[`${key}Path`]) {
174
+ requestObj[key] = stringifyRequest(loaderContext, i18nObj[`${key}Path`])
175
+ delete i18nObj[`${key}Path`]
176
+ }
177
+ })
178
+ i18nContent += ` const i18nCfg = ${JSON.stringify(i18nObj)}\n`
179
+ Object.keys(requestObj).forEach((key) => {
180
+ i18nContent += ` i18nCfg.${key} = require(${requestObj[key]})\n`
181
+ })
182
+ i18nContent += ' i18nCfg.legacy = false\n'
183
+ i18nContent += ` const i18n = createI18n(i18nCfg, VueI18n)
184
+ Vue.use(i18n)
185
+ Mpx.i18n = i18n\n`
186
+ return i18nContent
187
+ }
188
+
189
+ module.exports = {
190
+ buildPagesMap,
191
+ buildComponentsMap,
192
+ getRequireScript,
193
+ buildGlobalParams,
194
+ shallowStringify,
195
+ getAsyncChunkName,
196
+ stringifyRequest,
197
+ buildI18n
198
+ }
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@mpxjs/webpack-plugin",
3
- "version": "2.8.62",
3
+ "version": "2.8.64-bridgetest",
4
4
  "description": "mpx compile core",
5
5
  "keywords": [
6
6
  "mpx"
7
7
  ],
8
8
  "author": "donghongping",
9
- "license": "Apache",
9
+ "license": "Apache-2.0",
10
10
  "main": "lib/index.js",
11
11
  "directories": {
12
12
  "lib": "lib"
@@ -82,5 +82,5 @@
82
82
  "engines": {
83
83
  "node": ">=14.14.0"
84
84
  },
85
- "gitHead": "968ecb02c86fcd5e9f8f827645907ea4225266ff"
85
+ "gitHead": "4080623bc3f493d1cfb7a506dffaec0f79084af6"
86
86
  }