@mpxjs/webpack-plugin 2.8.35 → 2.9.0-beta.0
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 +4 -3
- package/lib/loader.js +28 -23
- package/lib/runtime/components/web/getInnerListeners.js +1 -2
- package/lib/runtime/components/web/mpx-image.vue +16 -13
- package/lib/runtime/components/web/mpx-text.vue +17 -17
- package/lib/runtime/components/web/mpx-web-view.vue +2 -4
- package/lib/runtime/optionProcessor.js +314 -271
- package/lib/web/processMainScript.js +56 -0
- package/lib/web/processScript.js +20 -200
- package/lib/web/processTemplate.js +5 -2
- package/lib/web/script-helper.js +195 -0
- package/package.json +2 -2
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
const addQuery = require('../utils/add-query')
|
|
2
|
+
const normalize = require('../utils/normalize')
|
|
3
|
+
const optionProcessorPath = normalize.lib('runtime/optionProcessor')
|
|
4
|
+
const { buildComponentsMap, buildPagesMap, buildGlobalParams, shallowStringify, stringifyRequest, buildI18n } = require('./script-helper')
|
|
5
|
+
|
|
6
|
+
module.exports = function (script, {
|
|
7
|
+
loaderContext,
|
|
8
|
+
ctorType,
|
|
9
|
+
srcMode,
|
|
10
|
+
moduleId,
|
|
11
|
+
isProduction,
|
|
12
|
+
jsonConfig,
|
|
13
|
+
localComponentsMap,
|
|
14
|
+
tabBar,
|
|
15
|
+
tabBarMap,
|
|
16
|
+
tabBarStr,
|
|
17
|
+
localPagesMap,
|
|
18
|
+
resource
|
|
19
|
+
}, callback) {
|
|
20
|
+
const { i18n, webConfig } = loaderContext.getMpx()
|
|
21
|
+
|
|
22
|
+
const { pagesMap, firstPage, globalTabBar } = buildPagesMap({ localPagesMap, loaderContext, tabBar, tabBarMap, tabBarStr })
|
|
23
|
+
|
|
24
|
+
const componentsMap = buildComponentsMap({ localComponentsMap, loaderContext })
|
|
25
|
+
|
|
26
|
+
const scriptSrcMode = script ? script.mode || srcMode : srcMode
|
|
27
|
+
|
|
28
|
+
let output = `\n import { processAppOption, getComponent } from ${stringifyRequest(loaderContext, optionProcessorPath)}
|
|
29
|
+
import '@mpxjs/webpack-plugin/lib/runtime/base.styl'
|
|
30
|
+
import Vue from 'vue'
|
|
31
|
+
import VueRouter from 'vue-router'
|
|
32
|
+
import Mpx from '@mpxjs/core'
|
|
33
|
+
import App from ${stringifyRequest(loaderContext, addQuery(resource, { isApp: true }))}
|
|
34
|
+
Vue.use(VueRouter)
|
|
35
|
+
\n`
|
|
36
|
+
|
|
37
|
+
if (i18n) {
|
|
38
|
+
output += buildI18n({ i18n, loaderContext })
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
output += buildGlobalParams({ moduleId, scriptSrcMode, loaderContext, isProduction, jsonConfig, webConfig, isMain: true, globalTabBar })
|
|
42
|
+
output += `export default processAppOption({
|
|
43
|
+
App,
|
|
44
|
+
tabBarMap: ${JSON.stringify(tabBarMap)},
|
|
45
|
+
firstPage: ${JSON.stringify(firstPage)},
|
|
46
|
+
pagesMap: ${shallowStringify(pagesMap)},
|
|
47
|
+
componentsMap: ${shallowStringify(componentsMap)},
|
|
48
|
+
Vue,
|
|
49
|
+
VueRouter,
|
|
50
|
+
webConfig: ${JSON.stringify(webConfig)}
|
|
51
|
+
})`
|
|
52
|
+
|
|
53
|
+
callback(null, {
|
|
54
|
+
output
|
|
55
|
+
})
|
|
56
|
+
}
|
package/lib/web/processScript.js
CHANGED
|
@@ -1,33 +1,8 @@
|
|
|
1
1
|
const genComponentTag = require('../utils/gen-component-tag')
|
|
2
2
|
const loaderUtils = require('loader-utils')
|
|
3
|
-
const addQuery = require('../utils/add-query')
|
|
4
3
|
const normalize = require('../utils/normalize')
|
|
5
|
-
const hasOwn = require('../utils/has-own')
|
|
6
|
-
const createHelpers = require('../helpers')
|
|
7
4
|
const optionProcessorPath = normalize.lib('runtime/optionProcessor')
|
|
8
|
-
const
|
|
9
|
-
const tabBarPath = normalize.lib('runtime/components/web/mpx-tab-bar.vue')
|
|
10
|
-
|
|
11
|
-
function shallowStringify (obj) {
|
|
12
|
-
const arr = []
|
|
13
|
-
for (const key in obj) {
|
|
14
|
-
if (hasOwn(obj, key)) {
|
|
15
|
-
let value = obj[key]
|
|
16
|
-
if (Array.isArray(value)) {
|
|
17
|
-
value = `[${value.join(',')}]`
|
|
18
|
-
}
|
|
19
|
-
arr.push(`'${key}':${value}`)
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
return `{${arr.join(',')}}`
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function getAsyncChunkName (chunkName) {
|
|
26
|
-
if (chunkName && typeof chunkName !== 'boolean') {
|
|
27
|
-
return `/* webpackChunkName: "${chunkName}" */`
|
|
28
|
-
}
|
|
29
|
-
return ''
|
|
30
|
-
}
|
|
5
|
+
const { buildComponentsMap, getRequireScript, buildGlobalParams, shallowStringify } = require('./script-helper')
|
|
31
6
|
|
|
32
7
|
module.exports = function (script, {
|
|
33
8
|
loaderContext,
|
|
@@ -38,50 +13,14 @@ module.exports = function (script, {
|
|
|
38
13
|
componentGenerics,
|
|
39
14
|
jsonConfig,
|
|
40
15
|
outputPath,
|
|
41
|
-
tabBarMap,
|
|
42
|
-
tabBarStr,
|
|
43
16
|
builtInComponentsMap,
|
|
44
17
|
genericsInfo,
|
|
45
18
|
wxsModuleMap,
|
|
46
|
-
localComponentsMap
|
|
47
|
-
localPagesMap
|
|
19
|
+
localComponentsMap
|
|
48
20
|
}, callback) {
|
|
49
|
-
const {
|
|
50
|
-
i18n,
|
|
51
|
-
projectRoot,
|
|
52
|
-
webConfig,
|
|
53
|
-
appInfo
|
|
54
|
-
} = loaderContext.getMpx()
|
|
55
|
-
const { getRequire } = createHelpers(loaderContext)
|
|
56
|
-
const tabBar = jsonConfig.tabBar
|
|
57
|
-
|
|
58
|
-
const emitWarning = (msg) => {
|
|
59
|
-
loaderContext.emitWarning(
|
|
60
|
-
new Error('[script processor][' + loaderContext.resource + ']: ' + msg)
|
|
61
|
-
)
|
|
62
|
-
}
|
|
21
|
+
const { projectRoot, appInfo } = loaderContext.getMpx()
|
|
63
22
|
|
|
64
23
|
const stringifyRequest = r => loaderUtils.stringifyRequest(loaderContext, r)
|
|
65
|
-
const tabBarPagesMap = {}
|
|
66
|
-
if (tabBar && tabBarMap) {
|
|
67
|
-
// 挂载tabBar组件
|
|
68
|
-
const tabBarRequest = stringifyRequest(addQuery(tabBar.custom ? './custom-tab-bar/index' : tabBarPath, { isComponent: true }))
|
|
69
|
-
tabBarPagesMap['mpx-tab-bar'] = `getComponent(require(${tabBarRequest}))`
|
|
70
|
-
// 挂载tabBar页面
|
|
71
|
-
Object.keys(tabBarMap).forEach((pagePath) => {
|
|
72
|
-
const pageCfg = localPagesMap[pagePath]
|
|
73
|
-
if (pageCfg) {
|
|
74
|
-
const pageRequest = stringifyRequest(pageCfg.resource)
|
|
75
|
-
if (pageCfg.async) {
|
|
76
|
-
tabBarPagesMap[pagePath] = `()=>import(${getAsyncChunkName(pageCfg.async)}${pageRequest}).then(res => getComponent(res, { __mpxPageRoute: ${JSON.stringify(pagePath)} }))`
|
|
77
|
-
} else {
|
|
78
|
-
tabBarPagesMap[pagePath] = `getComponent(require(${pageRequest}), { __mpxPageRoute: ${JSON.stringify(pagePath)} })`
|
|
79
|
-
}
|
|
80
|
-
} else {
|
|
81
|
-
emitWarning(`TabBar page path ${pagePath} is not exist in local page map, please check!`)
|
|
82
|
-
}
|
|
83
|
-
})
|
|
84
|
-
}
|
|
85
24
|
|
|
86
25
|
let output = '/* script */\n'
|
|
87
26
|
|
|
@@ -101,58 +40,7 @@ module.exports = function (script, {
|
|
|
101
40
|
return attrs
|
|
102
41
|
},
|
|
103
42
|
content (script) {
|
|
104
|
-
let content = `\n import
|
|
105
|
-
// add import
|
|
106
|
-
if (ctorType === 'app') {
|
|
107
|
-
content += ` import '@mpxjs/webpack-plugin/lib/runtime/base.styl'
|
|
108
|
-
import Vue from 'vue'
|
|
109
|
-
import VueRouter from 'vue-router'
|
|
110
|
-
import Mpx from '@mpxjs/core'
|
|
111
|
-
Vue.use(VueRouter)
|
|
112
|
-
global.getApp = function(){}
|
|
113
|
-
global.getCurrentPages = function(){
|
|
114
|
-
if(!global.__mpxRouter) return []
|
|
115
|
-
// @ts-ignore
|
|
116
|
-
return global.__mpxRouter.stack.map(item => {
|
|
117
|
-
let page
|
|
118
|
-
const vnode = item.vnode
|
|
119
|
-
if(vnode && vnode.componentInstance) {
|
|
120
|
-
page = vnode.tag.endsWith('mpx-tab-bar-container') ? vnode.componentInstance.$refs.tabBarPage : vnode.componentInstance
|
|
121
|
-
}
|
|
122
|
-
return page || { route: item.path.slice(1) }
|
|
123
|
-
})
|
|
124
|
-
}
|
|
125
|
-
global.__networkTimeout = ${JSON.stringify(jsonConfig.networkTimeout)}
|
|
126
|
-
global.__mpxGenericsMap = {}
|
|
127
|
-
global.__mpxOptionsMap = {}
|
|
128
|
-
global.__style = ${JSON.stringify(jsonConfig.style || 'v1')}
|
|
129
|
-
global.__mpxPageConfig = ${JSON.stringify(jsonConfig.window)}
|
|
130
|
-
global.__mpxTransRpxFn = ${webConfig.transRpxFn}\n`
|
|
131
|
-
if (i18n) {
|
|
132
|
-
const i18nObj = Object.assign({}, i18n)
|
|
133
|
-
content += ` import VueI18n from 'vue-i18n'
|
|
134
|
-
import { createI18n } from 'vue-i18n-bridge'
|
|
135
|
-
|
|
136
|
-
Vue.use(VueI18n , { bridge: true })\n`
|
|
137
|
-
const requestObj = {}
|
|
138
|
-
const i18nKeys = ['messages', 'dateTimeFormats', 'numberFormats']
|
|
139
|
-
i18nKeys.forEach((key) => {
|
|
140
|
-
if (i18nObj[`${key}Path`]) {
|
|
141
|
-
requestObj[key] = stringifyRequest(i18nObj[`${key}Path`])
|
|
142
|
-
delete i18nObj[`${key}Path`]
|
|
143
|
-
}
|
|
144
|
-
})
|
|
145
|
-
content += ` const i18nCfg = ${JSON.stringify(i18nObj)}\n`
|
|
146
|
-
Object.keys(requestObj).forEach((key) => {
|
|
147
|
-
content += ` i18nCfg.${key} = require(${requestObj[key]})\n`
|
|
148
|
-
})
|
|
149
|
-
content += ' i18nCfg.legacy = false\n'
|
|
150
|
-
content += ` const i18n = createI18n(i18nCfg, VueI18n)
|
|
151
|
-
Vue.use(i18n)
|
|
152
|
-
Mpx.i18n = i18n
|
|
153
|
-
\n`
|
|
154
|
-
}
|
|
155
|
-
}
|
|
43
|
+
let content = `\n import processComponentOption, { getComponent, getWxsMixin } from ${stringifyRequest(optionProcessorPath)}\n`
|
|
156
44
|
let hasApp = true
|
|
157
45
|
if (!appInfo.name) {
|
|
158
46
|
hasApp = false
|
|
@@ -166,63 +54,10 @@ module.exports = function (script, {
|
|
|
166
54
|
content += ` wxsModules.${module} = ${expression}\n`
|
|
167
55
|
})
|
|
168
56
|
}
|
|
169
|
-
let firstPage = ''
|
|
170
|
-
const pagesMap = {}
|
|
171
|
-
const componentsMap = {}
|
|
172
|
-
Object.keys(localPagesMap).forEach((pagePath) => {
|
|
173
|
-
const pageCfg = localPagesMap[pagePath]
|
|
174
|
-
const pageRequest = stringifyRequest(pageCfg.resource)
|
|
175
|
-
if (tabBarMap && tabBarMap[pagePath]) {
|
|
176
|
-
pagesMap[pagePath] = `getComponent(require(${stringifyRequest(tabBarContainerPath)}), { __mpxBuiltIn: true })`
|
|
177
|
-
} else {
|
|
178
|
-
if (pageCfg.async) {
|
|
179
|
-
pagesMap[pagePath] = `()=>import(${getAsyncChunkName(pageCfg.async)} ${pageRequest}).then(res => getComponent(res, { __mpxPageRoute: ${JSON.stringify(pagePath)} }))`
|
|
180
|
-
} else {
|
|
181
|
-
// 为了保持小程序中app->page->component的js执行顺序,所有的page和component都改为require引入
|
|
182
|
-
pagesMap[pagePath] = `getComponent(require(${pageRequest}), { __mpxPageRoute: ${JSON.stringify(pagePath)} })`
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
57
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
189
|
-
})
|
|
190
|
-
|
|
191
|
-
Object.keys(localComponentsMap).forEach((componentName) => {
|
|
192
|
-
const componentCfg = localComponentsMap[componentName]
|
|
193
|
-
const componentRequest = stringifyRequest(componentCfg.resource)
|
|
194
|
-
if (componentCfg.async) {
|
|
195
|
-
componentsMap[componentName] = `()=>import(${getAsyncChunkName(componentCfg.async)}${componentRequest}).then(res => getComponent(res))`
|
|
196
|
-
} else {
|
|
197
|
-
componentsMap[componentName] = `getComponent(require(${componentRequest}))`
|
|
198
|
-
}
|
|
199
|
-
})
|
|
200
|
-
|
|
201
|
-
Object.keys(builtInComponentsMap).forEach((componentName) => {
|
|
202
|
-
const componentCfg = builtInComponentsMap[componentName]
|
|
203
|
-
const componentRequest = stringifyRequest(componentCfg.resource)
|
|
204
|
-
componentsMap[componentName] = `getComponent(require(${componentRequest}), { __mpxBuiltIn: true })`
|
|
205
|
-
})
|
|
206
|
-
|
|
207
|
-
content += ` global.currentModuleId = ${JSON.stringify(moduleId)}\n`
|
|
208
|
-
content += ` global.currentSrcMode = ${JSON.stringify(scriptSrcMode)}\n`
|
|
209
|
-
content += ` global.currentInject = ${JSON.stringify({ moduleId })}\n`
|
|
210
|
-
if (!isProduction) {
|
|
211
|
-
content += ` global.currentResource = ${JSON.stringify(loaderContext.resourcePath)}\n`
|
|
212
|
-
}
|
|
58
|
+
// 获取组件集合
|
|
59
|
+
const componentsMap = buildComponentsMap({ localComponentsMap, builtInComponentsMap, loaderContext })
|
|
213
60
|
|
|
214
|
-
content += ' /** script content **/\n'
|
|
215
|
-
|
|
216
|
-
// 传递ctorType以补全js内容
|
|
217
|
-
const extraOptions = {
|
|
218
|
-
ctorType,
|
|
219
|
-
lang: script.lang || 'js'
|
|
220
|
-
}
|
|
221
|
-
// todo 仅靠vueContentCache保障模块唯一性还是不够严谨,后续需要考虑去除原始query后构建request
|
|
222
|
-
content += ` ${getRequire('script', script, extraOptions)}\n`
|
|
223
|
-
|
|
224
|
-
// createApp/Page/Component执行完成后立刻获取当前的option并暂存
|
|
225
|
-
content += ` const currentOption = global.__mpxOptionsMap[${JSON.stringify(moduleId)}]\n`
|
|
226
61
|
// 获取pageConfig
|
|
227
62
|
const pageConfig = {}
|
|
228
63
|
if (ctorType === 'page') {
|
|
@@ -237,36 +72,21 @@ module.exports = function (script, {
|
|
|
237
72
|
pageConfig[key] = jsonConfig[key]
|
|
238
73
|
})
|
|
239
74
|
}
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
// 通过processOption进行组件注册和路由注入
|
|
249
|
-
content += ` export default processOption(
|
|
250
|
-
currentOption,
|
|
251
|
-
${JSON.stringify(ctorType)},
|
|
252
|
-
${JSON.stringify(firstPage)},
|
|
253
|
-
${JSON.stringify(outputPath)},
|
|
254
|
-
${JSON.stringify(pageConfig)},
|
|
255
|
-
// @ts-ignore
|
|
256
|
-
${shallowStringify(pagesMap)},
|
|
75
|
+
|
|
76
|
+
content += buildGlobalParams({ moduleId, scriptSrcMode, loaderContext, isProduction })
|
|
77
|
+
content += getRequireScript({ ctorType, script, loaderContext })
|
|
78
|
+
content += ` export default processComponentOption({
|
|
79
|
+
option: global.__mpxOptionsMap[${JSON.stringify(moduleId)}],
|
|
80
|
+
ctorType: ${JSON.stringify(ctorType)},
|
|
81
|
+
outputPath: ${JSON.stringify(outputPath)},
|
|
82
|
+
pageConfig: ${JSON.stringify(pageConfig)},
|
|
257
83
|
// @ts-ignore
|
|
258
|
-
${shallowStringify(componentsMap)},
|
|
259
|
-
${JSON.stringify(
|
|
260
|
-
${JSON.stringify(
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
if (ctorType === 'app') {
|
|
265
|
-
content += `,
|
|
266
|
-
Vue,
|
|
267
|
-
VueRouter`
|
|
268
|
-
}
|
|
269
|
-
content += '\n )\n'
|
|
84
|
+
componentsMap: ${shallowStringify(componentsMap)},
|
|
85
|
+
componentGenerics: ${JSON.stringify(componentGenerics)},
|
|
86
|
+
genericsInfo: ${JSON.stringify(genericsInfo)},
|
|
87
|
+
mixin: getWxsMixin(wxsModules),
|
|
88
|
+
hasApp: ${hasApp}`
|
|
89
|
+
content += '\n })\n'
|
|
270
90
|
return content
|
|
271
91
|
}
|
|
272
92
|
})
|
|
@@ -38,7 +38,8 @@ module.exports = function (template, {
|
|
|
38
38
|
wxsContentMap,
|
|
39
39
|
decodeHTMLText,
|
|
40
40
|
externalClasses,
|
|
41
|
-
checkUsingComponents
|
|
41
|
+
checkUsingComponents,
|
|
42
|
+
webConfig
|
|
42
43
|
// autoVirtualHostRules
|
|
43
44
|
} = mpx
|
|
44
45
|
const { resourcePath } = parseRequest(loaderContext.resource)
|
|
@@ -48,9 +49,11 @@ module.exports = function (template, {
|
|
|
48
49
|
let output = '/* template */\n'
|
|
49
50
|
|
|
50
51
|
if (ctorType === 'app') {
|
|
52
|
+
const { el } = webConfig
|
|
53
|
+
const idName = el?.match(/#(.*)/)?.[1] || 'app'
|
|
51
54
|
template = {
|
|
52
55
|
tag: 'template',
|
|
53
|
-
content:
|
|
56
|
+
content: `<div id="${idName}" class="app"><mpx-keep-alive><router-view class="page"></router-view></mpx-keep-alive></div>`
|
|
54
57
|
}
|
|
55
58
|
builtInComponentsMap['mpx-keep-alive'] = {
|
|
56
59
|
resource: addQuery('@mpxjs/webpack-plugin/lib/runtime/components/web/mpx-keep-alive.vue', { isComponent: true })
|
|
@@ -0,0 +1,195 @@
|
|
|
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 }) {
|
|
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 (pageCfg.isFirst) {
|
|
104
|
+
firstPage = pagePath
|
|
105
|
+
}
|
|
106
|
+
})
|
|
107
|
+
return {
|
|
108
|
+
pagesMap,
|
|
109
|
+
firstPage,
|
|
110
|
+
globalTabBar
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function getRequireScript ({ ctorType, script, loaderContext }) {
|
|
115
|
+
let content = ' /** script content **/\n'
|
|
116
|
+
const extraOptions = { ctorType, lang: script.lang || 'js' }
|
|
117
|
+
const { getRequire } = createHelpers(loaderContext)
|
|
118
|
+
content += ` ${getRequire('script', script, extraOptions)}\n`
|
|
119
|
+
return content
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function buildGlobalParams ({ moduleId, scriptSrcMode, loaderContext, isProduction, jsonConfig, webConfig, isMain, globalTabBar }) {
|
|
123
|
+
let content = ''
|
|
124
|
+
if (isMain) {
|
|
125
|
+
content += `global.getApp = function(){}
|
|
126
|
+
global.getCurrentPages = function () {
|
|
127
|
+
if (!(typeof window !== 'undefined')) {
|
|
128
|
+
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')
|
|
129
|
+
return
|
|
130
|
+
}
|
|
131
|
+
if (!global.__mpxRouter) return []
|
|
132
|
+
// @ts-ignore
|
|
133
|
+
return global.__mpxRouter.stack.map(item => {
|
|
134
|
+
let page
|
|
135
|
+
const vnode = item.vnode
|
|
136
|
+
if (vnode && vnode.componentInstance) {
|
|
137
|
+
page = vnode.tag.endsWith('mpx-tab-bar-container') ? vnode.componentInstance.$refs.tabBarPage : vnode.componentInstance
|
|
138
|
+
}
|
|
139
|
+
return page || { route: item.path.slice(1) }
|
|
140
|
+
})
|
|
141
|
+
}
|
|
142
|
+
global.__networkTimeout = ${JSON.stringify(jsonConfig.networkTimeout)}
|
|
143
|
+
global.__mpxGenericsMap = {}
|
|
144
|
+
global.__mpxOptionsMap = {}
|
|
145
|
+
global.__style = ${JSON.stringify(jsonConfig.style || 'v1')}
|
|
146
|
+
global.__mpxPageConfig = ${JSON.stringify(jsonConfig.window)}
|
|
147
|
+
global.__mpxTransRpxFn = ${webConfig.transRpxFn}\n`
|
|
148
|
+
if (globalTabBar) {
|
|
149
|
+
content += globalTabBar
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
content += ` global.currentModuleId = ${JSON.stringify(moduleId)}\n`
|
|
153
|
+
content += ` global.currentSrcMode = ${JSON.stringify(scriptSrcMode)}\n`
|
|
154
|
+
content += ` global.currentInject = ${JSON.stringify({ moduleId })}\n`
|
|
155
|
+
if (!isProduction) {
|
|
156
|
+
content += ` global.currentResource = ${JSON.stringify(loaderContext.resourcePath)}\n`
|
|
157
|
+
}
|
|
158
|
+
return content
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function buildI18n ({ i18n, loaderContext }) {
|
|
162
|
+
let i18nContent = ''
|
|
163
|
+
const i18nObj = Object.assign({}, i18n)
|
|
164
|
+
i18nContent += ` import VueI18n from 'vue-i18n'
|
|
165
|
+
import { createI18n } from 'vue-i18n-bridge'
|
|
166
|
+
Vue.use(VueI18n , { bridge: true })\n`
|
|
167
|
+
const requestObj = {}
|
|
168
|
+
const i18nKeys = ['messages', 'dateTimeFormats', 'numberFormats']
|
|
169
|
+
i18nKeys.forEach((key) => {
|
|
170
|
+
if (i18nObj[`${key}Path`]) {
|
|
171
|
+
requestObj[key] = stringifyRequest(loaderContext, i18nObj[`${key}Path`])
|
|
172
|
+
delete i18nObj[`${key}Path`]
|
|
173
|
+
}
|
|
174
|
+
})
|
|
175
|
+
i18nContent += ` const i18nCfg = ${JSON.stringify(i18nObj)}\n`
|
|
176
|
+
Object.keys(requestObj).forEach((key) => {
|
|
177
|
+
i18nContent += ` i18nCfg.${key} = require(${requestObj[key]})\n`
|
|
178
|
+
})
|
|
179
|
+
i18nContent += ' i18nCfg.legacy = false\n'
|
|
180
|
+
i18nContent += ` const i18n = createI18n(i18nCfg, VueI18n)
|
|
181
|
+
Vue.use(i18n)
|
|
182
|
+
Mpx.i18n = i18n\n`
|
|
183
|
+
return i18nContent
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
module.exports = {
|
|
187
|
+
buildPagesMap,
|
|
188
|
+
buildComponentsMap,
|
|
189
|
+
getRequireScript,
|
|
190
|
+
buildGlobalParams,
|
|
191
|
+
shallowStringify,
|
|
192
|
+
getAsyncChunkName,
|
|
193
|
+
stringifyRequest,
|
|
194
|
+
buildI18n
|
|
195
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mpxjs/webpack-plugin",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.9.0-beta.0",
|
|
4
4
|
"description": "mpx compile core",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"mpx"
|
|
@@ -82,5 +82,5 @@
|
|
|
82
82
|
"engines": {
|
|
83
83
|
"node": ">=14.14.0"
|
|
84
84
|
},
|
|
85
|
-
"gitHead": "
|
|
85
|
+
"gitHead": "67b611aeae99f916d46ca8d5d9ed784ddf53f05c"
|
|
86
86
|
}
|