Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions packages/plugin-rsc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,43 @@ export default defineConfig({
})
```

### `@vitejs/plugin-rsc/transforms`

The CommonJS transform used by the RSC module runner is available as a
low-level framework integration API:

```js
import { transformCjsToEsm } from '@vitejs/plugin-rsc/transforms'
import { parseAstAsync } from 'vite'

const commonJsApplicationPlugin = {
name: 'framework:commonjs-application',
apply: 'serve',
applyToEnvironment: (env) => env.config.dev.moduleRunnerTransform,
transform: {
filter: { id: /\.cjs$/ },
async handler(code, id) {
const ast = await parseAstAsync(code)
const { output } = transformCjsToEsm(code, ast, { id })
return {
code: output.toString(),
map: output.generateMap({ hires: 'boundary' }),
}
},
},
}
```

This transform targets Vite's server module runner. The generated module uses
the module runner's `__vite_ssr_exportAll__` helper and top-level `await`, so it
is not a general production CommonJS bundler transform.

Nested `require()` calls are hoisted to asynchronous imports to keep the
surrounding function synchronous. This does not preserve Node.js semantics
when a nested require is conditional or depends on runtime state. Frameworks
should detect or implement their own policy for dynamic `require()` calls
before invoking this transform.

## RSC runtime (react-server-dom) API

### `@vitejs/plugin-rsc/rsc`
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-rsc/src/plugins/cjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createDebug } from '@hiogawa/utils'
import * as esModuleLexer from 'es-module-lexer'
import { parseAstAsync, type Plugin } from 'vite'
import { findClosestPkgJsonPath } from 'vitefu'
import { transformCjsToEsm } from '../transforms/cjs'
import { transformCjsToEsm } from '../transforms'
import { parseIdQuery } from './shared'

const debug = createDebug('vite-rsc:cjs')
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-rsc/src/transforms/cjs.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'node:path'
import { createServer, createServerModuleRunner, parseAstAsync } from 'vite'
import { describe, expect, it } from 'vitest'
import { transformCjsToEsm } from './cjs'
import { transformCjsToEsm } from './index'
import { debugSourceMap } from './test-utils'

describe(transformCjsToEsm, () => {
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-rsc/src/transforms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './proxy-export'
export * from './utils'
export * from './server-action'
export * from './expand-export-all'
export * from './cjs'
Loading