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: revert import config module as data #13731

Merged
merged 2 commits into from
Jul 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 10 additions & 15 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1150,23 +1150,18 @@ async function loadConfigFromBundledFile(
): Promise<UserConfigExport> {
// for esm, before we can register loaders without requiring users to run node
// with --experimental-loader themselves, we have to do a hack here:
// convert to base64, load it with native Node ESM.
// write it to disk, load it with native Node ESM, then delete the file.
if (isESM) {
const fileBase = `${fileName}.timestamp-${Date.now()}-${Math.random()
.toString(16)
.slice(2)}`
const fileNameTmp = `${fileBase}.mjs`
const fileUrl = `${pathToFileURL(fileBase)}.mjs`
await fsp.writeFile(fileNameTmp, bundledCode)
try {
// Postfix the bundled code with a timestamp to avoid Node's ESM loader cache
const configTimestamp = `${fileName}.timestamp:${Date.now()}-${Math.random()
.toString(16)
.slice(2)}`
return (
await dynamicImport(
'data:text/javascript;base64,' +
Buffer.from(`${bundledCode}\n//${configTimestamp}`).toString(
'base64',
),
)
).default
} catch (e) {
throw new Error(`${e.message} at ${fileName}`)
return (await dynamicImport(fileUrl)).default
} finally {
fs.unlink(fileNameTmp, () => {}) // Ignore errors
}
}
// for cjs, we can register a custom loader via `_require.extensions`
Expand Down