From b73e16a12e2b2799a99982cdbd44976164ae360d Mon Sep 17 00:00:00 2001 From: Erik Demaine Date: Thu, 22 Feb 2024 06:35:57 -0500 Subject: [PATCH] fix: esbuild emitFile should mkdir subdirectories too (#362) --- src/esbuild/utils.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/esbuild/utils.ts b/src/esbuild/utils.ts index d2b13f11..6a287258 100644 --- a/src/esbuild/utils.ts +++ b/src/esbuild/utils.ts @@ -126,13 +126,15 @@ export function createBuildContext(initialOptions: BuildOptions): UnpluginBuildC throw new Error('unplugin/esbuild: addWatchFile outside supported hooks (resolveId, load, transform)') }, emitFile(emittedFile) { - // Ensure output directory exists for this.emitFile - if (initialOptions.outdir && !fs.existsSync(initialOptions.outdir)) - fs.mkdirSync(initialOptions.outdir, { recursive: true }) - const outFileName = emittedFile.fileName || emittedFile.name - if (initialOptions.outdir && emittedFile.source && outFileName) - fs.writeFileSync(path.resolve(initialOptions.outdir, outFileName), emittedFile.source) + if (initialOptions.outdir && emittedFile.source && outFileName) { + const outPath = path.resolve(initialOptions.outdir, outFileName) + // Ensure output directory exists for this.emitFile + const outDir = path.dirname(outPath) + if (!fs.existsSync(outDir)) + fs.mkdirSync(outDir, { recursive: true }) + fs.writeFileSync(outPath, emittedFile.source) + } }, getWatchFiles() { return watchFiles