@vituum/vite-plugin-pug 1.0.0-alpha.1 → 1.0.0-beta.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.
package/README.md CHANGED
@@ -7,46 +7,29 @@
7
7
  import pug from '@vituum/vite-plugin-pug'
8
8
 
9
9
  export default {
10
- plugins: [
11
- pug({
12
- reload: true,
13
- root: null,
14
- filters: {},
15
- data: '*.json',
16
- globals: {
17
- template: 'path/to/template.pug'
18
- },
19
- filetypes: {
20
- html: /.(json.html|pug.json.html|pug.html)$/,
21
- json: /.(json.pug.html)$/
22
- },
23
- pug: {}
24
- })
25
- ]
10
+ plugins: [
11
+ pug()
12
+ ],
13
+ build: {
14
+ rollupOptions: {
15
+ input: ['index.pug.html']
16
+ }
17
+ }
26
18
  }
27
19
  ```
28
20
 
29
- Read the [docs](https://vituum.dev/config/integrations-options.html#vituum-pug) to learn more about the plugin options.
21
+ * Read the [docs](https://vituum.dev/plugins/pug.html) to learn more about the plugin options.
22
+ * Use with [Vituum](https://vituum.dev) to get multi-page support.
30
23
 
31
24
  ## Basic usage
32
25
 
33
26
  ```html
34
- <!-- index.html -->
35
- <script type="application/json" data-format="pug">
36
- {
37
- "template": "path/to/template.pug",
38
- "title": "Hello world"
39
- }
40
- </script>
41
- ```
42
- or
43
- ```html
44
- <!-- index.pug.html -->
27
+ <!-- index.pug -->
45
28
  include /path/to/template.pug
46
29
  ```
47
30
  or
48
31
  ```html
