@vituum/vite-plugin-pug 1.0.0 → 2.0.0-next.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 +2 -2
  2. package/index.js +98 -96
  3. package/package.json +18 -15
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/)
42
- - [Vite](https://vitejs.dev/)
41
+ - [Node.js LTS (24.x)](https://nodejs.org/en/download/)
42
+ - [Vite (8.x)](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,63 +19,65 @@ 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
  /**
@@ -83,45 +85,45 @@ const renderTemplate = async ({ filename, server, resolvedConfig }, content, opt
83
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 transform (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
+ async handler(content, { path, filename, server }) {
122
+ return pluginTransform(content, { path, filename, server }, { name, options, resolvedConfig, renderTemplate })
123
+ },
124
+ },
125
+ handleHotUpdate: ({ file, server }) => pluginReload({ file, server }, options),
126
+ }, pluginBundle(options.formats), pluginMiddleware(name, options.formats)]
125
127
  }
126
128
 
127
129
  export default plugin
package/package.json CHANGED
@@ -1,37 +1,40 @@
1
1
  {
2
2
  "name": "@vituum/vite-plugin-pug",
3
- "version": "1.0.0",
3
+ "version": "2.0.0-next.1",
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
- "pug": "^3.0.2",
13
- "lodash": "^4.17.21",
14
- "fast-glob": "^3.2.12",
15
- "vituum": "^1.0.0"
14
+ "pug": "^3.0",
15
+ "vituum": "^2.0.0-next.9"
16
16
  },
17
17
  "devDependencies": {
18
- "@types/node": "^20.3.2",
19
- "eslint": "^8.43.0",
20
- "eslint-config-standard": "^17.1.0",
21
- "typescript": "^5.1.3",
22
- "vite": "^4.3.9"
18
+ "@eslint/js": "^9.39",
19
+ "@stylistic/eslint-plugin": "^5.7",
20
+ "@types/node": "^25.0",
21
+ "eslint": "^9.39",
22
+ "globals": "^17.0",
23
+ "typescript": "^5.9",
24
+ "vite": "^8.0.0-beta.8"
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": ">=16.0.0",
34
- "npm": ">=9.0.0"
37
+ "node": "^24.0.0 || >=25.0.0"
35
38
  },
36
39
  "repository": {
37
40
  "type": "git",