@vituum/vite-plugin-pug 1.1.0 → 2.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
@@ -38,5 +38,5 @@ or
38
38
 
39
39
  ### Requirements
40
40
 
41
- - [Node.js LTS (16.x)](https://nodejs.org/en/download/)
41
+ - [Node.js LTS (20.x)](https://nodejs.org/en/download/)
42
42
  - [Vite](https://vitejs.dev/)
package/index.js CHANGED
@@ -1,16 +1,16 @@
1
1
  import { resolve, relative } from 'path'
2
2
  import fs from 'fs'
3
- import lodash from 'lodash'
4
3
  import pug from 'pug'
5
4
  import {
6
- getPackageInfo,
7
- merge,
8
- pluginBundle,
9
- pluginMiddleware,
10
- pluginReload,
11
- pluginTransform,
12
- processData
5
+ getPackageInfo,
6
+ deepMergeWith,
7
+ pluginBundle,
8
+ pluginMiddleware,
9
+ pluginReload,
10
+ pluginTransform,
11
+ processData,
13
12
  } from 'vituum/utils/common.js'
13
+ import { merge } from 'vituum/utils/merge.js'
14
14
  import { renameBuildEnd, renameBuildStart } from 'vituum/utils/build.js'
15
15
 
16
16
  const { name } = getPackageInfo(import.meta.url)
@@ -19,109 +19,112 @@ const { name } = getPackageInfo(import.meta.url)
19
19
  * @type {import('@vituum/vite-plugin-pug/types').PluginUserConfig}
20
20
  */
21
21
  const defaultOptions = {
22
- reload: true,
23
- root: null,
24
- filters: {},
25
- globals: {
26
- format: 'pug'
27
- },
28
- data: ['src/data/**/*.json'],
29
- formats: ['pug', 'json.pug', 'json'],
30
- ignoredPaths: [],
31
- options: {}
22
+ reload: true,
23
+ root: null,
24
+ filters: {},
25
+ globals: {
26
+ format: 'pug',
27
+ },
28
+ data: ['src/data/**/*.json'],
29
+ formats: ['pug', 'json.pug', 'json'],
30
+ ignoredPaths: [],
31
+ options: {},
32
32
  }
33
33
 
34
34
  const renderTemplate = async ({ filename, server, resolvedConfig }, content, options) => {
35
- const initialFilename = filename.replace('.html', '')
36
- const output = {}
37
- const context = options.data
38
- ? processData({
39
- paths: options.data,
40
- root: resolvedConfig.root
41
- }, options.globals)
42
- : options.globals
43
-
44
- if (initialFilename.endsWith('.json')) {
45
- lodash.merge(context, JSON.parse(content))
46
-
47
- output.template = true
48
-
49
- if (typeof context.template === 'undefined') {
50
- const error = `${name}: template must be defined for file ${initialFilename}`
51
-
52
- return new Promise((resolve) => {
53
- output.error = error
54
- resolve(output)
55
- })
56
- }
57
-
58
- context.template = relative(resolvedConfig.root, context.template).startsWith(relative(resolvedConfig.root, options.root)) ? resolve(resolvedConfig.root, context.template) : resolve(options.root, context.template)
59
- } else if (fs.existsSync(`${initialFilename}.json`)) {
60
- lodash.merge(context, JSON.parse(fs.readFileSync(`${initialFilename}.json`).toString()))
35
+ const initialFilename = filename.replace('.html', '')
36
+ const output = {}
37
+ const context = options.data
38
+ ? processData({
39
+ paths: options.data,
40
+ root: resolvedConfig.root,
41
+ }, options.globals)
42
+ : options.globals
43
+
44
+ if (initialFilename.endsWith('.json')) {
45
+ merge(context, JSON.parse(content))
46
+
47
+ output.template = true
48
+
49
+ if (typeof context.template === 'undefined') {
50
+ const error = `${name}: template must be defined for file ${initialFilename}`
51
+
52
+ return new Promise((resolve) => {
53
+ output.error = error
54
+ resolve(output)
55
+ })
61
56
  }
62
57
 
63
- return new Promise((resolve) => {
64
- try {
65
- const template = pug.compileFile(output.template ? context.template : server ? initialFilename : filename, Object.assign(options.options, {
66
- basedir: options.root,
67
- filters: options.filters
68
- }))
58
+ context.template = relative(resolvedConfig.root, context.template).startsWith(relative(resolvedConfig.root, options.root)) ? resolve(resolvedConfig.root, context.template) : resolve(options.root, context.template)
59
+ }
60
+ else if (fs.existsSync(`${initialFilename}.json`)) {
61
+ merge(context, JSON.parse(fs.readFileSync(`${initialFilename}.json`).toString()))
62
+ }
63
+
64
+ return new Promise((resolve) => {
65
+ try {
66
+ const template = pug.compileFile(output.template ? context.template : server ? initialFilename : filename, Object.assign(options.options, {
67
+ basedir: options.root,
68
+ filters: options.filters,
69
+ }))
69
70
 
70
- output.content = template(context)
71
+ output.content = template(context)
71
72
 
72
- resolve(output)
73
- } catch (error) {
74
- output.error = error
73
+ resolve(output)
74
+ }
75
+ catch (error) {
76
+ output.error = error
75
77
 
76
- resolve(output)
77
- }
78
- })
78
+ resolve(output)
79
+ }
80
+ })
79
81
  }
80
82
 
81
83
  /**
82
84
  * @param {import('@vituum/vite-plugin-pug/types').PluginUserConfig} options
83
- * @returns [import('vite').Plugin]
85
+ * @returns {import('vite').Plugin[]}
84
86
  */
85
87
  const plugin = (options = {}) => {
86
- let resolvedConfig
87
- let userEnv
88
-
89
- options = merge(defaultOptions, options)
90
-
91
- return [{
92
- name,
93
- config (userConfig, env) {
94
- userEnv = env
95
- },
96
- configResolved (config) {
97
- resolvedConfig = config
98
-
99
- if (!options.root) {
100
- options.root = config.root
101
- }
102
- },
103
- buildStart: async () => {
104
- if (userEnv.command !== 'build' || !resolvedConfig.build.rollupOptions.input) {
105
- return
106
- }
107
-
108
- await renameBuildStart(resolvedConfig.build.rollupOptions.input, options.formats)
109
- },
110
- buildEnd: async () => {
111
- if (userEnv.command !== 'build' || !resolvedConfig.build.rollupOptions.input) {
112
- return
113
- }
114
-
115
- await renameBuildEnd(resolvedConfig.build.rollupOptions.input, options.formats)
116
- },
117
- transformIndexHtml: {
118
- order: 'pre',
119
- async handler (content, { path, filename, server }) {
120
- return pluginTransform(content, { path, filename, server }, { name, options, resolvedConfig, renderTemplate })
121
- }
122
- },
123
- handleHotUpdate: ({ file, server }) => pluginReload({ file, server }, options)
124
- }, pluginBundle(options.formats), pluginMiddleware(name, options.formats)]
88
+ let resolvedConfig
89
+ let userEnv
90
+
91
+ options = deepMergeWith(defaultOptions, options)
92
+
93
+ return [{
94
+ name,
95
+ config(userConfig, env) {
96
+ userEnv = env
97
+ },
98
+ configResolved(config) {
99
+ resolvedConfig = config
100
+
101
+ if (!options.root) {
102
+ options.root = config.root
103
+ }
104
+ },
105
+ buildStart: async () => {
106
+ if (userEnv.command !== 'build' || !resolvedConfig.build.rollupOptions.input) {
107
+ return
108
+ }
109
+
110
+ await renameBuildStart(resolvedConfig.build.rollupOptions.input, options.formats)
111
+ },
112
+ buildEnd: async () => {
113
+ if (userEnv.command !== 'build' || !resolvedConfig.build.rollupOptions.input) {
114
+ return
115
+ }
116
+
117
+ await renameBuildEnd(resolvedConfig.build.rollupOptions.input, options.formats)
118
+ },
119
+ transformIndexHtml: {
120
+ order: 'pre',
121
+ /** @returns {Promise<string | Object>} */
122
+ async handler(content, { path, filename, server }) {
123
+ return pluginTransform(content, { path, filename, server }, { name, options, resolvedConfig, renderTemplate })
124
+ },
125
+ },
126
+ handleHotUpdate: ({ file, server }) => pluginReload({ file, server }, options),
127
+ }, pluginBundle(options.formats), pluginMiddleware(name, options.formats)]
125
128
  }
126
129
 
127
130
  export default plugin
package/package.json CHANGED
@@ -1,36 +1,40 @@
1
1
  {
2
2
  "name": "@vituum/vite-plugin-pug",
3
- "version": "1.1.0",
3
+ "version": "2.0.0",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "types": "types/index.d.ts",
7
7
  "scripts": {
8
+ "lint": "npm run eslint && npm run tsc",
8
9
  "tsc": "tsc",
9
- "eslint": "eslint '**/*.js' --fix"
10
+ "eslint": "eslint '**/*.js' --fix",
11
+ "publish-next": "npm publish --tag next"
10
12
  },
11
13
  "dependencies": {
12
14
  "pug": "^3.0",
13
- "lodash": "^4.17",
14
- "fast-glob": "^3.3",
15
- "vituum": "^1.1"
15
+ "vituum": "^2.0"
16
16
  },
17
17
  "devDependencies": {
18
- "@types/node": "^20.9.1",
19
- "eslint": "^8.53.0",
20
- "eslint-config-standard": "^17.1.0",
21
- "typescript": "^5.2.2",
22
- "vite": "^5.0.0"
18
+ "@eslint/js": "^10.0",
19
+ "@stylistic/eslint-plugin": "^5.10",
20
+ "@types/node": "^25.5",
21
+ "eslint": "^10.0",
22
+ "globals": "^17.4",
23
+ "typescript": "^5.9",
24
+ "vite": "^8.0"
23
25
  },
24
26
  "files": [
25
27
  "index.js",
26
28
  "types"
27
29
  ],
28
30
  "exports": {
29
- ".": "./index.js",
30
- "./types": "./types/*"
31
+ ".": {
32
+ "default": "./index.js",
33
+ "types": "./types/index.d.ts"
34
+ }
31
35
  },
32
36
  "engines": {
33
- "node": "^18.0.0 || >=20.0.0"
37
+ "node": "^20.19.0 || >=22.12.0"
34
38
  },
35
39
  "repository": {
36
40
  "type": "git",
package/types/index.d.ts CHANGED
@@ -36,4 +36,4 @@ export interface PluginUserConfig {
36
36
  options?: PugOptions
37
37
  }
38
38
 
39
- export default function plugin(options?: PluginUserConfig) : import('vite').Plugin
39
+ export default function plugin(options?: PluginUserConfig) : import('vite').Plugin[]