@vituum/vite-plugin-latte 1.4.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/handler-single.js CHANGED
@@ -1,3 +1,6 @@
1
+ import process from 'node:process'
2
+ import { Buffer } from 'node:buffer'
3
+
1
4
  let params = JSON.parse(process.argv[3])
2
5
  const path = process.argv[2]
3
6
 
@@ -6,5 +9,5 @@ params = params.map(value => Buffer.from(value, 'base64').toString('utf-8'))
6
9
  const output = await (await import([path])).default(...params)
7
10
 
8
11
  if (output) {
9
- console.log(Buffer.from(output.toString(), 'utf-8').toString('base64'))
12
+ console.log(Buffer.from(output.toString(), 'utf-8').toString('base64'))
10
13
  }
package/handler.js CHANGED
@@ -1,41 +1,44 @@
1
1
  import { resolve } from 'node:path'
2
2
  import fs from 'node:fs'
3
+ import process from 'node:process'
4
+ import { Buffer } from 'node:buffer'
3
5
 
4
- function find (obj) {
5
- if (typeof obj === 'object') {
6
- if (obj.name === '@vituum/vite-plugin-latte') {
7
- return obj
8
- } else {
9
- for (const key in obj) {
10
- const result = find(obj[key])
11
-
12
- if (result !== null) {
13
- return result
14
- }
15
- }
6
+ function find(obj) {
7
+ if (typeof obj === 'object') {
8
+ if (obj.name === '@vituum/vite-plugin-latte') {
9
+ return obj
10
+ }
11
+ else {
12
+ for (const key in obj) {
13
+ const result = find(obj[key])
14
+
15
+ if (result !== null) {
16
+ return result
16
17
  }
18
+ }
17
19
  }
20
+ }
18
21
 
19
- return null
22
+ return null
20
23
  }
21
24
 
22
25
  const DEFAULT_CONFIG_FILES = [
23
- 'vite.config.js',
24
- 'vite.config.mjs',
25
- 'vite.config.ts',
26
- 'vite.config.cjs',
27
- 'vite.config.mts',
28
- 'vite.config.cts'
26
+ 'vite.config.js',
27
+ 'vite.config.mjs',
28
+ 'vite.config.ts',
29
+ 'vite.config.cjs',
30
+ 'vite.config.mts',
31
+ 'vite.config.cts',
29
32
  ]
30
33
 
31
34
  let resolvedPath
32
35
 
33
36
  for (const filename of DEFAULT_CONFIG_FILES) {
34
- const filePath = resolve(process.cwd(), filename)
35
- if (!fs.existsSync(filePath)) continue
37
+ const filePath = resolve(process.cwd(), filename)
38
+ if (!fs.existsSync(filePath)) continue
36
39
 
37
- resolvedPath = filePath
38
- break
40
+ resolvedPath = filePath
41
+ break
39
42
  }
40
43
 
41
44
  const vite = (await import(resolvedPath)).default
@@ -48,5 +51,5 @@ params = params.map(value => Buffer.from(value, 'base64').toString('utf-8'))
48
51
  const output = await find(vite.plugins)._options[type][name](...params)
49
52
 
50
53
  if (output) {
51
- console.log(Buffer.from(output.toString(), 'utf-8').toString('base64'))
54
+ console.log(Buffer.from(output.toString(), 'utf-8').toString('base64'))
52
55
  }
package/index.js CHANGED
@@ -6,13 +6,13 @@ import * as childProcess from 'node:child_process'
6
6
  import FastGlob from 'fast-glob'
7
7
  import { minimatch } from 'minimatch'
8
8
  import {
9
- getPackageInfo,
10
- pluginError,
11
- pluginReload,
12
- merge,
13
- pluginBundle,
14
- normalizePath,
15
- pluginMiddleware
9
+ getPackageInfo,
10
+ pluginError,
11
+ pluginReload,
12
+ merge,
13
+ pluginBundle,
14
+ normalizePath,
15
+ pluginMiddleware,
16
16
  } from 'vituum/utils/common.js'
17
17
  import { renameBuildEnd, renameBuildStart } from 'vituum/utils/build.js'
18
18
 
@@ -22,70 +22,71 @@ const { name } = getPackageInfo(import.meta.url)
22
22
  * @type {import('@vituum/vite-plugin-latte/types').PluginUserConfig} options
23
23
  */
24
24
  const defaultOptions = {
25
- reload: true,
26
- root: null,
27
- filters: {},
28
- functions: {},
29
- tags: {},
30
- globals: {
31
- format: 'latte'
32
- },
33
- data: ['src/data/**/*.json'],
34
- formats: ['latte', 'json.latte', 'json'],
35
- bin: 'php',
36
- renderTransformedHtml: () => false,
37
- ignoredPaths: []
25
+ reload: true,
26
+ root: null,
27
+ filters: {},
28
+ functions: {},
29
+ tags: {},
30
+ globals: {
31
+ format: 'latte',
32
+ },
33
+ data: ['src/data/**/*.json'],
34
+ formats: ['latte', 'json.latte', 'json'],
35
+ bin: 'php',
36
+ renderTransformedHtml: () => false,
37
+ ignoredPaths: [],
38
38
  }
39
39
 
40
40
  const execSync = (cmd) => {
41
- try {
42
- return {
43
- output: childProcess.execSync(cmd).toString()
44
- }
45
- } catch ({ output }) {
46
- return {
47
- error: true,
48
- output: output[1].toString()
49
- }
41
+ try {
42
+ return {
43
+ output: childProcess.execSync(cmd).toString(),
44
+ }
45
+ }
46
+ catch ({ output }) {
47
+ return {
48
+ error: true,
49
+ output: output[1].toString(),
50
50
  }
51
+ }
51
52
  }
52
53
 
53
54
  const renderTemplate = ({ server, path, filename, cwd, packageRoot, viteServer }, options, content) => {
54
- const renderTransformedHtml = options.renderTransformedHtml(server ? filename.replace('.html', '') : filename)
55
+ const renderTransformedHtml = options.renderTransformedHtml(server ? filename.replace('.html', '') : filename)
55
56
 
56
- if (options.data) {
57
- const normalizePaths = Array.isArray(options.data) ? options.data.map(path => normalizePath(path)) : normalizePath(options.data)
57
+ if (options.data) {
58
+ const normalizePaths = Array.isArray(options.data) ? options.data.map(path => normalizePath(path)) : normalizePath(options.data)
58
59
 
59
- options.data = FastGlob.sync(normalizePaths).map(entry => resolve(cwd, entry))
60
- }
61
-
62
- Object.keys(options.filters).forEach(key => {
63
- if (typeof options.filters[key] === 'function') {
64
- options.filters[key] = options.filters[key].toString().match(/\(\s*([^)]+?)\s*\)/)[1].replace(/\s/g, '').split(',')
65
- }
66
- })
60
+ options.data = FastGlob.sync(normalizePaths).map(entry => resolve(cwd, entry))
61
+ }
67
62
 
68
- Object.keys(options.functions).forEach(key => {
69
- if (typeof options.functions[key] === 'function') {
70
- options.functions[key] = options.functions[key].toString().match(/\(\s*([^)]+?)\s*\)/)[1].replace(/\s/g, '').split(',')
71
- }
72
- })
63
+ Object.keys(options.filters).forEach((key) => {
64
+ if (typeof options.filters[key] === 'function') {
65
+ options.filters[key] = options.filters[key].toString().match(/\(\s*([^)]+?)\s*\)/)[1].replace(/\s/g, '').split(',')
66
+ }
67
+ })
73
68
 
74
- if (renderTransformedHtml) {
75
- const timestamp = Math.floor(Date.now() * Math.random())
69
+ Object.keys(options.functions).forEach((key) => {
70
+ if (typeof options.functions[key] === 'function') {
71
+ options.functions[key] = options.functions[key].toString().match(/\(\s*([^)]+?)\s*\)/)[1].replace(/\s/g, '').split(',')
72
+ }
73
+ })
76
74
 
77
- options.contentTimestamp = timestamp
75
+ if (renderTransformedHtml) {
76
+ const timestamp = Math.floor(Date.now() * Math.random())
78
77
 
79
- if (!fs.existsSync(resolve(packageRoot, 'temp'))) {
80
- fs.mkdirSync(resolve(packageRoot, 'temp'))
81
- }
78
+ options.contentTimestamp = timestamp
82
79
 
83
- fs.writeFileSync(resolve(packageRoot, `temp/${timestamp}.html`), content)
80
+ if (!fs.existsSync(resolve(packageRoot, 'temp'))) {
81
+ fs.mkdirSync(resolve(packageRoot, 'temp'))
84
82
  }
85
83
 
86
- const data = Object.assign({ packageRoot, cwd, isRenderTransformedHtml: renderTransformedHtml, viteServer }, options)
84
+ fs.writeFileSync(resolve(packageRoot, `temp/${timestamp}.html`), content)
85
+ }
86
+
87
+ const data = Object.assign({ packageRoot, cwd, isRenderTransformedHtml: renderTransformedHtml, viteServer }, options)
87
88
 
88
- return execSync(`${options.bin} ${packageRoot}/index.php ${join(options.root, server ? path.replace('.html', '') : path)} ${JSON.stringify(JSON.stringify(data))}`)
89
+ return execSync(`${options.bin} ${packageRoot}/index.php ${join(options.root, server ? path.replace('.html', '') : path)} ${JSON.stringify(JSON.stringify(data))}`)
89
90
  }
90
91
 
91
92
  /**
@@ -93,93 +94,95 @@ const renderTemplate = ({ server, path, filename, cwd, packageRoot, viteServer }
93
94
  * @returns [import('vite').Plugin]
94
95
  */
95
96
  const plugin = (options = {}) => {
96
- let resolvedConfig
97
- let userEnv
97
+ let resolvedConfig
98
+ let userEnv
98
99
 
99
- options = merge(defaultOptions, options)
100
+ options = merge(defaultOptions, options)
100
101
 
101
- const cwd = process.cwd()
102
- const packageRoot = dirname((fileURLToPath(import.meta.url)))
102
+ const cwd = process.cwd()
103
+ const packageRoot = dirname((fileURLToPath(import.meta.url)))
103
104
 
104
- if (fs.existsSync(resolve(packageRoot, 'temp'))) {
105
- fs.rmSync(resolve(packageRoot, 'temp'), { recursive: true, force: true })
106
- }
105
+ if (fs.existsSync(resolve(packageRoot, 'temp'))) {
106
+ fs.rmSync(resolve(packageRoot, 'temp'), { recursive: true, force: true })
107
+ }
107
108
 
108
- if (options.bin === 'docker') {
109
- options.bin = `docker run --rm --name index -v "${process.cwd()}":/usr/src/app -w /usr/src/app php:8-cli php`
110
- }
109
+ if (options.bin === 'docker') {
110
+ options.bin = `docker run --rm --name index -v "${process.cwd()}":/usr/src/app -w /usr/src/app php:8-cli php`
111
+ }
112
+
113
+ return [{
114
+ _options: options,
115
+ name,
116
+ config(userConfig, env) {
117
+ userEnv = env
118
+ },
119
+ configResolved(config) {
120
+ resolvedConfig = config
121
+
122
+ if (!options.root) {
123
+ options.root = config.root
124
+ }
125
+ else {
126
+ options.root = normalizePath(options.root)
127
+ }
128
+ },
129
+ buildStart: async () => {
130
+ if (userEnv.command !== 'build' || !resolvedConfig.build.rollupOptions.input) {
131
+ return
132
+ }
133
+
134
+ await renameBuildStart(resolvedConfig.build.rollupOptions.input, options.formats)
135
+ },
136
+ buildEnd: async () => {
137
+ if (userEnv.command !== 'build' || !resolvedConfig.build.rollupOptions.input) {
138
+ return
139
+ }
140
+
141
+ await renameBuildEnd(resolvedConfig.build.rollupOptions.input, options.formats)
142
+ },
143
+ transformIndexHtml: {
144
+ order: 'pre',
145
+ async handler(content, { path, filename, server }) {
146
+ if (options.ignoredPaths.find(ignoredPath => minimatch(path.replace('.html', ''), ignoredPath) === true)) {
147
+ return content
148
+ }
149
+
150
+ if (
151
+ !options.formats.find(format => filename.replace('.html', '').endsWith(format))
152
+ || (filename.replace('.html', '').endsWith('.json') && !content.startsWith('{'))
153
+ ) {
154
+ return content
155
+ }
111
156
 
112
- return [{
113
- _options: options,
114
- name,
115
- config (userConfig, env) {
116
- userEnv = env
117
- },
118
- configResolved (config) {
119
- resolvedConfig = config
120
-
121
- if (!options.root) {
122
- options.root = config.root
123
- } else {
124
- options.root = normalizePath(options.root)
125
- }
126
- },
127
- buildStart: async () => {
128
- if (userEnv.command !== 'build' || !resolvedConfig.build.rollupOptions.input) {
129
- return
130
- }
131
-
132
- await renameBuildStart(resolvedConfig.build.rollupOptions.input, options.formats)
133
- },
134
- buildEnd: async () => {
135
- if (userEnv.command !== 'build' || !resolvedConfig.build.rollupOptions.input) {
136
- return
137
- }
138
-
139
- await renameBuildEnd(resolvedConfig.build.rollupOptions.input, options.formats)
140
- },
141
- transformIndexHtml: {
142
- order: 'pre',
143
- async handler (content, { path, filename, server }) {
144
- if (options.ignoredPaths.find(ignoredPath => minimatch(path.replace('.html', ''), ignoredPath) === true)) {
145
- return content
146
- }
147
-
148
- if (
149
- !options.formats.find(format => filename.replace('.html', '').endsWith(format)) ||
150
- (filename.replace('.html', '').endsWith('.json') && !content.startsWith('{'))
151
- ) {
152
- return content
153
- }
154
-
155
- if (
156
- (filename.replace('.html', '').endsWith('.json') && content.startsWith('{')) &&
157
- (JSON.parse(content)?.format && !options.formats.includes(JSON.parse(content)?.format))
158
- ) {
159
- return content
160
- }
161
-
162
- const viteServer = !!server
163
-
164
- const render = renderTemplate({ server, path, filename, cwd, packageRoot, viteServer }, options, content)
165
- const warningLog = render.output.includes('Warning: Undefined')
166
-
167
- if (render.error || warningLog) {
168
- const message = warningLog ? 'Warning: Undefined' + render.output.split('Warning: Undefined').pop() : render.output
169
- const renderError = pluginError(message, server, name)
170
-
171
- if (renderError && server) {
172
- return
173
- } else if (renderError) {
174
- return renderError
175
- }
176
- }
177
-
178
- return render.output
179
- }
180
- },
181
- handleHotUpdate: ({ file, server }) => pluginReload({ file, server }, options)
182
- }, pluginBundle(options.formats), pluginMiddleware(name, options.formats)]
157
+ if (
158
+ (filename.replace('.html', '').endsWith('.json') && content.startsWith('{'))
159
+ && (JSON.parse(content)?.format && !options.formats.includes(JSON.parse(content)?.format))
160
+ ) {
161
+ return content
162
+ }
163
+
164
+ const viteServer = !!server
165
+
166
+ const render = renderTemplate({ server, path, filename, cwd, packageRoot, viteServer }, options, content)
167
+ const warningLog = render.output.includes('Warning: Undefined')
168
+
169
+ if (render.error || warningLog) {
170
+ const message = warningLog ? 'Warning: Undefined' + render.output.split('Warning: Undefined').pop() : render.output
171
+ const renderError = pluginError(message, server, name)
172
+
173
+ if (renderError && server) {
174
+ return
175
+ }
176
+ else if (renderError) {
177
+ return renderError
178
+ }
179
+ }
180
+
181
+ return render.output
182
+ },
183
+ },
184
+ handleHotUpdate: ({ file, server }) => pluginReload({ file, server }, options),
185
+ }, pluginBundle(options.formats), pluginMiddleware(name, options.formats)]
183
186
  }
184
187
 
185
188
  export default plugin
package/package.json CHANGED
@@ -1,28 +1,32 @@
1
1
  {
2
2
  "name": "@vituum/vite-plugin-latte",
3
- "version": "1.4.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
8
  "tsc": "tsc",
9
- "eslint": "eslint '**/*.js' --fix",
9
+ "eslint": "eslint '**/*.js'",
10
+ "eslint-fix": "eslint '**/*.js' --fix",
10
11
  "dev": "vite dev",
11
12
  "build": "vite build",
12
- "preview": "vite preview"
13
+ "preview": "vite preview",
14
+ "publish-next": "npm publish --tag next"
13
15
  },
14
16
  "dependencies": {
15
- "vituum": "^1.1",
17
+ "vituum": "^2.0.0-next.1",
16
18
  "fast-glob": "^3.3",
17
19
  "lodash": "^4.17",
18
- "minimatch": "^10.0"
20
+ "minimatch": "^10.1"
19
21
  },
20
22
  "devDependencies": {
21
- "@types/node": "^22.9.3",
22
- "vite": "^5.4.11",
23
- "eslint": "^8.57.1",
24
- "eslint-config-standard": "^17.1.0",
25
- "typescript": "^5.7.2"
23
+ "@eslint/js": "^9.39",
24
+ "@stylistic/eslint-plugin": "^5.6",
25
+ "@types/node": "^25",
26
+ "vite": "^8.0.0-beta.3",
27
+ "eslint": "^9.39",
28
+ "globals": "^16.5",
29
+ "typescript": "^5.9"
26
30
  },
27
31
  "files": [
28
32
  "index.js",