-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
183 lines (153 loc) · 6.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { resolve, dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import fs from 'node:fs'
import process from 'node:process'
import * as childProcess from 'node:child_process'
import FastGlob from 'fast-glob'
import { minimatch } from 'minimatch'
import {
getPackageInfo,
pluginError,
pluginReload,
merge,
pluginBundle,
normalizePath,
pluginMiddleware
} from 'vituum/utils/common.js'
import { renameBuildEnd, renameBuildStart } from 'vituum/utils/build.js'
const { name } = getPackageInfo(import.meta.url)
/**
* @type {import('@vituum/vite-plugin-latte/types').PluginUserConfig} options
*/
const defaultOptions = {
reload: true,
root: null,
filters: {},
functions: {},
tags: {},
globals: {
format: 'latte'
},
data: ['src/data/**/*.json'],
formats: ['latte', 'json.latte', 'json'],
bin: 'php',
renderTransformedHtml: () => false,
ignoredPaths: []
}
const execSync = (cmd) => {
try {
return {
output: childProcess.execSync(cmd).toString()
}
} catch ({ output }) {
return {
error: true,
output: output[1].toString()
}
}
}
const renderTemplate = ({ server, path, filename, cwd, packageRoot }, options, content) => {
const renderTransformedHtml = options.renderTransformedHtml(server ? filename.replace('.html', '') : filename)
if (options.data) {
const normalizePaths = Array.isArray(options.data) ? options.data.map(path => normalizePath(path)) : normalizePath(options.data)
options.data = FastGlob.sync(normalizePaths).map(entry => resolve(cwd, entry))
}
Object.keys(options.filters).forEach(key => {
if (typeof options.filters[key] === 'function') {
options.filters[key] = options.filters[key].toString().match(/\(\s*([^)]+?)\s*\)/)[1].replace(/\s/g, '').split(',')
}
})
Object.keys(options.functions).forEach(key => {
if (typeof options.functions[key] === 'function') {
options.functions[key] = options.functions[key].toString().match(/\(\s*([^)]+?)\s*\)/)[1].replace(/\s/g, '').split(',')
}
})
if (renderTransformedHtml) {
const timestamp = Math.floor(Date.now() * Math.random())
options.contentTimestamp = timestamp
if (!fs.existsSync(resolve(packageRoot, 'temp'))) {
fs.mkdirSync(resolve(packageRoot, 'temp'))
}
fs.writeFileSync(resolve(packageRoot, `temp/${timestamp}.html`), content)
}
const data = Object.assign({ packageRoot, cwd, isRenderTransformedHtml: renderTransformedHtml }, options)
return execSync(`${options.bin} ${packageRoot}/index.php ${join(options.root, server ? path.replace('.html', '') : path)} ${JSON.stringify(JSON.stringify(data))}`)
}
/**
* @param {import('@vituum/vite-plugin-latte/types').PluginUserConfig} options
* @returns [import('vite').Plugin]
*/
const plugin = (options = {}) => {
let resolvedConfig
let userEnv
options = merge(defaultOptions, options)
const cwd = process.cwd()
const packageRoot = dirname((fileURLToPath(import.meta.url)))
if (fs.existsSync(resolve(packageRoot, 'temp'))) {
fs.rmSync(resolve(packageRoot, 'temp'), { recursive: true, force: true })
}
if (options.bin === 'docker') {
options.bin = `docker run --rm --name index -v "${process.cwd()}":/usr/src/app -w /usr/src/app php:8-cli php`
}
return [{
_options: options,
name,
config (userConfig, env) {
userEnv = env
},
configResolved (config) {
resolvedConfig = config
if (!options.root) {
options.root = config.root
} else {
options.root = normalizePath(options.root)
}
},
buildStart: async () => {
if (userEnv.command !== 'build' || !resolvedConfig.build.rollupOptions.input) {
return
}
await renameBuildStart(resolvedConfig.build.rollupOptions.input, options.formats)
},
buildEnd: async () => {
if (userEnv.command !== 'build' || !resolvedConfig.build.rollupOptions.input) {
return
}
await renameBuildEnd(resolvedConfig.build.rollupOptions.input, options.formats)
},
transformIndexHtml: {
order: 'pre',
async handler (content, { path, filename, server }) {
if (options.ignoredPaths.find(ignoredPath => minimatch(path.replace('.html', ''), ignoredPath) === true)) {
return content
}
if (
!options.formats.find(format => filename.replace('.html', '').endsWith(format)) ||
(filename.replace('.html', '').endsWith('.json') && !content.startsWith('{'))
) {
return content
}
if (
(filename.replace('.html', '').endsWith('.json') && content.startsWith('{')) &&
(JSON.parse(content)?.format && !options.formats.includes(JSON.parse(content)?.format))
) {
return content
}
const render = renderTemplate({ server, path, filename, cwd, packageRoot }, options, content)
const warningLog = render.output.includes('Warning: Undefined')
if (render.error || warningLog) {
const message = warningLog ? 'Warning: Undefined' + render.output.split('Warning: Undefined').pop() : render.output
const renderError = pluginError(message, server, name)
if (renderError && server) {
return
} else if (renderError) {
return renderError
}
}
return render.output
}
},
handleHotUpdate: ({ file, server }) => pluginReload({ file, server }, options)
}, pluginBundle(options.formats), pluginMiddleware(name, options.formats)]
}
export default plugin