Skip to content

Commit

Permalink
fix: use Function instead of eval to dynamically import config files (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren authored Oct 7, 2021
1 parent 1113cf3 commit 10694dd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
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)')

0 comments on commit 10694dd

Please sign in to comment.