-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy pathscript-helper.js
More file actions
213 lines (200 loc) · 8.16 KB
/
Copy pathscript-helper.js
File metadata and controls
213 lines (200 loc) · 8.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const loaderUtils = require('loader-utils')
const createHelpers = require('../helpers')
const parseRequest = require('../utils/parse-request')
const shallowStringify = require('../utils/shallow-stringify')
const normalize = require('../utils/normalize')
const addQuery = require('../utils/add-query')
function stringifyRequest (loaderContext, request) {
return loaderUtils.stringifyRequest(loaderContext, request)
}
function getAsyncChunkName (chunkName) {
if (chunkName && typeof chunkName !== 'boolean') {
return `/* webpackChunkName: "${chunkName}/index" */`
}
return ''
}
function getAsyncSuspense (type, moduleId, componentRequest, componentName, chunkName, getFallback, getLoading) {
return `getAsyncSuspense({
type: ${JSON.stringify(type)},
moduleId: ${JSON.stringify(moduleId)},
chunkName: ${JSON.stringify(chunkName)},
${getFallback ? `getFallback: ${getFallback},` : ''}
${getLoading ? `getLoading: ${getLoading},` : ''}
getChildren () {
return import(${getAsyncChunkName(chunkName)}${componentRequest}).then(function (res) {
return getComponent(res, {displayName: ${JSON.stringify(componentName)}})
})
}
})`
}
function getComponent (componentRequest, componentName) {
return `getComponent(require(${componentRequest}), {displayName: ${JSON.stringify(componentName)}})`
}
function getBuiltInComponent (componentRequest) {
return `getComponent(require(${componentRequest}), {__mpxBuiltIn: true})`
}
// function getLazyPage (componentRequest) {
// return `getLazyPage(${getComponentGetter(getComponent(componentRequest, 'Page'))})`
// }
function getComponentGetter (component) {
return `function(){ return ${component} }`
}
function buildPagesMap ({ localPagesMap, loaderContext, jsonConfig, rnConfig }) {
let firstPage = ''
const pagesMap = {}
const mpx = loaderContext.getMpx()
Object.keys(localPagesMap).forEach((pagePath) => {
const pageCfg = localPagesMap[pagePath]
const pageRequest = stringifyRequest(loaderContext, pageCfg.resource)
if (pageCfg.async && rnConfig.supportSubpackage) {
const moduleId = mpx.getModuleId(pageCfg.resource)
const getFallback = rnConfig.asyncChunk && rnConfig.asyncChunk.fallback && getComponentGetter(getComponent(stringifyRequest(loaderContext, addQuery(rnConfig.asyncChunk.fallback, { isComponent: true })), 'PageFallback'))
const getLoading = rnConfig.asyncChunk && rnConfig.asyncChunk.loading && getComponentGetter(getComponent(stringifyRequest(loaderContext, addQuery(rnConfig.asyncChunk.loading, { isComponent: true })), 'PageLoading'))
pagesMap[pagePath] = getAsyncSuspense('page', moduleId, pageRequest, 'Page', pageCfg.async, getFallback, getLoading)
} else {
// 为了保持小程序中app->page->component的js执行顺序,所有的page和component都改为require引入
pagesMap[pagePath] = getComponentGetter(getComponent(pageRequest, 'Page'))
}
if (pagePath === jsonConfig.entryPagePath) {
firstPage = pagePath
}
if (!firstPage && pageCfg.isFirst) {
firstPage = pagePath
}
})
return {
pagesMap,
firstPage
}
}
function buildComponentsMap ({ localComponentsMap, builtInComponentsMap, loaderContext, jsonConfig, rnConfig }) {
const componentsMap = {}
const mpx = loaderContext.getMpx()
if (localComponentsMap) {
Object.keys(localComponentsMap).forEach((componentName) => {
const componentCfg = localComponentsMap[componentName]
const componentRequest = stringifyRequest(loaderContext, componentCfg.resource)
if (componentCfg.async && rnConfig.supportSubpackage) {
const moduleId = mpx.getModuleId(componentCfg.resource)
const placeholder = jsonConfig.componentPlaceholder && jsonConfig.componentPlaceholder[componentName]
let getFallback
if (placeholder) {
if (localComponentsMap[placeholder]) {
const placeholderCfg = localComponentsMap[placeholder]
if (placeholderCfg.async) {
loaderContext.emitWarning(
new Error(`[json processor][${loaderContext.resource}]: componentPlaceholder ${placeholder} should not be a async component, please check!`)
)
}
const placeholderRequest = stringifyRequest(loaderContext, placeholderCfg.resource)
getFallback = getComponentGetter(getComponent(placeholderRequest, placeholder))
} else {
loaderContext.emitError(
new Error(`[json processor][${loaderContext.resource}]: componentPlaceholder ${placeholder} is not built-in component or custom component, please check!`)
)
}
} else {
loaderContext.emitError(
new Error(`[json processor][${loaderContext.resource}]: ${componentName} has no componentPlaceholder, please check!`)
)
}
componentsMap[componentName] = getAsyncSuspense('component', moduleId, componentRequest, componentName, componentCfg.async, getFallback)
} else {
componentsMap[componentName] = getComponentGetter(getComponent(componentRequest, componentName))
}
})
}
if (builtInComponentsMap) {
Object.keys(builtInComponentsMap).forEach((componentName) => {
const componentCfg = builtInComponentsMap[componentName]
const componentRequest = stringifyRequest(loaderContext, componentCfg.resource)
componentsMap[componentName] = getComponentGetter(getBuiltInComponent(componentRequest))
})
}
return componentsMap
}
function getRequireScript ({ script, ctorType, loaderContext }) {
let content = '/** script content **/\n'
const { getRequire } = createHelpers(loaderContext)
const { resourcePath, queryObj } = parseRequest(loaderContext.resource)
const extraOptions = {
...script.src
? { ...queryObj, resourcePath }
: null,
ctorType,
lang: script.lang || 'js'
}
content += `${getRequire('script', script, extraOptions)}\n`
return content
}
function buildGlobalParams ({
moduleId,
scriptSrcMode,
loaderContext,
isProduction,
ctorType,
jsonConfig,
componentsMap,
pagesMap,
firstPage,
outputPath,
genericsInfo,
hasApp
}) {
let content = ''
if (ctorType === 'app') {
content += `
global.getApp = function () {}
global.getCurrentPages = function () { return [] }
global.__networkTimeout = ${JSON.stringify(jsonConfig.networkTimeout)}
global.__mpxGenericsMap = {}
global.__mpxOptionsMap = {}
global.__mpxPagesMap = {}
global.__style = ${JSON.stringify(jsonConfig.style || 'v1')}
global.__mpxPageConfig = ${JSON.stringify(jsonConfig.window)}
global.__appComponentsMap = ${shallowStringify(componentsMap)}
global.__preloadRule = ${JSON.stringify(jsonConfig.preloadRule)}
global.currentInject.pagesMap = ${shallowStringify(pagesMap)}
global.currentInject.firstPage = ${JSON.stringify(firstPage)}\n`
} else {
if (ctorType === 'page') {
const pageConfig = Object.assign({}, jsonConfig)
delete pageConfig.usingComponents
content += `global.currentInject.pageConfig = ${JSON.stringify(pageConfig)}\n`
}
content += `
var componentsMap = ${shallowStringify(componentsMap)}
global.currentInject.componentsMap = componentsMap\n`
if (genericsInfo) {
if (!hasApp) {
content += 'global.__mpxGenericsMap = global.__mpxGenericsMap || {}\n'
}
content += `
const genericHash = ${JSON.stringify(genericsInfo.hash)}\n
global.__mpxGenericsMap[genericHash] = componentsMap\n`
}
if (ctorType === 'component') {
content += `global.currentInject.componentPath = '/' + ${JSON.stringify(outputPath)}\n`
}
}
content += `global.currentModuleId = ${JSON.stringify(moduleId)}\n`
content += `global.currentSrcMode = ${JSON.stringify(scriptSrcMode)}\n`
if (!isProduction) {
content += `global.currentResource = ${JSON.stringify(loaderContext.resourcePath)}\n`
}
return content
}
function buildI18n ({ loaderContext }) {
const i18nWxsPath = normalize.lib('runtime/i18n.wxs')
const i18nWxsLoaderPath = normalize.lib('wxs/i18n-loader.js')
const i18nWxsRequest = i18nWxsLoaderPath + '!' + i18nWxsPath
return `require(${stringifyRequest(loaderContext, i18nWxsRequest)})\n`
}
module.exports = {
buildPagesMap,
buildComponentsMap,
getRequireScript,
buildGlobalParams,
stringifyRequest,
buildI18n
}