@vituum/vite-plugin-pug 0.0.1 → 0.1.2

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.
Files changed (3) hide show
  1. package/README.md +12 -2
  2. package/index.js +34 -19
  3. 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
- {{> "path/to/template.hbs"}}
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, extname, resolve, relative } from 'path'
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,13 +9,16 @@ 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: '',
15
17
  filetypes: {
16
18
  html: /.(json.html|pug.json.html|pug.html)$/,
17
19
  json: /.(json.pug.html)$/
18
- }
20
+ },
21
+ pug: {}
19
22
  }
20
23
 
21
24
  function processData(paths, data = {}) {
@@ -34,25 +37,27 @@ function processData(paths, data = {}) {
34
37
 
35
38
  const renderTemplate = async(filename, content, options) => {
36
39
  const output = {}
37
- const context = processData(options.data, options.globals)
38
- let isTemplate = false
40
+ const context = options.data ? processData(options.data, options.globals) : options.globals
41
+
42
+ const isJson = filename.endsWith('.json.html') || filename.endsWith('.json')
43
+ const isHtml = filename.endsWith('.html') && !filename.endsWith('.json.html')
44
+
45
+ if (isJson || isHtml) {
46
+ lodash.merge(context, isHtml ? content : JSON.parse(fs.readFileSync(filename).toString()))
39
47
 
40
- if (
41
- filename.endsWith('.json.html') ||
42
- filename.endsWith('.json')
43
- ) {
44
- lodash.merge(context, JSON.parse(fs.readFileSync(filename).toString()))
48
+ output.template = true
45
49
 
46
- context.template = relative(process.cwd(), context.template)
50
+ context.template = relative(process.cwd(), context.template).startsWith(relative(process.cwd(), options.root))
51
+ ? resolve(process.cwd(), context.template) : resolve(options.root, context.template)
47
52
  } else if (fs.existsSync(filename + '.json')) {
48
53
  lodash.merge(context, JSON.parse(fs.readFileSync(filename + '.json').toString()))
49
54
  }
50
55
 
51
56
  try {
52
- const template = pug.compileFile(isTemplate ? context.template : filename, {
57
+ const template = pug.compileFile(output.template ? context.template : filename, Object.assign(options.pug, {
53
58
  basedir: options.root,
54
59
  filters: options.filters
55
- })
60
+ }))
56
61
 
57
62
  output.content = template(context)
58
63
  } catch(error) {
@@ -68,21 +73,30 @@ const plugin = (options = {}) => {
68
73
  return {
69
74
  name,
70
75
  config: ({ root }) => {
71
- options.root = root
76
+ if (!options.root) {
77
+ options.root = root
78
+ }
72
79
  },
73
80
  transformIndexHtml: {
74
81
  enforce: 'pre',
75
82
  async transform(content, { path, filename, server }) {
83
+ path = path.replace('?raw', '')
84
+ filename = filename.replace('?raw', '')
85
+
76
86
  if (
77
87
  !options.filetypes.html.test(path) &&
78
88
  !options.filetypes.json.test(path) &&
79
- !content.startsWith('<script type="application/json"')
89
+ !content.startsWith('<script type="application/json" data-format="pug"')
80
90
  ) {
81
91
  return content
82
92
  }
83
93
 
84
- if (content.startsWith('<script type="application/json"') && !content.includes('data-format="pug"')) {
85
- return content
94
+ if (content.startsWith('<script type="application/json" data-format="pug"')) {
95
+ const matches = content.matchAll(/<script\b[^>]*data-format="(?<format>[^>]+)"[^>]*>(?<data>[\s\S]+?)<\/script>/gmi)
96
+
97
+ for (const match of matches) {
98
+ content = JSON.parse(match.groups.data)
99
+ }
86
100
  }
87
101
 
88
102
  const render = await renderTemplate(filename, content, options)
@@ -100,15 +114,16 @@ const plugin = (options = {}) => {
100
114
  plugin: name
101
115
  }
102
116
  })
103
-
104
- return '<html style="background: #222"><head></head><body></body></html>'
105
117
  }
106
118
 
107
119
  return render.content
108
120
  }
109
121
  },
110
122
  handleHotUpdate({ file, server }) {
111
- if (extname(file) === '.pug' || extname(file) === '.html' || extname(file) === '.json') {
123
+ if (
124
+ (typeof options.reload === 'function' && options.reload(file)) ||
125
+ (options.reload && (options.filetypes.html.test(file) || options.filetypes.json.test(file)))
126
+ ) {
112
127
  server.ws.send({ type: 'full-reload' })
113
128
  }
114
129
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vituum/vite-plugin-pug",
3
- "version": "0.0.1",
3
+ "version": "0.1.2",
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.21.0",
13
+ "eslint": "^8.23.0",
14
14
  "eslint-config-standard": "^17.0.0"
15
15
  },
16
16
  "files": [