@vituum/vite-plugin-pug 1.0.0-alpha.2 → 1.0.0

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
@@ -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
 
@@ -38,5 +38,5 @@ or
38
38
 
39
39
  ### Requirements
40
40
 
41
- - [Node.js LTS (18.x)](https://nodejs.org/en/download/)
41
+ - [Node.js LTS (16.x)](https://nodejs.org/en/download/)
42
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,
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,7 @@ 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 = relative(resolvedConfig.root, context.template).startsWith(relative(resolvedConfig.root, options.root)) ? resolve(resolvedConfig.root, context.template) : resolve(options.root, context.template)
53
59
  } else if (fs.existsSync(`${initialFilename}.json`)) {
54
60
  lodash.merge(context, JSON.parse(fs.readFileSync(`${initialFilename}.json`).toString()))
55
61
  }
@@ -95,14 +101,14 @@ const plugin = (options = {}) => {
95
101
  }
96
102
  },
97
103
  buildStart: async () => {
98
- if (userEnv.command !== 'build') {
104
+ if (userEnv.command !== 'build' || !resolvedConfig.build.rollupOptions.input) {
99
105
  return
100
106
  }
101
107
 
102
108
  await renameBuildStart(resolvedConfig.build.rollupOptions.input, options.formats)
103
109
  },
104
110
  buildEnd: async () => {
105
- if (userEnv.command !== 'build') {
111
+ if (userEnv.command !== 'build' || !resolvedConfig.build.rollupOptions.input) {
106
112
  return
107
113
  }
108
114
 
@@ -111,38 +117,11 @@ const plugin = (options = {}) => {
111
117
  transformIndexHtml: {
112
118
  order: 'pre',
113
119
  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
120
+ return pluginTransform(content, { path, filename, server }, { name, options, resolvedConfig, renderTemplate })
142
121
  }
143
122
  },
144
123
  handleHotUpdate: ({ file, server }) => pluginReload({ file, server }, options)
145
- }, pluginBundle(options.formats)]
124
+ }, pluginBundle(options.formats), pluginMiddleware(name, options.formats)]
146
125
  }
147
126
 
148
127
  export default plugin
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@vituum/vite-plugin-pug",
3
- "version": "1.0.0-alpha.2",
3
+ "version": "1.0.0",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
+ "types": "types/index.d.ts",
6
7
  "scripts": {
7
8
  "tsc": "tsc",
8
9
  "eslint": "eslint '**/*.js' --fix"
@@ -11,11 +12,10 @@
11
12
  "pug": "^3.0.2",
12
13
  "lodash": "^4.17.21",
13
14
  "fast-glob": "^3.2.12",
14
- "vituum": "^1.0.0-alpha.23",
15
- "minimatch": "^9.0.1"
15
+ "vituum": "^1.0.0"
16
16
  },
17
17
  "devDependencies": {
18
- "@types/node": "^20.3.1",
18
+ "@types/node": "^20.3.2",
19
19
  "eslint": "^8.43.0",
20
20
  "eslint-config-standard": "^17.1.0",
21
21
  "typescript": "^5.1.3",
@@ -30,7 +30,7 @@
30
30
  "./types": "./types/*"
31
31
  },
32
32
  "engines": {
33
- "node": ">=18.0.0",
33
+ "node": ">=16.0.0",
34
34
  "npm": ">=9.0.0"
35
35
  },
36
36
  "repository": {
package/types/index.d.ts CHANGED
@@ -35,3 +35,5 @@ export interface PluginUserConfig {
35
35
  ignoredPaths?: string[]
36
36
  options?: PugOptions
37
37
  }
38
+
39
+ export default function plugin(options?: PluginUserConfig) : import('vite').Plugin