From 8d18a910128db25b120218260ab911dd31ff6a5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Mon, 9 Oct 2023 20:13:35 +0800 Subject: [PATCH] refactor!: remove jest condition (#14544) --- packages/vite/src/node/ssr/ssrModuleLoader.ts | 19 +++---------------- packages/vite/src/node/utils.ts | 11 +---------- .../ssr-deps/__tests__/ssr-deps.spec.ts | 3 --- 3 files changed, 4 insertions(+), 29 deletions(-) diff --git a/packages/vite/src/node/ssr/ssrModuleLoader.ts b/packages/vite/src/node/ssr/ssrModuleLoader.ts index 2d64cd5bc1105f..dcb4912c2a6c69 100644 --- a/packages/vite/src/node/ssr/ssrModuleLoader.ts +++ b/packages/vite/src/node/ssr/ssrModuleLoader.ts @@ -2,12 +2,7 @@ import path from 'node:path' import { pathToFileURL } from 'node:url' import colors from 'picocolors' import type { ViteDevServer } from '../server' -import { - dynamicImport, - isBuiltin, - unwrapId, - usingDynamicImport, -} from '../utils' +import { dynamicImport, isBuiltin, unwrapId } from '../utils' import { transformRequest } from '../server/transformRequest' import type { InternalResolveOptionsWithOverrideConditions } from '../plugins/resolve' import { tryNodeResolve } from '../plugins/resolve' @@ -278,12 +273,7 @@ async function nodeImport( const resolved = tryNodeResolve( id, importer, - // Non-external modules can import ESM-only modules, but only outside - // of test runs, because we use Node `require` in Jest to avoid segfault. - // @ts-expect-error jest only exists when running Jest - typeof jest === 'undefined' - ? { ...resolveOptions, tryEsmOnly: true } - : resolveOptions, + { ...resolveOptions, tryEsmOnly: true }, false, undefined, true, @@ -295,10 +285,7 @@ async function nodeImport( err.code = 'ERR_MODULE_NOT_FOUND' throw err } - url = resolved.id - if (usingDynamicImport) { - url = pathToFileURL(url).toString() - } + url = pathToFileURL(resolved.id).toString() } const mod = await dynamicImport(url) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 76ab58ba92f9f9..ef425c2c3c6131 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -955,21 +955,12 @@ export const multilineCommentsRE = /\/\*[^*]*\*+(?:[^/*][^*]*\*+)*\//g export const singlelineCommentsRE = /\/\/.*/g export const requestQuerySplitRE = /\?(?!.*[/|}])/ -// @ts-expect-error jest only exists when running Jest -export const usingDynamicImport = typeof jest === 'undefined' - /** * Dynamically import files. It will make sure it's not being compiled away by TS/Rollup. * - * As a temporary workaround for Jest's lack of stable ESM support, we fallback to require - * if we're in a Jest environment. - * See https://github.com/vitejs/vite/pull/5197#issuecomment-938054077 - * * @param file File path to import. */ -export const dynamicImport = usingDynamicImport - ? new Function('file', 'return import(file)') - : _require +export const dynamicImport = new Function('file', 'return import(file)') export function parseRequest(id: string): Record | null { const [_, search] = id.split(requestQuerySplitRE, 2) diff --git a/playground/ssr-deps/__tests__/ssr-deps.spec.ts b/playground/ssr-deps/__tests__/ssr-deps.spec.ts index 2af338507c5a0d..c8794ce915dc21 100644 --- a/playground/ssr-deps/__tests__/ssr-deps.spec.ts +++ b/playground/ssr-deps/__tests__/ssr-deps.spec.ts @@ -6,9 +6,6 @@ const url = `http://localhost:${port}` /** * test for #5809 - * - * NOTE: This test will always succeed now, unless the temporary workaround for Jest can be removed - * See https://github.com/vitejs/vite/pull/5197#issuecomment-938054077 */ test('msg should be encrypted', async () => { await page.goto(url)