Skip to content

Commit

Permalink
fix: fix __dirname, __filename for CJS preview
Browse files Browse the repository at this point in the history
  • Loading branch information
ambar committed Apr 11, 2024
1 parent c11e062 commit d7abfe0
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 3 deletions.
8 changes: 8 additions & 0 deletions example/demo-node.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ new Map([
['a', 1],
['b', 2],
])

// Node globals
__dirname
__filename
typeof require
typeof module
// skip bundling
eval('require')('./utils.cjs').now()
3 changes: 3 additions & 0 deletions example/node-react.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {createElement} from 'react'

createElement('div', null, 'Hello, world!')
1 change: 1 addition & 0 deletions example/utils.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.now = Date.now
1 change: 1 addition & 0 deletions src/extension/__tests__/bundle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ describe('node', () => {
const opts: BundleOpts = {
platform: 'node',
sourcemap: false,
filename: __filename,
workspaceFolder: __dirname,
}

Expand Down
2 changes: 1 addition & 1 deletion src/sandbox/__tests__/nodeVM.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Message} from 'console-feed/src/definitions/Console'
import {Message} from 'console-feed/lib/definitions/Console'
import {create} from '../../utils/promise'
import * as nodeVM from '../nodeVM'
import transform from '../transform'
Expand Down
2 changes: 1 addition & 1 deletion src/sandbox/nodeVM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const runInNewContext = async (
) => {
return new Promise<Result>((resolve, reject) => {
const worker = new Worker(workerPath, {
// canot use `eval: true` due to scope bug in `vm.runInThisContext`
// cannot use `eval: true` due to scope bug in `vm.runInThisContext`
workerData: {code, filename},
})
if (workerRef) {
Expand Down
11 changes: 10 additions & 1 deletion src/sandbox/nodeWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const {workerData, parentPort} = require('worker_threads')
const {format} = require('pretty-format')
const Hook = require('console-feed/lib/Hook').default
const vm = require('vm')
const path = require('path')
const {createRequire} = require('module')

const {code, filename} = workerData
const result = []
Expand Down Expand Up @@ -39,7 +41,14 @@ Object.assign(globalThis, {
})

// https://nodejs.org/api/globals.html#global-objects
const globalObjects = {module, exports, require, __dirname, __filename}
const globalObjects = {
module,
exports,
require: createRequire(filename),
__dirname: path.dirname(filename),
__filename: filename,
}
// TODO: https://nodejs.org/api/vm.html#class-vmmodule
vm.runInThisContext(
`(({module, exports, require, __dirname, __filename}) => { ${code} })`,
{filename}
Expand Down

0 comments on commit d7abfe0

Please sign in to comment.