Skip to content

Commit 4fc492c

Browse files
authoredJan 30, 2023
fix: cjs exports has Object.prototype instead of null (#2769)
* fix: cjs exports has Object.prototype instead of null * chore: cleanup
1 parent b666506 commit 4fc492c

File tree

8 files changed

+36
-2
lines changed

8 files changed

+36
-2
lines changed
 

‎packages/vite-node/src/client.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,15 @@ export class ViteNodeRunner {
310310
enumerable: false,
311311
configurable: false,
312312
})
313-
// this prosxy is triggered only on exports.{name} and module.exports access
313+
// this proxy is triggered only on exports.{name} and module.exports access
314+
// inside the module itself. imported module is always "exports"
314315
const cjsExports = new Proxy(exports, {
316+
get: (target, p, receiver) => {
317+
if (Reflect.has(target, p))
318+
return Reflect.get(target, p, receiver)
319+
return Reflect.get(Object.prototype, p, receiver)
320+
},
321+
getPrototypeOf: () => Object.prototype,
315322
set: (_, p, value) => {
316323
// treat "module.exports =" the same as "exports.default =" to not have nested "default.default",
317324
// so "exports.default" becomes the actual module

‎test/cjs/src/prototype.cjs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
exports.test = () => {
2+
// eslint-disable-next-line no-prototype-builtins
3+
return exports.hasOwnProperty('test')
4+
}
5+
exports.getPrototype = () => {
6+
return Object.getPrototypeOf(exports)
7+
}

‎test/cjs/src/prototype.d.cts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const test: () => boolean
2+
export const getPrototype: () => any

‎test/cjs/test/prototype.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { expect, it } from 'vitest'
2+
import * as cjsExports from '../src/prototype.cjs'
3+
4+
it('has object prototype', () => {
5+
expect(cjsExports.getPrototype()).toBe(Object.prototype)
6+
expect(() => cjsExports.test()).not.toThrow()
7+
expect(cjsExports.test()).toBe(true)
8+
})

‎test/esm/src/prototype.d.mts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare export const test: number

‎test/esm/src/prototype.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const test = 1

‎test/esm/test/prototype.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { expect, it } from 'vitest'
2+
import * as exports from '../src/prototype.mjs'
3+
4+
it('prototype is null', () => {
5+
expect(Object.getPrototypeOf(exports)).toBe(null)
6+
expect({}.hasOwnProperty).toBeTypeOf('function')
7+
expect(exports.hasOwnProperty).toBeTypeOf('undefined')
8+
})

‎test/esm/vite.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { defineConfig } from 'vitest/config'
33
export default defineConfig({
44
test: {
55
deps: {
6-
external: [/tslib/, /css-what/],
6+
external: [/tslib/, /css-what/, /prototype\.mjs/],
77
registerNodeLoader: true,
88
},
99
},

0 commit comments

Comments
 (0)
Please sign in to comment.