@maizzle/framework 4.0.0-alpha.1 → 4.0.0-alpha.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maizzle/framework",
3
- "version": "4.0.0-alpha.1",
3
+ "version": "4.0.0-alpha.4",
4
4
  "description": "Maizzle is a framework that helps you quickly build HTML emails with Tailwind CSS.",
5
5
  "license": "MIT",
6
6
  "main": "src/index.js",
@@ -48,7 +48,7 @@
48
48
  "postcss": "^8.4.4",
49
49
  "postcss-import": "^14.0.0",
50
50
  "postcss-merge-longhand": "^5.0.1",
51
- "posthtml": "^0.16.4",
51
+ "posthtml": "^0.16.6",
52
52
  "posthtml-attrs-parser": "^0.1.1",
53
53
  "posthtml-base-url": "^1.0.1",
54
54
  "posthtml-content": "^0.0.3",
@@ -56,9 +56,9 @@
56
56
  "posthtml-extend": "^0.6.0",
57
57
  "posthtml-extra-attributes": "^1.0.0",
58
58
  "posthtml-fetch": "^2.0.0",
59
- "posthtml-markdownit": "^1.2.2",
59
+ "posthtml-markdownit": "^1.3.0",
60
60
  "posthtml-modules": "^0.8.0",
61
- "posthtml-mso": "^1.0.2",
61
+ "posthtml-mso": "^1.0.4",
62
62
  "posthtml-postcss-merge-longhand": "^1.0.2",
63
63
  "posthtml-remove-attributes": "^1.0.0",
64
64
  "posthtml-safe-class-names": "^1.0.4",
@@ -67,8 +67,7 @@
67
67
  "prevent-widows": "^1.0.2",
68
68
  "query-string": "^7.1.0",
69
69
  "string-strip-html": "^8.2.0",
70
- "tailwindcss": "^3.0.0",
71
- "tailwindcss-box-shadow": "^1.0.0"
70
+ "tailwindcss": "^3.0.0"
72
71
  },
