Skip to content

Commit

Permalink
feat: layouts support
Browse files Browse the repository at this point in the history
  • Loading branch information
vclemenzi committed Mar 21, 2024
1 parent 3081349 commit 5fe4acc
Show file tree
Hide file tree
Showing 28 changed files with 164 additions and 286 deletions.
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# vite-plugin-multip docs
Documentation of the plugin (web version in arrtive) vite-plugin-multip

- [custom html](https://github.com/vclemenzi/vite-plugin-multip/blob/main/docs/custom-html.md)
- [layouts](https://github.com/vclemenzi/vite-plugin-multip/blob/main/docs/layouts.md)
28 changes: 0 additions & 28 deletions docs/custom-html.md

This file was deleted.

65 changes: 65 additions & 0 deletions docs/layouts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
## Layouts with vite-plugin-multip

When utilizing the `vite-plugin-multip` plugin, you gain the capability to finely tailor the structure and appearance of your pages by employing custom layouts. This functionality empowers you to craft distinct layouts for various pages or groups of pages, thereby enhancing the flexibility and customization options for your multipage application.

### Custom Layout Example

Below is a minimalist illustration of a custom layout file, `layout.html`, showcasing the fundamental structure:

```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Custom Layout</title>
</head>
<body>
<header>
<h1>Header</h1>
</header>

<main>
<!-- Page content will be inserted here -->
<slot />
</main>

<footer>
<p>Footer</p>
</footer>
</body>
</html>
```

In this example:

- The `layout.html` file contains a basic HTML structure with a header, a main section (where the specific page content will be inserted), and a footer.
- The `<slot />` tag indicates where the content of the specific page will be inserted into the layout.
- This layout can be further customized by adding CSS styles, JavaScript scripts, or other HTML elements according to the specific project requirements.

### Layout Structure

Each page in your application can utilize its own layout, and layouts are resolved based on proximity. Here's an example directory structure:

```bash
pages/
layout.html # This layout is named "1"
index.svelte # Uses layout "1"
foo/
index.svelte # Uses layout "1"
hello/
layout.html # This layout is named "2"
index.svelte # Uses layout "2"
world/
index.svelte # Uses layout "2"
```

In this structure:

- Pages located closer to a specific layout file will use that layout.
- For instance, `index.svelte` and `foo/index.svelte` will both use the layout defined in `layout.html` because it's the nearest layout file.
- Pages within the `hello` directory and its subdirectories will use the layout defined in `hello/layout.html`.

This approach allows you to organize your layouts and pages logically and apply different layouts as needed throughout your application.

For an example of layout usage with `vite-plugin-multip`, you can refer to the official plugin repository on GitHub at the following link: [https://github.com/vclemenzi/vite-plugin-multip/tree/main/examples/layouts](https://github.com/vclemenzi/vite-plugin-multip/tree/main/examples/layouts).
15 changes: 0 additions & 15 deletions examples/custom-html/custom.html

This file was deleted.

1 change: 0 additions & 1 deletion examples/custom-html/src/pages/index.svelte

This file was deleted.

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions examples/layouts/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + Svelte + TS</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "custom-html",
"name": "layouts",
"private": true,
"version": "0.0.0",
"type": "module",
Expand All @@ -13,9 +13,9 @@
"@sveltejs/vite-plugin-svelte": "^3.0.2",
"@tsconfig/svelte": "^5.0.2",
"svelte": "^4.2.12",
"svelte-check": "^3.6.6",
"svelte-check": "^3.6.7",
"tslib": "^2.6.2",
"typescript": "^5.2.2",
"vite": "^5.1.6"
"vite": "^5.2.0"
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes
1 change: 1 addition & 0 deletions examples/layouts/src/pages/index.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>content</h1>
14 changes: 14 additions & 0 deletions examples/layouts/src/pages/layout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Layout 1</title>
</head>

<body>
<slot />
</body>

</html>
1 change: 1 addition & 0 deletions examples/layouts/src/pages/layout2/index.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>content</h1>
14 changes: 14 additions & 0 deletions examples/layouts/src/pages/layout2/layout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Layout 2</title>
</head>

<body>
<slot />
</body>

</html>
1 change: 1 addition & 0 deletions examples/layouts/src/pages/layout2/sub/index.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>content</h1>
1 change: 1 addition & 0 deletions examples/layouts/src/pages/sub/index.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>content</h1>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import { multipage } from '../../src/index'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [svelte(), multipage({ customHtml: 'custom.html' })],
plugins: [svelte(), multipage()],
})
8 changes: 4 additions & 4 deletions src/boilerplate/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { minify } from "html-minifier-terser";
import type { Config } from "../types";
import fs from "fs";

export const html = async (body: string, config?: Config): Promise<string> => {
export const html = async (body: string, config?: Config, layout?: string) => {
let code = "";

if (config?.customHtml) {
const customHtml = fs.readFileSync(config.customHtml, "utf-8");
if (layout && fs.existsSync(layout)) {
const customHtml = fs.readFileSync(layout, "utf-8");

code = customHtml.replace("{% body %}", body);
code = customHtml.replace("<slot />", body);
} else {
code = `
<!DOCTYPE html>
Expand Down
8 changes: 4 additions & 4 deletions src/boilerplate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { svelte } from "./frameworks/svelte";
import { vue } from "./frameworks/vue";
import { html } from "./html";

export const generateBoilerplate = async (file: string, framework: string, config: Config): Promise<string> => {
export const generateBoilerplate = async (file: string, framework: string, config: Config, layout: string) => {
switch (framework) {
case "svelte":
return await html(svelte(file), config);
return await html(svelte(file), config, layout);
case "vue":
return await html(vue(file), config);
return await html(vue(file), config, layout);
case "tsx" || "jsx":
return await html(react(file), config);
return await html(react(file), config, layout);
default:
return "";
};
Expand Down
Loading

0 comments on commit 5fe4acc

Please sign in to comment.