@xh/hoist-dev-utils 16.3.0 → 16.4.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 +69 -1
- package/configureRsbuild.d.ts +8 -0
- package/configureRsbuild.js +78 -26
- package/configureVitest.js +89 -25
- package/lib/common.js +1 -0
- package/lib/inlinePackages.js +267 -0
- package/lib/vitestInlineHoist.mjs +38 -21
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -112,6 +112,70 @@ direnv or mise can set it per worktree.
|
|
|
112
112
|
Inline mode fails fast when the path holds no hoist-react checkout. The Rsbuild build banner shows
|
|
113
113
|
the path and branch it builds from.
|
|
114
114
|
|
|
115
|
+
### Inline packages
|
|
116
|
+
|
|
117
|
+
Inline packages extend inline mode to Hoist companion packages that ship as source, such as
|
|
118
|
+
`@xh/hoist-chat-kit`, `@xh/package-template` or a client library built on hoist-react. The build
|
|
119
|
+
runs a local checkout of the package in place of the installed copy, so a change to the package
|
|
120
|
+
shows up in the app as you make it.
|
|
121
|
+
|
|
122
|
+
Set `XH_INLINE_PACKAGES` to a comma-separated list of package names. Add `=/abs/path` to a name to
|
|
123
|
+
use a checkout at that path, which cannot contain a comma. Without a path, the checkout is a sibling
|
|
124
|
+
of the app's repo, named for the package without its scope:
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
# ../../hoist-chat-kit from client-app
|
|
128
|
+
XH_INLINE_PACKAGES=@xh/hoist-chat-kit pnpm start
|
|
129
|
+
|
|
130
|
+
# A worktree, together with inline Hoist
|
|
131
|
+
XH_INLINE_PACKAGES=@xh/hoist-chat-kit=$HOME/dev/hoist-chat-kit-worktrees/my-feature pnpm startWithHoist
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
`readCliEnv()` maps the variable onto the `inlinePackages` option, and `configureVitest()` reads it
|
|
135
|
+
too. An app config needs no change. Production builds ignore it, with a warning.
|
|
136
|
+
|
|
137
|
+
A checkout installs its own dependencies, which hold a second copy of Hoist and React for its tests
|
|
138
|
+
and type checks. Two copies of either break the app, so the build shares one copy of these with the
|
|
139
|
+
app:
|
|
140
|
+
|
|
141
|
+
- every `peerDependency` of an inline package
|
|
142
|
+
- `@xh/hoist`, from the inline hoist-react checkout under inline Hoist
|
|
143
|
+
- React, react-dom and ag-Grid, if the app has them
|
|
144
|
+
|
|
145
|
+
A shared package resolves to an inline checkout if it is one, else to the app's copy. A peer that
|
|
146
|
+
the app does not install resolves to Hoist's own copy. All other dependencies of the package resolve
|
|
147
|
+
from its checkout, as its lockfile lays them out. A companion package must declare Hoist, React and
|
|
148
|
+
any other library it shares with the app as `peerDependencies`.
|
|
149
|
+
|
|
150
|
+
The build fails fast if a checkout is missing, holds another package, has no `node_modules`, or
|
|
151
|
+
lacks a required peer. It warns if a peer range does not match the version the app runs, or if a
|
|
152
|
+
shared library is under `dependencies`. The build banner shows the path and branch of each checkout
|
|
153
|
+
and the packages it shares.
|
|
154
|
+
|
|
155
|
+
Bundler aliases do not reach `tsc` or the IDE, which still read the installed package's types. To
|
|
156
|
+
type-check against the checkout, add a `paths` entry for the package to the app's `tsconfig.json`,
|
|
157
|
+
as for inline Hoist. Do not commit it.
|
|
158
|
+
|
|
159
|
+
```json
|
|
160
|
+
"paths": {
|
|
161
|
+
"@xh/hoist-chat-kit/*": ["../../hoist-chat-kit/*"]
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
The build aliases each shared package to its directory, which bypasses the `exports` map in that
|
|
166
|
+
package's `package.json`. The directory then resolves through its `main`, `module` or `browser`
|
|
167
|
+
field. React, react-dom, ag-Grid and Hoist resolve correctly this way. A peer that has no such
|
|
168
|
+
field, or maps subpaths only through `exports`, fails to resolve, and the build reports the error.
|
|
169
|
+
Inline Hoist has the same limit.
|
|
170
|
+
|
|
171
|
+
An app alias for a shared package, such as an older hand-written alias to a checkout, gives way to
|
|
172
|
+
the shared copy, under inline Hoist too. The build and `configureVitest()` log a warning that names
|
|
173
|
+
it.
|
|
174
|
+
|
|
175
|
+
Rsbuild compiles TypeScript from any directory, `node_modules` included. An installed companion
|
|
176
|
+
package that ships only TypeScript therefore needs no `extraIncludePaths` entry. Add one only for a
|
|
177
|
+
package that also ships plain `.js` source.
|
|
178
|
+
|
|
115
179
|
### Migrating from v15 (webpack)
|
|
116
180
|
|
|
117
181
|
Dev-utils 16 is Rsbuild only - `configureWebpack()` is gone. To move an app:
|
|
@@ -197,7 +261,7 @@ warns about keys it does not know.
|
|
|
197
261
|
| Option | In tests |
|
|
198
262
|
|---|---|
|
|
199
263
|
| `appCode` (required), `appName`, `appVersion`, `appBuild`, `baseUrl` | Set in `XH`, with the build defaults |
|
|
200
|
-
| `inlineHoist`, `extraIncludePaths`, `extraExcludePaths`, `resolveAliases`, `swcOptions` | As in the build. An alias value that starts with `.` is relative to the root. `swcOptions` drops any `env` key, which SWC rejects with `jsc.target`. |
|
|
264
|
+
| `inlineHoist`, `inlinePackages`, `extraIncludePaths`, `extraExcludePaths`, `resolveAliases`, `swcOptions` | As in the build. An alias value that starts with `.` is relative to the root. `swcOptions` drops any `env` key, which SWC rejects with `jsc.target`. |
|
|
201
265
|
| `extraModuleRules` | Ignored, with a warning. Rspack rules cannot run under Vite. |
|
|
202
266
|
| Other build options | Ignored |
|
|
203
267
|
| `root` | Project root. Default: the current directory. |
|
|
@@ -224,6 +288,10 @@ app's own copies, if the app has them. A Node resolve hook does the same for the
|
|
|
224
288
|
dependencies, which Vitest loads outside Vite. Inline mode works under pnpm. It is untested under
|
|
225
289
|
yarn and npm.
|
|
226
290
|
|
|
291
|
+
`XH_INLINE_PACKAGES` (or `inlinePackages`) runs the tests against local checkouts of companion
|
|
292
|
+
packages, with the shared copies described in [Inline packages](#inline-packages). The preset does
|
|
293
|
+
not collect a checkout's own specs, because `include` globs match under the project root only.
|
|
294
|
+
|
|
227
295
|
The name matches Vitest's own `configureVitest` plugin hook. The two are unrelated: this one is a
|
|
228
296
|
config factory, like `configureRsbuild()`.
|
|
229
297
|
|
package/configureRsbuild.d.ts
CHANGED
|
@@ -64,6 +64,14 @@ declare namespace configureRsbuild {
|
|
|
64
64
|
* variable. Dev-mode only for the build.
|
|
65
65
|
*/
|
|
66
66
|
inlineHoist?: boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Companion packages to run from a local checkout, as inlineHoist does for hoist-react -
|
|
69
|
+
* each `name` (a sibling checkout at `../../<name without scope>`) or `name=/abs/path`. A
|
|
70
|
+
* comma-separated string is also accepted. Set by the `XH_INLINE_PACKAGES` environment
|
|
71
|
+
* variable. Peers of an inline package, `@xh/hoist` and React resolve to one copy shared
|
|
72
|
+
* with the app. Dev-mode only for the build.
|
|
73
|
+
*/
|
|
74
|
+
inlinePackages?: string | string[];
|
|
67
75
|
/** Additional paths to transpile with the settings used for app and hoist-react code. */
|
|
68
76
|
extraIncludePaths?: string[];
|
|
69
77
|
/** Paths to exclude from transpiling, e.g. a local package's nested node_modules. */
|
package/configureRsbuild.js
CHANGED
|
@@ -39,13 +39,15 @@ const {
|
|
|
39
39
|
logMsg
|
|
40
40
|
} = require('./lib/common');
|
|
41
41
|
|
|
42
|
+
const {HOIST_SWC, resolveAppIdentity, resolveHoistPath, xhDefines} = require('./lib/hoistCompile');
|
|
43
|
+
|
|
42
44
|
const {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
} = require('./lib/
|
|
45
|
+
INLINE_PACKAGES_VAR,
|
|
46
|
+
parseInlinePackages,
|
|
47
|
+
planInlinePackages,
|
|
48
|
+
replacedAppAliases,
|
|
49
|
+
withInlineCacheDigest
|
|
50
|
+
} = require('./lib/inlinePackages');
|
|
49
51
|
|
|
50
52
|
// These are not deps of hoist-dev-utils but of the consuming app, so resolve them from the
|
|
51
53
|
// app's own directory (basePath).
|
|
@@ -65,10 +67,12 @@ const hoistReactPkg = resolveAppPackage('@xh/hoist', basePath),
|
|
|
65
67
|
* Sample commands to run configurations produced by this method include:
|
|
66
68
|
* `rsbuild build --env-mode prod` to trigger a production build
|
|
67
69
|
* `rsbuild dev --env-mode inlineHoist` to run dev server w/hoist-react in inline mode
|
|
70
|
+
* `XH_INLINE_PACKAGES=@xh/hoist-chat-kit rsbuild dev` to run a companion package inline
|
|
68
71
|
*
|
|
69
72
|
* Note that Rsbuild's CLI has no `--env key=value` flag. Build-time overrides (appVersion, appBuild)
|
|
70
73
|
* flow in as environment variables - see the `{@link readCliEnv}` helper, which maps
|
|
71
|
-
* `XH_APP_VERSION`, `XH_APP_BUILD`, `XH_PROD_BUILD`, `XH_INLINE_HOIST`, etc.
|
|
74
|
+
* `XH_APP_VERSION`, `XH_APP_BUILD`, `XH_PROD_BUILD`, `XH_INLINE_HOIST`, `XH_INLINE_PACKAGES`, etc.
|
|
75
|
+
* onto env options.
|
|
72
76
|
*
|
|
73
77
|
* @param {Object} env - config passed in from the app's rsbuild config.
|
|
74
78
|
* @param {string} env.appCode - short, internal code for the application - baked into client as XH.appCode. Should be
|
|
@@ -83,6 +87,15 @@ const hoistReactPkg = resolveAppPackage('@xh/hoist', basePath),
|
|
|
83
87
|
* hoist-react developers to test plugin changes. Dev-mode only. The checkout is a sibling of
|
|
84
88
|
* the app's repo at `../../hoist-react`, unless the `XH_HOIST_REACT_PATH` environment variable
|
|
85
89
|
* names another (absolute) path, e.g. a git worktree.
|
|
90
|
+
* @param {(string|string[])} [env.inlinePackages] - companion packages to run from a local checkout
|
|
91
|
+
* in place of the installed copy, as inlineHoist does for hoist-react - e.g.
|
|
92
|
+
* `@xh/hoist-chat-kit`. Each entry is `name`, for a checkout that is a sibling of the app's repo
|
|
93
|
+
* named for the package without its scope (`../../hoist-chat-kit`), or `name=/abs/path`. A
|
|
94
|
+
* comma-separated string is also accepted, as set by the `XH_INLINE_PACKAGES` environment
|
|
95
|
+
* variable. Every peerDependency of an inline package, plus `@xh/hoist` and React, resolves to
|
|
96
|
+
* a single copy shared with the app - the inline checkout where there is one. Its other
|
|
97
|
+
* dependencies resolve from its own checkout. Dev-mode only - ignored with a warning when
|
|
98
|
+
* prodBuild is true.
|
|
86
99
|
* @param {boolean} [env.reactProdMode=false] - true to use the production build of React
|
|
87
100
|
* when running the dev server.
|
|
88
101
|
* @param {Object} [env.resolveAliases] - object mapping for custom module resolution.
|
|
@@ -166,6 +179,8 @@ async function configureRsbuild(env) {
|
|
|
166
179
|
|
|
167
180
|
const prodBuild = parseFlag(env.prodBuild, false) === true,
|
|
168
181
|
inlineHoist = !prodBuild && parseFlag(env.inlineHoist, false) === true,
|
|
182
|
+
// Not parsed for prod builds, where a stray value must not fail the build.
|
|
183
|
+
inlinePackageSpecs = prodBuild ? [] : parseInlinePackages(env.inlinePackages),
|
|
169
184
|
reactProdMode = prodBuild || parseFlag(env.reactProdMode, false) === true,
|
|
170
185
|
resolveAliases = Object.assign({}, env.resolveAliases),
|
|
171
186
|
buildCache = _.isPlainObject(env.buildCache)
|
|
@@ -219,6 +234,15 @@ async function configureRsbuild(env) {
|
|
|
219
234
|
// symlinking package managers (e.g. pnpm).
|
|
220
235
|
const hoistPath = resolveHoistPath(basePath, inlineHoist);
|
|
221
236
|
|
|
237
|
+
// Plan inline packages - throws here, as above, when a checkout or a required peer is missing.
|
|
238
|
+
const inlinePlan = planInlinePackages({
|
|
239
|
+
appRoot: basePath,
|
|
240
|
+
specs: inlinePackageSpecs,
|
|
241
|
+
hoistPath,
|
|
242
|
+
inlineHoist
|
|
243
|
+
}),
|
|
244
|
+
inlinePackages = inlinePlan.packages;
|
|
245
|
+
|
|
222
246
|
process.env.NODE_ENV = prodBuild ? 'production' : 'development';
|
|
223
247
|
process.env.REACT_NODE_ENV = reactProdMode ? 'production' : 'development';
|
|
224
248
|
|
|
@@ -230,6 +254,10 @@ async function configureRsbuild(env) {
|
|
|
230
254
|
if (prodBuild) logMsg('🚀 Production build enabled');
|
|
231
255
|
if (!prodBuild) logMsg('💻 Development mode enabled');
|
|
232
256
|
if (inlineHoist) logMsg('🏗️ Inline Hoist enabled');
|
|
257
|
+
if (inlinePackages.length) logMsg('📦 Inline packages enabled');
|
|
258
|
+
if (prodBuild && !_.isEmpty(env.inlinePackages)) {
|
|
259
|
+
logMsg(`⚠️ Inline packages ignored for production builds (${INLINE_PACKAGES_VAR}).`);
|
|
260
|
+
}
|
|
233
261
|
if (reactProdMode) logMsg('⚛️ React Production mode enabled');
|
|
234
262
|
if (buildCache) logMsg('💾 Persistent build cache enabled');
|
|
235
263
|
if (prodBuild && precompressAssets) logMsg('🗜️ Asset pre-compression enabled');
|
|
@@ -241,6 +269,10 @@ async function configureRsbuild(env) {
|
|
|
241
269
|
} else {
|
|
242
270
|
logMsg(` > @xh/hoist v${hoistReactPkg.version}`);
|
|
243
271
|
}
|
|
272
|
+
inlinePackages.forEach(({name, path: dir}) => {
|
|
273
|
+
const branch = gitBranch(dir);
|
|
274
|
+
logMsg(` > ${name} INLINE from ${dir}${branch ? ` @ ${branch}` : ''}`);
|
|
275
|
+
});
|
|
244
276
|
logMsg(` > @xh/hoist-dev-utils v${devUtilsPkg.version}`);
|
|
245
277
|
logMsg(` > react v${reactPkg.version}`);
|
|
246
278
|
logMsg(` > @rsbuild/core v${rsbuildVersion}`);
|
|
@@ -249,6 +281,15 @@ async function configureRsbuild(env) {
|
|
|
249
281
|
logSep();
|
|
250
282
|
logMsg('🎯 Targets:');
|
|
251
283
|
targetBrowsers.forEach(it => logMsg(` > ${it}`));
|
|
284
|
+
if (inlinePlan.shared.length) {
|
|
285
|
+
logSep();
|
|
286
|
+
logMsg('🔗 Shared inline, one copy each:');
|
|
287
|
+
inlinePlan.shared.forEach(({name, dir}) => logMsg(` > ${name} from ${dir}`));
|
|
288
|
+
inlinePlan.warnings.forEach(it => logMsg(`⚠️ ${it}`));
|
|
289
|
+
replacedAppAliases(env.resolveAliases, inlinePlan.shared).forEach(key =>
|
|
290
|
+
logMsg(`⚠️ App alias "${key}" replaced by the shared copy above.`)
|
|
291
|
+
);
|
|
292
|
+
}
|
|
252
293
|
|
|
253
294
|
const srcPath = path.resolve(basePath, 'src'),
|
|
254
295
|
outPath = path.resolve(basePath, 'build'),
|
|
@@ -270,20 +311,25 @@ async function configureRsbuild(env) {
|
|
|
270
311
|
? null
|
|
271
312
|
: generateBlueprintIconStubs(basePath, hoistPath);
|
|
272
313
|
|
|
273
|
-
//
|
|
274
|
-
//
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
}
|
|
314
|
+
// Inline mode: alias each shared name to the one copy every importer gets - each checkout,
|
|
315
|
+
// hoist-react's included, plus their peers and the libraries hoist-react must share with the
|
|
316
|
+
// app (React for hooks, ag-Grid for `useGridMenuItem`). Aliases are global, so the dependencies
|
|
317
|
+
// a checkout installs share that copy too. See lib/inlinePackages.js.
|
|
318
|
+
replacedAppAliases(resolveAliases, inlinePlan.shared).forEach(key => {
|
|
319
|
+
delete resolveAliases[key];
|
|
320
|
+
});
|
|
321
|
+
inlinePlan.shared.forEach(({name, dir}) => {
|
|
322
|
+
resolveAliases[name] = dir;
|
|
323
|
+
});
|
|
284
324
|
|
|
285
|
-
// When running inline, resolve
|
|
286
|
-
|
|
325
|
+
// When running inline, resolve the checkouts' own node_modules so we can exclude them. Their
|
|
326
|
+
// dependencies ship built - compiling them would be slow, and could break them.
|
|
327
|
+
const hoistNodeModulesPath = inlineHoist ? path.resolve(hoistPath, 'node_modules') : null,
|
|
328
|
+
inlineSrcPaths = inlinePackages.map(it => it.path),
|
|
329
|
+
inlineExcludePaths = _.compact([
|
|
330
|
+
hoistNodeModulesPath,
|
|
331
|
+
...inlinePackages.map(it => it.nodeModules)
|
|
332
|
+
]);
|
|
287
333
|
|
|
288
334
|
// Determine source map (devtool) mode.
|
|
289
335
|
let devtool;
|
|
@@ -368,8 +414,8 @@ async function configureRsbuild(env) {
|
|
|
368
414
|
splitChunks: false,
|
|
369
415
|
reactRefreshOptions: {
|
|
370
416
|
// Refresh app source, hoist-react (raw TS in node_modules) and custom packages.
|
|
371
|
-
include: [srcPath, hoistPath, ...extraIncludePaths],
|
|
372
|
-
exclude:
|
|
417
|
+
include: [srcPath, hoistPath, ...inlineSrcPaths, ...extraIncludePaths],
|
|
418
|
+
exclude: inlineExcludePaths
|
|
373
419
|
}
|
|
374
420
|
}),
|
|
375
421
|
|
|
@@ -391,9 +437,9 @@ async function configureRsbuild(env) {
|
|
|
391
437
|
// currently transpiling anything in hoist-react on its own. (Rsbuild's default include
|
|
392
438
|
// also compiles every .ts/.tsx it encounters, so this is largely belt-and-braces for
|
|
393
439
|
// hoist's plain-JS `polyfills.js` and any custom packages.)
|
|
394
|
-
include: [srcPath, hoistPath, ...extraIncludePaths],
|
|
395
|
-
// In inline mode also *avoid* transpiling
|
|
396
|
-
exclude:
|
|
440
|
+
include: [srcPath, hoistPath, ...inlineSrcPaths, ...extraIncludePaths],
|
|
441
|
+
// In inline mode also *avoid* transpiling the checkouts' own node_modules libraries.
|
|
442
|
+
exclude: [...inlineExcludePaths, ...extraExcludePaths],
|
|
397
443
|
|
|
398
444
|
// TC39 Stage 3 (2023-11) decorators, transformed by SWC alone - no Babel pass. This is
|
|
399
445
|
// the emit hoist-react >= 88 is written against: `@observable accessor` / `@bindable
|
|
@@ -548,7 +594,12 @@ async function configureRsbuild(env) {
|
|
|
548
594
|
},
|
|
549
595
|
|
|
550
596
|
performance: {
|
|
551
|
-
|
|
597
|
+
// Inline settings change module resolution, so they key the cache too.
|
|
598
|
+
buildCache: withInlineCacheDigest(buildCache, {
|
|
599
|
+
inlineHoist,
|
|
600
|
+
hoistPath,
|
|
601
|
+
packages: inlinePackages
|
|
602
|
+
}),
|
|
552
603
|
printFileSize: prodBuild ? {detail: false} : false
|
|
553
604
|
},
|
|
554
605
|
|
|
@@ -852,6 +903,7 @@ function readCliEnv(processEnv = process.env) {
|
|
|
852
903
|
XH_APP_BUILD: 'appBuild',
|
|
853
904
|
XH_PROD_BUILD: 'prodBuild',
|
|
854
905
|
XH_INLINE_HOIST: 'inlineHoist',
|
|
906
|
+
[INLINE_PACKAGES_VAR]: 'inlinePackages',
|
|
855
907
|
XH_REACT_PROD_MODE: 'reactProdMode',
|
|
856
908
|
XH_BUILD_CACHE: 'buildCache',
|
|
857
909
|
XH_MINIFY: 'minify',
|
package/configureVitest.js
CHANGED
|
@@ -26,13 +26,19 @@ const {
|
|
|
26
26
|
} = require('./lib/common');
|
|
27
27
|
|
|
28
28
|
const {
|
|
29
|
-
INLINE_HOIST_SINGLETONS,
|
|
30
29
|
hoistSwcTransform,
|
|
31
30
|
resolveAppIdentity,
|
|
32
31
|
resolveHoistPath,
|
|
33
32
|
xhDefines
|
|
34
33
|
} = require('./lib/hoistCompile');
|
|
35
34
|
|
|
35
|
+
const {
|
|
36
|
+
INLINE_PACKAGES_VAR,
|
|
37
|
+
parseInlinePackages,
|
|
38
|
+
planInlinePackages,
|
|
39
|
+
replacedAppAliases
|
|
40
|
+
} = require('./lib/inlinePackages');
|
|
41
|
+
|
|
36
42
|
// Build options a test run cannot honor - warned, then ignored. Every other build option is
|
|
37
43
|
// accepted silently, so an app can pass one `env` object to both configs.
|
|
38
44
|
const IGNORED_BUILD_OPTIONS = {extraModuleRules: 'Rspack rules cannot run under Vite'};
|
|
@@ -60,8 +66,8 @@ const HOIST_IMPORT = /^@xh\/hoist(?=\/|$)/;
|
|
|
60
66
|
* against a local hoist-react checkout with `XH_INLINE_HOIST=true vitest`.
|
|
61
67
|
*
|
|
62
68
|
* Takes the same `env` object as `configureRsbuild()`. App identity, `baseUrl`, `inlineHoist`,
|
|
63
|
-
* `extraIncludePaths`, `extraExcludePaths`, `resolveAliases` and `swcOptions`
|
|
64
|
-
* build. Other build options are ignored - `extraModuleRules` with a warning.
|
|
69
|
+
* `inlinePackages`, `extraIncludePaths`, `extraExcludePaths`, `resolveAliases` and `swcOptions`
|
|
70
|
+
* apply as in the build. Other build options are ignored - `extraModuleRules` with a warning.
|
|
65
71
|
*
|
|
66
72
|
* @param {Object} env - config passed in from the app's Vitest config.
|
|
67
73
|
* @param {string} env.appCode - as for configureRsbuild(), baked into tests as XH.appCode.
|
|
@@ -73,6 +79,10 @@ const HOIST_IMPORT = /^@xh\/hoist(?=\/|$)/;
|
|
|
73
79
|
* @param {boolean} [env.inlineHoist=false] - true to test against a local hoist-react checkout, as
|
|
74
80
|
* for configureRsbuild() - a sibling at `../../hoist-react`, or the path in the
|
|
75
81
|
* `XH_HOIST_REACT_PATH` environment variable. Also on when `XH_INLINE_HOIST` is 'true'.
|
|
82
|
+
* @param {(string|string[])} [env.inlinePackages] - companion packages to test from a local
|
|
83
|
+
* checkout, as for configureRsbuild(). Read from the `XH_INLINE_PACKAGES` environment
|
|
84
|
+
* variable when not passed. The checkout's own specs are not collected - `include` globs
|
|
85
|
+
* match under the root only.
|
|
76
86
|
* @param {string[]} [env.extraIncludePaths] - custom packages to compile, as in the build.
|
|
77
87
|
* @param {string[]} [env.extraExcludePaths] - paths not to compile, as in the build.
|
|
78
88
|
* @param {Object} [env.resolveAliases] - module aliases, as in the build. A key ending in `$`
|
|
@@ -114,7 +124,18 @@ function createConfig(env) {
|
|
|
114
124
|
include = env.include ?? ['src/**/*.spec.{ts,tsx}'],
|
|
115
125
|
setupFiles = _.castArray(env.setupFiles ?? []),
|
|
116
126
|
timeZone = env.timeZone === undefined ? 'America/New_York' : env.timeZone,
|
|
117
|
-
hoistSetup = path.join(hoistPath, 'test-support/setup.ts')
|
|
127
|
+
hoistSetup = path.join(hoistPath, 'test-support/setup.ts'),
|
|
128
|
+
inlinePlan = planInlinePackages({
|
|
129
|
+
appRoot: root,
|
|
130
|
+
specs: selfHost
|
|
131
|
+
? []
|
|
132
|
+
: parseInlinePackages(env.inlinePackages ?? process.env[INLINE_PACKAGES_VAR]),
|
|
133
|
+
hoistPath,
|
|
134
|
+
inlineHoist,
|
|
135
|
+
extraSingletons: INLINE_TEST_SINGLETONS
|
|
136
|
+
}),
|
|
137
|
+
inlinePackages = inlinePlan.packages,
|
|
138
|
+
anyInline = inlineHoist || inlinePackages.length > 0;
|
|
118
139
|
|
|
119
140
|
checkSwcApi();
|
|
120
141
|
if (!fs.existsSync(hoistSetup)) {
|
|
@@ -123,25 +144,44 @@ function createConfig(env) {
|
|
|
123
144
|
// Packaged mode only - a checkout or hoist-react itself may be mid-way through a vitest bump.
|
|
124
145
|
if (!selfHost && !inlineHoist) checkVitestMajor(root, hoistPath);
|
|
125
146
|
warnIgnoredOptions(env);
|
|
147
|
+
inlinePlan.warnings.forEach(warn);
|
|
148
|
+
|
|
149
|
+
// As in the build, the shared copies win over an app alias for the same package.
|
|
150
|
+
const replacedAliases = replacedAppAliases(env.resolveAliases, inlinePlan.shared);
|
|
151
|
+
replacedAliases.forEach(key => warn(`App alias "${key}" replaced by an inline shared copy.`));
|
|
126
152
|
|
|
127
153
|
// Set in the main process, so test workers inherit them. Nothing reads the preset version yet -
|
|
128
154
|
// it is there for `@xh/hoist/test-support` to check against a minimum of its own.
|
|
129
155
|
if (timeZone != null) process.env.TZ = timeZone;
|
|
130
156
|
process.env.XH_VITEST_PRESET_VERSION = devUtilsPkg.version;
|
|
131
157
|
|
|
132
|
-
// Inline mode: dedupe
|
|
133
|
-
//
|
|
134
|
-
//
|
|
135
|
-
const inlineSingletons =
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
158
|
+
// Inline mode: dedupe the shared packages the app has its own copy of - singletons and the
|
|
159
|
+
// peers of inline packages. Hoist and the checkouts are aliased below. See
|
|
160
|
+
// lib/inlinePackages.js.
|
|
161
|
+
const inlineSingletons = inlinePlan.shared
|
|
162
|
+
.filter(it => it.source === 'app' && it.name !== '@xh/hoist')
|
|
163
|
+
.map(it => it.name);
|
|
164
|
+
|
|
165
|
+
// Aliases for the inline packages and for a peer taken from Hoist's own copy, which the app
|
|
166
|
+
// does not install - `dedupe` resolves from the root only.
|
|
167
|
+
const inlineAliases = inlinePlan.shared
|
|
168
|
+
.filter(it => it.name !== '@xh/hoist' && it.source !== 'app')
|
|
169
|
+
.map(it => ({find: it.name, replacement: it.dir}));
|
|
170
|
+
|
|
171
|
+
// Pass the Node hook its settings through a private variable. Reusing XH_INLINE_HOIST would
|
|
172
|
+
// leave inline mode on for the rest of a watch session.
|
|
173
|
+
if (anyInline) {
|
|
141
174
|
process.env.XH_VITEST_INLINE_HOIST_CFG = JSON.stringify({
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
175
|
+
fromDirs: [
|
|
176
|
+
...(inlineHoist ? [path.join(hoistPath, 'node_modules')] : []),
|
|
177
|
+
...inlinePackages.map(it => it.nodeModules)
|
|
178
|
+
],
|
|
179
|
+
singletons: [
|
|
180
|
+
...inlineSingletons.map(name => ({name, from: root})),
|
|
181
|
+
...inlinePlan.shared
|
|
182
|
+
.filter(it => it.source === 'hoist')
|
|
183
|
+
.map(it => ({name: it.name, from: nodeModulesParent(it.dir)}))
|
|
184
|
+
]
|
|
145
185
|
});
|
|
146
186
|
} else {
|
|
147
187
|
delete process.env.XH_VITEST_INLINE_HOIST_CFG;
|
|
@@ -153,11 +193,15 @@ function createConfig(env) {
|
|
|
153
193
|
xhSwcPlugin({
|
|
154
194
|
// In selfHost, hoist-react is the root - its source already sits outside
|
|
155
195
|
// node_modules, and listing the root would also compile its dependencies' JS.
|
|
156
|
-
includePaths: selfHost
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
196
|
+
includePaths: selfHost
|
|
197
|
+
? extraIncludePaths
|
|
198
|
+
: [hoistPath, ...inlinePackages.map(it => it.path), ...extraIncludePaths],
|
|
199
|
+
// As in the build, inline mode skips the checkouts' own node_modules.
|
|
200
|
+
excludePaths: [
|
|
201
|
+
...(inlineHoist ? [path.join(hoistPath, 'node_modules')] : []),
|
|
202
|
+
...inlinePackages.map(it => it.nodeModules),
|
|
203
|
+
...extraExcludePaths
|
|
204
|
+
],
|
|
161
205
|
swcOptions: env.swcOptions || {}
|
|
162
206
|
}),
|
|
163
207
|
xhAppChangelogPlugin(),
|
|
@@ -178,8 +222,10 @@ function createConfig(env) {
|
|
|
178
222
|
|
|
179
223
|
resolve: {
|
|
180
224
|
alias: [
|
|
181
|
-
...toViteAliases(env.resolveAliases, root),
|
|
182
|
-
...
|
|
225
|
+
...toViteAliases(_.omit(env.resolveAliases, replacedAliases), root),
|
|
226
|
+
...inlineAliases,
|
|
227
|
+
// Inline packages import Hoist too, and must reach this copy, not their own.
|
|
228
|
+
...(selfHost || anyInline ? [{find: HOIST_IMPORT, replacement: hoistPath}] : [])
|
|
183
229
|
],
|
|
184
230
|
dedupe: inlineSingletons,
|
|
185
231
|
// Vitest externalizes JS in `"type": "module"` packages, so Node would load an opt-in
|
|
@@ -189,7 +235,18 @@ function createConfig(env) {
|
|
|
189
235
|
|
|
190
236
|
// Vite serves files outside root only from allowed dirs. Setting this replaces the default,
|
|
191
237
|
// so root is listed again. This package's dir holds the inline-mode setup file.
|
|
192
|
-
server:
|
|
238
|
+
server: anyInline
|
|
239
|
+
? {
|
|
240
|
+
fs: {
|
|
241
|
+
allow: [
|
|
242
|
+
root,
|
|
243
|
+
...(inlineHoist ? [hoistPath] : []),
|
|
244
|
+
...inlinePackages.map(it => it.path),
|
|
245
|
+
__dirname
|
|
246
|
+
]
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
: {},
|
|
193
250
|
|
|
194
251
|
// The settings the test kit's isolation contract needs. No `exclude` or `reporters`, so an
|
|
195
252
|
// app's values set through mergeConfig() win.
|
|
@@ -200,7 +257,7 @@ function createConfig(env) {
|
|
|
200
257
|
isolate: true,
|
|
201
258
|
include,
|
|
202
259
|
setupFiles: [
|
|
203
|
-
...(
|
|
260
|
+
...(anyInline ? [path.join(__dirname, 'lib/vitestInlineHoist.mjs')] : []),
|
|
204
261
|
hoistSetup,
|
|
205
262
|
...setupFiles
|
|
206
263
|
],
|
|
@@ -468,6 +525,13 @@ function toViteAliases(resolveAliases, root) {
|
|
|
468
525
|
);
|
|
469
526
|
}
|
|
470
527
|
|
|
528
|
+
// The dir a package dir is found from, as Node resolves it - the parent of its node_modules.
|
|
529
|
+
function nodeModulesParent(dir) {
|
|
530
|
+
const parts = dir.split(path.sep),
|
|
531
|
+
idx = parts.lastIndexOf('node_modules');
|
|
532
|
+
return idx > 0 ? parts.slice(0, idx).join(path.sep) : path.dirname(dir);
|
|
533
|
+
}
|
|
534
|
+
|
|
471
535
|
// A directory path in posix form with a trailing slash, for prefix matches against Vite ids.
|
|
472
536
|
function dirPrefix(p) {
|
|
473
537
|
return p.split(path.sep).join('/').replace(/\/?$/, '/');
|
package/lib/common.js
CHANGED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This file belongs to Hoist, an application development toolkit
|
|
3
|
+
* developed by Extremely Heavy Industries (www.xh.io | info@xh.io)
|
|
4
|
+
*
|
|
5
|
+
* Copyright © 2026 Extremely Heavy Industries Inc.
|
|
6
|
+
*/
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Inline mode: run local checkouts in place of installed packages - hoist-react under `inlineHoist`,
|
|
11
|
+
* and Hoist companion packages (e.g. `@xh/hoist-chat-kit`, `@xh/package-template` or a client's own
|
|
12
|
+
* overlay on hoist-react) under `inlinePackages`. `configureRsbuild()` and `configureVitest()` both
|
|
13
|
+
* build on the plan made here. `resolveHoistPath()` in `lib/hoistCompile.js` still finds the
|
|
14
|
+
* hoist-react checkout, as the build uses that path well beyond module resolution. No bundler API,
|
|
15
|
+
* as for `lib/common.js`.
|
|
16
|
+
*
|
|
17
|
+
* A checkout has its own node_modules, holding its peers (`@xh/hoist`, React) as devDependencies.
|
|
18
|
+
* Resolved from there, they would load a second Hoist and a second React into the app. So every
|
|
19
|
+
* peer of an inline package, plus `@xh/hoist` and the inline Hoist singletons, is "shared": the
|
|
20
|
+
* plan maps each to one directory that every importer gets. The package's own dependencies still
|
|
21
|
+
* resolve from its checkout, as its lockfile lays them out.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
const _ = require('lodash'),
|
|
25
|
+
path = require('path'),
|
|
26
|
+
fs = require('fs'),
|
|
27
|
+
semver = require('semver'),
|
|
28
|
+
{safeRealpath} = require('./common'),
|
|
29
|
+
{INLINE_HOIST_SINGLETONS} = require('./hoistCompile');
|
|
30
|
+
|
|
31
|
+
/** Lists the packages to run inline, as `name` or `name=/abs/path`, comma-separated. */
|
|
32
|
+
const INLINE_PACKAGES_VAR = 'XH_INLINE_PACKAGES';
|
|
33
|
+
|
|
34
|
+
const HOIST = '@xh/hoist';
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Parse inline package specs from the `inlinePackages` option or `XH_INLINE_PACKAGES`: a
|
|
38
|
+
* comma-separated string or an array, each entry `name` or `name=/abs/path`.
|
|
39
|
+
* @returns {{name: string, path: ?string}[]}
|
|
40
|
+
*/
|
|
41
|
+
function parseInlinePackages(val) {
|
|
42
|
+
// 'false' arrives raw from the env var in tests - the build's readCliEnv() parses it first.
|
|
43
|
+
if (val == null || val === false || val === '' || val === 'false') return [];
|
|
44
|
+
const entries = _.isString(val) ? val.split(',') : _.castArray(val);
|
|
45
|
+
if (!entries.every(_.isString)) {
|
|
46
|
+
throw `inlinePackages takes package names, as "name" or "name=/abs/path".`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const ret = [];
|
|
50
|
+
entries
|
|
51
|
+
.map(it => it.trim())
|
|
52
|
+
.filter(Boolean)
|
|
53
|
+
.forEach(entry => {
|
|
54
|
+
// Split at the first '=' - a scoped name holds no '=', a path may.
|
|
55
|
+
const idx = entry.indexOf('='),
|
|
56
|
+
name = (idx < 0 ? entry : entry.slice(0, idx)).trim(),
|
|
57
|
+
dir = idx < 0 ? null : entry.slice(idx + 1).trim();
|
|
58
|
+
|
|
59
|
+
if (!name) throw `inlinePackages entry "${entry}" has no package name.`;
|
|
60
|
+
if (dir === '') throw `inlinePackages entry "${entry}" has an empty path.`;
|
|
61
|
+
if (name === HOIST || name.startsWith(HOIST + '/')) {
|
|
62
|
+
throw `inlinePackages cannot hold ${HOIST} - use inlineHoist for hoist-react.`;
|
|
63
|
+
}
|
|
64
|
+
if (dir && !path.isAbsolute(dir)) {
|
|
65
|
+
throw `inlinePackages path for ${name} must be absolute - found "${dir}".`;
|
|
66
|
+
}
|
|
67
|
+
if (ret.some(it => it.name === name)) {
|
|
68
|
+
throw `inlinePackages lists ${name} more than once.`;
|
|
69
|
+
}
|
|
70
|
+
ret.push({name, path: dir});
|
|
71
|
+
});
|
|
72
|
+
return ret;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Plan inline mode: locate each package checkout, then pick one directory for each shared name.
|
|
77
|
+
* Plans nothing when neither Hoist nor any package is inline. Throws, before the build logs
|
|
78
|
+
* anything, when a checkout or a required peer is missing. Returns warnings for the caller to log.
|
|
79
|
+
*
|
|
80
|
+
* @param {Object} opts
|
|
81
|
+
* @param {string} opts.appRoot - the app's root dir (client-app), a real path.
|
|
82
|
+
* @param {{name: string, path: ?string}[]} opts.specs - from `parseInlinePackages()`.
|
|
83
|
+
* @param {string} opts.hoistPath - hoist-react's real path, inline checkout or installed.
|
|
84
|
+
* @param {boolean} opts.inlineHoist - true when hoistPath is an inline checkout.
|
|
85
|
+
* @param {string[]} [opts.extraSingletons] - more names to share when the app has them.
|
|
86
|
+
* @returns {{packages: Object[], shared: Object[], warnings: string[]}} - `packages` holds
|
|
87
|
+
* `{name, path, nodeModules, pkg}` per checkout, `shared` holds `{name, dir, source}` per
|
|
88
|
+
* shared name, where source is 'inline' (a checkout), 'app' or 'hoist' (Hoist's own copy, for
|
|
89
|
+
* a transitive peer the app does not install).
|
|
90
|
+
*/
|
|
91
|
+
function planInlinePackages({appRoot, specs, hoistPath, inlineHoist, extraSingletons = []}) {
|
|
92
|
+
const warnings = [],
|
|
93
|
+
packages = specs.map(spec => locatePackage(appRoot, spec));
|
|
94
|
+
if (!packages.length && !inlineHoist) return {packages, shared: [], warnings};
|
|
95
|
+
|
|
96
|
+
// Checkouts first - an import of an inline name always goes to its checkout.
|
|
97
|
+
const inlineDirs = new Map(packages.map(p => [p.name, p.path]));
|
|
98
|
+
if (inlineHoist) inlineDirs.set(HOIST, hoistPath);
|
|
99
|
+
|
|
100
|
+
const shared = new Map();
|
|
101
|
+
const share = (name, dir, source) => shared.set(name, {name, dir, source});
|
|
102
|
+
inlineDirs.forEach((dir, name) => share(name, dir, 'inline'));
|
|
103
|
+
|
|
104
|
+
// Hoist is a singleton whether or not it is inline - it is our `hoistPath` either way.
|
|
105
|
+
if (!shared.has(HOIST)) share(HOIST, hoistPath, 'app');
|
|
106
|
+
|
|
107
|
+
// Peers. A required peer must resolve, an optional one is shared only when found.
|
|
108
|
+
packages.forEach(({name, pkg}) => {
|
|
109
|
+
const optional = pkg.peerDependenciesMeta ?? {};
|
|
110
|
+
Object.keys(pkg.peerDependencies ?? {}).forEach(peer => {
|
|
111
|
+
if (shared.has(peer)) return;
|
|
112
|
+
const found = findSharedCopy(peer, appRoot, hoistPath);
|
|
113
|
+
if (found) return share(peer, found.dir, found.source);
|
|
114
|
+
if (optional[peer]?.optional) return;
|
|
115
|
+
throw (
|
|
116
|
+
`Inline package ${name} needs peer "${peer}", which was not found in ` +
|
|
117
|
+
`${path.join(appRoot, 'node_modules')} or from ${hoistPath}. Add "${peer}" ` +
|
|
118
|
+
`to the app's dependencies.`
|
|
119
|
+
);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
// Singletons, when the app installs them itself. Unlike a peer, a singleton the app lacks is
|
|
124
|
+
// not one the app loads, so the checkouts' copies serve it.
|
|
125
|
+
[...INLINE_HOIST_SINGLETONS, ...extraSingletons].forEach(name => {
|
|
126
|
+
const dir = shared.has(name) ? null : findInAppNodeModules(name, appRoot);
|
|
127
|
+
if (dir) share(name, dir, 'app');
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
packages.forEach(p => warnings.push(...checkPackage(p, shared, appRoot)));
|
|
131
|
+
return {packages, shared: [...shared.values()], warnings};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The keys of an app's `resolveAliases` that name a shared package - `react` or `react$`. The plan
|
|
136
|
+
* replaces them, so a hand-written alias to a checkout from before inline packages stops applying.
|
|
137
|
+
*/
|
|
138
|
+
function replacedAppAliases(resolveAliases, shared) {
|
|
139
|
+
const names = new Set(shared.map(it => it.name));
|
|
140
|
+
return Object.keys(resolveAliases ?? {}).filter(key => names.has(key.replace(/\$$/, '')));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Add the inline settings to a `buildCache` config's `cacheDigest`, keeping any digest the app
|
|
145
|
+
* passed. Rsbuild keys the persistent cache on that digest, so a run with other inline settings
|
|
146
|
+
* gets a cache of its own, not snapshots that resolve to the wrong copy. A build cache that is off,
|
|
147
|
+
* or a run with nothing inline, is returned as passed.
|
|
148
|
+
*/
|
|
149
|
+
function withInlineCacheDigest(buildCache, {inlineHoist, hoistPath, packages}) {
|
|
150
|
+
const digest = [
|
|
151
|
+
...(inlineHoist ? [`inlineHoist:${hoistPath}`] : []),
|
|
152
|
+
...packages.map(it => `inlinePackage:${it.name}=${it.path}`)
|
|
153
|
+
];
|
|
154
|
+
if (!buildCache || !digest.length) return buildCache;
|
|
155
|
+
|
|
156
|
+
const config = _.isPlainObject(buildCache) ? buildCache : {};
|
|
157
|
+
return {...config, cacheDigest: [...(config.cacheDigest ?? []), ...digest]};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
//------------------------
|
|
161
|
+
// Implementation
|
|
162
|
+
//------------------------
|
|
163
|
+
function locatePackage(appRoot, {name, path: explicitPath}) {
|
|
164
|
+
// Default to a sibling of the app's repo, named for the package without its scope - as
|
|
165
|
+
// hoist-react's default checkout is `../../hoist-react`.
|
|
166
|
+
const dir = safeRealpath(explicitPath || path.resolve(appRoot, '../..', name.split('/').pop())),
|
|
167
|
+
pkg = readJson(path.join(dir, 'package.json'));
|
|
168
|
+
|
|
169
|
+
if (!pkg) {
|
|
170
|
+
throw explicitPath
|
|
171
|
+
? `Inline package ${name} points at ${dir}, which holds no package.json.`
|
|
172
|
+
: `Inline package ${name} found no checkout at ${dir}. Clone it there, or pass its ` +
|
|
173
|
+
`path as "${name}=/abs/path" in ${INLINE_PACKAGES_VAR}.`;
|
|
174
|
+
}
|
|
175
|
+
if (pkg.name !== name) {
|
|
176
|
+
throw `Inline package ${name} points at ${dir}, which holds "${pkg.name}" instead.`;
|
|
177
|
+
}
|
|
178
|
+
// Its dependencies resolve from the checkout, so they must be installed there. Peers come
|
|
179
|
+
// from the app, so a package with none of its own needs no install.
|
|
180
|
+
const nodeModules = path.join(dir, 'node_modules');
|
|
181
|
+
if (!_.isEmpty(pkg.dependencies) && !fs.existsSync(nodeModules)) {
|
|
182
|
+
throw (
|
|
183
|
+
`Inline package ${name} at ${dir} has no node_modules - install its dependencies ` +
|
|
184
|
+
`there first (e.g. \`pnpm install\`).`
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
return {name, path: dir, nodeModules, pkg};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* The copy of a shared name that every importer gets: the app's own, else the one Hoist loads -
|
|
192
|
+
* e.g. MobX, which an app takes through `@xh/hoist/mobx` and does not install itself. The app's
|
|
193
|
+
* copy must be in its own node_modules, as for singletons - a stray copy above the app is one
|
|
194
|
+
* Hoist does not load.
|
|
195
|
+
*/
|
|
196
|
+
function findSharedCopy(name, appRoot, hoistPath) {
|
|
197
|
+
const appDir = findInAppNodeModules(name, appRoot);
|
|
198
|
+
if (appDir) return {dir: appDir, source: 'app'};
|
|
199
|
+
const hoistDir = findInNodeModules(name, hoistPath);
|
|
200
|
+
return hoistDir ? {dir: hoistDir, source: 'hoist'} : null;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** A package in the app's own node_modules, by its real path, or null. */
|
|
204
|
+
function findInAppNodeModules(name, appRoot) {
|
|
205
|
+
const dir = path.join(appRoot, 'node_modules', name);
|
|
206
|
+
return fs.existsSync(path.join(dir, 'package.json')) ? safeRealpath(dir) : null;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Find a package's directory as Node would from `fromDir` - each node_modules up the tree - by its
|
|
211
|
+
* package.json. A plain lookup, as `require.resolve('x/package.json')` fails on packages whose
|
|
212
|
+
* `exports` field hides their package.json. Returns the real path, or null.
|
|
213
|
+
*/
|
|
214
|
+
function findInNodeModules(name, fromDir) {
|
|
215
|
+
for (let dir = fromDir; ; dir = path.dirname(dir)) {
|
|
216
|
+
const candidate = path.join(dir, 'node_modules', name);
|
|
217
|
+
if (fs.existsSync(path.join(candidate, 'package.json'))) return safeRealpath(candidate);
|
|
218
|
+
if (path.dirname(dir) === dir) return null;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Warnings for one inline package: app pairing, peer ranges and misplaced dependencies. */
|
|
223
|
+
function checkPackage({name, pkg}, shared, appRoot) {
|
|
224
|
+
const ret = [],
|
|
225
|
+
appPkg = readJson(path.join(appRoot, 'package.json')) ?? {};
|
|
226
|
+
|
|
227
|
+
if (!appPkg.dependencies?.[name] && !appPkg.devDependencies?.[name]) {
|
|
228
|
+
ret.push(`${name} is not in the app's package.json - running it inline anyway.`);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Warn only - a mismatch may be deliberate mid-way through a paired upgrade. Prereleases count,
|
|
232
|
+
// or every Hoist snapshot would fail a plain floor such as ">=89.3.1".
|
|
233
|
+
_.forEach(pkg.peerDependencies, (range, peer) => {
|
|
234
|
+
const dir = shared.get(peer)?.dir,
|
|
235
|
+
version = dir && readJson(path.join(dir, 'package.json'))?.version;
|
|
236
|
+
if (!version || !semver.validRange(range)) return;
|
|
237
|
+
if (!semver.satisfies(version, range, {includePrerelease: true})) {
|
|
238
|
+
ret.push(`${name} wants ${peer} "${range}", but the app will run ${version}.`);
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
// A shared name under `dependencies` still gets the shared copy here, but an install from npm
|
|
243
|
+
// gives the package its own - declare it a peer so both agree. Other @xh packages are flagged
|
|
244
|
+
// too: a second copy of a companion package would not be shared.
|
|
245
|
+
Object.keys(pkg.dependencies ?? {}).forEach(dep => {
|
|
246
|
+
if (shared.has(dep) || dep.startsWith('@xh/')) {
|
|
247
|
+
ret.push(`${name} lists "${dep}" in dependencies - it should be a peerDependency.`);
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
return ret;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function readJson(file) {
|
|
254
|
+
try {
|
|
255
|
+
return JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
256
|
+
} catch (e) {
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
module.exports = {
|
|
262
|
+
INLINE_PACKAGES_VAR,
|
|
263
|
+
parseInlinePackages,
|
|
264
|
+
planInlinePackages,
|
|
265
|
+
replacedAppAliases,
|
|
266
|
+
withInlineCacheDigest
|
|
267
|
+
};
|
|
@@ -6,15 +6,20 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
* First setup file of an inline
|
|
9
|
+
* First setup file of an inline test run - inline Hoist, inline packages or both - added by
|
|
10
|
+
* `configureVitest()`.
|
|
10
11
|
*
|
|
11
|
-
* Vite's `resolve.dedupe`
|
|
12
|
-
* checkout's own dependencies (e.g. mobx-react-lite, Blueprint, ag-grid-react), so
|
|
13
|
-
* and resolves their `react` from the checkout's node_modules - a second React,
|
|
14
|
-
* This hook sends
|
|
12
|
+
* Vite's `resolve.dedupe` and aliases reach only the modules Vite resolves. Vitest externalizes a
|
|
13
|
+
* checkout's own dependencies (e.g. mobx-react-lite, Blueprint, ag-grid-react, @ag-ui/client), so
|
|
14
|
+
* Node loads them and resolves their `react` from the checkout's node_modules - a second React,
|
|
15
|
+
* and hooks fail. This hook sends imports of each shared name made from a checkout's node_modules
|
|
16
|
+
* to the one copy the build uses, as the build's aliases do. A shared name that is itself a
|
|
17
|
+
* checkout (`@xh/hoist` under inline Hoist) is not redirected - a library a checkout installs does
|
|
18
|
+
* not import its host.
|
|
15
19
|
*
|
|
16
|
-
* Settings arrive in `XH_VITEST_INLINE_HOIST_CFG`, set by `configureVitest()
|
|
17
|
-
* `
|
|
20
|
+
* Settings arrive in `XH_VITEST_INLINE_HOIST_CFG`, set by `configureVitest()`: `fromDirs`, the
|
|
21
|
+
* checkouts' node_modules dirs, and `singletons`, each `{name, from}` with the dir to resolve that
|
|
22
|
+
* name from. `module.registerHooks()` needs Node >= 22.15.
|
|
18
23
|
*/
|
|
19
24
|
import {createRequire, registerHooks} from 'node:module';
|
|
20
25
|
import path from 'node:path';
|
|
@@ -22,25 +27,37 @@ import {pathToFileURL} from 'node:url';
|
|
|
22
27
|
|
|
23
28
|
const cfg = JSON.parse(process.env.XH_VITEST_INLINE_HOIST_CFG ?? 'null');
|
|
24
29
|
|
|
25
|
-
if (cfg?.singletons?.length) {
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
if (cfg?.singletons?.length && cfg.fromDirs?.length) {
|
|
31
|
+
const fromUrls = cfg.fromDirs.map(it => pathToFileURL(path.join(it, '/')).href),
|
|
32
|
+
escape = it => it.replace(/[.*+?^${}()|[\]\\/]/g, '\\$&'),
|
|
33
|
+
targets = cfg.singletons.map(({name, from}) => {
|
|
34
|
+
const parentUrl = pathToFileURL(path.join(from, 'package.json')).href;
|
|
35
|
+
return {
|
|
36
|
+
re: new RegExp(`^${escape(name)}(?:/|$)`),
|
|
37
|
+
parentUrl,
|
|
38
|
+
require: createRequire(parentUrl)
|
|
39
|
+
};
|
|
40
|
+
});
|
|
31
41
|
|
|
32
42
|
registerHooks({
|
|
33
43
|
resolve(specifier, context, nextResolve) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
44
|
+
const parent = context.parentURL,
|
|
45
|
+
target =
|
|
46
|
+
parent && fromUrls.some(it => parent.startsWith(it))
|
|
47
|
+
? targets.find(it => it.re.test(specifier))
|
|
48
|
+
: null;
|
|
49
|
+
if (!target) return nextResolve(specifier, context);
|
|
50
|
+
|
|
51
|
+
// An ESM import resolves from the shared copy's location with its own conditions, so
|
|
52
|
+
// it still gets a package's ESM build (ag-Grid ships both). A changed parentURL does
|
|
53
|
+
// not redirect a CJS require(), so that resolves through a require from there instead.
|
|
40
54
|
if (context.conditions?.includes('import')) {
|
|
41
|
-
return nextResolve(specifier, {...context, parentURL:
|
|
55
|
+
return nextResolve(specifier, {...context, parentURL: target.parentUrl});
|
|
42
56
|
}
|
|
43
|
-
return {
|
|
57
|
+
return {
|
|
58
|
+
url: pathToFileURL(target.require.resolve(specifier)).href,
|
|
59
|
+
shortCircuit: true
|
|
60
|
+
};
|
|
44
61
|
}
|
|
45
62
|
});
|
|
46
63
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xh/hoist-dev-utils",
|
|
3
|
-
"version": "16.
|
|
3
|
+
"version": "16.4.0",
|
|
4
4
|
"description": "Rsbuild (Rspack + SWC) build configuration and shared dev dependencies for Hoist React applications.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
"changelog-parser": "~4.1.0",
|
|
37
37
|
"lodash": "^4.18.1",
|
|
38
38
|
"sass-embedded": "^1.108.2",
|
|
39
|
+
"semver": "^7.8.5",
|
|
39
40
|
"type-fest": "^5.10.0"
|
|
40
41
|
},
|
|
41
42
|
"devDependencies": {
|