@vituum/vite-plugin-pug 0.1.0 → 0.1.1

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 +29 -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,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
- compileOptions: {}
21
+ pug: {}
20
22
  }
21
23
 
22
24
  function processData(paths, data = {}) {
@@ -35,24 +37,25 @@ 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
- if (
42
- filename.endsWith('.json.html') ||
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') && !filename.endsWith('.json.html')
46
44
 
47
- isTemplate = true
45
+ if (isJson || isHtml) {
46
+ lodash.merge(context, isHtml ? content : JSON.parse(fs.readFileSync(filename).toString()))
48
47
 
49
- context.template = relative(process.cwd(), context.template)
48
+ output.template = true
49
+
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)
50
52
  } else if (fs.existsSync(filename + '.json')) {
51
53
  lodash.merge(context, JSON.parse(fs.readFileSync(filename + '.json').toString()))
52
54
  }
53
55
 
54
56
  try {
55
- const template = pug.compileFile(isTemplate ? context.template : filename, Object.assign(options.compileOptions, {
57
+ const template = pug.compileFile(output.template ? context.template : filename, Object.assign(options.pug, {
58
+ basedir: options.root,
56
59
  filters: options.filters
57
60
  }))
58
61
 
@@ -70,7 +73,9 @@ const plugin = (options = {}) => {
70
73
  return {
71
74
  name,
72
75
  config: ({ root }) => {
73
- options.root = root
76
+ if (!options.root) {
77
+ options.root = root
78
+ }
74
79
  },
75
80
  transformIndexHtml: {
76
81
  enforce: 'pre',
@@ -78,13 +83,17 @@ const plugin = (options = {}) => {
78
83
  if (
79
84
  !options.filetypes.html.test(path) &&
80
85
  !options.filetypes.json.test(path) &&
81
- !content.startsWith('<script type="application/json"')
86
+ !content.startsWith('<script type="application/json" data-format="pug"')
82
87
  ) {
83
88
  return content
84
89
  }
85
90
 
86
- if (content.startsWith('<script type="application/json"') && !content.includes('data-format="pug"')) {
87
- return content
91
+ if (content.startsWith('<script type="application/json" data-format="pug"')) {
92
+ const matches = content.matchAll(/<script\b[^>]*data-format="(?<format>[^>]+)"[^>]*>(?<data>[\s\S]+?)<\/script>/gmi)
93
+
94
+ for (const match of matches) {
95
+ content = JSON.parse(match.groups.data)
96
+ }
88
97
  }
89
98
 
90
99
  const render = await renderTemplate(filename, content, options)
@@ -102,15 +111,16 @@ const plugin = (options = {}) => {
102
111
  plugin: name
103
112
  }
104
113
  })
105
-
106
- return '<html style="background: #222"><head></head><body></body></html>'
107
114
  }
108
115
 
109
116
  return render.content
110
117
  }
111
118
  },
112
119
  handleHotUpdate({ file, server }) {
113
- if (extname(file) === '.pug' || extname(file) === '.html' || extname(file) === '.json') {
120
+ if (
121
+ typeof options.reload === 'function' && options.reload(file) ||
122
+ options.reload && (options.filetypes.html.test(file) || options.filetypes.json.test(file))
123
+ ) {
114
124
  server.ws.send({ type: 'full-reload' })
115
125
  }
116
126
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vituum/vite-plugin-pug",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
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": [