From a64fbd037a663c6196c022a75f7ee719a07237a0 Mon Sep 17 00:00:00 2001 From: pa001024 Date: Thu, 11 Dec 2025 14:51:31 +0800 Subject: [PATCH 1/4] Add bundleHTML option for HTML file handling --- src/index.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index 113caae..488a18e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,6 +30,7 @@ export async function staticPlugin({ etag: useETag = true, extension = true, indexHTML = true, + bundleHTML = true, decodeURI, silent }: StaticOptions = {}): Promise { @@ -87,13 +88,13 @@ export async function staticPlugin({ let pathName = normalizePath(path.join(prefix, relativePath)) if (isBun && absolutePath.endsWith('.html')) { - const htmlBundle = await import(absolutePath) + const htmlFile = bundleHTML ? (await import(absolutePath)).default : getFile(absolutePath) - app.get(pathName, htmlBundle.default) + app.get(pathName, htmlFile) if (indexHTML && pathName.endsWith('/index.html')) app.get( pathName.replace('/index.html', ''), - htmlBundle.default + htmlFile ) continue @@ -233,13 +234,13 @@ export async function staticPlugin({ let relativePath = absolutePath.replace(assetsDir, '') const pathName = normalizePath(path.join(prefix, relativePath)) - const htmlBundle = await import(absolutePath) + const htmlFile = bundleHTML ? (await import(absolutePath)).default : getFile(absolutePath) - app.get(pathName, htmlBundle.default) + app.get(pathName, htmlFile) if (indexHTML && pathName.endsWith('/index.html')) app.get( pathName.replace('/index.html', ''), - htmlBundle.default + htmlFile ) } } From ae774e2813e16f75371e792ad372ccf33f251859 Mon Sep 17 00:00:00 2001 From: pa001024 Date: Thu, 11 Dec 2025 14:55:04 +0800 Subject: [PATCH 2/4] Add optional bundleHTML property to types --- src/types.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/types.ts b/src/types.ts index fa98cef..6db3954 100644 --- a/src/types.ts +++ b/src/types.ts @@ -101,6 +101,13 @@ export interface StaticOptions { */ indexHTML?: boolean + /** + * @default true + * + * Enable bun bundle of *.html + */ + bundleHTML?: boolean + /** * decodeURI * From b986176130de4d96d514a3963daafbb5d87d5cc9 Mon Sep 17 00:00:00 2001 From: pa001024 Date: Thu, 11 Dec 2025 15:21:39 +0800 Subject: [PATCH 3/4] Implement error handling for HTML file loading Added error handling for loading HTML files to prevent crashes and provide console error messages. --- src/index.ts | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 488a18e..8f08160 100644 --- a/src/index.ts +++ b/src/index.ts @@ -88,7 +88,17 @@ export async function staticPlugin({ let pathName = normalizePath(path.join(prefix, relativePath)) if (isBun && absolutePath.endsWith('.html')) { - const htmlFile = bundleHTML ? (await import(absolutePath)).default : getFile(absolutePath) + let htmlFile + try { + htmlFile = bundleHTML ? (await import(absolutePath)).default : getFile(absolutePath) + } catch (error) { + if (!silent) + console.error( + `[@elysiajs/static] Failed to load HTML file: ${absolutePath}`, + error + ) + continue + } app.get(pathName, htmlFile) if (indexHTML && pathName.endsWith('/index.html')) @@ -234,7 +244,17 @@ export async function staticPlugin({ let relativePath = absolutePath.replace(assetsDir, '') const pathName = normalizePath(path.join(prefix, relativePath)) - const htmlFile = bundleHTML ? (await import(absolutePath)).default : getFile(absolutePath) + let htmlFile + try { + htmlFile = bundleHTML ? (await import(absolutePath)).default : getFile(absolutePath) + } catch (error) { + if (!silent) + console.error( + `[@elysiajs/static] Failed to load HTML file: ${absolutePath}`, + error + ) + continue + } app.get(pathName, htmlFile) if (indexHTML && pathName.endsWith('/index.html')) From 6ce9d54c87c97e1987a5b5a2b2949e824d159734 Mon Sep 17 00:00:00 2001 From: pa001024 Date: Thu, 11 Dec 2025 15:28:44 +0800 Subject: [PATCH 4/4] Clarify bundleHTML option in types.ts Updated documentation for bundleHTML option to clarify functionality. --- src/types.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/types.ts b/src/types.ts index 6db3954..ee95457 100644 --- a/src/types.ts +++ b/src/types.ts @@ -104,7 +104,9 @@ export interface StaticOptions { /** * @default true * - * Enable bun bundle of *.html + * Enable bundling of HTML files (Bun only). + * When true, HTML imports using Bun’s bundler, JavaScript transpiler and CSS parser. [See more](https://bun.com/docs/bundler/fullstack) + * When false, HTML files are served directly from disk. */ bundleHTML?: boolean