what-react 0.10.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -11
- package/package.json +7 -3
- package/src/dom.js +52 -29
- package/src/hooks.js +351 -0
- package/src/index.js +264 -386
- package/src/runtime.js +1147 -0
- package/src/vite-plugin.js +64 -3
package/src/vite-plugin.js
CHANGED
|
@@ -50,6 +50,56 @@ const KNOWN_REACT_PACKAGES = [
|
|
|
50
50
|
'use-sync-external-store', 'immer',
|
|
51
51
|
];
|
|
52
52
|
|
|
53
|
+
// Pure-CJS transitive utilities that do NOT import 'react' themselves (they
|
|
54
|
+
// touch react-is / symbols / DOM only). They must stay IN Vite's optimizeDeps
|
|
55
|
+
// so esbuild pre-bundles them and synthesizes the ESM default/named exports
|
|
56
|
+
// that React UI libs import (e.g. `import hoistStatics from 'hoist-non-react-statics'`).
|
|
57
|
+
//
|
|
58
|
+
// If these are excluded (or left un-pre-bundled), Vite serves the raw CJS file
|
|
59
|
+
// and the browser throws "does not provide an export named 'default'", which
|
|
60
|
+
// blank-screens the whole app. This is exactly what happens with react-select
|
|
61
|
+
// and @emotion/* — their CJS dep chain pulls in hoist-non-react-statics.
|
|
62
|
+
//
|
|
63
|
+
// These carry NO dual-React-instance risk: none of them `require('react')`,
|
|
64
|
+
// so there is no second copy of React/hooks to worry about. The react→what-react
|
|
65
|
+
// alias still applies to anything that DOES import 'react'.
|
|
66
|
+
const PURE_CJS_INTEROP_DEPS = [
|
|
67
|
+
'hoist-non-react-statics',
|
|
68
|
+
'react-is',
|
|
69
|
+
'prop-types',
|
|
70
|
+
'memoize-one',
|
|
71
|
+
'use-isomorphic-layout-effect',
|
|
72
|
+
'shallowequal',
|
|
73
|
+
'stylis',
|
|
74
|
+
'react-fast-compare',
|
|
75
|
+
'warning',
|
|
76
|
+
'invariant',
|
|
77
|
+
'object-assign',
|
|
78
|
+
'classnames',
|
|
79
|
+
'tiny-invariant',
|
|
80
|
+
'tiny-warning',
|
|
81
|
+
'dom-helpers',
|
|
82
|
+
];
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Detect which of the pure-CJS interop deps are actually installed, so we only
|
|
86
|
+
* ask Vite to pre-bundle ones that resolve (an `include` for a missing dep is a
|
|
87
|
+
* hard error in Vite).
|
|
88
|
+
*/
|
|
89
|
+
function detectInstalledInteropDeps(projectRoot) {
|
|
90
|
+
const installed = [];
|
|
91
|
+
for (const pkg of PURE_CJS_INTEROP_DEPS) {
|
|
92
|
+
try {
|
|
93
|
+
const require = createRequire(projectRoot + '/');
|
|
94
|
+
require.resolve(pkg);
|
|
95
|
+
installed.push(pkg);
|
|
96
|
+
} catch {
|
|
97
|
+
// Not installed (it's transitive and may not be hoisted), skip.
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return installed;
|
|
101
|
+
}
|
|
102
|
+
|
|
53
103
|
/**
|
|
54
104
|
* Resolve the directory of a package from the user's project
|
|
55
105
|
*/
|
|
@@ -136,14 +186,23 @@ export function reactCompat(options = {}) {
|
|
|
136
186
|
|
|
137
187
|
// Auto-detect installed React ecosystem packages
|
|
138
188
|
const autoExclude = autoDetect ? detectInstalledReactPackages(root) : [];
|
|
189
|
+
|
|
190
|
+
// Pure-CJS utilities that MUST be pre-bundled for ESM interop (so their
|
|
191
|
+
// `import x from 'cjs-only-pkg'` consumers don't blank-screen). Only the
|
|
192
|
+
// ones actually resolvable get included.
|
|
193
|
+
const interopInclude = autoDetect ? detectInstalledInteropDeps(root) : [];
|
|
194
|
+
const includeSet = new Set(interopInclude);
|
|
195
|
+
|
|
139
196
|
const allExclude = [
|
|
140
197
|
'what-core', 'what-react',
|
|
141
198
|
'react', 'react-dom',
|
|
142
199
|
...autoExclude,
|
|
143
200
|
...exclude,
|
|
144
201
|
];
|
|
145
|
-
// Deduplicate
|
|
146
|
-
|
|
202
|
+
// Deduplicate, and make sure nothing we explicitly want pre-bundled
|
|
203
|
+
// (interop deps) leaks back into the exclude list.
|
|
204
|
+
const uniqueExclude = [...new Set(allExclude)].filter((p) => !includeSet.has(p));
|
|
205
|
+
const uniqueInclude = [...includeSet];
|
|
147
206
|
|
|
148
207
|
// Build alias map
|
|
149
208
|
const aliases = {
|
|
@@ -180,6 +239,7 @@ export function reactCompat(options = {}) {
|
|
|
180
239
|
},
|
|
181
240
|
optimizeDeps: {
|
|
182
241
|
exclude: uniqueExclude,
|
|
242
|
+
include: uniqueInclude,
|
|
183
243
|
},
|
|
184
244
|
};
|
|
185
245
|
},
|
|
@@ -187,7 +247,8 @@ export function reactCompat(options = {}) {
|
|
|
187
247
|
configResolved(config) {
|
|
188
248
|
// Log what we set up
|
|
189
249
|
const excluded = config.optimizeDeps?.exclude?.length || 0;
|
|
190
|
-
|
|
250
|
+
const included = config.optimizeDeps?.include?.length || 0;
|
|
251
|
+
console.log(`\n ⚡ what-react compat active — ${excluded} packages excluded from pre-bundling` + (included ? `, ${included} pure-CJS deps pre-bundled for interop` : '') + `\n`);
|
|
191
252
|
},
|
|
192
253
|
};
|
|
193
254
|
}
|