From 4172f02b70a8ae44bb8f3bc22d5fd5cffe458274 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Wed, 14 Feb 2024 20:19:06 +0100 Subject: [PATCH] refactor(runtime): seal ES module namespace object instead of feezing (#15914) --- .../node/ssr/runtime/__tests__/server-runtime.spec.ts | 11 +++++++++++ packages/vite/src/node/ssr/runtime/esmRunner.ts | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/ssr/runtime/__tests__/server-runtime.spec.ts b/packages/vite/src/node/ssr/runtime/__tests__/server-runtime.spec.ts index 5dd45fba53fbcc..a27ee8034523e6 100644 --- a/packages/vite/src/node/ssr/runtime/__tests__/server-runtime.spec.ts +++ b/packages/vite/src/node/ssr/runtime/__tests__/server-runtime.spec.ts @@ -87,11 +87,22 @@ describe('vite-runtime initialization', async () => { it('exports is not modifiable', async ({ runtime }) => { const mod = await runtime.executeUrl('/fixtures/simple.js') + expect(Object.isSealed(mod)).toBe(true) expect(() => { mod.test = 'I am modified' }).toThrowErrorMatchingInlineSnapshot( `[TypeError: Cannot set property test of [object Module] which has only a getter]`, ) + expect(() => { + delete mod.test + }).toThrowErrorMatchingInlineSnapshot( + `[TypeError: Cannot delete property 'test' of [object Module]]`, + ) + expect(() => { + Object.defineProperty(mod, 'test', { value: 'I am modified' }) + }).toThrowErrorMatchingInlineSnapshot( + `[TypeError: Cannot redefine property: test]`, + ) expect(() => { mod.other = 'I am added' }).toThrowErrorMatchingInlineSnapshot( diff --git a/packages/vite/src/node/ssr/runtime/esmRunner.ts b/packages/vite/src/node/ssr/runtime/esmRunner.ts index a9aacd8fbea13f..47078c370fb852 100644 --- a/packages/vite/src/node/ssr/runtime/esmRunner.ts +++ b/packages/vite/src/node/ssr/runtime/esmRunner.ts @@ -34,7 +34,7 @@ export class ESModulesRunner implements ViteModuleRunner { context[ssrExportAllKey], ) - Object.freeze(context[ssrModuleExportsKey]) + Object.seal(context[ssrModuleExportsKey]) } runExternalModule(filepath: string): Promise {