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

fix: use Function instead of eval to dynamically import config files #5213

Merged
merged 1 commit into from
Oct 7, 2021
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
10 changes: 5 additions & 5 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
isExternalUrl,
isObject,
lookupFile,
normalizePath
normalizePath,
dynamicImport
} from './utils'
import { resolvePlugins } from './plugins'
import chalk from 'chalk'
Expand Down Expand Up @@ -819,16 +820,15 @@ export async function loadConfigFromFile(
const bundled = await bundleConfigFile(resolvedPath, true)
dependencies = bundled.dependencies
fs.writeFileSync(resolvedPath + '.js', bundled.code)
userConfig = (await eval(`import(fileUrl + '.js?t=${Date.now()}')`))
userConfig = (await dynamicImport(`${fileUrl}.js?t=${Date.now()}`))
.default
fs.unlinkSync(resolvedPath + '.js')
debug(`TS + native esm config loaded in ${getTime()}`, fileUrl)
} else {
// using eval to avoid this from being compiled away by TS/Rollup
// using Function to avoid this from being compiled away by TS/Rollup
// append a query so that we force reload fresh config in case of
// server restart
userConfig = (await eval(`import(fileUrl + '?t=${Date.now()}')`))
.default
userConfig = (await dynamicImport(`${fileUrl}?t=${Date.now()}`)).default
debug(`native esm config loaded in ${getTime()}`, fileUrl)
}
}
Expand Down
7 changes: 7 additions & 0 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,3 +568,10 @@ export function toUpperCaseDriveLetter(pathName: string): string {

export const multilineCommentsRE = /\/\*(.|[\r\n])*?\*\//gm
export const singlelineCommentsRE = /\/\/.*/g

/**
* 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)')