From af3b2595f219dcb463f8804420ed9500193f1a24 Mon Sep 17 00:00:00 2001 From: John Gozde Date: Sun, 9 Jul 2023 11:00:08 -0600 Subject: [PATCH] Rewrite asset paths if they reference actual esbuild outputs --- .changeset/honest-lions-breathe.md | 5 + .../src/html-entry-point/write-template.ts | 94 ++++++++++--------- .../esbd-build-html.test.ts.snap | 48 ++++++++++ packages/esbd/test/esbd-build-html.test.ts | 32 +++++++ 4 files changed, 133 insertions(+), 46 deletions(-) create mode 100644 .changeset/honest-lions-breathe.md diff --git a/.changeset/honest-lions-breathe.md b/.changeset/honest-lions-breathe.md new file mode 100644 index 0000000..6ff51fa --- /dev/null +++ b/.changeset/honest-lions-breathe.md @@ -0,0 +1,5 @@ +--- +'esbd': minor +--- + +Rewrite asset paths if they reference actual esbuild outputs diff --git a/packages/esbd/src/html-entry-point/write-template.ts b/packages/esbd/src/html-entry-point/write-template.ts index b516636..75ad394 100644 --- a/packages/esbd/src/html-entry-point/write-template.ts +++ b/packages/esbd/src/html-entry-point/write-template.ts @@ -34,8 +34,8 @@ export interface WriteTemplateOptions extends EsbuildHtmlOptions { } interface FileSystem { - copyFile: typeof fsp['copyFile']; - writeFile: typeof fsp['writeFile']; + copyFile: (typeof fsp)['copyFile']; + writeFile: (typeof fsp)['writeFile']; } export async function writeTemplate( @@ -64,7 +64,6 @@ export async function writeTemplate( } const copyFile = cachedCopyFile(fs.copyFile); - const outputCache = new Set(); const { document, head, body } = template; @@ -73,7 +72,10 @@ export async function writeTemplate( const templateOutputPath = path.resolve(absOutDir, template.outputPath); const needsModuleType = format === 'esm'; - const outputs: [entryPoint: string, outputPath: string][] = Object.keys(metafile.outputs) + const outputFilenames = Object.keys(metafile.outputs); + + // Find emitted entry points and convert them to absolute paths + const entryOutputs: [entryPoint: string, outputPath: string][] = outputFilenames .map(o => [metafile.outputs[o], o] as const) .filter(([output]) => !!output.entryPoint) .map(([output, outputPath]) => [ @@ -81,32 +83,27 @@ export async function writeTemplate( path.relative(absTemplateDir, path.resolve(basedir, outputPath)), ]); - const cssOutput = new Map(outputs.filter(([, o]) => o.endsWith('.css'))); - const jsOutput = new Map(outputs.filter(([, o]) => o.endsWith('.js'))); - - // Check whether any of the output file names have changed since the last - // build finished - let modified = false; - const currentOutputs = new Set([ - ...Array.from(cssOutput.values()), - ...Array.from(jsOutput.values()), - ]); - for (const output of currentOutputs) { - if (!outputCache.has(output)) { - outputCache.add(output); - modified = true; - } - } - for (const output of outputCache) { - if (!currentOutputs.has(output)) { - outputCache.delete(output); - modified = true; - } - } - - // If no output filenames have changed, then there is no need to emit - // the HTML - if (!modified) return; + // Find any output files that were produced from inputs with matching "href" + // attributes in any or