@vituum/vite-plugin-pug 1.1.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.
- package/README.md +2 -2
- package/index.js +98 -96
- package/package.json +17 -13
package/README.md
CHANGED
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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
-
|
|
71
|
+
output.content = template(context)
|
|
71
72
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
resolve(output)
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
output.error = error
|
|
75
77
|
|
|
76
|
-
|
|
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
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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,36 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vituum/vite-plugin-pug",
|
|
3
|
-
"version": "
|
|
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
14
|
"pug": "^3.0",
|
|
13
|
-
"
|
|
14
|
-
"fast-glob": "^3.3",
|
|
15
|
-
"vituum": "^1.1"
|
|
15
|
+
"vituum": "^2.0.0-next.9"
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
|
-
"@
|
|
19
|
-
"eslint": "^
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
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
|
-
".":
|
|
30
|
-
|
|
31
|
+
".": {
|
|
32
|
+
"default": "./index.js",
|
|
33
|
+
"types": "./types/index.d.ts"
|
|
34
|
+
}
|
|
31
35
|
},
|
|
32
36
|
"engines": {
|
|
33
|
-
"node": "^
|
|
37
|
+
"node": "^24.0.0 || >=25.0.0"
|
|
34
38
|
},
|
|
35
39
|
"repository": {
|
|
36
40
|
"type": "git",
|