Skip to content

Commit

Permalink
refactor: remove jest condition
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Oct 6, 2023
1 parent a2e9fb5 commit 61e6729
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 29 deletions.
19 changes: 3 additions & 16 deletions packages/vite/src/node/ssr/ssrModuleLoader.ts
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
11 changes: 1 addition & 10 deletions packages/vite/src/node/utils.ts
Expand Up @@ -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<string, string> | null {
const [_, search] = id.split(requestQuerySplitRE, 2)
Expand Down
3 changes: 0 additions & 3 deletions playground/ssr-deps/__tests__/ssr-deps.spec.ts
Expand Up @@ -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)
Expand Down

0 comments on commit 61e6729

Please sign in to comment.