49
- <!-- index.json.html or index.pug.json.html -->
32
+ <!-- index.json -->
50
33
  {
51
34
  "template": "path/to/template.pug",
52
35
  "title": "Hello world"
@@ -55,5 +38,5 @@ or
55
38
 
56
39
  ### Requirements
57
40
 
58
- - [Node.js LTS (16.x)](https://nodejs.org/en/download/)
59
- - [Vite](https://vitejs.dev/) or [Vituum](https://vituum.dev/)
41
+ - [Node.js LTS (18.x)](https://nodejs.org/en/download/)
42
+ - [Vite](https://vitejs.dev/)
package/index.js CHANGED
@@ -1,11 +1,17 @@
1
1
  import { resolve, relative } from 'path'
2
2
  import fs from 'fs'
3
- import process from 'node:process'
4
3
  import lodash from 'lodash'
5
4
  import pug from 'pug'
6
- import { getPackageInfo, merge, pluginBundle, pluginError, pluginReload, processData } from 'vituum/utils/common.js'
5
+ import {
6
+ getPackageInfo,
7
+ merge, normalizePath,
8
+ pluginBundle,
9
+ pluginMiddleware,
10
+ pluginReload,
11
+ pluginTransform,
12
+ processData
13
+ } from 'vituum/utils/common.js'
7
14
  import { renameBuildEnd, renameBuildStart } from 'vituum/utils/build.js'
8
- import { minimatch } from 'minimatch'
9
15
 
10
16
  const { name } = getPackageInfo(import.meta.url)
11
17
 
@@ -21,16 +27,17 @@ const defaultOptions = {
21
27
  },
22
28
  data: ['src/data/**/*.json'],
23
29
  formats: ['pug', 'json.pug', 'json'],
30
+ ignoredPaths: [],
24
31
  options: {}
25
32
  }
26
33
 
27
- const renderTemplate = async ({ filename, server, root }, content, options) => {
34
+ const renderTemplate = async ({ filename, server, resolvedConfig }, content, options) => {
28
35
  const initialFilename = filename.replace('.html', '')
29
36
  const output = {}
30
37
  const context = options.data
31
38
  ? processData({
32
39
  paths: options.data,
33
- root
40
+ root: resolvedConfig.root
34
41
  }, options.globals)
35
42
  : options.globals
36
43
 
@@ -48,14 +55,15 @@ const renderTemplate = async ({ filename, server, root }, content, options) => {
48
55
  })
49
56
  }
50
57
 
51
- context.template = relative(process.cwd(), context.template).startsWith(relative(process.cwd(), options.root)) ? resolve(process.cwd(), context.template) : resolve(options.root, context.template)
58
+ context.template = normalizePath(context.template)
59
+ context.template = relative(resolvedConfig.root, context.template).startsWith(relative(resolvedConfig.root, options.root)) ? resolve(resolvedConfig.root, context.template) : resolve(options.root, context.template)
52
60
  } else if (fs.existsSync(`${initialFilename}.json`)) {
53
61
  lodash.merge(context, JSON.parse(fs.readFileSync(`${initialFilename}.json`).toString()))
54
62
  }
55
63
 
56
64
  return new Promise((resolve) => {
57
65
  try {
58
- const template = pug.compileFile(output.template ? context.template : filename, Object.assign(options.options, {
66
+ const template = pug.compileFile(output.template ? context.template : server ? initialFilename : filename, Object.assign(options.options, {
59
67
  basedir: options.root,
60
68
  filters: options.filters
61
69
  }))
@@ -91,17 +99,19 @@ const plugin = (options = {}) => {
91
99
 
92
100
  if (!options.root) {
93
101
  options.root = config.root
102
+ } else {
103
+ options.root = normalizePath(options.root)
94
104
  }
95
105
  },
96
106
  buildStart: async () => {
97
- if (userEnv.command !== 'build') {
107
+ if (userEnv.command !== 'build' || !resolvedConfig.build.rollupOptions.input) {
98
108
  return
99
109
  }
100
110
 
101
111
  await renameBuildStart(resolvedConfig.build.rollupOptions.input, options.formats)
102
112
  },
103
113
  buildEnd: async () => {
104
- if (userEnv.command !== 'build') {
114
+ if (userEnv.command !== 'build' || !resolvedConfig.build.rollupOptions.input) {
105
115
  return
106
116
  }
107
117
 
@@ -110,38 +120,11 @@ const plugin = (options = {}) => {
110
120
  transformIndexHtml: {
111
121
  order: 'pre',
112
122
  async transform (content, { path, filename, server }) {
113
- if (
114
- !options.formats.find(format => filename.replace('.html', '').endsWith(format)) ||
115
- (filename.replace('.html', '').endsWith('.json') && !content.startsWith('{'))
116
- ) {
117
- return content
118
- }
119
-
120
- if (
121
- (filename.replace('.html', '').endsWith('.json') && content.startsWith('{')) &&
122
- (JSON.parse(content)?.format && !options.formats.includes(JSON.parse(content)?.format))
123
- ) {
124
- return content
125
- }
126
-
127
- if (options.ignoredPaths.find(ignoredPath => minimatch(path.replace('.html', ''), ignoredPath) === true)) {
128
- return content
129
- }
130
-
131
- const render = await renderTemplate({ filename, server, root: resolvedConfig.root }, content, options)
132
- const renderError = pluginError(render.error, server, name)
133
-
134
- if (renderError && server) {
135
- return
136
- } else if (renderError) {
137
- return renderError
138
- }
139
-
140
- return render.content
123
+ return pluginTransform(content, { path, filename, server }, { name, options, resolvedConfig, renderTemplate })
141
124
  }
142
125
  },
143
126
  handleHotUpdate: ({ file, server }) => pluginReload({ file, server }, options)
144
- }, pluginBundle(options.formats)]
127
+ }, pluginBundle(options.formats), pluginMiddleware(name, options.formats)]
145
128
  }
146
129
 
147
130
  export default plugin
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vituum/vite-plugin-pug",
3
- "version": "1.0.0-alpha.1",
3
+ "version": "1.0.0-beta.1",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -11,8 +11,7 @@
11
11
  "pug": "^3.0.2",
12
12
  "lodash": "^4.17.21",
13
13
  "fast-glob": "^3.2.12",
14
- "vituum": "^1.0.0-alpha.23",
15
- "minimatch": "^9.0.1"
14
+ "vituum": "^1.0.0-beta.3"
16
15
  },
17
16
  "devDependencies": {
18
17
  "@types/node": "^20.3.1",
package/types/index.d.ts CHANGED
@@ -32,6 +32,6 @@ export interface PluginUserConfig {
32
32
  globals?: Object
33
33
  data?: string | string[]
34
34
  formats?: string[]
35
- options?: PugOptions
36
35
  ignoredPaths?: string[]
36
+ options?: PugOptions
37
37
  }