Skip to content

Commit 17329ed

Browse files
committed
Handle require.ensure errors well.
Introduce a new Promise library which runs on the same eventloop.
1 parent b5a03a3 commit 17329ed

File tree

2 files changed

+111
-26
lines changed

2 files changed

+111
-26
lines changed

lib/dynamic.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,89 @@ export function flushChunks () {
5353
currentChunks = []
5454
return chunks
5555
}
56+
57+
export class SameLoopPromise {
58+
constructor (cb) {
59+
this.onResultCallbacks = []
60+
this.onErrorCallbacks = []
61+
62+
if (cb) {
63+
cb(
64+
(result) => this.setResult(result),
65+
(error) => this.setError(error)
66+
)
67+
}
68+
}
69+
70+
setResult (result) {
71+
this.gotResult = true
72+
this.result = result
73+
this.onResultCallbacks.forEach((cb) => cb(result))
74+
this.onResultCallbacks = []
75+
}
76+
77+
setError (error) {
78+
this.gotError = true
79+
this.error = error
80+
this.onErrorCallbacks.forEach((cb) => cb(error))
81+
this.onErrorCallbacks = []
82+
}
83+
84+
then (onResult, onError) {
85+
const promise = new SameLoopPromise()
86+
87+
const handleError = () => {
88+
if (onError) {
89+
promise.setResult(onError(this.error))
90+
} else {
91+
promise.setError(this.error)
92+
}
93+
}
94+
95+
const handleResult = () => {
96+
promise.setResult(onResult(this.result))
97+
}
98+
99+
if (this.gotResult) {
100+
handleResult()
101+
return promise
102+
}
103+
104+
if (this.gotError) {
105+
handleError()
106+
return promise
107+
}
108+
109+
this.onResultCallbacks.push(handleResult)
110+
this.onErrorCallbacks.push(handleError)
111+
112+
return promise
113+
}
114+
115+
catch (onError) {
116+
const promise = new SameLoopPromise()
117+
118+
const handleError = () => {
119+
promise.setResult(onError(this.error))
120+
}
121+
122+
const handleResult = () => {
123+
promise.setResult(this.result)
124+
}
125+
126+
if (this.gotResult) {
127+
handleResult()
128+
return promise
129+
}
130+
131+
if (this.gotError) {
132+
handleError()
133+
return promise
134+
}
135+
136+
this.onErrorCallbacks.push(handleError)
137+
this.onResultCallbacks.push(handleResult)
138+
139+
return promise
140+
}
141+
}

server/build/babel/plugins/handle-import.js

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,34 @@ const TYPE_IMPORT = 'Import'
88

99
const buildImport = (args) => (template(`
1010
(
11-
typeof window === 'undefined' ?
12-
{
13-
then(cb) {
14-
eval('require.ensure = function (deps, callback) { callback(require) }')
15-
require.ensure([], (require) => {
16-
let m = require(SOURCE)
17-
m = m.default || m
18-
m.__webpackChunkName = '${args.name}.js'
19-
cb(m);
20-
}, 'chunks/${args.name}.js');
21-
},
22-
catch() {}
23-
} :
24-
{
25-
then(cb) {
26-
const weakId = require.resolveWeak(SOURCE)
27-
try {
28-
const weakModule = __webpack_require__(weakId)
29-
return cb(weakModule.default || weakModule)
30-
} catch (err) {}
11+
typeof window === 'undefined' ?
12+
new (require('next/dynamic').SameLoopPromise)((resolve, reject) => {
13+
eval('require.ensure = function (deps, callback) { callback(require) }')
14+
require.ensure([], (require) => {
15+
let m = require(SOURCE)
16+
m = m.default || m
17+
m.__webpackChunkName = '${args.name}.js'
18+
resolve(m);
19+
}, 'chunks/${args.name}.js');
20+
})
21+
:
22+
new (require('next/dynamic').SameLoopPromise)((resolve, reject) => {
23+
const weakId = require.resolveWeak(SOURCE)
24+
try {
25+
const weakModule = __webpack_require__(weakId)
26+
return resolve(weakModule.default || weakModule)
27+
} catch (err) {}
3128
32-
require.ensure([], (require) => {
29+
require.ensure([], (require) => {
30+
try {
3331
let m = require(SOURCE)
3432
m = m.default || m
35-
cb(m);
36-
}, 'chunks/${args.name}.js');
37-
},
38-
catch () {}
39-
}
33+
resolve(m)
34+
} catch(error) {
35+
reject(error)
36+
}
37+
}, 'chunks/${args.name}.js');
38+
})
4039
)
4140
`))
4241

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy