@mpxjs/webpack-plugin 2.8.42 → 2.9.0-beta.1

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,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,12 +1,12 @@
1
1
  {
2
2
  "name": "@mpxjs/webpack-plugin",
3
- "version": "2.8.42",
3
+ "version": "2.9.0-beta.1",
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": "a0d7f0171b8e4535f1962bdb7415fec0d25abe93"
85
+ "gitHead": "5e4b44c9b8132e9192cfd119f63196ecc6c4486c"
86
86
  }