@vituum/vite-plugin-latte 1.0.0-alpha.5 → 1.0.0-alpha.7

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/index.js +23 -14
  2. package/index.php +1 -1
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -19,7 +19,9 @@ const defaultOptions = {
19
19
  filters: {},
20
20
  functions: {},
21
21
  tags: {},
22
- globals: {},
22
+ globals: {
23
+ format: 'latte'
24
+ },
23
25
  data: ['src/data/**/*.json'],
24
26
  formats: ['latte', 'json.latte', 'json'],
25
27
  bin: 'php',
@@ -40,31 +42,31 @@ const execSync = (cmd) => {
40
42
  }
41
43
  }
42
44
 
43
- const renderTemplate = ({ server, path, filename, cwd, packageRoot }, params, content) => {
44
- const renderTransformedHtml = params.renderTransformedHtml(server ? filename.replace('.html', '') : filename)
45
+ const renderTemplate = ({ server, path, filename, cwd, packageRoot }, options, content) => {
46
+ const renderTransformedHtml = options.renderTransformedHtml(server ? filename.replace('.html', '') : filename)
45
47
 
46
- if (params.data) {
47
- const normalizePaths = Array.isArray(params.data) ? params.data.map(path => path.replace(/\\/g, '/')) : params.data.replace(/\\/g, '/')
48
+ if (options.data) {
49
+ const normalizePaths = Array.isArray(options.data) ? options.data.map(path => path.replace(/\\/g, '/')) : options.data.replace(/\\/g, '/')
48
50
 
49
- params.data = FastGlob.sync(normalizePaths).map(entry => resolve(process.cwd(), entry))
51
+ options.data = FastGlob.sync(normalizePaths).map(entry => resolve(process.cwd(), entry))
50
52
  }
51
53
 
52
- Object.keys(params.filters).forEach(key => {
53
- if (typeof params.filters[key] === 'function') {
54
- params.filters[key] = params.filters[key].toString().match(/\(\s*([^)]+?)\s*\)/)[1].replace(/\s/g, '').split(',')
54
+ Object.keys(options.filters).forEach(key => {
55
+ if (typeof options.filters[key] === 'function') {
56
+ options.filters[key] = options.filters[key].toString().match(/\(\s*([^)]+?)\s*\)/)[1].replace(/\s/g, '').split(',')
55
57
  }
56
58
  })
57
59
 
58
- Object.keys(params.functions).forEach(key => {
59
- if (typeof params.functions[key] === 'function') {
60
- params.functions[key] = params.functions[key].toString().match(/\(\s*([^)]+?)\s*\)/)[1].replace(/\s/g, '').split(',')
60
+ Object.keys(options.functions).forEach(key => {
61
+ if (typeof options.functions[key] === 'function') {
62
+ options.functions[key] = options.functions[key].toString().match(/\(\s*([^)]+?)\s*\)/)[1].replace(/\s/g, '').split(',')
61
63
  }
62
64
  })
63
65
 
64
66
  if (renderTransformedHtml) {
65
67
  const timestamp = Math.floor(Date.now() * Math.random())
66
68
 
67
- params.contentTimestamp = timestamp
69
+ options.contentTimestamp = timestamp
68
70
 
69
71
  if (!fs.existsSync(resolve(packageRoot, 'temp'))) {
70
72
  fs.mkdirSync(resolve(packageRoot, 'temp'))
@@ -73,7 +75,9 @@ const renderTemplate = ({ server, path, filename, cwd, packageRoot }, params, co
73
75
  fs.writeFileSync(resolve(packageRoot, `temp/${timestamp}.html`), content)
74
76
  }
75
77
 
76
- return execSync(`${params.bin} ${packageRoot}/index.php ${join(params.root, server ? path.replace('.html', '') : path)} ${JSON.stringify(JSON.stringify(Object.assign({ packageRoot, cwd, renderTransformedHtml }, params)))}`)
78
+ const data = Object.assign({ packageRoot, cwd, isRenderTransformedHtml: renderTransformedHtml }, options)
79
+
80
+ return execSync(`${options.bin} ${packageRoot}/index.php ${join(options.root, server ? path.replace('.html', '') : path)} ${JSON.stringify(JSON.stringify(data))}`)
77
81
  }
78
82
 
79
83
  /**
@@ -135,6 +139,11 @@ const plugin = (options = {}) => {
135
139
  return content
136
140
  }
137
141
 
142
+ // @ts-ignore
143
+ if (!options.formats.includes(options.globals.format)) {
144
+ return content
145
+ }
146
+
138
147
  const render = renderTemplate({ server, path, filename, cwd, packageRoot }, options, content)
139
148
  const warningLog = render.output.includes('Warning: Undefined')
140
149
 
package/index.php CHANGED
@@ -164,7 +164,7 @@ if (!file_exists(str_replace($config->cwd, ROOT_DIR, $params->template))) {
164
164
  throw new Error('File not found ' . str_replace($config->cwd, ROOT_DIR, $params->template));
165
165
  }
166
166
 
167
- if (isset($config->contentTimestamp) && $config->contentTimestamp) {
167
+ if (isset($config->isRenderTransformedHtml) && $config->isRenderTransformedHtml) {
168
168
  if (isset($config->contentTimestamp)) {
169
169
  $content = file_get_contents(__DIR__ . '/temp/' . $config->contentTimestamp . '.html');
170
170
  unlink(__DIR__ . '/temp/' . $config->contentTimestamp . '.html');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vituum/vite-plugin-latte",
3
- "version": "1.0.0-alpha.5",
3
+ "version": "1.0.0-alpha.7",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "scripts": {