Skip to content

Commit 604d3e9

Browse files
authored
fix: React next fails on build (#726) (#732)
* fix: Fix build on react@next * Working on ^16.9 and lower than 16.8 * Solution works in 16.8 also * Fixes from code review * Import only the function we need
1 parent 3c9d7b4 commit 604d3e9

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
"license": "MIT",
4545
"dependencies": {
4646
"@babel/runtime": "^7.10.3",
47-
"@testing-library/dom": "^7.17.1"
47+
"@testing-library/dom": "^7.17.1",
48+
"semver": "^7.3.2"
4849
},
4950
"devDependencies": {
5051
"@reach/router": "^1.3.3",

src/flush-microtasks.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import React from 'react'
2+
import satisfies from 'semver/functions/satisfies'
3+
14
/* istanbul ignore file */
25
// the part of this file that we need tested is definitely being run
36
// and the part that is not cannot easily have useful tests written
@@ -15,8 +18,15 @@ function getIsUsingFakeTimers() {
1518
)
1619
}
1720

21+
const globalObj = typeof window === 'undefined' ? global : window
22+
let Scheduler = globalObj.Scheduler
23+
const isModernScheduleCallbackSupported = satisfies(React.version, '>16.8.6', {
24+
includePrerelease: true,
25+
})
26+
1827
let didWarnAboutMessageChannel = false
1928
let enqueueTask
29+
2030
try {
2131
// read require off the module object to get around the bundlers.
2232
// we don't want them to detect a require and bundle a Node polyfill.
@@ -25,6 +35,8 @@ try {
2535
// assuming we're in node, let's try to get node's
2636
// version of setImmediate, bypassing fake timers if any.
2737
enqueueTask = nodeRequire.call(module, 'timers').setImmediate
38+
// import React's scheduler so we'll be able to schedule our tasks later on.
39+
Scheduler = nodeRequire.call(module, 'scheduler')
2840
} catch (_err) {
2941
// we're in a browser
3042
// we can't use regular timers because they may still be faked
@@ -49,6 +61,20 @@ try {
4961
}
5062
}
5163

64+
function scheduleCallback(cb) {
65+
const NormalPriority = Scheduler
66+
? Scheduler.NormalPriority || Scheduler.unstable_NormalPriority
67+
: null
68+
69+
const scheduleFn = Scheduler
70+
? Scheduler.scheduleCallback || Scheduler.unstable_scheduleCallback
71+
: callback => callback()
72+
73+
return isModernScheduleCallbackSupported
74+
? scheduleFn(NormalPriority, cb)
75+
: scheduleFn(cb)
76+
}
77+
5278
export default function flushMicroTasks() {
5379
return {
5480
then(resolve) {
@@ -59,7 +85,11 @@ export default function flushMicroTasks() {
5985
jest.advanceTimersByTime(0)
6086
resolve()
6187
} else {
62-
enqueueTask(resolve)
88+
scheduleCallback(() => {
89+
enqueueTask(() => {
90+
resolve()
91+
})
92+
})
6393
}
6494
},
6595
}

0 commit comments

Comments
 (0)