Skip to content

Commit

Permalink
refactor: use dynamic import directly (#14661)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Oct 17, 2023
1 parent 1f2a982 commit af60592
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 11 deletions.
3 changes: 1 addition & 2 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
asyncFlatten,
createDebugger,
createFilter,
dynamicImport,
isBuiltin,
isExternalUrl,
isNodeBuiltin,
Expand Down Expand Up @@ -1180,7 +1179,7 @@ async function loadConfigFromBundledFile(
const fileUrl = `${pathToFileURL(fileBase)}.mjs`
await fsp.writeFile(fileNameTmp, bundledCode)
try {
return (await dynamicImport(fileUrl)).default
return (await import(fileUrl)).default
} finally {
fs.unlink(fileNameTmp, () => {}) // Ignore errors
}
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/ssr/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +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 } from '../utils'
import { isBuiltin, unwrapId } from '../utils'
import { transformRequest } from '../server/transformRequest'
import type { InternalResolveOptionsWithOverrideConditions } from '../plugins/resolve'
import { tryNodeResolve } from '../plugins/resolve'
Expand Down Expand Up @@ -288,7 +288,7 @@ async function nodeImport(
url = pathToFileURL(resolved.id).toString()
}

const mod = await dynamicImport(url)
const mod = await import(url)
return proxyESM(mod)
}

Expand Down
7 changes: 0 additions & 7 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -973,13 +973,6 @@ export const multilineCommentsRE = /\/\*[^*]*\*+(?:[^/*][^*]*\*+)*\//g
export const singlelineCommentsRE = /\/\/.*/g
export const requestQuerySplitRE = /\?(?!.*[/|}])/

/**
* Dynamically import files. It will make sure it's not being compiled away by TS/Rollup.
*
* @param file File path to import.
*/
export const dynamicImport = new Function('file', 'return import(file)')

export function parseRequest(id: string): Record<string, string> | null {
const [_, search] = id.split(requestQuerySplitRE, 2)
if (!search) {
Expand Down

0 comments on commit af60592

Please sign in to comment.