From b564c9d865f351b28aa6bf3b330675014a180554 Mon Sep 17 00:00:00 2001 From: s10y10 <248496385@qq.com> Date: Fri, 19 May 2023 17:21:25 +0800 Subject: [PATCH] fix: unexpected temporary file (#13267) --- packages/vite/src/node/config.ts | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index c2c1929abcf09a..98b79a98e47008 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -1141,18 +1141,17 @@ async function loadConfigFromBundledFile( ): Promise { // 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 - } finally { - fs.unlink(fileNameTmp, () => {}) // Ignore errors + return ( + await dynamicImport( + 'data:text/javascript;base64,' + + Buffer.from(bundledCode).toString('base64'), + ) + ).default + } catch (e) { + throw new Error(`${e.message} at ${fileName}`) } } // for cjs, we can register a custom loader via `_require.extensions`