-
Notifications
You must be signed in to change notification settings - Fork 2
/
rsc.js
89 lines (82 loc) · 3.65 KB
/
rsc.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
// @ts-check
if (0) {
// Trick cjs-module-lexer into adding named exports for all React exports.
// (if imported with `import()`, they will appear in `.default` as well.)
// This way, cjs-module-lexer will let all of react's (named) exports through unchanged.
module.exports = require("react");
}
// missing functions
module.exports.createContext = polyfillMissingFn("createContext");
// @ts-ignore
module.exports.createFactory = polyfillMissingFn("createFactory");
module.exports.act = polyfillMissingFn("act");
// @ts-ignore
module.exports.unstable_act = polyfillMissingFn("unstable_act");
module.exports.unstable_useCacheRefresh = polyfillMissingFn("unstable_useCacheRefresh");
module.exports.useContext = polyfillMissingFn("useContext");
module.exports.useDeferredValue = polyfillMissingFn("useDeferredValue");
module.exports.useEffect = polyfillMissingFn("useEffect");
module.exports.useImperativeHandle = polyfillMissingFn("useImperativeHandle");
module.exports.useInsertionEffect = polyfillMissingFn("useInsertionEffect");
module.exports.useLayoutEffect = polyfillMissingFn("useLayoutEffect");
module.exports.useReducer = polyfillMissingFn("useReducer");
module.exports.useRef = polyfillMissingFn("useRef");
module.exports.useState = polyfillMissingFn("useState");
module.exports.useSyncExternalStore = polyfillMissingFn("useSyncExternalStore");
module.exports.useTransition = polyfillMissingFn("useTransition");
module.exports.useOptimistic = polyfillMissingFn("useOptimistic");
// We don't want bundlers to error when they encounter usage of any of these exports.
// It's up to the package author to ensure that if they access React internals,
// they do so in a safe way that won't break if React changes how they use these internals.
// (e.g. only access them in development, and only in an optional way that won't
// break if internals are not there or do not have the expected structure)
// @ts-ignore
module.exports.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED = undefined;
// @ts-ignore
module.exports.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE = undefined;
// @ts-ignore
module.exports.__SERVER_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE = undefined;
// missing classes
module.exports.Component = polyfillMissingClass("Component");
module.exports.PureComponent = polyfillMissingClass("PureComponent");
module.exports.createContext = function unsupportedCreateContext() {
return {
Provider: function throwNoContext() {
throw new Error("Context is not available in this environment.");
},
Consumer: function throwNoContext() {
throw new Error("Context is not available in this environment.");
},
};
};
// @ts-ignore
module.exports.createFactory = function unsupportedCreateFactory() {
return function throwNoCreateFactory() {
throw new Error("createFactory is not available in this environment.");
};
};
// Here we actually pull in the React library and add everything
// it exports to our own `module.exports`.
// If React suddenly were to add one of the above "polyfilled" exports,
// the React version would overwrite our version, so this should be
// future-proof.
Object.assign(module.exports, require("react"));
function polyfillMissingFn(exportName) {
const name = "nonExistingExport__" + exportName;
return /** @type {any} */ (
{
[name]() {
throw new Error(`React functionality '${exportName}' is not available in this environment.`);
},
}[name]
);
}
function polyfillMissingClass(exportName) {
return /** @type {any} */ (
class NonExistentClass {
constructor() {
throw new Error(`React class '${exportName}' is not available in this environment.`);
}
}
);
}