-
Notifications
You must be signed in to change notification settings - Fork 18
/
wrapAsyncRequire.ejs
94 lines (89 loc) · 3.44 KB
/
wrapAsyncRequire.ejs
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
const asyncRequire = require('<%= options.asyncRequirePath %>')
// runtime as small as possible
const urlJoin = require('general-tools/src/url-join')
const chunkModuleIdToHashMap = require('<%= options.packageName %>/src/tpl/chunkModuleIdToHashMap')
/**
* load additional a single chunk
* @param { string } chunkId
* @returns
*/
const requireEnsure = (chunkId) => {
// stores loaded and loading chunk
// undefined = chunk not loaded, null = chunk preloaded/prefetched
// [resolve, reject, promise] = chunk loading, 0 = chunk loaded
const installedChunks = global.installedChunks = global.installedChunks || {
// 'buz.ios.bundle': 0 // [chunkId]: [state]
}
const promises = []
let installedChunkData = installedChunks[chunkId]
if (installedChunkData !== 0) { // chunkId load is not complete
if (installedChunkData) { // [] loading
promises.push(installedChunkData[2])
} else { // undefined not loaded
const promise = new Promise(function(resolve, reject) {
installedChunkData = installedChunks[chunkId] = [resolve, reject]
})
promises.push(installedChunkData[2] = promise)
const error = new Error() // create errors before stack expansion so that useful stack traces can be obtained later
const resourceUri = urlJoin(
global.<%= options.globalInlayVarName %>.publicPath,
'<%= options.relativeChunkDir %>',
chunkModuleIdToHashMap[chunkId]['hash'] + '<%= options.fileSuffix %>'
)
let timeoutId
const onComplete = ({ code, data, msg }) => { // fetch success or failure | js timeout
clearTimeout(timeoutId)
const state = installedChunks[chunkId]
if (state === undefined) return // js timeout but fetch give the results
const [resolve, reject] = state
if (code !== 200) { // failure
error.message = msg
error.type = error.name = 'ChunkLoadError'
error.request = resourceUri
installedChunks[chunkId] = undefined
return reject(error)
}
// successful
// compared eval(data) Faster and safer https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
Function(`"use strict"; ${data}`)()
installedChunks[chunkId] = 0
resolve()
}
// timeout handling
const timeoutTime = <%= options.timeoutTime %>
timeoutId = setTimeout(
onComplete.bind(null, {
code: 0,
msg: `load chunk:${chunkId} failure(timeout:${timeoutTime})`
}),
timeoutTime
)
fetch(resourceUri).then(res => {
if (res.status === 200) {
res.text().then(res => {
onComplete({ code: 200, data: res })
}).catch(err => {
onComplete({ code: 0, msg: err.message })
})
} else {
onComplete({ code: res.status, msg: res.statusText })
}
}).catch(err => {
onComplete({ code: 0, msg: err.message })
})
}
}
return Promise.all(promises)
}
const wrapAsyncRequire = async moduleId => {
const hashMap = chunkModuleIdToHashMap[moduleId]
if (!hashMap) { // a module is knocked down into the main package, But there are places for asynchrony
await Promise.resolve()
} else if (Array.isArray(hashMap)) { // TODO the reserved
await Promise.all(hashMap.map(v => requireEnsure(v)))
} else {
await requireEnsure(moduleId)
}
return asyncRequire(moduleId)
}
module.exports = wrapAsyncRequire