Skip to content

Commit

Permalink
fix: unexpected temporary file (vitejs#13267)
Browse files Browse the repository at this point in the history
  • Loading branch information
s10y10 authored and yang.song committed May 22, 2023
1 parent 3f3fff2 commit 9f52e21
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fs from 'node:fs'
import fsp from 'node:fs/promises'
import path from 'node:path'
import { pathToFileURL } from 'node:url'
import { promisify } from 'node:util'
Expand Down Expand Up @@ -1091,18 +1090,17 @@ 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:
// write it to disk, load it with native Node ESM, then delete the file.
// convert to base64, load it with native Node ESM.
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 {
return (await dynamicImport(fileUrl)).default
return (
await dynamicImport(
'data:text/javascript;base64,' +
Buffer.from(bundledCode).toString('base64'),
)
).default
} finally {
fs.unlink(fileNameTmp, () => {}) // Ignore errors
// Ignore errors
}
}
// for cjs, we can register a custom loader via `_require.extensions`
Expand Down

0 comments on commit 9f52e21

Please sign in to comment.