@vituum/vite-plugin-pug 0.1.0 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -2
- package/index.js +36 -19
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -4,9 +4,13 @@
|
|
|
4
4
|
# ⚡️🐕 VitePug
|
|
5
5
|
|
|
6
6
|
```js
|
|
7
|
+
import pug from '@vituum/vite-plugin-pug'
|
|
8
|
+
|
|
7
9
|
export default {
|
|
8
10
|
plugins: [
|
|
9
11
|
pug({
|
|
12
|
+
reload: true,
|
|
13
|
+
root: null,
|
|
10
14
|
filters: {},
|
|
11
15
|
data: '*.json',
|
|
12
16
|
globals: {
|
|
@@ -15,12 +19,17 @@ export default {
|
|
|
15
19
|
filetypes: {
|
|
16
20
|
html: /.(json.html|pug.json.html|pug.html)$/,
|
|
17
21
|
json: /.(json.pug.html)$/
|
|
18
|
-
}
|
|
22
|
+
},
|
|
23
|
+
pug: {}
|
|
19
24
|
})
|
|
20
25
|
]
|
|
21
26
|
}
|
|
22
27
|
```
|
|
23
28
|
|
|
29
|
+
Read the [docs](https://vituum.dev/config/integrations-options.html#vituum-pug) to learn more about the plugin options.
|
|
30
|
+
|
|
31
|
+
## Basic usage
|
|
32
|
+
|
|
24
33
|
```html
|
|
25
34
|
<!-- index.html -->
|
|
26
35
|
<script type="application/json" data-format="pug">
|
|
@@ -33,7 +42,7 @@ export default {
|
|
|
33
42
|
or
|
|
34
43
|
```html
|
|
35
44
|
<!-- index.pug.html -->
|
|
36
|
-
|
|
45
|
+
include /path/to/template.pug
|
|
37
46
|
```
|
|
38
47
|
or
|
|
39
48
|
```html
|
|
@@ -47,3 +56,4 @@ or
|
|
|
47
56
|
### Requirements
|
|
48
57
|
|
|
49
58
|
- [Node.js LTS (16.x)](https://nodejs.org/en/download/)
|
|
59
|
+
- [Vite](https://vitejs.dev/) or [Vituum](https://vituum.dev/)
|
package/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { dirname,
|
|
1
|
+
import { dirname, resolve, relative } from 'path'
|
|
2
2
|
import fs from 'fs'
|
|
3
3
|
import process from 'node:process'
|
|
4
4
|
import FastGlob from 'fast-glob'
|
|
@@ -9,6 +9,8 @@ import { fileURLToPath } from 'url'
|
|
|
9
9
|
|
|
10
10
|
const { name } = JSON.parse(fs.readFileSync(resolve(dirname((fileURLToPath(import.meta.url))), 'package.json')).toString())
|
|
11
11
|
const defaultOptions = {
|
|
12
|
+
reload: true,
|
|
13
|
+
root: null,
|
|
12
14
|
filters: {},
|
|
13
15
|
globals: {},
|
|
14
16
|
data: '',
|
|
@@ -16,7 +18,7 @@ const defaultOptions = {
|
|
|
16
18
|
html: /.(json.html|pug.json.html|pug.html)$/,
|
|
17
19
|
json: /.(json.pug.html)$/
|
|
18
20
|
},
|
|
19
|
-
|
|
21
|
+
pug: {}
|
|
20
22
|
}
|
|
21
23
|
|
|
22
24
|
function processData(paths, data = {}) {
|
|
@@ -35,24 +37,29 @@ function processData(paths, data = {}) {
|
|
|
35
37
|
|
|
36
38
|
const renderTemplate = async(filename, content, options) => {
|
|
37
39
|
const output = {}
|
|
38
|
-
const context = processData(options.data, options.globals)
|
|
39
|
-
let isTemplate = false
|
|
40
|
+
const context = options.data ? processData(options.data, options.globals) : options.globals
|
|
40
41
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
filename.endsWith('.json')
|
|
44
|
-
) {
|
|
45
|
-
lodash.merge(context, JSON.parse(fs.readFileSync(filename).toString()))
|
|
42
|
+
const isJson = filename.endsWith('.json.html') || filename.endsWith('.json')
|
|
43
|
+
const isHtml = filename.endsWith('.html') && !options.filetypes.html.test(filename) && !options.filetypes.json.test(filename) && !isJson
|
|
46
44
|
|
|
47
|
-
|
|
45
|
+
if (isJson || isHtml) {
|
|
46
|
+
lodash.merge(context, isHtml ? content : JSON.parse(fs.readFileSync(filename).toString()))
|
|
48
47
|
|
|
49
|
-
|
|
48
|
+
output.template = true
|
|
49
|
+
|
|
50
|
+
if (typeof context.template === 'undefined') {
|
|
51
|
+
console.error(chalk.red(name + ' template must be defined - ' + filename))
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
context.template = relative(process.cwd(), context.template).startsWith(relative(process.cwd(), options.root))
|
|
55
|
+
? resolve(process.cwd(), context.template) : resolve(options.root, context.template)
|
|
50
56
|
} else if (fs.existsSync(filename + '.json')) {
|
|
51
57
|
lodash.merge(context, JSON.parse(fs.readFileSync(filename + '.json').toString()))
|
|
52
58
|
}
|
|
53
59
|
|
|
54
60
|
try {
|
|
55
|
-
const template = pug.compileFile(
|
|
61
|
+
const template = pug.compileFile(output.template ? context.template : filename, Object.assign(options.pug, {
|
|
62
|
+
basedir: options.root,
|
|
56
63
|
filters: options.filters
|
|
57
64
|
}))
|
|
58
65
|
|
|
@@ -70,21 +77,30 @@ const plugin = (options = {}) => {
|
|
|
70
77
|
return {
|
|
71
78
|
name,
|
|
72
79
|
config: ({ root }) => {
|
|
73
|
-
options.root
|
|
80
|
+
if (!options.root) {
|
|
81
|
+
options.root = root
|
|
82
|
+
}
|
|
74
83
|
},
|
|
75
84
|
transformIndexHtml: {
|
|
76
85
|
enforce: 'pre',
|
|
77
86
|
async transform(content, { path, filename, server }) {
|
|
87
|
+
path = path.replace('?raw', '')
|
|
88
|
+
filename = filename.replace('?raw', '')
|
|
89
|
+
|
|
78
90
|
if (
|
|
79
91
|
!options.filetypes.html.test(path) &&
|
|
80
92
|
!options.filetypes.json.test(path) &&
|
|
81
|
-
!content.startsWith('<script type="application/json"')
|
|
93
|
+
!content.startsWith('<script type="application/json" data-format="pug"')
|
|
82
94
|
) {
|
|
83
95
|
return content
|
|
84
96
|
}
|
|
85
97
|
|
|
86
|
-
if (content.startsWith('<script type="application/json"
|
|
87
|
-
|
|
98
|
+
if (content.startsWith('<script type="application/json" data-format="pug"')) {
|
|
99
|
+
const matches = content.matchAll(/<script\b[^>]*data-format="(?<format>[^>]+)"[^>]*>(?<data>[\s\S]+?)<\/script>/gmi)
|
|
100
|
+
|
|
101
|
+
for (const match of matches) {
|
|
102
|
+
content = JSON.parse(match.groups.data)
|
|
103
|
+
}
|
|
88
104
|
}
|
|
89
105
|
|
|
90
106
|
const render = await renderTemplate(filename, content, options)
|
|
@@ -102,15 +118,16 @@ const plugin = (options = {}) => {
|
|
|
102
118
|
plugin: name
|
|
103
119
|
}
|
|
104
120
|
})
|
|
105
|
-
|
|
106
|
-
return '<html style="background: #222"><head></head><body></body></html>'
|
|
107
121
|
}
|
|
108
122
|
|
|
109
123
|
return render.content
|
|
110
124
|
}
|
|
111
125
|
},
|
|
112
126
|
handleHotUpdate({ file, server }) {
|
|
113
|
-
if (
|
|
127
|
+
if (
|
|
128
|
+
(typeof options.reload === 'function' && options.reload(file)) ||
|
|
129
|
+
(options.reload && (options.filetypes.html.test(file) || options.filetypes.json.test(file)))
|
|
130
|
+
) {
|
|
114
131
|
server.ws.send({ type: 'full-reload' })
|
|
115
132
|
}
|
|
116
133
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vituum/vite-plugin-pug",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"dependencies": {
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"fast-glob": "^3.2.11"
|
|
11
11
|
},
|
|
12
12
|
"devDependencies": {
|
|
13
|
-
"eslint": "^8.
|
|
13
|
+
"eslint": "^8.23.0",
|
|
14
14
|
"eslint-config-standard": "^17.0.0"
|
|
15
15
|
},
|
|
16
16
|
"files": [
|