diff --git a/examples/layouts/src/pages/2/sub/layout.ts b/examples/layouts/src/pages/2/sub/layout.ts new file mode 100644 index 0000000..45732e9 --- /dev/null +++ b/examples/layouts/src/pages/2/sub/layout.ts @@ -0,0 +1,2 @@ +// First layout will not work +console.log("Hello!") diff --git a/src/boilerplate/html.ts b/src/boilerplate/html.ts index d5af631..74c54e4 100644 --- a/src/boilerplate/html.ts +++ b/src/boilerplate/html.ts @@ -1,12 +1,13 @@ import { minify } from "html-minifier-terser"; import type { Config } from "../types"; import fs from "fs"; -import glob from "tiny-glob"; import { resolve } from "../utils/resolve"; +import { getLayoutByType } from "../utils/layouts"; import { fixPath } from "../utils/path"; export const html = async ( body: string, + file: string, config?: Config, layout?: string, dev?: boolean @@ -14,38 +15,7 @@ export const html = async ( let code = ""; if (layout && fs.existsSync(layout) && typeof layout === "string") { - const customHtml = fs.readFileSync(layout, "utf-8"); - - const cssPath = await glob( - layout.replace(".html", ".{css,scss,sass,less}"), - { - filesOnly: true, - } - ); - - const scriptPath = await glob(layout.replace(".html", ".{ts,js}"), { - filesOnly: true, - }); - - if (cssPath.length > 1 || scriptPath.length > 1) { - throw new Error("Multiple CSS or script files found for the layout."); - } - - code = customHtml; - - if (cssPath[0] && fs.existsSync(cssPath[0])) { - code = customHtml.replace( - "", - `` - ); - } - - if (scriptPath[0] && fs.existsSync(scriptPath[0])) { - code = code.replace( - "", - `` - ); - } + code = fs.readFileSync(layout, "utf-8"); code = code.replace("", body); } else if (fs.existsSync(resolve("index.html"))) { @@ -78,6 +48,23 @@ export const html = async ( `; } + const cssPath = await getLayoutByType(file, "{css,scss,sass,less}"); + const scriptPath = await getLayoutByType(file, "{ts,js}"); + + if (cssPath) { + code = code.replace( + "", + `` + ) + } + + if (scriptPath) { + code = code.replace( + "", + `` + ) + } + const result = await minify(code, { collapseWhitespace: config?.minify?.collapseWhitespace || true, removeComments: config?.minify?.removeComments || true, diff --git a/src/boilerplate/index.ts b/src/boilerplate/index.ts index 9e88ce2..e236817 100644 --- a/src/boilerplate/index.ts +++ b/src/boilerplate/index.ts @@ -25,6 +25,7 @@ export const generateBoilerplate = async (options: BoilerplateOptions) => { case "svelte": return await html( svelte(file, css, scripts, dev, config.directory || "src/pages"), + file, config, layout, dev @@ -34,6 +35,7 @@ export const generateBoilerplate = async (options: BoilerplateOptions) => { return await html( vue(file, css, scripts, dev, config.directory || "src/pages", init), + file, config, layout, dev @@ -41,6 +43,7 @@ export const generateBoilerplate = async (options: BoilerplateOptions) => { case "react": return await html( react(file, css, scripts, dev, config.directory || "src/pages"), + file, config, layout, dev @@ -48,6 +51,7 @@ export const generateBoilerplate = async (options: BoilerplateOptions) => { case "solid": return await html( solid(file, css, scripts, dev, config.directory || "src/pages"), + file, config, layout, dev @@ -55,6 +59,7 @@ export const generateBoilerplate = async (options: BoilerplateOptions) => { case "md": return await html( markdown(file, css, scripts, dev, config.directory || "src/pages"), + file, config, layout, dev @@ -62,6 +67,7 @@ export const generateBoilerplate = async (options: BoilerplateOptions) => { case "html": return await html( vanilla(file, css, scripts, dev, config.directory || "src/pages"), + file, config, layout, dev diff --git a/src/utils/layouts.ts b/src/utils/layouts.ts index ccf5214..96f1476 100644 --- a/src/utils/layouts.ts +++ b/src/utils/layouts.ts @@ -2,8 +2,8 @@ import glob from "tiny-glob"; import { dirname } from "path"; import { resolve } from "./resolve"; -export const getLayout = async (id: string): Promise => { - const layouts = await glob("../**/layout.html", { +export const getLayoutByType = async (id: string, type: string): Promise => { + const layouts = await glob(`../**/layout.${type}`, { cwd: dirname(id), filesOnly: true, }); @@ -23,3 +23,7 @@ export const getLayout = async (id: string): Promise => { return resolve(dirname(id), layout); }; + +export const getLayout = async (id: string): Promise => { + return await getLayoutByType(id, "html"); +};