73
72
  "devDependencies": {
74
73
  "ava": "^4.0.1",
@@ -0,0 +1,32 @@
1
+ const ora = require('ora')
2
+ const {get} = require('lodash')
3
+ const Output = require('../generators/output')
4
+ const {clearConsole} = require('../utils/helpers')
5
+
6
+ const build = async (env = 'local', config = {}) => {
7
+ const start = new Date()
8
+ const spinner = ora('Building emails...').start()
9
+
10
+ try {
11
+ const {files, parsed} = await Output.toDisk(env, spinner, config)
12
+
13
+ const elapsedSeconds = (Date.now() - start) / 1000
14
+
15
+ if (get(config, 'build.command') === 'serve') {
16
+ if (get(config, 'build.console.clear')) {
17
+ clearConsole()
18
+ }
19
+
20
+ spinner.succeed(`Re-built ${parsed.length} templates in ${elapsedSeconds}s`)
21
+ } else {
22
+ spinner.succeed(`Built ${parsed.length} templates in ${elapsedSeconds}s`)
23
+ }
24
+
25
+ return {files}
26
+ } catch (error) {
27
+ spinner.fail(error.message)
28
+ throw error
29
+ }
30
+ }
31
+
32
+ module.exports = build
@@ -0,0 +1,139 @@
1
+ const ora = require('ora')
2
+ const path = require('path')
3
+ const fs = require('fs-extra')
4
+
5
+ const Config = require('../generators/config')
6
+ const buildToFile = require('../commands/build')
7
+ const renderToString = require('../functions/render')
8
+
9
+ const {get, merge, isObject} = require('lodash')
10
+ const {clearConsole} = require('../utils/helpers')
11
+
12
+ const browsersync = () => {
13
+ if (!global.cachedBrowserSync) {
14
+ const bs = require('browser-sync')
15
+ global.cachedBrowserSync = bs.create()
16
+ }
17
+
18
+ return global.cachedBrowserSync
19
+ }
20
+
21
+ const serve = async (env = 'local', config = {}) => {
22
+ config = merge(
23
+ config,
24
+ await Config.getMerged(env),
25
+ {
26
+ build: {
27
+ command: 'serve'
28
+ }
29
+ }
30
+ )
31
+
32
+ const spinner = ora()
33
+
34
+ try {
35
+ await buildToFile(env, config)
36
+
37
+ let templates = get(config, 'build.templates')
38
+ templates = Array.isArray(templates) ? templates : [templates]
39
+
40
+ const templatePaths = [...new Set(templates.map(config => `${get(config, 'source', 'src')}/**`))]
41
+ const globalPaths = [
42
+ 'src/**',
43
+ get(config, 'build.tailwind.config', 'tailwind.config.js'),
44
+ [...new Set(get(config, 'build.browsersync.watch', []))]
45
+ ]
46
+
47
+ // Watch for Template file changes
48
+ browsersync()
49
+ .watch(templatePaths)
50
+ .on('change', async file => {
51
+ if (get(config, 'build.console.clear')) {
52
+ clearConsole()
53
+ }
54
+
55
+ const start = new Date()
56
+
57
+ spinner.start('Building email...')
58
+
59
+ file = file.replace(/\\/g, '/')
60
+
61
+ if (config.events && typeof config.events.beforeCreate === 'function') {
62
+ await config.events.beforeCreate(config)
63
+ }
64
+
65
+ const renderOptions = {
66
+ maizzle: config,
67
+ ...config.events
68
+ }
69
+
70
+ renderToString(
71
+ await fs.readFile(file, 'utf8'),
72
+ renderOptions
73
+ )
74
+ .then(async ({html, config}) => {
75
+ let dest = ''
76
+ let ext = ''
77
+
78
+ if (Array.isArray(config.build.templates)) {
79
+ const match = config.build.templates.find(template => template.source === path.parse(file).dir)
80
+ dest = get(match, 'destination.path', 'build_local')
81
+ ext = get(match, 'destination.ext', 'html')
82
+ } else if (isObject(config.build.templates)) {
83
+ dest = get(config, 'build.templates.destination.path', 'build_local')
84
+ ext = get(config, 'build.templates.destination.ext', 'html')
85
+ }
86
+
87
+ const finalDestination = path.join(dest, `${path.parse(file).name}.${ext}`)
88
+
89
+ await fs.outputFile(config.permalink || finalDestination, html)
90
+ })
91
+ .then(() => {
92
+ browsersync().reload()
93
+ spinner.succeed(`Compiled in ${(Date.now() - start) / 1000}s [${file}]`)
94
+ })
95
+ .catch(() => spinner.warn(`Received empty HTML, please save your file again [${file}]`))
96
+ })
97
+
98
+ // Watch for changes in all other files
99
+ browsersync()
100
+ .watch(globalPaths, {ignored: templatePaths})
101
+ .on('change', () => buildToFile(env, config).then(() => browsersync().reload()))
102
+ .on('unlink', () => buildToFile(env, config).then(() => browsersync().reload()))
103
+
104
+ // Watch for changes in config files
105
+ browsersync()
106
+ .watch('config*.js')
107
+ .on('change', async file => {
108
+ const parsedEnv = path.parse(file).name.split('.')[1] || 'local'
109
+
110
+ Config
111
+ .getMerged(parsedEnv)
112
+ .then(config => buildToFile(parsedEnv, config).then(() => browsersync().reload()))
113
+ })
114
+
115
+ // Browsersync options
116
+ const baseDir = templates.map(t => t.destination.path)
117
+
118
+ // Initialize Browsersync
119
+ browsersync()
120
+ .init({
121
+ notify: false,
122
+ open: false,
123
+ port: 3000,
124
+ server: {
125
+ baseDir,
126
+ directory: true
127
+ },
128
+ tunnel: false,
129
+ ui: {port: 3001},
130
+ logFileChanges: false,
131
+ ...get(config, 'build.browsersync', {})
132
+ }, () => {})
133
+ } catch (error) {
134
+ spinner.fail(error)
135
+ throw error
136
+ }
137
+ }
138
+
139
+ module.exports = serve
@@ -0,0 +1,5 @@
1
+ const Plaintext = require('../generators/plaintext')
2
+
3
+ const toPlaintext = async (html, config = {}) => Plaintext.generate(html, false, config)
4
+
5
+ module.exports = toPlaintext
@@ -0,0 +1,5 @@
1
+ const Output = require('../generators/output')
2
+
3
+ const render = async (html, options) => Output.toString(html, options)
4
+
5
+ module.exports = render
@@ -1,195 +1,208 @@
1
- const path = require('path')
2
- const fs = require('fs-extra')
3
- const glob = require('glob-promise')
4
- const {get, isEmpty, merge} = require('lodash')
5
- const {asyncForEach} = require('../../utils/helpers')
6
- const removePlaintextTags = require('../../transformers/plaintext')
7
-
8
- const Config = require('../config')
9
- const Tailwind = require('../tailwindcss')
10
- const Plaintext = require('../plaintext')
11
-
12
- const render = require('./to-string')
13
-
14
- module.exports = async (env, spinner, config) => {
15
- process.env.NODE_ENV = env || 'local'
16
-
17
- if (isEmpty(config)) {
18
- config = await Config.getMerged(env).catch(error => {
19
- spinner.fail('Build failed')
20
- throw error
21
- })
22
- }
23
-
24
- const buildTemplates = get(config, 'build.templates')
25
- const templatesConfig = Array.isArray(buildTemplates) ? buildTemplates : [buildTemplates]
26
-
27
- const parsed = []
28
- let files = []
29
-
30
- const css = (typeof get(config, 'tailwind.compiled') === 'string')
31
- ? config.tailwind.compiled
32
- : await Tailwind.compile('@tailwind components; @tailwind utilities;', '', {}, config, spinner)
33
-
34
- // Parse each template config object
35
- await asyncForEach(templatesConfig, async templateConfig => {
36
- const outputDir = get(templateConfig, 'destination.path', `build_${env}`)
37
-
38
- await fs.remove(outputDir)
39
-
40
- /**
41
- * Get all files in the template config's source directory
42
- * Supports `source` defined as:
43
- * - string
44
- * - array of strings
45
- * - function that returns either of the above
46
- *
47
- * */
48
- const templateSource = []
49
-
50
- if (typeof templateConfig.source === 'function') {
51
- const sources = templateConfig.source()
52
- if (Array.isArray(sources)) {
53
- templateSource.push(...sources)
54
- } else {
55
- templateSource.push(sources)
56
- }
57
- } else {
58
- if (Array.isArray(templateConfig.source)) {
59
- templateSource.push(...templateConfig.source)
60
- } else {
61
- templateSource.push(templateConfig.source)
62
- }
63
- }
64
-
65
- // Parse each template source
66
- await asyncForEach(templateSource, async source => {
67
- await fs
68
- .copy(source, outputDir)
69
- .then(async () => {
70
- const extensions = Array.isArray(templateConfig.filetypes)
71
- ? templateConfig.filetypes.join('|')
72
- : templateConfig.filetypes || get(templateConfig, 'filetypes', 'html')
73
-
74
- const templates = await glob(`${outputDir}/**/*.+(${extensions})`)
75
-
76
- if (templates.length === 0) {
77
- spinner.warn(`Error: no files with the .${extensions} extension found in ${templateConfig.source}`)
78
- return
79
- }
80
-
81
- // Store template config currently being processed
82
- config.build.currentTemplates = templateConfig
83
-
84
- if (config.events && typeof config.events.beforeCreate === 'function') {
85
- await config.events.beforeCreate(config)
86
- }
87
-
88
- await asyncForEach(templates, async file => {
89
- const html = await fs.readFile(file, 'utf8')
90
-
91
- await render(html, {
92
- maizzle: {
93
- ...config,
94
- env
95
- },
96
- tailwind: {
97
- compiled: css
98
- },
99
- ...config.events
100
- })
101
- .then(async ({html, config}) => {
102
- const destination = config.permalink || file
103
-
104
- /**
105
- * Generate plaintext
106
- *
107
- * We do this first so that we can remove the <plaintext>
108
- * tags from the markup before outputting the file.
109
- */
110
-
111
- const plaintextConfig = get(templateConfig, 'plaintext')
112
- const plaintextDestination = get(plaintextConfig, 'destination', config.permalink || file)
113
-
114
- if ((typeof plaintextConfig === 'boolean' && plaintextConfig) || !isEmpty(plaintextConfig)) {
115
- await Plaintext
116
- .generate(html, plaintextDestination, merge(config, {filepath: file}))
117
- .then(({plaintext, destination}) => fs.outputFile(destination, plaintext))
118
- }
119
-
120
- html = removePlaintextTags(html, config)
121
-
122
- /**
123
- * Output file
124
- */
125
- const parts = path.parse(destination)
126
- const extension = get(templateConfig, 'destination.extension', 'html')
127
- const finalDestination = `${parts.dir}/${parts.name}.${extension}`
128
-
129
- await fs.outputFile(finalDestination, html)
130
- .then(async () => {
131
- /**
132
- * Remove original file if its path is different
133
- * from the final destination path.
134
- *
135
- * This ensures non-HTML files do not pollute
136
- * the build destination folder.
137
- */
138
- if (finalDestination !== file) {
139
- await fs.remove(file)
140
- }
141
-
142
- // Keep track of handled files
143
- files.push(file)
144
- parsed.push(file)
145
- })
146
- })
147
- .catch(error => {
148
- switch (config.build.fail) {
149
- case 'silent':
150
- spinner.warn(`Failed to compile template: ${path.basename(file)}`)
151
- break
152
- case 'verbose':
153
- spinner.warn(`Failed to compile template: ${path.basename(file)}`)
154
- console.error(error)
155
- break
156
- default:
157
- spinner.fail(`Failed to compile template: ${path.basename(file)}`)
158
- throw error
159
- }
160
- })
161
- })
162
-
163
- const assets = {source: '', destination: 'assets', ...get(templateConfig, 'assets')}
164
-
165
- if (Array.isArray(assets.source)) {
166
- await asyncForEach(assets.source, async source => {
167
- if (fs.existsSync(source)) {
168
- await fs.copy(source, path.join(templateConfig.destination.path, assets.destination)).catch(error => spinner.warn(error.message))
169
- }
170
- })
171
- } else {
172
- if (fs.existsSync(assets.source)) {
173
- await fs.copy(assets.source, path.join(templateConfig.destination.path, assets.destination)).catch(error => spinner.warn(error.message))
174
- }
175
- }
176
-
177
- await glob(path.join(templateConfig.destination.path, '/**/*.*'))
178
- .then(contents => {
179
- files = [...new Set([...files, ...contents])]
180
- })
181
- })
182
- .catch(error => spinner.warn(error.message))
183
- })
184
- })
185
-
186
- if (config.events && typeof config.events.afterBuild === 'function') {
187
- await config.events.afterBuild(files)
188
- }
189
-
190
- return {
191
- files,
192
- parsed,
193
- css
194
- }
195
- }
1
+ const path = require('path')
2
+ const fs = require('fs-extra')
3
+ const glob = require('glob-promise')
4
+ const {get, isEmpty, merge} = require('lodash')
5
+ const {asyncForEach} = require('../../utils/helpers')
6
+ const removePlaintextTags = require('../../transformers/plaintext')
7
+
8
+ const Config = require('../config')
9
+ const Tailwind = require('../tailwindcss')
10
+ const Plaintext = require('../plaintext')
11
+
12
+ const render = require('./to-string')
13
+
14
+ module.exports = async (env, spinner, config) => {
15
+ process.env.NODE_ENV = env || 'local'
16
+
17
+ if (isEmpty(config)) {
18
+ config = await Config.getMerged(env).catch(error => {
19
+ spinner.fail('Build failed')
20
+ throw error
21
+ })
22
+ }
23
+
24
+ const buildTemplates = get(config, 'build.templates')
25
+ const templatesConfig = Array.isArray(buildTemplates) ? buildTemplates : [buildTemplates]
26
+
27
+ const parsed = []
28
+ let files = []
29
+
30
+ const css = (typeof get(config, 'tailwind.compiled') === 'string')
31
+ ? config.tailwind.compiled
32
+ : await Tailwind.compile('@tailwind components; @tailwind utilities;', '', {}, config, spinner)
33
+
34
+ // Parse each template config object
35
+ await asyncForEach(templatesConfig, async templateConfig => {
36
+ const outputDir = get(templateConfig, 'destination.path', `build_${env}`)
37
+
38
+ await fs.remove(outputDir)
39
+
40
+ /**
41
+ * Get all files in the template config's source directory
42
+ * Supports `source` defined as:
43
+ * - string
44
+ * - array of strings
45
+ * - function that returns either of the above
46
+ *
47
+ * */
48
+ const templateSource = []
49
+ const templateTypeErrorMessage = 'Invalid template source: expected string or array of strings, got '
50
+
51
+ if (typeof templateConfig.source === 'function') {
52
+ const sources = templateConfig.source(config)
53
+
54
+ if (Array.isArray(sources)) {
55
+ templateSource.push(...sources)
56
+ } else if (typeof sources === 'string') {
57
+ templateSource.push(sources)
58
+ } else {
59
+ throw new TypeError(templateTypeErrorMessage + typeof sources)
60
+ }
61
+ } else {
62
+ if (Array.isArray(templateConfig.source)) {
63
+ templateSource.push(...templateConfig.source)
64
+ } else if (typeof templateConfig.source === 'string') {
65
+ templateSource.push(templateConfig.source)
66
+ } else {
67
+ throw new TypeError(templateTypeErrorMessage + typeof templateConfig.source)
68
+ }
69
+ }
70
+
71
+ // Parse each template source
72
+ await asyncForEach(templateSource, async source => {
73
+ /**
74
+ * Copy single-file sources correctly
75
+ * If `src` is a file, `dest` cannot be a directory
76
+ * https://github.com/jprichardson/node-fs-extra/issues/323
77
+ */
78
+ const out = fs.lstatSync(source).isFile() ? `${outputDir}/${path.basename(source)}` : outputDir
79
+
80
+ await fs
81
+ .copy(source, out)
82
+ .then(async () => {
83
+ const extensions = Array.isArray(templateConfig.filetypes)
84
+ ? templateConfig.filetypes.join('|')
85
+ : templateConfig.filetypes || get(templateConfig, 'filetypes', 'html')
86
+
87
+ const templates = await glob(`${outputDir}/**/*.+(${extensions})`)
88
+
89
+ if (templates.length === 0) {
90
+ spinner.warn(`Error: no files with the .${extensions} extension found in ${templateConfig.source}`)
91
+ return
92
+ }
93
+
94
+ if (config.events && typeof config.events.beforeCreate === 'function') {
95
+ await config.events.beforeCreate(config)
96
+ }
97
+
98
+ await asyncForEach(templates, async file => {
99
+ const html = await fs.readFile(file, 'utf8')
100
+
101
+ try {
102
+ const compiled = await render(html, {
103
+ maizzle: {
104
+ ...config,
105
+ env
106
+ },
107
+ tailwind: {
108
+ compiled: css
109
+ },
110
+ ...config.events
111
+ })
112
+
113
+ const destination = config.permalink || file
114
+
115
+ /**
116
+ * Generate plaintext
117
+ *
118
+ * We do this first so that we can remove the <plaintext>
119
+ * tags from the markup before outputting the file.
120
+ */
121
+
122
+ const plaintextConfig = get(templateConfig, 'plaintext')
123
+ const plaintextPath = get(plaintextConfig, 'destination.path', config.permalink || file)
124
+
125
+ if (Boolean(plaintextConfig) || !isEmpty(plaintextConfig)) {
126
+ await Plaintext
127
+ .generate(
128
+ compiled.html,
129
+ plaintextPath,
130
+ merge(plaintextConfig, {filepath: file})
131
+ )
132
+ .then(({plaintext, destination}) => fs.outputFile(destination, plaintext))
133
+ }
134
+
135
+ compiled.html = removePlaintextTags(compiled.html, config)
136
+
137
+ /**
138
+ * Output file
139
+ */
140
+ const parts = path.parse(destination)
141
+ const extension = get(templateConfig, 'destination.extension', 'html')
142
+ const finalDestination = `${parts.dir}/${parts.name}.${extension}`
143
+
144
+ await fs.outputFile(finalDestination, compiled.html)
145
+
146
+ /**
147
+ * Remove original file if its path is different
148
+ * from the final destination path.
149
+ *
150
+ * This ensures non-HTML files do not pollute
151
+ * the build destination folder.
152
+ */
153
+ if (finalDestination !== file) {
154
+ await fs.remove(file)
155
+ }
156
+
157
+ // Keep track of handled files
158
+ files.push(file)
159
+ parsed.push(file)
160
+ } catch (error) {
161
+ switch (config.build.fail) {
162
+ case 'silent':
163
+ spinner.warn(`Failed to compile template: ${path.basename(file)}`)
164
+ break
165
+ case 'verbose':
166
+ spinner.warn(`Failed to compile template: ${path.basename(file)}`)
167
+ console.error(error)
168
+ break
169
+ default:
170
+ spinner.fail(`Failed to compile template: ${path.basename(file)}`)
171
+ throw error
172
+ }
173
+ }
174
+ })
175
+
176
+ const assets = {source: '', destination: 'assets', ...get(templateConfig, 'assets')}
177
+
178
+ if (Array.isArray(assets.source)) {
179
+ await asyncForEach(assets.source, async source => {
180
+ if (fs.existsSync(source)) {
181
+ await fs.copy(source, path.join(templateConfig.destination.path, assets.destination)).catch(error => spinner.warn(error.message))
182
+ }
183
+ })
184
+ } else {
185
+ if (fs.existsSync(assets.source)) {
186
+ await fs.copy(assets.source, path.join(templateConfig.destination.path, assets.destination)).catch(error => spinner.warn(error.message))
187
+ }
188
+ }
189
+
190
+ await glob(path.join(templateConfig.destination.path, '/**/*.*'))
191
+ .then(contents => {
192
+ files = [...new Set([...files, ...contents])]
193
+ })
194
+ })
195
+ .catch(error => spinner.warn(error.message))
196
+ })
197
+ })
198
+
199
+ if (config.events && typeof config.events.afterBuild === 'function') {
200
+ await config.events.afterBuild(files)
201
+ }
202
+
203
+ return {
204
+ files,
205
+ parsed,
206
+ css
207
+ }
208
+ }