Skip to content

Commit

Permalink
Merge pull request #10 from phryneas/pr/undefined-internals
Browse files Browse the repository at this point in the history
Add `undefined` exports for potentially missing React internals.
  • Loading branch information
phryneas authored Apr 23, 2024
2 parents adc6524 + 5023da2 commit 1150f7e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 34 deletions.
24 changes: 23 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,24 @@
"use strict";
module.exports = require("react");
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");
}
// 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;
// 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"));
33 changes: 6 additions & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rehackt",
"version": "0.0.6",
"version": "0.1.0",
"description": "A wrapper around React that will hide hooks from the React Server Component compiler",
"author": "Lenz Weber-Tronic",
"repository": {
Expand All @@ -27,8 +27,8 @@
"LICENSE.md"
],
"peerDependencies": {
"react": "*",
"@types/react": "*"
"@types/react": "*",
"react": "*"
},
"peerDependenciesMeta": {
"react": {
Expand All @@ -39,8 +39,8 @@
}
},
"devDependencies": {
"react": "^18.3.0-canary-ab31a9ed2-20230824",
"@types/node": "^20.5.7"
"@types/node": "^20.5.7",
"react": "^19.0.0-canary-33a32441e9-20240418"
},
"prettier": {
"printWidth": 120
Expand Down
15 changes: 14 additions & 1 deletion rsc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ if (0) {

// 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");
Expand All @@ -25,6 +27,17 @@ 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");
Expand All @@ -40,7 +53,7 @@ module.exports.createContext = function unsupportedCreateContext() {
},
};
};

// @ts-ignore
module.exports.createFactory = function unsupportedCreateFactory() {
return function throwNoCreateFactory() {
throw new Error("createFactory is not available in this environment.");
Expand Down

0 comments on commit 1150f7e

Please sign in to comment.