@xh/hoist-dev-utils 16.0.0 → 16.1.1
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 +100 -26
- package/configureRsbuild.d.ts +193 -0
- package/configureRsbuild.js +65 -36
- package/configureVitest.d.ts +58 -0
- package/configureVitest.js +497 -0
- package/lib/common.js +38 -18
- package/lib/hoistCompile.js +114 -0
- package/lib/vitestInlineHoist.mjs +46 -0
- package/package.json +14 -8
package/README.md
CHANGED
|
@@ -10,8 +10,8 @@ applications.
|
|
|
10
10
|
The package.json file in this repository specifies a set of development dependencies required for
|
|
11
11
|
building Hoist React applications. Those applications can specify `@xh/hoist-dev-utils` as a dev
|
|
12
12
|
dependency and transitively bring in [Rsbuild](https://rsbuild.rs) (Rspack + SWC) and the plugins
|
|
13
|
-
used in app builds, including the Rsbuild dev server
|
|
14
|
-
decorators
|
|
13
|
+
used in app builds, including the Rsbuild dev server and Sass. SWC transpiles Hoist's TC39
|
|
14
|
+
(`2023-11`) decorators, so no Babel install is needed.
|
|
15
15
|
|
|
16
16
|
While Hoist Dev Utils provides most essential dev dependencies for Hoist React, apps typically also include:
|
|
17
17
|
|
|
@@ -29,7 +29,8 @@ The `configureRsbuild.js` module exports a single `configureRsbuild()` function
|
|
|
29
29
|
complete [Rsbuild](https://rsbuild.rs) (Rspack + SWC) configuration. This includes transpiling and
|
|
30
30
|
bundling multiple client application entry points, styles (CSS/SASS), HTML index file generation,
|
|
31
31
|
and pre-compressed assets for production builds. See the docs within `configureRsbuild.js` for
|
|
32
|
-
supported arguments and additional details.
|
|
32
|
+
supported arguments and additional details. The package ships type declarations for both config
|
|
33
|
+
modules. Add `// @ts-check` to the top of `rsbuild.config.mjs` to have the IDE check its options.
|
|
33
34
|
|
|
34
35
|
The generated configuration also sets the value of several XH globals within the built JS code, via
|
|
35
36
|
Rspack's DefinePlugin. These include `XH.appCode` and `XH.appName` (both required), `XH.appVersion`
|
|
@@ -119,6 +120,92 @@ Dev-utils 16 is Rsbuild only - `configureWebpack()` is gone. To move an app:
|
|
|
119
120
|
twins of compressible assets. Chunk boundaries differ from webpack's, so per-file names and sizes
|
|
120
121
|
will not line up.
|
|
121
122
|
|
|
123
|
+
## Unit tests with Vitest
|
|
124
|
+
|
|
125
|
+
The `configureVitest.js` module exports `configureVitest()`, a [Vitest](https://vitest.dev) preset
|
|
126
|
+
for app unit tests. It compiles specs with the SWC inside Rspack and the same settings as
|
|
127
|
+
`configureRsbuild()`, so tests run the code the app ships. It also sets the same `XH` constants and
|
|
128
|
+
loads hoist-react's test setup, which starts a fake hoist-core server for each test file.
|
|
129
|
+
|
|
130
|
+
**Requires hoist-react >= 89.** The preset loads `test-support/setup.ts` from the installed
|
|
131
|
+
`@xh/hoist`, and hoist-react ships its `test-support/` folder from v89. With an older hoist-react,
|
|
132
|
+
the preset fails with a message that names the version it found. Builds still work with that
|
|
133
|
+
version.
|
|
134
|
+
|
|
135
|
+
This package does not bring the test runners. Add them to the app as devDependencies:
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
pnpm add -D vitest jsdom msw @testing-library/react @testing-library/dom
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Take the latest versions that fit the optional peer ranges in the `package.json` of `@xh/hoist`.
|
|
142
|
+
pnpm warns about any version outside those ranges. Then add scripts:
|
|
143
|
+
|
|
144
|
+
```json
|
|
145
|
+
"scripts": {
|
|
146
|
+
"test": "vitest run",
|
|
147
|
+
"test:watch": "vitest",
|
|
148
|
+
"testWithHoist": "(cd ../../hoist-react && pnpm install) && pnpm install && XH_INLINE_HOIST=true vitest"
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
hoist-react's test setup unmounts rendered components after each test, so it needs React Testing
|
|
153
|
+
Library even if the app's specs never render. Then add a `vitest.config.mts` next to
|
|
154
|
+
`rsbuild.config.mjs`:
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
import configureVitest from '@xh/hoist-dev-utils/configureVitest';
|
|
158
|
+
import {defineConfig} from 'vitest/config';
|
|
159
|
+
|
|
160
|
+
export default defineConfig(configureVitest({appCode: 'myApp'}));
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Import the preset by package name. Vite bundles a relative import of this CommonJS file, and the
|
|
164
|
+
bundled copy fails. The preset's type declarations let the IDE check its options. Put specs next to the code they test, as `src/**/*.spec.ts`. The build skips
|
|
165
|
+
`*.spec.*` and `*.test.*` files in `src/apps/`, so a spec there does not become an app entry.
|
|
166
|
+
See hoist-react's `docs/unit-testing.md` for how to write specs.
|
|
167
|
+
|
|
168
|
+
To add Vitest settings, wrap the result in `mergeConfig()` from `vitest/config`. A scalar setting
|
|
169
|
+
from the app wins. `mergeConfig()` joins arrays, so it cannot narrow a list: pass `include` and
|
|
170
|
+
`setupFiles` to the preset instead.
|
|
171
|
+
|
|
172
|
+
`configureVitest()` takes the same `env` object as `configureRsbuild()`, so an app can share one
|
|
173
|
+
object between its two configs. Add the test-only keys in the Vitest config alone, as in
|
|
174
|
+
`configureVitest({...env, setupFiles: ['./src/test-support/setup.ts']})`. `configureRsbuild()`
|
|
175
|
+
warns about keys it does not know.
|
|
176
|
+
|
|
177
|
+
| Option | In tests |
|
|
178
|
+
|---|---|
|
|
179
|
+
| `appCode` (required), `appName`, `appVersion`, `appBuild`, `baseUrl` | Set in `XH`, with the build defaults |
|
|
180
|
+
| `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`. |
|
|
181
|
+
| `extraModuleRules` | Ignored, with a warning. Rspack rules cannot run under Vite. |
|
|
182
|
+
| Other build options | Ignored |
|
|
183
|
+
| `root` | Project root. Default: the current directory. |
|
|
184
|
+
| `include` | Spec globs. Default: `['src/**/*.spec.{ts,tsx}']`. |
|
|
185
|
+
| `setupFiles` | App setup files, run after hoist-react's `test-support/setup.ts`. Default: `[]`. |
|
|
186
|
+
| `timeZone` | Sets `TZ` for the run. Default: `'America/New_York'`. `null` keeps the machine zone. |
|
|
187
|
+
| `selfHost` | For hoist-react's own config only. Aliases `@xh/hoist` to the project root. |
|
|
188
|
+
|
|
189
|
+
The preset gives hoist-react's test kit a fixed setup. `XH.isDevelopmentMode` is false, and client
|
|
190
|
+
app names come from `src/apps`, or `['app']` without that folder. Each test file runs isolated in
|
|
191
|
+
jsdom, with mocks, env stubs and global stubs restored after each test. Stylesheet imports resolve to
|
|
192
|
+
an empty module, and with `?inline`, `?raw` or `?url` to an empty string. `@xh/app-changelog.json`
|
|
193
|
+
resolves to `{}`, and a `.md` import to its text.
|
|
194
|
+
|
|
195
|
+
A run with `--no-isolate`, or in the `vmThreads` or `vmForks` pool, fails, because each test file
|
|
196
|
+
boots its own `XH`. If the installed `@xh/hoist` declares a `vitest` peer dependency, the preset also
|
|
197
|
+
fails on a Vitest major outside that range.
|
|
198
|
+
|
|
199
|
+
`XH_INLINE_HOIST=true` (or `inlineHoist: true`) runs the app's tests against a hoist-react
|
|
200
|
+
checkout at `../../hoist-react`, as `startWithHoist` does for the dev server. The preset aliases
|
|
201
|
+
`@xh/hoist` to the checkout. It dedupes React, ag-Grid, msw, React Testing Library and MobX to the
|
|
202
|
+
app's own copies, if the app has them. A Node resolve hook does the same for the checkout's
|
|
203
|
+
dependencies, which Vitest loads outside Vite. Inline mode works under pnpm. It is untested under
|
|
204
|
+
yarn and npm.
|
|
205
|
+
|
|
206
|
+
The name matches Vitest's own `configureVitest` plugin hook. The two are unrelated: this one is a
|
|
207
|
+
config factory, like `configureRsbuild()`.
|
|
208
|
+
|
|
122
209
|
## Favicons
|
|
123
210
|
|
|
124
211
|
To include a favicon with your app, provide the `favicon` option to `configureRsbuild()`. This can be either
|
|
@@ -140,29 +227,15 @@ The following files will be automatically bundled in your app's `manifest.json`
|
|
|
140
227
|
+ `favicon-512.png` (512px x 512px)
|
|
141
228
|
+ `apple-touch-icon.png` (180px x 180px)
|
|
142
229
|
|
|
143
|
-
### Generating favicons
|
|
144
|
-
|
|
145
|
-
You can use the `svg-favicon.sh` script included in this repo to automatically create these favicons from a square SVG.
|
|
146
|
-
Note that this script requires inkscape to be installed. Download the latest version
|
|
147
|
-
from [https://inkscape.org/](https://inkscape.org/) or install on Mac via Homebrew with `brew install inkscape`.
|
|
230
|
+
### Generating favicons with the Toolbox Favicon Designer
|
|
148
231
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
/usr/local/bin/inkscape
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
Then run the script, passing a path to the SVG file as the argument. The command below assumes that you have
|
|
160
|
-
`hoist-dev-utils` checked out as a sibling of your top-level project directory, and that you are running the command
|
|
161
|
-
from within `$projectDir/client-app/public`:
|
|
162
|
-
|
|
163
|
-
```shell
|
|
164
|
-
../../../hoist-dev-utils/svg-favicon.sh favicon.svg
|
|
165
|
-
```
|
|
232
|
+
Use the [Favicon Designer](https://toolbox.xh.io/favicons/) example app in Toolbox to create a
|
|
233
|
+
full favicon set. Pick any Font Awesome Pro glyph, choose colors, a backdrop shape and padding, and
|
|
234
|
+
preview the result in browser tabs and on a phone home screen. Download a zip of `favicon.svg`,
|
|
235
|
+
`favicon-192.png`, `favicon-512.png` and `apple-touch-icon.png`, unzip it into your app's
|
|
236
|
+
`client-app/public` folder, and paste the generated `configureRsbuild()` snippet into
|
|
237
|
+
`rsbuild.config.mjs`. The design is encoded in the page URL, and in a comment within the generated
|
|
238
|
+
`favicon.svg`, so you can reopen and tweak it later.
|
|
166
239
|
|
|
167
240
|
## ESLint Configuration
|
|
168
241
|
|
|
@@ -232,7 +305,8 @@ manager the app itself uses.
|
|
|
232
305
|
This repo itself is managed with [pnpm](https://pnpm.io) - run `pnpm install` to install its
|
|
233
306
|
dependencies. The required pnpm version is pinned via the `packageManager` field in `package.json`
|
|
234
307
|
and will be provisioned automatically by [corepack](https://nodejs.org/api/corepack.html)
|
|
235
|
-
(`corepack enable pnpm`) or by a standalone pnpm install of v10+.
|
|
308
|
+
(`corepack enable pnpm`) or by a standalone pnpm install of v10+. Run `pnpm test` for the
|
|
309
|
+
`node --test` specs under `test/`.
|
|
236
310
|
|
|
237
311
|
------------------------------------------
|
|
238
312
|
|
|
@@ -0,0 +1,193 @@
|
|
|
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
|
+
|
|
8
|
+
// Hand-written declarations for configureRsbuild.js. Keep in step with its JSDoc and with
|
|
9
|
+
// KNOWN_OPTIONS in lib/common.js - test/declarations.test.js checks the keys.
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Consolidated Rsbuild (Rspack + SWC) configuration for both dev-time and production builds of
|
|
13
|
+
* Hoist React web applications. Apps consume it from an `rsbuild.config.mjs`:
|
|
14
|
+
*
|
|
15
|
+
* import {defineConfig} from '@rsbuild/core';
|
|
16
|
+
* import configureRsbuild from '@xh/hoist-dev-utils/configureRsbuild';
|
|
17
|
+
* export default defineConfig(({envMode}) => configureRsbuild({appCode: 'myApp', ...}));
|
|
18
|
+
*
|
|
19
|
+
* Build-time overrides flow in as `XH_*` environment variables - see `readCliEnv()`.
|
|
20
|
+
*
|
|
21
|
+
* @returns a complete Rsbuild config object, typed loosely. Rsbuild's `defineConfig()` checks it.
|
|
22
|
+
*/
|
|
23
|
+
declare function configureRsbuild(env: configureRsbuild.RsbuildEnv): Promise<Record<string, any>>;
|
|
24
|
+
|
|
25
|
+
declare namespace configureRsbuild {
|
|
26
|
+
/** Also exported by name, for `import {configureRsbuild} from ...`. */
|
|
27
|
+
const configureRsbuild: (env: RsbuildEnv) => Promise<Record<string, any>>;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Read the standard `XH_*` environment variables into env options, for CLI-driven overrides.
|
|
31
|
+
* Spread the result into the config passed to `configureRsbuild()` after the app's own
|
|
32
|
+
* defaults. Rsbuild loads `.env`, `.env.local`, `.env.<mode>` and `.env.<mode>.local` into
|
|
33
|
+
* `process.env` before it evaluates the config, so the variables can live there too.
|
|
34
|
+
*
|
|
35
|
+
* 'true' and 'false' become booleans. Unset variables are omitted, so app defaults win.
|
|
36
|
+
*/
|
|
37
|
+
function readCliEnv(processEnv?: Record<string, string | undefined>): Partial<RsbuildEnv>;
|
|
38
|
+
|
|
39
|
+
/** Options that `configureRsbuild()` and `configureVitest()` both honor. */
|
|
40
|
+
interface SharedEnv {
|
|
41
|
+
/**
|
|
42
|
+
* Short, internal code for the application - baked into client as `XH.appCode`. Should be
|
|
43
|
+
* lowercase, dash-separated, and should match the Gradle project name (e.g.
|
|
44
|
+
* portfolio-manager).
|
|
45
|
+
*/
|
|
46
|
+
appCode: string;
|
|
47
|
+
/**
|
|
48
|
+
* User-facing display name for the application - baked into client as `XH.appName`.
|
|
49
|
+
* Defaults from appCode (portfolio-manager -> Portfolio Manager).
|
|
50
|
+
*/
|
|
51
|
+
appName?: string;
|
|
52
|
+
/** Client version - baked into client as `XH.appVersion`. Default '1.0-SNAPSHOT'. */
|
|
53
|
+
appVersion?: string;
|
|
54
|
+
/** Build/git tag - baked into client as `XH.appBuild`. Default 'UNKNOWN'. */
|
|
55
|
+
appBuild?: string;
|
|
56
|
+
/**
|
|
57
|
+
* Root path prepended to all relative URLs called via FetchService. Defaults to `/api/`,
|
|
58
|
+
* which the dev server proxies to the Grails backend at `devHost:devGrailsPort`.
|
|
59
|
+
*/
|
|
60
|
+
baseUrl?: string;
|
|
61
|
+
/**
|
|
62
|
+
* True to use a sibling hoist-react checkout at `../../hoist-react` in place of the
|
|
63
|
+
* installed package. Dev-mode only for the build.
|
|
64
|
+
*/
|
|
65
|
+
inlineHoist?: boolean;
|
|
66
|
+
/** Additional paths to transpile with the settings used for app and hoist-react code. */
|
|
67
|
+
extraIncludePaths?: string[];
|
|
68
|
+
/** Paths to exclude from transpiling, e.g. a local package's nested node_modules. */
|
|
69
|
+
extraExcludePaths?: string[];
|
|
70
|
+
/** @deprecated renamed to `extraIncludePaths`. */
|
|
71
|
+
babelIncludePaths?: string[];
|
|
72
|
+
/** @deprecated renamed to `extraExcludePaths`. */
|
|
73
|
+
babelExcludePaths?: string[];
|
|
74
|
+
/**
|
|
75
|
+
* Module aliases. When inlineHoist=true, an alias from `@xh/hoist` to the checkout is
|
|
76
|
+
* added.
|
|
77
|
+
*/
|
|
78
|
+
resolveAliases?: Record<string, string | string[] | false>;
|
|
79
|
+
/**
|
|
80
|
+
* Overrides for SWC options, applied on top of the defaults - either an object to
|
|
81
|
+
* deep-merge, or a function receiving the options to mutate or replace.
|
|
82
|
+
*/
|
|
83
|
+
swcOptions?:
|
|
84
|
+
Record<string, any> | ((options: Record<string, any>) => Record<string, any> | void);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Options for `configureRsbuild()`. */
|
|
88
|
+
interface RsbuildEnv extends SharedEnv {
|
|
89
|
+
/** True for a production build, as opposed to a run of the dev server. */
|
|
90
|
+
prodBuild?: boolean;
|
|
91
|
+
/** True to use the production build of React when running the dev server. */
|
|
92
|
+
reactProdMode?: boolean;
|
|
93
|
+
/**
|
|
94
|
+
* Source map generation. `true` for defaults specific to dev vs. prod builds, `false` to
|
|
95
|
+
* disable, `'devOnly'` for the dev default only, or any valid Rspack `devtool` string.
|
|
96
|
+
* Default true.
|
|
97
|
+
*/
|
|
98
|
+
sourceMaps?: boolean | string;
|
|
99
|
+
/**
|
|
100
|
+
* Browserslist queries for the target browsers, driving JS transpiling, polyfill
|
|
101
|
+
* selection and CSS prefixing.
|
|
102
|
+
*/
|
|
103
|
+
targetBrowsers?: string[];
|
|
104
|
+
/** False to skip JS/CSS minification in production builds. Default true. */
|
|
105
|
+
minify?: boolean;
|
|
106
|
+
/**
|
|
107
|
+
* True to enable Rspack's persistent build cache, or Rsbuild's options object
|
|
108
|
+
* (`cacheDirectory`, `cacheDigest`, `buildDependencies`). Default false.
|
|
109
|
+
*/
|
|
110
|
+
buildCache?: boolean | Record<string, any>;
|
|
111
|
+
/** Rsbuild log level. Default 'info'. */
|
|
112
|
+
logLevel?: 'info' | 'warn' | 'error' | 'silent';
|
|
113
|
+
/**
|
|
114
|
+
* Options to deep-merge onto the defaults for the SWC minimizer in production builds -
|
|
115
|
+
* `compress`, `mangle` and `format`, in the same shape as Terser's.
|
|
116
|
+
*/
|
|
117
|
+
minifyOptions?: Record<string, any>;
|
|
118
|
+
/**
|
|
119
|
+
* Additional Rspack module rules, added ahead of the built-in rules. Declare any loaders
|
|
120
|
+
* they use as devDependencies of the app.
|
|
121
|
+
*/
|
|
122
|
+
extraModuleRules?: Record<string, any>[];
|
|
123
|
+
/**
|
|
124
|
+
* True to load all BlueprintJs icons. Default false loads only the icons Hoist React
|
|
125
|
+
* needs, for a much smaller bundle.
|
|
126
|
+
*/
|
|
127
|
+
loadAllBlueprintJsIcons?: boolean;
|
|
128
|
+
/** Root path from which the app will be served, used as the base path for static files. */
|
|
129
|
+
contextRoot?: string;
|
|
130
|
+
/**
|
|
131
|
+
* True to copy the `/client-app/public` contents into the root of the build, unprocessed.
|
|
132
|
+
* Required for favicons. Default true.
|
|
133
|
+
*/
|
|
134
|
+
copyPublicAssets?: boolean;
|
|
135
|
+
/**
|
|
136
|
+
* True to parse a `CHANGELOG.md` in the project root into JSON for
|
|
137
|
+
* `XH.changelogService`. Default true.
|
|
138
|
+
*/
|
|
139
|
+
parseChangelog?: boolean;
|
|
140
|
+
/**
|
|
141
|
+
* Pre-compressed `.br` and `.gz` copies of bundled assets, for nginx. `false` to disable,
|
|
142
|
+
* or an object to override the compression plugin defaults. Production builds only.
|
|
143
|
+
* Default true.
|
|
144
|
+
*/
|
|
145
|
+
precompressAssets?:
|
|
146
|
+
| boolean
|
|
147
|
+
| {
|
|
148
|
+
test?: RegExp | string | (RegExp | string)[];
|
|
149
|
+
include?: RegExp | string | (RegExp | string)[];
|
|
150
|
+
exclude?: RegExp | string | (RegExp | string)[];
|
|
151
|
+
threshold?: number;
|
|
152
|
+
minRatio?: number;
|
|
153
|
+
};
|
|
154
|
+
/** Relative path to a primary favicon source image, `png` or `svg`. */
|
|
155
|
+
favicon?: string;
|
|
156
|
+
/** Override values for each app's manifest.json. */
|
|
157
|
+
manifestConfig?: Record<string, any>;
|
|
158
|
+
/** Background color for the preloader spinner. Default white. */
|
|
159
|
+
preloadBackgroundColor?: string;
|
|
160
|
+
/** Stroke color for the preloader spinner SVG. Default '#888'. */
|
|
161
|
+
preloadSpinnerColor?: string;
|
|
162
|
+
/**
|
|
163
|
+
* Dev-server overlay behavior, in webpack-dev-server's `{errors, runtimeErrors}` shape.
|
|
164
|
+
* Default shows compilation errors only. Dev-mode only.
|
|
165
|
+
*/
|
|
166
|
+
devClientOverlay?: boolean | {errors?: boolean; runtimeErrors?: boolean};
|
|
167
|
+
/** Hostname for both the local Grails and dev servers. Default 'localhost'. Dev-mode only. */
|
|
168
|
+
devHost?: string;
|
|
169
|
+
/** Port of the local Grails server. Default 8080. Dev-mode only. */
|
|
170
|
+
devGrailsPort?: number | string;
|
|
171
|
+
/** Port on which to start the dev server (historic name). Default 3000. Dev-mode only. */
|
|
172
|
+
devWebpackPort?: number | string;
|
|
173
|
+
/**
|
|
174
|
+
* Path to open when the dev server starts. Unset to open nothing. Also where the dev server
|
|
175
|
+
* redirects the bare root `/` - unset, the `app` entry or a sole entry. Dev-mode only.
|
|
176
|
+
*/
|
|
177
|
+
devServerOpenPage?: string;
|
|
178
|
+
/**
|
|
179
|
+
* `true` to run the dev server over SSL with a self-signed cert, or Node
|
|
180
|
+
* `https.createServer` options for a custom cert/key. Default false. Dev-mode only.
|
|
181
|
+
*/
|
|
182
|
+
devHttps?: boolean | Record<string, any>;
|
|
183
|
+
/**
|
|
184
|
+
* False to stop the dev server reloading the page when an edit cannot be hot-swapped.
|
|
185
|
+
* Default true. Dev-mode only.
|
|
186
|
+
*/
|
|
187
|
+
devLiveReload?: boolean;
|
|
188
|
+
/** Options to spread onto the defaults for Rsbuild's `server` config. Dev-mode only. */
|
|
189
|
+
devServerOptions?: Record<string, any>;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export = configureRsbuild;
|
package/configureRsbuild.js
CHANGED
|
@@ -37,6 +37,14 @@ const {
|
|
|
37
37
|
logMsg
|
|
38
38
|
} = require('./lib/common');
|
|
39
39
|
|
|
40
|
+
const {
|
|
41
|
+
HOIST_SWC,
|
|
42
|
+
INLINE_HOIST_SINGLETONS,
|
|
43
|
+
resolveAppIdentity,
|
|
44
|
+
resolveHoistPath,
|
|
45
|
+
xhDefines
|
|
46
|
+
} = require('./lib/hoistCompile');
|
|
47
|
+
|
|
40
48
|
// These are not deps of hoist-dev-utils but of the consuming app, so resolve them from the
|
|
41
49
|
// app's own directory (basePath).
|
|
42
50
|
const hoistReactPkg = resolveAppPackage('@xh/hoist', basePath),
|
|
@@ -134,7 +142,8 @@ const hoistReactPkg = resolveAppPackage('@xh/hoist', basePath),
|
|
|
134
142
|
* @param {number} [env.devGrailsPort] - port of local Grails server. Dev-mode only.
|
|
135
143
|
* @param {number} [env.devWebpackPort] - port on which to start the dev server. (Historic name retained.) Dev-mode only.
|
|
136
144
|
* @param {string} [env.devServerOpenPage] - path to auto-open when the dev server starts. Leave null to disable
|
|
137
|
-
* automatic page open on startup.
|
|
145
|
+
* automatic page open on startup. Also where the dev server redirects a request for the bare root `/` -
|
|
146
|
+
* unset, that redirect goes to the `app` entry, or to the only entry if there is one. Dev-mode only.
|
|
138
147
|
* @param {boolean} [env.devLiveReload=true] - false to stop the dev server from reloading the page when an edit
|
|
139
148
|
* cannot be hot-swapped (the equivalent of webpack-dev-server's `--no-live-reload`). Hot-swaps of CSS and
|
|
140
149
|
* Fast-Refresh-eligible components still apply; other edits are rebuilt but not applied until a manual
|
|
@@ -147,14 +156,11 @@ const hoistReactPkg = resolveAppPackage('@xh/hoist', basePath),
|
|
|
147
156
|
* @returns {Promise<Object>} a complete Rsbuild config object.
|
|
148
157
|
*/
|
|
149
158
|
async function configureRsbuild(env) {
|
|
150
|
-
|
|
159
|
+
// Identity defaults are shared with configureVitest(). Throws when `appCode` is missing.
|
|
160
|
+
const {appCode, appName, appVersion, appBuild, baseUrl} = resolveAppIdentity(env);
|
|
151
161
|
rejectUnsupported(env);
|
|
152
162
|
|
|
153
|
-
const
|
|
154
|
-
appName = env.appName || _.startCase(appCode),
|
|
155
|
-
appVersion = env.appVersion || '1.0-SNAPSHOT',
|
|
156
|
-
appBuild = env.appBuild || 'UNKNOWN',
|
|
157
|
-
prodBuild = parseFlag(env.prodBuild, false) === true,
|
|
163
|
+
const prodBuild = parseFlag(env.prodBuild, false) === true,
|
|
158
164
|
inlineHoist = !prodBuild && parseFlag(env.inlineHoist, false) === true,
|
|
159
165
|
reactProdMode = prodBuild || parseFlag(env.reactProdMode, false) === true,
|
|
160
166
|
resolveAliases = Object.assign({}, env.resolveAliases),
|
|
@@ -169,7 +175,6 @@ async function configureRsbuild(env) {
|
|
|
169
175
|
devGrailsPort = env.devGrailsPort || 8080,
|
|
170
176
|
devWebpackPort = env.devWebpackPort || 3000,
|
|
171
177
|
devServerOptions = env.devServerOptions || {},
|
|
172
|
-
baseUrl = env.baseUrl || '/api/',
|
|
173
178
|
extraIncludePaths = (env.extraIncludePaths || env.babelIncludePaths || []).map(
|
|
174
179
|
safeRealpath
|
|
175
180
|
),
|
|
@@ -239,11 +244,7 @@ async function configureRsbuild(env) {
|
|
|
239
244
|
// Resolve symlinks (a no-op for flat layouts) so the path matches the real module paths
|
|
240
245
|
// Rspack produces via its default resolve.symlinks behavior - required for the transpile
|
|
241
246
|
// include below to match under symlinking package managers (e.g. pnpm).
|
|
242
|
-
const hoistPath =
|
|
243
|
-
inlineHoist
|
|
244
|
-
? path.resolve(basePath, '../../hoist-react')
|
|
245
|
-
: path.resolve(basePath, 'node_modules/@xh/hoist')
|
|
246
|
-
);
|
|
247
|
+
const hoistPath = resolveHoistPath(basePath, inlineHoist);
|
|
247
248
|
|
|
248
249
|
// Check for and resolve standard/expected favicons.
|
|
249
250
|
const {manifestIcons, appleTouchIconExists} = resolveIcons(
|
|
@@ -263,17 +264,11 @@ async function configureRsbuild(env) {
|
|
|
263
264
|
if (inlineHoist) {
|
|
264
265
|
resolveAliases['@xh/hoist'] = hoistPath;
|
|
265
266
|
|
|
266
|
-
//
|
|
267
|
-
//
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
resolveAliases['react-dom'] = path.resolve('./node_modules/react-dom');
|
|
272
|
-
|
|
273
|
-
// Also ensure a single instance of ag-Grid when Hoist is inline, needed to support use of
|
|
274
|
-
// `useGridMenuItem` hook.
|
|
275
|
-
resolveAliases['ag-grid-react'] = path.resolve('./node_modules/ag-grid-react');
|
|
276
|
-
resolveAliases['ag-grid-community'] = path.resolve('./node_modules/ag-grid-community');
|
|
267
|
+
// Use the app's instance of each library hoist-react must share with it - React for hooks,
|
|
268
|
+
// ag-Grid for `useGridMenuItem`. See INLINE_HOIST_SINGLETONS.
|
|
269
|
+
INLINE_HOIST_SINGLETONS.forEach(name => {
|
|
270
|
+
resolveAliases[name] = path.resolve('./node_modules', name);
|
|
271
|
+
});
|
|
277
272
|
}
|
|
278
273
|
|
|
279
274
|
// When running inline, resolve inline Hoist's own node_modules so we can exclude them.
|
|
@@ -298,11 +293,28 @@ async function configureRsbuild(env) {
|
|
|
298
293
|
// TS-only support - fail fast on any .jsx source.
|
|
299
294
|
checkNoJsxFiles([srcPath, ...extraIncludePaths]);
|
|
300
295
|
|
|
301
|
-
// Resolve app entry points - one for each
|
|
296
|
+
// Resolve app entry points - one for each script within src/apps/, minus unit tests - to create
|
|
297
|
+
// bundles below.
|
|
302
298
|
const clientApps = discoverClientApps(srcPath),
|
|
303
299
|
clientAppNames = clientApps.map(it => it.name),
|
|
304
300
|
appEntryPoints = Object.fromEntries(clientApps.map(it => [it.name, it.path]));
|
|
305
301
|
|
|
302
|
+
// Dev-server redirect target for the bare root, which has no entry of its own and would
|
|
303
|
+
// otherwise 404 - the open page when set, else the `app` entry, else a sole entry. Skipped
|
|
304
|
+
// for prod builds, and `URL.parse` returns null rather than throwing on a malformed value.
|
|
305
|
+
const openPagePath =
|
|
306
|
+
!prodBuild && env.devServerOpenPage
|
|
307
|
+
? URL.parse(env.devServerOpenPage, 'http://localhost/')?.pathname
|
|
308
|
+
: null,
|
|
309
|
+
devRootRedirect =
|
|
310
|
+
openPagePath && openPagePath !== '/'
|
|
311
|
+
? openPagePath
|
|
312
|
+
: clientAppNames.includes('app')
|
|
313
|
+
? '/app/'
|
|
314
|
+
: clientAppNames.length === 1
|
|
315
|
+
? `/${clientAppNames[0]}/`
|
|
316
|
+
: null;
|
|
317
|
+
|
|
306
318
|
// Hoist-react's polyfills.js (a single `core-js/stable` import) is prepended to every app
|
|
307
319
|
// entry. With SWC's preset-env in `entry` mode (see `tools.swc` below), that import is
|
|
308
320
|
// rewritten into the specific polyfills needed for the configured target browsers.
|
|
@@ -340,7 +352,7 @@ async function configureRsbuild(env) {
|
|
|
340
352
|
// `export const AppComponent = hoistCmp({...})`) - Hoist's camelCase element-factory
|
|
341
353
|
// exports and model classes fall back to a (sub-second) full reload of the page.
|
|
342
354
|
pluginReact({
|
|
343
|
-
swcReactOptions: {runtime:
|
|
355
|
+
swcReactOptions: {runtime: HOIST_SWC.reactRuntime},
|
|
344
356
|
// Rsbuild's React-specific vendor chunking is tied to its own split presets.
|
|
345
357
|
splitChunks: false,
|
|
346
358
|
reactRefreshOptions: {
|
|
@@ -378,7 +390,7 @@ async function configureRsbuild(env) {
|
|
|
378
390
|
// `@lookup` returning a field initializer. Also Rsbuild's own default, set explicitly
|
|
379
391
|
// here because it is load-bearing: pointed at `legacy`, every `@observable` and
|
|
380
392
|
// `@bindable` in a v88 app silently stops working.
|
|
381
|
-
decorators: {version:
|
|
393
|
+
decorators: {version: HOIST_SWC.decoratorVersion},
|
|
382
394
|
|
|
383
395
|
// Avoid importing every FA icon ever made - rewrite named imports from the FontAwesome
|
|
384
396
|
// icon packs to per-icon deep imports. See https://github.com/FortAwesome/react-fontawesome/issues/70
|
|
@@ -403,14 +415,16 @@ async function configureRsbuild(env) {
|
|
|
403
415
|
// have no `process` global). Most-specific keys win, so NODE_ENV above still
|
|
404
416
|
// resolves to its real value.
|
|
405
417
|
'process.env': '{}',
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
418
|
+
...xhDefines({
|
|
419
|
+
appCode,
|
|
420
|
+
appName,
|
|
421
|
+
appVersion,
|
|
422
|
+
appBuild,
|
|
423
|
+
baseUrl,
|
|
424
|
+
buildTimestamp: buildDate.getTime(),
|
|
425
|
+
clientApps: clientAppNames,
|
|
426
|
+
isDevelopmentMode: !prodBuild
|
|
427
|
+
})
|
|
414
428
|
}
|
|
415
429
|
},
|
|
416
430
|
|
|
@@ -556,6 +570,9 @@ async function configureRsbuild(env) {
|
|
|
556
570
|
: {
|
|
557
571
|
host: devHost,
|
|
558
572
|
port: devWebpackPort,
|
|
573
|
+
// Fail on a busy port, as webpack-dev-server did. Rsbuild otherwise moves to
|
|
574
|
+
// the next free port, breaking OAuth redirect URIs and fixed-port scripts.
|
|
575
|
+
strictPort: true,
|
|
559
576
|
https: _.isPlainObject(devHttps) ? devHttps : undefined,
|
|
560
577
|
open: env.devServerOpenPage ? [env.devServerOpenPage] : false,
|
|
561
578
|
// Support HTML5 history routes for apps, with /appName/ as the base route for each
|
|
@@ -565,6 +582,18 @@ async function configureRsbuild(env) {
|
|
|
565
582
|
to: `/${appName}/index.html`
|
|
566
583
|
}))
|
|
567
584
|
},
|
|
585
|
+
// Redirect the bare root to a default app, as the `location = /` block in
|
|
586
|
+
// Hoist's nginx configs does. Runs ahead of Rsbuild's own middleware.
|
|
587
|
+
setup: devRootRedirect
|
|
588
|
+
? ({server}) => {
|
|
589
|
+
server.middlewares.use((req, res, next) => {
|
|
590
|
+
if (req.url !== '/' && !req.url.startsWith('/?')) return next();
|
|
591
|
+
res.statusCode = 302;
|
|
592
|
+
res.setHeader('Location', devRootRedirect);
|
|
593
|
+
res.end();
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
: undefined,
|
|
568
597
|
// Proxy API requests to the Grails backend, mirroring the production nginx setup.
|
|
569
598
|
// Only needed when baseUrl is a relative path (default '/api/') - if baseUrl is
|
|
570
599
|
// an absolute URL, the app will call the remote server directly. Note the
|
|
@@ -597,7 +626,7 @@ async function configureRsbuild(env) {
|
|
|
597
626
|
// (`useDefineForClassFields: true`) and what TC39 decorators assume - `accessor`
|
|
598
627
|
// fields desugar to a getter/setter pair over private storage, and plain fields
|
|
599
628
|
// must still define rather than assign so they do not trip inherited setters.
|
|
600
|
-
transform.useDefineForClassFields =
|
|
629
|
+
transform.useDefineForClassFields = HOIST_SWC.useDefineForClassFields;
|
|
601
630
|
|
|
602
631
|
// Rewrite the `core-js/stable` import in hoist-react's polyfills.js (prepended to
|
|
603
632
|
// every app entry above) into the polyfills needed for the target browsers.
|
|
@@ -0,0 +1,58 @@
|
|
|
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
|
+
|
|
8
|
+
// Hand-written declarations for configureVitest.js. Keep in step with its JSDoc and with
|
|
9
|
+
// TEST_OPTIONS there - test/declarations.test.js checks the keys. No vite or vitest types, as
|
|
10
|
+
// `vitest/config` does not resolve from this package under pnpm.
|
|
11
|
+
|
|
12
|
+
import configureRsbuild = require('./configureRsbuild');
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Vitest preset for Hoist unit tests. Compiles app and hoist-react code with the same SWC settings
|
|
16
|
+
* as `configureRsbuild()`, sets the same `XH` constants, and loads hoist-react's test setup.
|
|
17
|
+
* Requires hoist-react >= 89. Apps consume it from a `vitest.config.mts`:
|
|
18
|
+
*
|
|
19
|
+
* import configureVitest from '@xh/hoist-dev-utils/configureVitest';
|
|
20
|
+
* import {defineConfig} from 'vitest/config';
|
|
21
|
+
* export default defineConfig(configureVitest({appCode: 'myApp'}));
|
|
22
|
+
*
|
|
23
|
+
* Wrap the result with `mergeConfig()` to add settings. `mergeConfig()` concatenates arrays, so
|
|
24
|
+
* pass `include` and `setupFiles` here.
|
|
25
|
+
*
|
|
26
|
+
* @returns a Vite + Vitest config object, typed loosely. Vitest's `defineConfig()` checks it.
|
|
27
|
+
*/
|
|
28
|
+
declare function configureVitest(env: configureVitest.VitestEnv): Record<string, any>;
|
|
29
|
+
|
|
30
|
+
declare namespace configureVitest {
|
|
31
|
+
/** Also exported by name, for `import {configureVitest} from ...`. */
|
|
32
|
+
const configureVitest: (env: VitestEnv) => Record<string, any>;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Options for `configureVitest()`. Build options beyond these are ignored, so an app can share
|
|
36
|
+
* one `env` object between its two configs: `configureVitest({...env, setupFiles: [...]})`.
|
|
37
|
+
*/
|
|
38
|
+
interface VitestEnv extends configureRsbuild.SharedEnv {
|
|
39
|
+
/** Project root. Defaults to the current directory. */
|
|
40
|
+
root?: string;
|
|
41
|
+
/** Spec globs. Default `['src/**\/*.spec.{ts,tsx}']`. */
|
|
42
|
+
include?: string[];
|
|
43
|
+
/** App setup files, run after hoist-react's `test-support/setup.ts`. */
|
|
44
|
+
setupFiles?: string | string[];
|
|
45
|
+
/**
|
|
46
|
+
* Time zone for the run, set as `TZ`. Default 'America/New_York'. Null keeps the
|
|
47
|
+
* machine's zone.
|
|
48
|
+
*/
|
|
49
|
+
timeZone?: string | null;
|
|
50
|
+
/**
|
|
51
|
+
* True only for hoist-react's own config. Aliases `@xh/hoist` to the root and loads its
|
|
52
|
+
* `test-support/setup.ts`.
|
|
53
|
+
*/
|
|
54
|
+
selfHost?: boolean;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export = configureVitest;
|