diff --git a/src/index.ts b/src/index.ts index 113caae..8f08160 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,23 @@ export async function staticPlugin({ let pathName = normalizePath(path.join(prefix, relativePath)) if (isBun && absolutePath.endsWith('.html')) { - const htmlBundle = await import(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, htmlBundle.default) + app.get(pathName, htmlFile) if (indexHTML && pathName.endsWith('/index.html')) app.get( pathName.replace('/index.html', ''), - htmlBundle.default + htmlFile ) continue @@ -233,13 +244,23 @@ export async function staticPlugin({ let relativePath = absolutePath.replace(assetsDir, '') const pathName = normalizePath(path.join(prefix, relativePath)) - const htmlBundle = await import(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, htmlBundle.default) + app.get(pathName, htmlFile) if (indexHTML && pathName.endsWith('/index.html')) app.get( pathName.replace('/index.html', ''), - htmlBundle.default + htmlFile ) } } diff --git a/src/types.ts b/src/types.ts index fa98cef..ee95457 100644 --- a/src/types.ts +++ b/src/types.ts @@ -101,6 +101,15 @@ export interface StaticOptions { */ indexHTML?: boolean + /** + * @default true + * + * 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 + /** * decodeURI *