@vituum/vite-plugin-latte 0.1.9 → 0.1.12

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
@@ -4,6 +4,8 @@
4
4
  # ⚡️☕ ViteLatte
5
5
 
6
6
  ```js
7
+ import latte from '@vituum/vite-plugin-latte'
8
+
7
9
  export default {
8
10
  plugins: [
9
11
  latte({
@@ -16,12 +18,22 @@ export default {
16
18
  tags: {},
17
19
  globals: {
18
20
  template: 'playground/templates/Layout/Main.latte'
21
+ },
22
+ data: '*.json',
23
+ isStringFilter: undefined,
24
+ filetypes: {
25
+ html: /.(json.html|latte.json.html|latte.html)$/,
26
+ json: /.(json.latte.html)$/
19
27
  }
20
28
  })
21
29
  ]
22
30
  }
23
31
  ```
24
32
 
33
+ Read the [docs](https://vituum.dev/config/integrations-options.html#vituum-latte) to learn more about the plugin options.
34
+
35
+ ## Basic usage
36
+
25
37
  ```html
26
38
  <!-- index.html -->
27
39
  <script type="application/json">
@@ -31,9 +43,22 @@ export default {
31
43
  }
32
44
  </script>
33
45
  ```
46
+ or
47
+ ```html
48
+ <!-- index.latte.html with index.latte.json -->
49
+ {$title}
50
+ ```
51
+ or
52
+ ```html
53
+ <!-- index.latte.html or index.latte.json.html -->
54
+ {
55
+ "template": "path/to/template.latte",
56
+ "title": "Hello world"
57
+ }
58
+ ```
34
59
 
35
60
  ### Requirements
36
61
 
37
62
  - [Node.js LTS (16.x)](https://nodejs.org/en/download/)
38
- - [NPM (8.x)](https://www.npmjs.com/package/npm) or [Yarn (2.x)](https://yarnpkg.com/)
39
63
  - [PHP (8.x)](https://www.php.net/) or [Docker PHP (8.x)](https://hub.docker.com/_/php)
64
+ - [Vite](https://vitejs.dev/) or [Vituum](https://vituum.dev/)
package/handler.js CHANGED
@@ -1,9 +1,12 @@
1
1
  import { resolve } from 'path'
2
2
 
3
3
  const vite = (await import(resolve(process.cwd(), 'vite.config.js'))).default
4
- const params = JSON.parse(process.argv[2])
5
- const name = params[0]
4
+ let params = JSON.parse(process.argv[4])
5
+ const name = process.argv[2]
6
+ const type = process.argv[3]
6
7
 
7
- params.shift()
8
+ params = params.map(value => Buffer.from(value, 'base64').toString('utf-8'))
8
9
 
9
- vite.plugins.filter(({ name }) => name === '@vituum/vite-plugin-latte')[0]._params.filters[name](...params)
10
+ const output = await vite.plugins.filter(({ name }) => name === '@vituum/vite-plugin-latte')[0]._params[type][name](...params)
11
+
12
+ console.log(Buffer.from(output.toString(), 'utf-8').toString('base64'))
package/index.js CHANGED
@@ -1,12 +1,15 @@
1
- import { extname, resolve, dirname } from 'path'
1
+ import { resolve, dirname } from 'path'
2
2
  import { fileURLToPath } from 'url'
3
3
  import fs from 'fs'
4
4
  import process from 'node:process'
5
5
  import * as childProcess from 'child_process'
6
6
  import FastGlob from 'fast-glob'
7
7
  import lodash from 'lodash'
8
+ import chalk from 'chalk'
8
9
 
9
10
  const defaultParams = {
11
+ reload: true,
12
+ root: null,
10
13
  bin: 'php',
11
14
  filters: {},
12
15
  functions: {},
@@ -84,23 +87,26 @@ const latte = (params = {}) => {
84
87
  _params: params,
85
88
  name: '@vituum/vite-plugin-latte',
86
89
  config: ({ root }) => {
87
- params.root = root
90
+ if (!params.root) {
91
+ params.root = root
92
+ }
88
93
  },
89
94
  transformIndexHtml: {
90
95
  enforce: 'pre',
91
96
  async transform(content, { path, filename, server }) {
97
+ path = path.replace('?raw', '')
98
+ filename = filename.replace('?raw', '')
99
+
100
+ const start = new Date()
101
+
92
102
  if (
93
103
  !params.filetypes.html.test(path) &&
94
104
  !params.filetypes.json.test(path) &&
95
- !content.startsWith('<script type="application/json"')
105
+ !content.startsWith('<script type="application/json" data-format="latte"')
96
106
  ) {
97
107
  return content
98
108
  }
99
109
 
100
- if (content.startsWith('<script type="application/json"') && !content.includes('data-format="latte"')) {
101
- return content
102
- }
103
-
104
110
  if (typeof params.isStringFilter === 'function' && params.isStringFilter(filename)) {
105
111
  params.isString = true
106
112
  } else {
@@ -108,17 +114,22 @@ const latte = (params = {}) => {
108
114
  }
109
115
 
110
116
  const renderLatte = renderTemplate(path, params, content)
117
+ const warningLog = renderLatte.output.includes('Warning: Undefined')
111
118
 
112
- if (renderLatte.error) {
119
+ console.info(`${chalk.cyan('@vituum/vite-plugin-latte')} ${chalk.green(`finished in ${chalk.grey(new Date() - start + 'ms')}`)}`)
120
+
121
+ if (renderLatte.error || warningLog) {
113
122
  if (!server) {
114
123
  console.error(renderLatte.output)
115
124
  return
116
125
  }
117
126
 
127
+ const message = warningLog ? 'Warning: Undefined' + renderLatte.output.split('Warning: Undefined').pop() : renderLatte.output
128
+
118
129
  server.ws.send({
119
130
  type: 'error',
120
131
  err: {
121
- message: renderLatte.output,
132
+ message,
122
133
  plugin: '@vituum/vite-plugin-latte'
123
134
  }
124
135
  })
@@ -130,7 +141,10 @@ const latte = (params = {}) => {
130
141
  }
131
142
  },
132
143
  handleHotUpdate({ file, server }) {
133
- if (extname(file) === '.latte' || extname(file) === '.html' || extname(file) === '.json') {
144
+ if (
145
+ (typeof params.reload === 'function' && params.reload(file)) ||
146
+ (typeof params.reload === 'boolean' && params.reload && (params.filetypes.html.test(file) || params.filetypes.json.test(file)))
147
+ ) {
134
148
  server.ws.send({ type: 'full-reload' })
135
149
  }
136
150
  }
package/index.php CHANGED
@@ -32,12 +32,12 @@ define("PACKAGE_DIR", str_replace($config->cwd, ROOT_DIR, $config->packageRoot))
32
32
  /**
33
33
  * @throws JsonException
34
34
  */
35
- function NodeHandler($name, ...$params) : string {
36
- array_unshift($params, $name);
35
+ function NodeHandler($name, $type, ...$params) : string {
36
+ $params = array_map(static function($value) { return base64_encode((string)$value); }, $params);
37
37
 
38
- exec('node '. PACKAGE_DIR .'/handler.js ' . json_encode(json_encode($params, JSON_THROW_ON_ERROR), JSON_THROW_ON_ERROR), $output);
38
+ exec('node '. PACKAGE_DIR .'/handler.js ' . $name . ' ' . $type .' ' . json_encode(json_encode($params, JSON_HEX_QUOT | JSON_HEX_TAG), JSON_THROW_ON_ERROR), $output);
39
39
 
40
- return $output[0] ?? $params[1];
40
+ return base64_decode($output[0] ?? '') ?? $params[1];
41
41
  }
42
42
 
43
43
  if (!file_exists(__DIR__ . '/temp') && !mkdir($concurrentDirectory = __DIR__ . '/temp') && !is_dir($concurrentDirectory)) {
@@ -127,7 +127,7 @@ foreach ($config->filters as $filter => $path) {
127
127
  $latte->addFilter($filter, 'App\Latte\\' . ucfirst($filter) . 'Filter::execute');
128
128
  } elseif (!$isDocker) {
129
129
  $latte->addFilter($filter, function (...$params) use ($filter) : string {
130
- return NodeHandler($filter, ...$params);
130
+ return NodeHandler($filter, 'filters', ...$params);
131
131
  });
132
132
  }
133
133
  }
@@ -138,7 +138,7 @@ foreach ($config->functions as $function => $path) {
138
138
  $latte->addFunction($function, 'App\Latte\\' . ucfirst($function) . 'Function::execute');
139
139
  } elseif (!$isDocker) {
140
140
  $latte->addFunction($function, function (...$params) use ($function) : string {
141
- return NodeHandler($function, ...$params);
141
+ return NodeHandler($function, 'functions', ...$params);
142
142
  });
143
143
  }
144
144
  }
@@ -6,6 +6,6 @@ class AssetFilter
6
6
  {
7
7
  public static function execute($source): string
8
8
  {
9
- return $source;
9
+ return str_starts_with($source, 'http') ? $source : str_replace("/src/", "/", $source);
10
10
  }
11
11
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vituum/vite-plugin-latte",
3
- "version": "0.1.9",
3
+ "version": "0.1.12",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -10,11 +10,12 @@
10
10
  },
11
11
  "dependencies": {
12
12
  "fast-glob": "^3.2.11",
13
- "lodash": "^4.17.21"
13
+ "lodash": "^4.17.21",
14
+ "chalk": "^5.0.1"
14
15
  },
15
16
  "devDependencies": {
16
- "vite": "^3.0.4",
17
- "eslint": "^8.21.0",
17
+ "vite": "^3.0.9",
18
+ "eslint": "^8.23.0",
18
19
  "eslint-config-standard": "^17.0.0"
19
20
  },
20
21
  "files": [