@vituum/vite-plugin-pug 1.0.0-alpha.2 → 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.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/index.js +19 -37
  3. package/package.json +2 -3
package/README.md CHANGED
@@ -19,7 +19,7 @@ export default {
19
19
  ```
20
20
 
21
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 full functionality.
22
+ * Use with [Vituum](https://vituum.dev) to get multi-page support.
23
23
 
24
24
  ## Basic usage
25
25
 
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
 
@@ -25,13 +31,13 @@ const defaultOptions = {
25
31
  options: {}
26
32
  }
27
33
 
28
- const renderTemplate = async ({ filename, server, root }, content, options) => {
34
+ const renderTemplate = async ({ filename, server, resolvedConfig }, content, options) => {
29
35
  const initialFilename = filename.replace('.html', '')
30
36
  const output = {}
31
37
  const context = options.data
32
38
  ? processData({
33
39
  paths: options.data,
34
- root
40
+ root: resolvedConfig.root
35
41
  }, options.globals)
36
42
  : options.globals
37
43
 
@@ -49,7 +55,8 @@ const renderTemplate = async ({ filename, server, root }, content, options) => {
49
55
  })
50
56
  }
51
57
 
52
- 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)
53
60
  } else if (fs.existsSync(`${initialFilename}.json`)) {
54
61
  lodash.merge(context, JSON.parse(fs.readFileSync(`${initialFilename}.json`).toString()))
55
62
  }
@@ -92,17 +99,19 @@ const plugin = (options = {}) => {
92
99
 
93
100
  if (!options.root) {
94
101
  options.root = config.root
102
+ } else {
103
+ options.root = normalizePath(options.root)
95
104
  }
96
105
  },
97
106
  buildStart: async () => {
98
- if (userEnv.command !== 'build') {
107
+ if (userEnv.command !== 'build' || !resolvedConfig.build.rollupOptions.input) {
99
108
  return
100
109
  }
101
110
 
102
111
  await renameBuildStart(resolvedConfig.build.rollupOptions.input, options.formats)
103
112
  },
104
113
  buildEnd: async () => {
105
- if (userEnv.command !== 'build') {
114
+ if (userEnv.command !== 'build' || !resolvedConfig.build.rollupOptions.input) {
106
115
  return
107
116
  }
108
117
 
@@ -111,38 +120,11 @@ const plugin = (options = {}) => {
111
120
  transformIndexHtml: {
112
121
  order: 'pre',
113
122
  async transform (content, { path, filename, server }) {
114
- if (
115
- !options.formats.find(format => filename.replace('.html', '').endsWith(format)) ||
116
- (filename.replace('.html', '').endsWith('.json') && !content.startsWith('{'))
117
- ) {
118
- return content
119
- }
120
-
121
- if (
122
- (filename.replace('.html', '').endsWith('.json') && content.startsWith('{')) &&
123
- (JSON.parse(content)?.format && !options.formats.includes(JSON.parse(content)?.format))
124
- ) {
125
- return content
126
- }
127
-
128
- if (options.ignoredPaths.find(ignoredPath => minimatch(path.replace('.html', ''), ignoredPath) === true)) {
129
- return content
130
- }
131
-
132
- const render = await renderTemplate({ filename, server, root: resolvedConfig.root }, content, options)
133
- const renderError = pluginError(render.error, server, name)
134
-
135
- if (renderError && server) {
136
- return
137
- } else if (renderError) {
138
- return renderError
139
- }
140
-
141
- return render.content
123
+ return pluginTransform(content, { path, filename, server }, { name, options, resolvedConfig, renderTemplate })
142
124
  }
143
125
  },
144
126
  handleHotUpdate: ({ file, server }) => pluginReload({ file, server }, options)
145
- }, pluginBundle(options.formats)]
127
+ }, pluginBundle(options.formats), pluginMiddleware(name, options.formats)]
146
128
  }
147
129
 
148
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.2",
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",