Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: standalone layouts #19

Merged
merged 2 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/layouts/src/pages/2/sub/layout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// First layout will not work
console.log("Hello!")
53 changes: 20 additions & 33 deletions src/boilerplate/html.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,21 @@
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
) => {
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(
"</head>",
`<script type="module">import "${!dev ? resolve(cssPath[0]) : fixPath(resolve(cssPath[0]), config?.directory || "src/pages")}";</script></head>`
);
}

if (scriptPath[0] && fs.existsSync(scriptPath[0])) {
code = code.replace(
"</body>",
`<script type="module" src="${!dev ? resolve(scriptPath[0]) : fixPath(resolve(scriptPath[0]), config?.directory || "src/pages")}"></script></body>`
);
}
code = fs.readFileSync(layout, "utf-8");

code = code.replace("<slot />", body);
} else if (fs.existsSync(resolve("index.html"))) {
Expand Down Expand Up @@ -78,6 +48,23 @@ export const html = async (
</html>`;
}

const cssPath = await getLayoutByType(file, "{css,scss,sass,less}");
const scriptPath = await getLayoutByType(file, "{ts,js}");

if (cssPath) {
code = code.replace(
"</head>",
`<script type="module">import "${!dev ? resolve(cssPath) : fixPath(resolve(cssPath), config?.directory || "src/pages")}";</script></head>`
)
}

if (scriptPath) {
code = code.replace(
"</body>",
`<script type="module">import "${!dev ? resolve(scriptPath) : fixPath(resolve(scriptPath), config?.directory || "src/pages")}";</script></body>`
)
}

const result = await minify(code, {
collapseWhitespace: config?.minify?.collapseWhitespace || true,
removeComments: config?.minify?.removeComments || true,
Expand Down
6 changes: 6 additions & 0 deletions src/boilerplate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,34 +35,39 @@ export const generateBoilerplate = async (options: BoilerplateOptions) => {

return await html(
vue(file, css, scripts, dev, config.directory || "src/pages", init),
file,
config,
layout,
dev
);
case "react":
return await html(
react(file, css, scripts, dev, config.directory || "src/pages"),
file,
config,
layout,
dev
);
case "solid":
return await html(
solid(file, css, scripts, dev, config.directory || "src/pages"),
file,
config,
layout,
dev
);
case "md":
return await html(
markdown(file, css, scripts, dev, config.directory || "src/pages"),
file,
config,
layout,
dev
);
case "html":
return await html(
vanilla(file, css, scripts, dev, config.directory || "src/pages"),
file,
config,
layout,
dev
Expand Down
8 changes: 6 additions & 2 deletions src/utils/layouts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import glob from "tiny-glob";
import { dirname } from "path";
import { resolve } from "./resolve";

export const getLayout = async (id: string): Promise<string> => {
const layouts = await glob("../**/layout.html", {
export const getLayoutByType = async (id: string, type: string): Promise<string> => {
const layouts = await glob(`../**/layout.${type}`, {
cwd: dirname(id),
filesOnly: true,
});
Expand All @@ -23,3 +23,7 @@ export const getLayout = async (id: string): Promise<string> => {

return resolve(dirname(id), layout);
};

export const getLayout = async (id: string): Promise<string> => {
return await getLayoutByType(id, "html");
};
Loading