Skip to content

Commit b3365d5

Browse files
committed
fix(rsc/cjs): add __filename and __dirname
1 parent 1216caf commit b3365d5

File tree

1 file changed

+7
-0
lines changed
  • packages/plugin-rsc/src/plugins

1 file changed

+7
-0
lines changed

packages/plugin-rsc/src/plugins/cjs.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as esModuleLexer from 'es-module-lexer'
66
import { transformCjsToEsm } from '../transforms/cjs'
77
import { createDebug } from '@hiogawa/utils'
88
import { parseIdQuery } from './shared'
9+
import { fileURLToPath, pathToFileURL } from 'node:url'
910

1011
const debug = createDebug('vite-rsc:cjs')
1112

@@ -62,6 +63,12 @@ export function cjsModuleRunnerPlugin(): Plugin[] {
6263
export default module.exports;
6364
export const __cjs_module_runner_transform = true;
6465
`)
66+
// https://github.com/vitest-dev/vitest/blob/965cefc19722a6c899cd1d3decb3cc33e72af696/packages/vite-node/src/client.ts#L548-L554
67+
const __filename = fileURLToPath(pathToFileURL(id).href)
68+
const __dirname = path.dirname(__filename)
69+
output.prepend(
70+
`let __filename = ${JSON.stringify(__filename)}; let __dirname = ${JSON.stringify(__dirname)};\n`,
71+
)
6572
return {
6673
code: output.toString(),
6774
map: output.generateMap({ hires: 'boundary' }),

0 commit comments

Comments
 (0)