Skip to content

Commit a3953ec

Browse files
hi-ogawaclaude
andcommitted
docs(rsc): document viteRsc.import API
Add documentation for the new import.meta.viteRsc.import() API in both README.md and types/index.d.ts with JSDoc, examples, and type signatures. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 29f4440 commit a3953ec

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

packages/plugin-rsc/README.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,18 +245,33 @@ ssrModule.renderHTML(...);
245245
export function renderHTML(...) {}
246246
```
247247

248-
#### `import.meta.viteRsc.import(..., { environment: ... })`
248+
#### `import.meta.viteRsc.import`
249249

250-
TODO
250+
- Type: `<T>(specifier: string, options: { environment: string }) => Promise<T>`
251+
252+
A more ergonomic alternative to `loadModule`:
253+
254+
1. No manual `rollupOptions.input` config needed - entries are auto-discovered
255+
2. Specifier matches the path in `typeof import(...)` type annotations
256+
257+
**Comparison:**
251258

252259
```ts
253-
const ssrModule = await import.meta.viteRsc.import(
254-
"./entry.ssr.tsx",
255-
{ with: { environment: "ssr" } }
260+
// Before (loadModule) - requires vite.config.ts:
261+
// environments.ssr.build.rollupOptions.input = { index: './entry.ssr.tsx' }
262+
import.meta.viteRsc.loadModule<typeof import('./entry.ssr.tsx')>('ssr', 'index')
263+
264+
// After (import) - no config needed, auto-discovered
265+
import.meta.viteRsc.import<typeof import('./entry.ssr.tsx')>(
266+
'./entry.ssr.tsx',
267+
{ environment: 'ssr' },
256268
)
257-
ssrModule.renderHTML(...);
258269
```
259270

271+
During development, this works the same as `loadModule`, using the `__VITE_ENVIRONMENT_RUNNER_IMPORT__` function to import modules in the target environment.
272+
273+
During production build, the plugin auto-discovers these imports and emits them as entries in the target environment. A manifest file (`__vite_rsc_env_imports_manifest.js`) is generated to map module specifiers to their output filenames.
274+
260275
### Available on `rsc` environment
261276

262277
#### `import.meta.viteRsc.loadCss`

packages/plugin-rsc/types/index.d.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,28 @@ declare global {
55
loadModule: <T>(environmentName: string, entryName?: string) => Promise<T>
66
loadBootstrapScriptContent: (entryName: string) => Promise<string>
77
/**
8-
* TODO: jsdoc
9-
* @experimental
8+
* Import a module from another environment using a module specifier.
9+
*
10+
* A more ergonomic alternative to `loadModule` that takes a relative path
11+
* instead of an entry name, so the specifier matches what you'd use in
12+
* `typeof import(...)` type annotations.
13+
*
14+
* @example
15+
* ```ts
16+
* const ssr = await import.meta.viteRsc.import<typeof import('./entry.ssr')>(
17+
* './entry.ssr',
18+
* { environment: 'ssr' }
19+
* );
20+
* ```
21+
*
22+
* @param specifier - Relative path to the module (e.g., './entry.ssr')
23+
* @param options - Options object with `environment` specifying the target environment
24+
* @returns Promise resolving to the module exports
1025
*/
11-
import: <T>(specifier, attributes: { environment: string }) => Promise<T>
26+
import: <T>(
27+
specifier: string,
28+
options: { environment: string },
29+
) => Promise<T>
1230
}
1331
}
1432

0 commit comments

Comments
 (0)