-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathrequireUniversalModule.js
233 lines (199 loc) · 5.35 KB
/
requireUniversalModule.js
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// @flow
import type {
Tools,
ModuleOptions,
Ids,
Config,
ConfigFunc,
Props,
Load
} from './flowTypes'
import {
isWebpack,
tryRequire,
resolveExport,
callForString,
loadFromCache,
loadFromPromiseCache,
cacheProm,
isServer,
isTest
} from './utils'
declare var __webpack_require__: Function
declare var __webpack_modules__: Object
export const CHUNK_NAMES = new Set()
export const MODULE_IDS = new Set()
export default function requireUniversalModule<Props: Props>(
universalConfig: Config | ConfigFunc,
options: ModuleOptions,
props: Props,
prevProps?: Props
): Tools {
const {
key,
timeout = 15000,
onLoad,
onError,
isDynamic,
modCache,
promCache,
usesBabelPlugin
} = options
const config = getConfig(isDynamic, universalConfig, options, props)
const { chunkName, path, resolve, load } = config
const asyncOnly = (!path && !resolve) || typeof chunkName === 'function'
const requireSync = (props: Object, context: Object): ?any => {
let exp = loadFromCache(chunkName, props, modCache)
if (!exp) {
let mod
if (!isWebpack() && path) {
const modulePath = callForString(path, props) || ''
mod = tryRequire(modulePath)
}
else if (isWebpack() && resolve) {
const weakId = callForString(resolve, props)
if (__webpack_modules__[weakId]) {
mod = tryRequire(weakId)
}
}
if (mod) {
exp = resolveExport(
mod,
key,
onLoad,
chunkName,
props,
context,
modCache,
true
)
}
}
return exp
}
const requireAsync = (props: Object, context: Object): Promise<?any> => {
const exp = loadFromCache(chunkName, props, modCache)
if (exp) return Promise.resolve(exp)
const cachedPromise = loadFromPromiseCache(chunkName, props, promCache)
if (cachedPromise) return cachedPromise
const prom = new Promise((res, rej) => {
const reject = error => {
error = error || new Error('timeout exceeded')
clearTimeout(timer)
if (onError) {
const isServer = typeof window === 'undefined'
const info = { isServer }
onError(error, info)
}
rej(error)
}
// const timer = timeout && setTimeout(reject, timeout)
const timer = timeout && setTimeout(reject, timeout)
const resolve = mod => {
clearTimeout(timer)
const exp = resolveExport(
mod,
key,
onLoad,
chunkName,
props,
context,
modCache
)
if (exp) return res(exp)
reject(new Error('export not found'))
}
const request = load(props, { resolve, reject })
// if load doesn't return a promise, it must call resolveImport
// itself. Most common is the promise implementation below.
if (!request || typeof request.then !== 'function') return
request.then(resolve).catch(reject)
})
cacheProm(prom, chunkName, props, promCache)
return prom
}
const addModule = (props: Object): ?string => {
if (isServer || isTest) {
if (chunkName) {
let name = callForString(chunkName, props)
if (usesBabelPlugin) {
// if ignoreBabelRename is true, don't apply regex
const shouldKeepName = options && !!options.ignoreBabelRename
if (!shouldKeepName) {
name = name.replace(/\//g, '-')
}
}
if (name) CHUNK_NAMES.add(name)
if (!isTest) return name // makes tests way smaller to run both kinds
}
if (isWebpack()) {
const weakId = callForString(resolve, props)
if (weakId) MODULE_IDS.add(weakId)
return weakId
}
if (!isWebpack()) {
const modulePath = callForString(path, props)
if (modulePath) MODULE_IDS.add(modulePath)
return modulePath
}
}
}
const shouldUpdate = (next, prev): boolean => {
const cacheKey = callForString(chunkName, next)
const config = getConfig(isDynamic, universalConfig, options, prev)
const prevCacheKey = callForString(config.chunkName, prev)
return cacheKey !== prevCacheKey
}
return {
requireSync,
requireAsync,
addModule,
shouldUpdate,
asyncOnly
}
}
export const flushChunkNames = (): Ids => {
const chunks = Array.from(CHUNK_NAMES)
CHUNK_NAMES.clear()
return chunks
}
export const flushModuleIds = (): Ids => {
const ids = Array.from(MODULE_IDS)
MODULE_IDS.clear()
return ids
}
export const clearChunks = (): void => {
CHUNK_NAMES.clear()
MODULE_IDS.clear()
}
const getConfig = (
isDynamic: ?boolean,
universalConfig: Config | ConfigFunc,
options: ModuleOptions,
props: Props
): Config => {
if (isDynamic) {
let resultingConfig =
typeof universalConfig === 'function'
? universalConfig(props)
: universalConfig
if (options) {
resultingConfig = { ...resultingConfig, ...options }
}
return resultingConfig
}
const load: Load =
typeof universalConfig === 'function'
? universalConfig
: // $FlowIssue
() => universalConfig
return {
file: 'default',
id: options.id || 'default',
chunkName: options.chunkName || 'default',
resolve: options.resolve || '',
path: options.path || '',
load,
ignoreBabelRename: true
}
}