Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: remove jest condition #14544

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions packages/vite/src/node/ssr/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
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()
bluwy marked this conversation as resolved.
Show resolved Hide resolved
}
url = pathToFileURL(resolved.id).toString()
}

const mod = await dynamicImport(url)
Expand Down
11 changes: 1 addition & 10 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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