@servicetitan/docs-uikit 36.3.1 → 37.0.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/docs/BREAKING_CHANGES.mdx +9 -1
- package/docs/ai-marketplace.mdx +8 -4
- package/docs/datadog-rum.mdx +346 -43
- package/docs/eslint-config.mdx +10 -10
- package/docs/install.mdx +11 -7
- package/docs/lazy-module.mdx +1 -1
- package/docs/microfrontends/hosting.mdx +7 -5
- package/docs/microfrontends/sharing-dependencies.mdx +15 -17
- package/docs/startup/build/build.mdx +280 -0
- package/docs/startup/build/optional-peer-dependencies.mdx +131 -0
- package/docs/startup/build/vite.mdx +291 -0
- package/docs/startup/build/webpack.mdx +127 -0
- package/docs/startup/clean.mdx +1 -1
- package/docs/startup/lint.mdx +1 -1
- package/docs/startup/mfe-list.mdx +2 -2
- package/docs/startup/review.mdx +36 -11
- package/docs/startup/start/start.mdx +48 -0
- package/docs/startup/start/vite.mdx +109 -0
- package/docs/startup/start/webpack.mdx +49 -0
- package/docs/startup/startup.mdx +114 -10
- package/docs/startup/test/jest.mdx +4 -4
- package/docs/startup/test/test.mdx +1 -1
- package/docs/startup/test/vitest.mdx +6 -6
- package/package.json +2 -2
- package/docs/startup/build.mdx +0 -310
- package/docs/startup/start.mdx +0 -73
|
@@ -27,7 +27,11 @@ By default, MFEs and hosts share the following dependencies (see [here](https://
|
|
|
27
27
|
MFE's are not required to use all the default dependencies; `startup` and `Loader` ignore any that aren't used.
|
|
28
28
|
:::
|
|
29
29
|
|
|
30
|
-
To customize which dependencies are shared, set `cli.
|
|
30
|
+
To customize which dependencies are shared, set `cli.shared-dependencies` to an object that maps dependencies to `SharedDependencies.{name}`. By convention `{name}` is the camel-cased package name.
|
|
31
|
+
|
|
32
|
+
:::note
|
|
33
|
+
In versions before **36.4.0**, this setting was under `cli.webpack.shared-dependencies`. The old location still works for backward compatibility.
|
|
34
|
+
:::
|
|
31
35
|
|
|
32
36
|
- Use the special **defaults** key with the empty string to incorporate the default list.
|
|
33
37
|
- To exclude a default dependency, map it to the empty string.
|
|
@@ -39,11 +43,9 @@ To add a dependency, use the special **defaults** key to first include the defau
|
|
|
39
43
|
```json title="package.json"
|
|
40
44
|
{
|
|
41
45
|
"cli": {
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
|
|
45
|
-
"lodash": "SharedDependencies.Lodash" // add lodash
|
|
46
|
-
}
|
|
46
|
+
"shared-dependencies": {
|
|
47
|
+
"defaults": "", // add default list
|
|
48
|
+
"lodash": "SharedDependencies.Lodash" // add lodash
|
|
47
49
|
}
|
|
48
50
|
}
|
|
49
51
|
}
|
|
@@ -65,11 +67,9 @@ To omit an default dependency, map it to the empty string. For example, to omit
|
|
|
65
67
|
```json title="package.json"
|
|
66
68
|
{
|
|
67
69
|
"cli": {
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
|
|
71
|
-
"@servicetitan/design-system": "" // drop @servicetitan/design-system
|
|
72
|
-
}
|
|
70
|
+
"shared-dependencies": {
|
|
71
|
+
"defaults": "", // add default list
|
|
72
|
+
"@servicetitan/design-system": "" // drop @servicetitan/design-system
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
}
|
|
@@ -82,12 +82,10 @@ To completely customize the list of shared dependencies, omit the **defaults** k
|
|
|
82
82
|
```json title="package.json"
|
|
83
83
|
{
|
|
84
84
|
"cli": {
|
|
85
|
-
"
|
|
86
|
-
"
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
"react-dom": "SharedDependencies.ReactDOM"
|
|
90
|
-
}
|
|
85
|
+
"shared-dependencies": {
|
|
86
|
+
"@servicetitan/anvil2": "SharedDependencies.ServiceTitan.Anvil2",
|
|
87
|
+
"react": "SharedDependencies.React",
|
|
88
|
+
"react-dom": "SharedDependencies.ReactDOM"
|
|
91
89
|
}
|
|
92
90
|
}
|
|
93
91
|
}
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: build
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
import Tabs from '@theme/Tabs';
|
|
6
|
+
import TabItem from '@theme/TabItem';
|
|
7
|
+
|
|
8
|
+
Build packages for production to the `dist/bundle` folders. It bundles them in production mode and optimizes the build for the best performance. The builds are minified and the filenames include the hashes. Apps are ready to be deployed.
|
|
9
|
+
|
|
10
|
+
`startup` supports two bundlers: [Webpack](./webpack) (default) and [Vite](./vite) (opt-in via `--bundler=vite`).
|
|
11
|
+
|
|
12
|
+
## Options
|
|
13
|
+
|
|
14
|
+
| Option | Webpack | Vite | Description |
|
|
15
|
+
| :----------------- | :------ | :--- | :------------------------------------------------------------------------ |
|
|
16
|
+
| `--scope <glob>` | Yes | Yes | Include only packages with names matching the given glob. |
|
|
17
|
+
| `--ignore <glob>` | Yes | Yes | Exclude packages with names matching the given glob. |
|
|
18
|
+
| `--cdn-path <url>` | Yes | Yes | Specify the base path for all the assets within the application. |
|
|
19
|
+
| `--stat` | Yes | Yes | Generate bundle analysis report. |
|
|
20
|
+
| `--code-coverage` | Yes | Yes | Add instrumentation to bundled code in order to collect code coverage. |
|
|
21
|
+
| `--use-tsc` | Yes | Yes | Use TSC for TypeScript compilation. |
|
|
22
|
+
| `--bundler <name>` | Yes | Yes | Select the bundler: `vite` or `webpack` (default). |
|
|
23
|
+
| `--preview` | No | Yes | Preview production build locally via a static server. |
|
|
24
|
+
| `--config <path>` | Yes | No | Custom config file path. Vite uses convention-based config files instead. |
|
|
25
|
+
|
|
26
|
+
## Build Steps
|
|
27
|
+
|
|
28
|
+
The `build` command executes these steps:
|
|
29
|
+
|
|
30
|
+
1. Activate Kendo UI license.
|
|
31
|
+
1. Run package **precompile** scripts, if any.
|
|
32
|
+
1. Compile Typescript.
|
|
33
|
+
1. Generate TypeScript definitions from CSS, LESS, and SASS modules, and copy assets and style files to the output folder.
|
|
34
|
+
1. Run type check via [TypeScript project references](https://www.typescriptlang.org/docs/handbook/project-references.html).
|
|
35
|
+
1. Bundle application and MFE packages.
|
|
36
|
+
|
|
37
|
+
### Precompile Script
|
|
38
|
+
|
|
39
|
+
✨ New in **v34.0.0** ✨
|
|
40
|
+
|
|
41
|
+
The `build` command runs the `precompile` script defined in each package's `package.json` before compiling TypeScript. This allows packages to execute custom setup tasks during the build process. If a package doesn't define a `precompile` script, this step is skipped.
|
|
42
|
+
|
|
43
|
+
## Package Setup
|
|
44
|
+
|
|
45
|
+
Because `startup` compiles and builds in separate steps, packages must export compiled Javascript instead of the TypeScript source files. E.g.,
|
|
46
|
+
|
|
47
|
+
```json title="package.json"
|
|
48
|
+
{
|
|
49
|
+
"exports": "./dist/index.js",
|
|
50
|
+
"main": "./dist/index.js",
|
|
51
|
+
"typings": "./dist/index.d.ts"
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
And, to ensure packages are compiled in the correct order, [TypeScript project references](https://www.typescriptlang.org/docs/handbook/project-references.html) must accurately describe the dependencies between packages. E.g.,
|
|
56
|
+
|
|
57
|
+
```json title="tsconfig.json"
|
|
58
|
+
{
|
|
59
|
+
"references": [
|
|
60
|
+
{ "path": "../component-a" },
|
|
61
|
+
{ "path": "../component-b" },
|
|
62
|
+
{ "path": "../component-c" }
|
|
63
|
+
]
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## CSS Modules
|
|
68
|
+
|
|
69
|
+
`Startup` supports [CSS Modules](https://github.com/css-modules/css-modules) alongside regular stylesheets using the `[name].module.{css,less,scss}` file naming convention. CSS Modules provide locally scoped class names that prevent global CSS collisions, making styles more predictable and easier to refactor.
|
|
70
|
+
|
|
71
|
+
## SVG Transformation
|
|
72
|
+
|
|
73
|
+
By default, SVGs are loaded as assets. Depending on the file size, the asset is converted to a base64 URI string, or emitted as separate file. E.g.,
|
|
74
|
+
|
|
75
|
+
```tsx
|
|
76
|
+
import MySVG from './my.svg';
|
|
77
|
+
|
|
78
|
+
function MyImage() {
|
|
79
|
+
return <img src={MySVG} />;
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Importing SVGs as React Components
|
|
84
|
+
|
|
85
|
+
By default `startup` imports SVGs from `@servicetitan/anvil2` as React components. E.g.,
|
|
86
|
+
|
|
87
|
+
```tsx
|
|
88
|
+
import { Button } from '@servicetitan/anvil2';
|
|
89
|
+
import CheckIcon from '@servicetitan/anvil2/assets/icons/material/round/check.svg';
|
|
90
|
+
|
|
91
|
+
export function CheckButton() {
|
|
92
|
+
return <Button icon={CheckIcon} />;
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
To import SVGs from other sources as React components, append `?component` to the import statement. E.g.,
|
|
97
|
+
|
|
98
|
+
```tsx
|
|
99
|
+
import { Button } from '@servicetitan/anvil2';
|
|
100
|
+
import CheckIcon from './assets/check.svg?component';
|
|
101
|
+
|
|
102
|
+
export function CheckButton() {
|
|
103
|
+
return <Button icon={CheckIcon} />;
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Importing Anvil 2 SVGs as Assets
|
|
108
|
+
|
|
109
|
+
By default `startup` imports SVGs from `@servicetitan/anvil2` as React components.
|
|
110
|
+
To import an Anvil 2 SVG as an asset, append `?asset` to the import statement. E.g.,
|
|
111
|
+
|
|
112
|
+
```tsx
|
|
113
|
+
import CheckIcon from '@servicetitan/anvil2/assets/icons/material/round/check.svg?asset';
|
|
114
|
+
|
|
115
|
+
function CheckImage() {
|
|
116
|
+
return <img src={CheckIcon} />;
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Configuration
|
|
121
|
+
|
|
122
|
+
### Library Packages
|
|
123
|
+
|
|
124
|
+
Set `cli.webpack` or `cli.vite` to `false` to disable bundling for non-application packages, such as component and utility libraries.
|
|
125
|
+
|
|
126
|
+
```json title="package.json"
|
|
127
|
+
{
|
|
128
|
+
"cli": {
|
|
129
|
+
"webpack": false
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
:::caution
|
|
135
|
+
`cli.webpack` and `cli.vite` must agree. Setting one to `false` while the other is configured is contradictory and fails the build. To disable bundling, set `cli.webpack` or `cli.vite` to `false` and leave the other unset, or set both to `false`.
|
|
136
|
+
:::
|
|
137
|
+
|
|
138
|
+
### Type Checking
|
|
139
|
+
|
|
140
|
+
Use package-specific `tsconfig.json` and `tsconfig.build.json` files to set or override type checking options. E.g.,
|
|
141
|
+
|
|
142
|
+
```json title="tsconfig.json"
|
|
143
|
+
{
|
|
144
|
+
"extends": "@servicetitan/startup/tsconfig/base",
|
|
145
|
+
"compilerOptions": {
|
|
146
|
+
"outDir": "dist",
|
|
147
|
+
"rootDir": "src"
|
|
148
|
+
// Other type checking options (https://www.typescriptlang.org/tsconfig/)
|
|
149
|
+
},
|
|
150
|
+
"include": ["src/**/*"]
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Use `tsconfig.build.json` that extends from `tsconfig.json` to exclude tests, stories, and mocks from type checking. E.g.,
|
|
155
|
+
|
|
156
|
+
```json title="tsconfig.build.json"
|
|
157
|
+
{
|
|
158
|
+
"extends": "./tsconfig.json",
|
|
159
|
+
"exclude": ["**/__tests__/*", "**/*.test.*", "**/__mocks__/*", "**/*.stories.*"],
|
|
160
|
+
"references": [
|
|
161
|
+
{ "path": "../feature-a" },
|
|
162
|
+
{ "path": "../feature-b" },
|
|
163
|
+
{ "path": "../feature-c" }
|
|
164
|
+
]
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
:::caution
|
|
169
|
+
Project references must be duplicated in `tsconfig.build.json` because [references are not inherited](https://github.com/microsoft/TypeScript/issues/27098).
|
|
170
|
+
:::
|
|
171
|
+
|
|
172
|
+
#### Root-Level Config Files {#root-level-config-files}
|
|
173
|
+
|
|
174
|
+
By default, TypeScript checks only the files inside the package `rootDir`, so config files that sit outside it, like `vite.*.config.ts`, get neither type checking nor type-aware linting. Helpers like `defineConfig` give you IDE autocomplete through the language service, but otherwise nothing checks these files.
|
|
175
|
+
|
|
176
|
+
To cover them, add a second compilation unit that doesn't impose `rootDir`. The split looks slightly different depending on the project shape:
|
|
177
|
+
|
|
178
|
+
<Tabs
|
|
179
|
+
defaultValue="monorepo"
|
|
180
|
+
values={[
|
|
181
|
+
{ label: 'Monorepo', value: 'monorepo' },
|
|
182
|
+
{ label: 'Single Package', value: 'single' },
|
|
183
|
+
]}
|
|
184
|
+
>
|
|
185
|
+
<TabItem value="monorepo">
|
|
186
|
+
|
|
187
|
+
The split lives one level up: a root `tsconfig.json` whose `include` covers root-level configs sits above the per-package tsconfigs scoped to `src/`. For example,
|
|
188
|
+
|
|
189
|
+
```json title="tsconfig.json"
|
|
190
|
+
{
|
|
191
|
+
"extends": "@servicetitan/startup/tsconfig/base",
|
|
192
|
+
"include": ["*.ts", "**/vite.*.config.ts"]
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
</TabItem>
|
|
197
|
+
<TabItem value="single">
|
|
198
|
+
|
|
199
|
+
Create a sibling `tsconfig.node.json` and reference it from the main `tsconfig.json`:
|
|
200
|
+
|
|
201
|
+
```json title="tsconfig.json"
|
|
202
|
+
{
|
|
203
|
+
"extends": "@servicetitan/startup/tsconfig/base",
|
|
204
|
+
"compilerOptions": {
|
|
205
|
+
"outDir": "dist",
|
|
206
|
+
"rootDir": "src"
|
|
207
|
+
},
|
|
208
|
+
"include": ["src/**/*"],
|
|
209
|
+
"references": [{ "path": "./tsconfig.node.json" }]
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
```json title="tsconfig.node.json"
|
|
214
|
+
{
|
|
215
|
+
"extends": "@servicetitan/startup/tsconfig/base",
|
|
216
|
+
"compilerOptions": { "composite": true },
|
|
217
|
+
"include": ["*.ts"]
|
|
218
|
+
}
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
The `*.ts` glob is non-recursive and picks up only root-level config files. The `tsconfig.node.json` filename follows the Vite convention. The include itself is general and not specific to Vite.
|
|
222
|
+
|
|
223
|
+
</TabItem>
|
|
224
|
+
</Tabs>
|
|
225
|
+
|
|
226
|
+
### TypeScript Compilation (SWC)
|
|
227
|
+
|
|
228
|
+
:::info
|
|
229
|
+
`Startup` only maps a subset of TypeScript settings from `tsconfig.json` to SWC options. See [cliOptions](https://github.com/search?q=repo%3Aservicetitan%2Fuikit+%22cliOptions%3A%22+-path%3A**%2F*.test.ts&type=code) and [swcOptions](https://github.com/search?q=repo%3Aservicetitan%2Fuikit+%22swcOptions%3A%22+-path%3A**%2F*.test.ts&type=code) for the details.
|
|
230
|
+
:::
|
|
231
|
+
|
|
232
|
+
Use `cli.swc-compile-package` to set or override SWC options. E.g.,
|
|
233
|
+
|
|
234
|
+
```json title="package.json"
|
|
235
|
+
{
|
|
236
|
+
"cli": {
|
|
237
|
+
"swc-compile-package": {
|
|
238
|
+
"cliOptions": {
|
|
239
|
+
// CLI options (https://swc.rs/docs/usage/cli)
|
|
240
|
+
},
|
|
241
|
+
"swcOptions": {
|
|
242
|
+
// Compilation options (https://swc.rs/docs/configuration/compilation)
|
|
243
|
+
// Module options (https://swc.rs/docs/configuration/modules)
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
### TypeScript Compilation (TSC)
|
|
251
|
+
|
|
252
|
+
:::caution
|
|
253
|
+
Compilation via TSC is deprecated.
|
|
254
|
+
:::
|
|
255
|
+
|
|
256
|
+
Use package-specific `tsconfig.json` and `tsconfig.build.json` files to set or override compiler options. See [Type checking](#type-checking) for more information.
|
|
257
|
+
|
|
258
|
+
### Exposing Environment Variables
|
|
259
|
+
|
|
260
|
+
✨ New in **v37.0.0** ✨
|
|
261
|
+
|
|
262
|
+
To forward a `process.env` variable (for example an API URL) into client code that reads `process.env.NAME`, list its name under `cli.expose-process-env`. At build time `startup` replaces the `process.env.NAME` references with the variable's value. A variable unset at build time resolves to `undefined`.
|
|
263
|
+
|
|
264
|
+
```json title="package.json"
|
|
265
|
+
{
|
|
266
|
+
"cli": {
|
|
267
|
+
"expose-process-env": ["API_URL", "CLIENT_CDN_PATH", "FEATURE_FLAG"]
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
Names are matched case-sensitively, so use the conventional `ALL_CAPS` form.
|
|
273
|
+
|
|
274
|
+
:::note
|
|
275
|
+
This is a convenient, package.json-only way to forward variables straight from the environment. It does not replace webpack's `DefinePlugin` or Vite's `define`, which map a name to a value you specify (not necessarily the live environment value). Use those when you need to control the value and use `expose-process-env` when you just want to pass an environment value through as-is.
|
|
276
|
+
:::
|
|
277
|
+
|
|
278
|
+
### Bundler-Specific Configuration
|
|
279
|
+
|
|
280
|
+
See [Webpack](./webpack) or [Vite](./vite) for bundler-specific configuration options, including minimization, custom config files, and plugin overrides.
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Optional peer dependencies (internal)
|
|
3
|
+
draft: true
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
:::note
|
|
7
|
+
**Internal mechanism, not a supported public API.** This page is a Docusaurus `draft`: it is excluded from the published site and visible only in local `docusaurus start`. It documents, for maintainers, how `startup` lets uikit's own packages (for example `@servicetitan/web-components`) keep optional peers such as `@servicetitan/design-system` tree-shakeable. Outside consumers should not rely on `cli.peerDependenciesMeta`.
|
|
8
|
+
:::
|
|
9
|
+
|
|
10
|
+
A library marks a peer as optional via npm's [`peerDependenciesMeta`](https://docs.npmjs.com/cli/v11/configuring-npm/package-json#peerdependenciesmeta). Consumers may or may not install it, but the library's source still references it, so the bundler has to decide what to do when the package is missing.
|
|
11
|
+
|
|
12
|
+
`startup` resolves missing optional peers to a stub module. The library picks the stub mode via `cli.peerDependenciesMeta[<pkg>].fallback`:
|
|
13
|
+
|
|
14
|
+
| Mode | Summary |
|
|
15
|
+
| :------ | :-------------------------------------------------------------------------- |
|
|
16
|
+
| `throw` | Stub throws when the missing peer is referenced. This is the default value. |
|
|
17
|
+
| `empty` | Stub resolves to a module with no exports. |
|
|
18
|
+
|
|
19
|
+
`throw` is the default and needs no extra configuration beyond marking the peer optional:
|
|
20
|
+
|
|
21
|
+
```json title="package.json"
|
|
22
|
+
{
|
|
23
|
+
"peerDependenciesMeta": {
|
|
24
|
+
"react-router-dom": { "optional": true }
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
To switch a specific peer to `empty`, add `cli.peerDependenciesMeta[<pkg>].fallback`. `cli.peerDependenciesMeta` only carries the `fallback` value; npm's `peerDependenciesMeta[<pkg>].optional: true` is still required:
|
|
30
|
+
|
|
31
|
+
```json title="package.json"
|
|
32
|
+
{
|
|
33
|
+
"peerDependenciesMeta": {
|
|
34
|
+
"react-router-dom": { "optional": true }
|
|
35
|
+
},
|
|
36
|
+
"cli": {
|
|
37
|
+
"peerDependenciesMeta": {
|
|
38
|
+
"react-router-dom": { "fallback": "empty" }
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
The setting applies to both webpack and Vite builds.
|
|
45
|
+
|
|
46
|
+
## `throw` (default)
|
|
47
|
+
|
|
48
|
+
Stub throws `Cannot find module '<pkg>'` when evaluated.
|
|
49
|
+
|
|
50
|
+
Top-level static imports of the missing peer crash the importing module:
|
|
51
|
+
|
|
52
|
+
```ts title="src/optional-feature.ts"
|
|
53
|
+
/*
|
|
54
|
+
* If 'optional' is not installed, importing this file throws
|
|
55
|
+
* "Cannot find module 'optional'". Any module that imports
|
|
56
|
+
* './optional-feature' will fail to evaluate too.
|
|
57
|
+
*/
|
|
58
|
+
import { something } from 'optional';
|
|
59
|
+
|
|
60
|
+
export function doSomething() {
|
|
61
|
+
return something();
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
`try { require(...) } catch {}` lets the library detect a missing peer without crashing:
|
|
66
|
+
|
|
67
|
+
```ts title="src/optional-feature.ts"
|
|
68
|
+
let optional;
|
|
69
|
+
try {
|
|
70
|
+
optional = require('optional');
|
|
71
|
+
} catch {
|
|
72
|
+
// peer not installed
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function doSomething() {
|
|
76
|
+
return optional ? optional.something() : doFallback();
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
:::caution
|
|
81
|
+
The `try { require(...) } catch {}` pattern works under webpack but not under Vite, since Vite/Rolldown does not support bare `require` in ESM source. It also defeats tree-shaking: `require()` pulls the whole package into the bundle. Prefer the [`empty`](#empty) fallback with `import * as X`, which tree-shakes to the used exports.
|
|
82
|
+
:::
|
|
83
|
+
|
|
84
|
+
## `empty`
|
|
85
|
+
|
|
86
|
+
Stub has no exports. All members of the imported namespace are `undefined`.
|
|
87
|
+
|
|
88
|
+
`import * as X` lets the library detect a missing peer by probing for an expected member, and tree-shakes to only the exports it uses:
|
|
89
|
+
|
|
90
|
+
```ts title="src/optional-feature.ts"
|
|
91
|
+
import * as optional from 'optional';
|
|
92
|
+
|
|
93
|
+
export function doSomething() {
|
|
94
|
+
if (optional.something === undefined) {
|
|
95
|
+
// peer not installed
|
|
96
|
+
return doFallback();
|
|
97
|
+
}
|
|
98
|
+
return optional.something();
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Why not dynamic `import()`?
|
|
103
|
+
|
|
104
|
+
A dynamic `import()` can be wrapped in `try/catch`, so it looks like a way to tolerate a missing peer. Two reasons it is a poor fit:
|
|
105
|
+
|
|
106
|
+
- **It is async.** `import()` returns a promise, so the value is not available synchronously. Consuming it from synchronous code (for example a React component that renders a provider inline) forces the call site into `await`/`Suspense`, which a static `import * as X` avoids.
|
|
107
|
+
|
|
108
|
+
- **Its tree-shaking is fragile.** It survives only when a single member is read directly on the import result. Destructuring several members at once, or storing the namespace, pulls the whole package in:
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
// tree-shakes: one member, read directly on the result
|
|
112
|
+
let something;
|
|
113
|
+
try {
|
|
114
|
+
something = (await import('optional')).something;
|
|
115
|
+
} catch {
|
|
116
|
+
// peer not installed
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// does NOT tree-shake: whole namespace stored, then used later
|
|
120
|
+
let optional;
|
|
121
|
+
try {
|
|
122
|
+
optional = await import('optional');
|
|
123
|
+
} catch {}
|
|
124
|
+
optional?.something();
|
|
125
|
+
|
|
126
|
+
// does NOT tree-shake: destructuring several members at once
|
|
127
|
+
let a, b;
|
|
128
|
+
try {
|
|
129
|
+
({ a, b } = await import('optional'));
|
|
130
|
+
} catch {}
|
|
131
|
+
```
|