@newlogic-digital/core 2.0.0-alpha.7 → 2.0.0-alpha.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. package/index.js +62 -12
  2. package/package.json +7 -4
  3. package/src/twig.js +1 -1
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
- import fs from 'fs'
2
- import os from 'os'
3
- import { dirname, resolve, join } from 'path'
1
+ import fs from 'node:fs'
2
+ import os from 'node:os'
3
+ import { dirname, resolve, join, relative } from 'node:path'
4
4
  import postHtml from 'posthtml'
5
5
  import vituum from 'vituum'
6
6
  import posthtml from '@vituum/vite-plugin-posthtml'
@@ -13,6 +13,9 @@ import { getPackageInfo, merge } from 'vituum/utils/common.js'
13
13
  import parseMinifyHtml from './src/minify.js'
14
14
  import highlight from './src/prism.js'
15
15
  import twigOptions from './src/twig.js'
16
+ import FastGlob from 'fast-glob'
17
+ import fse from 'fs-extra'
18
+ import pc from 'picocolors'
16
19
 
17
20
  const { name } = getPackageInfo(import.meta.url)
18
21
 
@@ -41,7 +44,9 @@ const posthtmlPrism = {
41
44
  * @type {import('@newlogic-digital/core/types').PluginUserConfig}
42
45
  */
43
46
  const defaultOptions = {
47
+ mode: null,
44
48
  cert: 'localhost',
49
+ format: ['latte'],
45
50
  emails: {
46
51
  outputDir: resolve(process.cwd(), 'public/email'),
47
52
  appDir: resolve(process.cwd(), 'app/Templates/Emails')
@@ -92,12 +97,21 @@ const defaultOptions = {
92
97
  const plugin = (options = {}) => {
93
98
  options = merge(defaultOptions, options)
94
99
 
100
+ const templatesPlugins = []
101
+
102
+ if (options.format.includes('twig')) {
103
+ templatesPlugins.push(twig(options.twig))
104
+ }
105
+
106
+ if (options.format.includes('latte')) {
107
+ templatesPlugins.push(latte(options.latte))
108
+ }
109
+
95
110
  const plugins = [
96
111
  vituum(options.vituum),
97
112
  tailwindcss(options.tailwindcss),
98
113
  posthtml(options.posthtml),
99
- latte(options.latte),
100
- twig(options.twig),
114
+ ...templatesPlugins,
101
115
  juice(options.juice),
102
116
  send(options.send),
103
117
  posthtmlPrism
@@ -106,11 +120,38 @@ const plugin = (options = {}) => {
106
120
  return [{
107
121
  name,
108
122
  enforce: 'pre',
109
- config (userConfig) {
123
+ config (userConfig, userEnv) {
110
124
  const isHttps = userConfig?.server?.https !== false &&
111
125
  fs.existsSync(join(os.homedir(), `.ssh/${options.cert}.pem`)) &&
112
126
  fs.existsSync(join(os.homedir(), `.ssh/${options.cert}-key.pem`))
113
127
 
128
+ let defaultInput = [
129
+ './src/views/**/*.{json,latte,twig,liquid,njk,hbs,pug,html}',
130
+ '!./src/views/**/*.{latte,twig,liquid,njk,hbs,pug,html}.json',
131
+ './src/styles/*.{css,pcss,scss,sass,less,styl,stylus}',
132
+ './src/scripts/*.{js,ts,mjs}'
133
+ ]
134
+
135
+ if (!options.mode) {
136
+ options.mode = userEnv.mode
137
+ }
138
+
139
+ if (userEnv.mode === 'headless') {
140
+ userEnv.mode = 'production'
141
+
142
+ defaultInput = [
143
+ './src/styles/*.{css,pcss,scss,sass,less,styl,stylus}',
144
+ './src/scripts/*.{js,ts,mjs}'
145
+ ]
146
+ } else if (userEnv.mode === 'emails') {
147
+ userEnv.mode = 'production'
148
+
149
+ defaultInput = [
150
+ './src/views/email/**/*.{json,latte,twig,liquid,njk,hbs,pug,html}',
151
+ '!./src/views/email/**/*.{latte,twig,liquid,njk,hbs,pug,html}.json'
152
+ ]
153
+ }
154
+
114
155
  userConfig.build = Object.assign({
115
156
  manifest: true,
116
157
  emptyOutDir: false,
@@ -118,12 +159,7 @@ const plugin = (options = {}) => {
118
159
  assetsInlineLimit: 0,
119
160
  outDir: resolve(userConfig.root ?? process.cwd(), 'public'),
120
161
  rollupOptions: {
121
- input: [
122
- './src/styles/*.{css,pcss,scss,sass,less,styl,stylus}',
123
- './src/scripts/*.{js,ts,mjs}',
124
- './src/views/**/*.{json,latte,twig,liquid,njk,hbs,pug,html}',
125
- '!./src/views/**/*.{latte,twig,liquid,njk,hbs,pug,html}.json'
126
- ]
162
+ input: defaultInput
127
163
  }
128
164
  }, userConfig.build ?? {})
129
165
 
@@ -139,6 +175,20 @@ const plugin = (options = {}) => {
139
175
  }
140
176
  : false
141
177
  }, userConfig.server ?? {})
178
+ },
179
+ writeBundle: async () => {
180
+ if (options.mode === 'emails') {
181
+ const emails = FastGlob.sync(`${resolve(process.cwd(), options.emails.outputDir)}/**`).filter(entry => !entry.endsWith('test.html'))
182
+ const emailsProd = emails.map(path => {
183
+ return path.replace(resolve(process.cwd(), options.emails.outputDir), resolve(process.cwd(), options.emails.appDir)).replace('.html', '.latte')
184
+ })
185
+
186
+ await Promise.all(emails.map((file, i) =>
187
+ fse.move(file, emailsProd[i], { overwrite: true })
188
+ ))
189
+
190
+ console.info(`${pc.cyan('@newlogic-digital/core')} ${pc.green(`all email files were moved to ${relative(process.cwd(), options.emails.appDir)}`)}`)
191
+ }
142
192
  }
143
193
  }, ...plugins]
144
194
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@newlogic-digital/core",
3
3
  "type": "module",
4
- "version": "2.0.0-alpha.7",
4
+ "version": "2.0.0-alpha.9",
5
5
  "main": "index.js",
6
6
  "author": "New Logic Studio s.r.o.",
7
7
  "description": "Set of tools that can be used to create modern web applications",
@@ -13,8 +13,8 @@
13
13
  "dependencies": {
14
14
  "@vituum/vite-plugin-posthtml": "^1.0.0-alpha.3",
15
15
  "@vituum/vite-plugin-juice": "^1.0.0-alpha.2",
16
- "@vituum/vite-plugin-latte": "^1.0.0-alpha.5",
17
- "@vituum/vite-plugin-twig": "^1.0.0-alpha.4",
16
+ "@vituum/vite-plugin-latte": "^1.0.0-alpha.8",
17
+ "@vituum/vite-plugin-twig": "^1.0.0-alpha.6",
18
18
  "@vituum/vite-plugin-tailwindcss": "^1.0.0-alpha.2",
19
19
  "@vituum/vite-plugin-send": "^1.0.0-alpha.2",
20
20
  "vituum": "^1.0.0-alpha.18",
@@ -22,7 +22,10 @@
22
22
  "posthtml-prism": "^2.0.0",
23
23
  "prismjs": "^1.29.0",
24
24
  "html-minifier-terser": "^7.2.0",
25
- "lodash": "^4.17.21"
25
+ "lodash": "^4.17.21",
26
+ "fast-glob": "^3.2.12",
27
+ "fs-extra": "^11.1.1",
28
+ "picocolors": "^1.0.0"
26
29
  },
27
30
  "devDependencies": {
28
31
  "@types/node": "^20.3.1",
package/src/twig.js CHANGED
@@ -67,7 +67,7 @@ export default {
67
67
  },
68
68
  filters: {
69
69
  asset: (url) => {
70
- return url.replace('/src/', '/')
70
+ return url
71
71
  },
72
72
  rem: (value) => {
73
73
  return `${value / 16}rem`