Skip to content

Commit bcef412

Browse files
authored
Merge pull request #3 from SubZtep/render-locals
Render locals
2 parents 4f72b2e + c11b79a commit bcef412

20 files changed

+1979
-713
lines changed

README.md

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,59 @@
22

33
![test](https://github.com/SubZtep/vite-plugin-pug/workflows/npm%20test/badge.svg)
44

5-
A plugin that makes [Vite](https://vitejs.dev/) parse `<pug file="example.pug"></pug>` in your `index.html`. The rendered template replaces this tag with the compiled markup.
5+
A plugin that makes [Vite](https://vitejs.dev/) parse `<pug src="example.pug"></pug>` in your `index.html`. The rendered template replaces this tag with the compiled markup.
66

7-
> :information_source: **Vue** single file components don't require this plugin, adding [Pug](https://www.npmjs.com/package/pug) to the dependency list is enough. — aka `npm i -D pug`
7+
> :information_source: **Vue** single file components dont require this plugin, adding [Pug](https://www.npmjs.com/package/pug) to the dependency list is enough. — aka `npm i -D pug`
88
9-
- Handle self-closing tags.
10-
- Works with multiple `pug` tags.
11-
- Generated _TypeScript_ declarations.
12-
- Common JS module but tested as ES module.
13-
- Auto-reload browser when saving changes to `.pug` file.
9+
## Features (exists and awaiting)
10+
11+
- [x] CommonJS and ES module builds.
12+
- [x] Handle self-closing `pug` tags.
13+
- [x] Works with multiple `pug` tags.
14+
- [x] Generated _TypeScript_ declarations.
15+
- [x] Reload when saving changes on a `.pug` file.
16+
- [x] Support Pug local variables.
17+
- [ ] _Experimental hot module reloading functionality._
18+
- [ ] _Handle adding or removing files._
1419

1520
## Installation
1621

1722
Using npm:
1823

19-
```console
20-
npm install vite-plugin-pug --save-dev
24+
```sh
25+
$ npm install vite-plugin-pug --save-dev
2126
```
2227

2328
## Configuration
2429

2530
Create a `vite.config.js` [configuration file](https://vitejs.dev/config/) and import the plugin:
2631

2732
```js
33+
// vite.config.(js|ts)
2834
import { defineConfig } from "vite"
2935
import pugPlugin from "vite-plugin-pug"
3036

37+
const options = { pretty: true } // FIXME: pug pretty is deprecated!
38+
const locals = { name: "My Pug" }
39+
3140
export default defineConfig({
3241
plugins: [
33-
pugPlugin()
42+
pugPlugin(options, locals)
3443
]
3544
})
3645
```
3746

38-
### Plugin Options
47+
### Plugin Parameters
3948

40-
Accept [Pug options](https://pugjs.org/api/reference.html#options) as an optional parameter.
41-
42-
```js
43-
// vite.config.js
44-
pugPlugin({
45-
pretty: true
46-
})
47-
```
49+
| Name | Required | Description |
50+
| ------- | -------- | ------------------------------------------------------------------- |
51+
| options | optional | [Pug options](https://pugjs.org/api/reference.html#options) object. |
52+
| locals | optional | Data object with Pug locals. |
4853

4954
## Usage
5055

56+
### Simple
57+
5158
Create a template file.
5259

5360
```pug
@@ -56,24 +63,28 @@ h1 Hello World
5663
p I'm a cool Vite project!
5764
```
5865

59-
Embed `pug` tag with `file` attribute somewhere.
66+
Embed `pug` tag with `src` attribute somewhere.
6067

6168
```html
6269
<!-- index.html -->
6370
<html>
64-
<body>
65-
<pug file="index.pug" />
66-
<script type="module" src="/main.ts"></script>
67-
</body>
71+
<body>
72+
<pug src="index.pug" />
73+
<script type="module" src="/main.ts"></script>
74+
</body>
6875
</html>
6976
```
7077

7178
That's it.
7279

73-
> :bulb: Check out **an example** implementation [in this repository](https://github.com/SubZtep/css-tetris-3d).
80+
> :bulb: Check out **its starter** implementation [in this repository](https://github.com/SubZtep/css-tetris-3d).
81+
82+
## Example
83+
84+
Please find the [examples](examples/multipage) folder in this repository.
7485

7586
## Contribution
7687

77-
~~After Rollup~~ I started to use _**Vite**_ recently but this is not a reason to leave my beloved template format behind. Its [lack of active](https://github.com/marlonmarcello/vite-plugin-pug) Pug plugins made me make one quickly. It does the job to me, I will extend it when I need it. :suspect:
88+
~~After Rollup~~ I started to use _**Vite**_ recently but this is not a reason to leave my beloved template format behind. Its [lack of active](https://github.com/marlonmarcello/vite-plugin-pug) Pug plugins made me make one quickly. It does the job to me, I will extend it when I need it. :suspect:
7889

79-
If it doesn't match with your setup please [start a new discussion](https://github.com/SubZtep/vite-plugin-pug/discussions/new) about it, I'm interested to see other workflows. If something is simply not working, please [raise an issue](https://github.com/SubZtep/vite-plugin-pug/issues/new). **PRs certainly welcome!** (.❛ ᴗ ❛.)
90+
If it doesn't match with your setup please [start a new discussion](https://github.com/SubZtep/vite-plugin-pug/discussions/new) about it, I'm interested to see other workflows. If something is simply not working, please [raise an issue](https://github.com/SubZtep/vite-plugin-pug/issues/new). **PRs certainly welcome!** (.❛ ᴗ ❛.)

__tests__/index.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { pugs } from "../src/index"
2+
3+
describe("html parser", () => {
4+
const html = `<html>
5+
<pug>moo</pug>
6+
<pug file="inner.pug"/>
7+
<pug file="INner2.pug" />
8+
<pug file="./nested/inner3.pug"></pug>
9+
<pug src="nested/inner4.pug">hello</pug>
10+
<p>a</p>
11+
</html>`
12+
13+
test("warning for deprecated", () => {
14+
const spy = jest.spyOn(console, "warn").mockImplementation()
15+
pugs(html, _ => "", console)
16+
expect(console.warn).toHaveBeenCalledTimes(3)
17+
spy.mockRestore()
18+
})
19+
20+
test("find all source files", () => {
21+
let filenames: string[] = []
22+
pugs(html, filename => filenames.push(filename).toString())
23+
expect(filenames).toEqual(["inner.pug", "INner2.pug", "./nested/inner3.pug", "nested/inner4.pug"]
24+
)
25+
})
26+
27+
test("replace tags", () => {
28+
const parsed = pugs(html, _ => "")
29+
expect(parsed).toContain("<html>")
30+
expect(parsed.split("pug")).toHaveLength(3)
31+
})
32+
})

__tests__/transformer.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.

examples/multipage/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# :paw_prints:
2+
3+
A basic example of how to use this plugin with **multiple pages** including **Pug locals**.
4+
5+
Bootstrapped with the TypeScript variant of vanilla Vite template with the following command.
6+
7+
```sh
8+
$ npm init @vitejs/app
9+
```
10+
11+
To try out install dependencies and start the local server.
12+
13+
```sh
14+
$ npm i
15+
$ npm run dev
16+
```
17+
18+
Open up http://localhost:3000/ and close down when you finish. :mushroom:

examples/multipage/favicon.svg

Lines changed: 15 additions & 0 deletions
Loading

examples/multipage/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="favicon.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Vite App</title>
8+
</head>
9+
<body>
10+
<pug src="./index.pug"></pug>
11+
<div id="app"></div>
12+
<script type="module" src="/src/main.ts"></script>
13+
</body>
14+
</html>

examples/multipage/index.pug

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
include nested/index.pug
2+
3+
p
4+
a(href=baseUrl) simply nested
5+
=""
6+
a(href=baseUrl+"test.html") html link
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
html
2+
<pug src="./nested/index.pug"></pug>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
h1 hello #{hello}
2+
p nested pug
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test html

0 commit comments

Comments
 (0)