@maizzle/framework 4.0.0-alpha.2 → 4.0.0-alpha.5

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.2",
3
+ "version": "4.0.0-alpha.5",
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",
@@ -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
@@ -29,7 +29,7 @@ module.exports = async (env, spinner, config) => {
29
29
 
30
30
  const css = (typeof get(config, 'tailwind.compiled') === 'string')
31
31
  ? config.tailwind.compiled
32
- : await Tailwind.compile('@tailwind components; @tailwind utilities;', '', {}, config, spinner)
32
+ : await Tailwind.compile('', '', {}, config, spinner)
33
33
 
34
34
  // Parse each template config object
35
35
  await asyncForEach(templatesConfig, async templateConfig => {
@@ -50,6 +50,7 @@ module.exports = async (env, spinner, config) => {
50
50
 
51
51
  if (typeof templateConfig.source === 'function') {
52
52
  const sources = templateConfig.source(config)
53
+
53
54
  if (Array.isArray(sources)) {
54
55
  templateSource.push(...sources)
55
56
  } else if (typeof sources === 'string') {
@@ -69,8 +70,15 @@ module.exports = async (env, spinner, config) => {
69
70
 
70
71
  // Parse each template source
71
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
+
72
80
  await fs
73
- .copy(source, outputDir)
81
+ .copy(source, out)
74
82
  .then(async () => {
75
83
  const extensions = Array.isArray(templateConfig.filetypes)
76
84
  ? templateConfig.filetypes.join('|')
@@ -83,9 +91,6 @@ module.exports = async (env, spinner, config) => {
83
91
  return
84
92
  }
85
93
 
86
- // Store template config currently being processed
87
- config.build.currentTemplates = templateConfig
88
-
89
94
  if (config.events && typeof config.events.beforeCreate === 'function') {
90
95
  await config.events.beforeCreate(config)
91
96
  }
@@ -93,76 +98,79 @@ module.exports = async (env, spinner, config) => {
93
98
  await asyncForEach(templates, async file => {
94
99
  const html = await fs.readFile(file, 'utf8')
95
100
 
96
- await render(html, {
97
- maizzle: {
98
- ...config,
99
- env
100
- },
101
- tailwind: {
102
- compiled: css
103
- },
104
- ...config.events
105
- })
106
- .then(async ({html, config}) => {
107
- const destination = config.permalink || file
108
-
109
- /**
110
- * Generate plaintext
111
- *
112
- * We do this first so that we can remove the <plaintext>
113
- * tags from the markup before outputting the file.
114
- */
115
-
116
- const plaintextConfig = get(templateConfig, 'plaintext')
117
- const plaintextDestination = get(plaintextConfig, 'destination', config.permalink || file)
118
-
119
- if ((typeof plaintextConfig === 'boolean' && plaintextConfig) || !isEmpty(plaintextConfig)) {
120
- await Plaintext
121
- .generate(html, plaintextDestination, merge(config, {filepath: file}))
122
- .then(({plaintext, destination}) => fs.outputFile(destination, plaintext))
123
- }
124
-
125
- html = removePlaintextTags(html, config)
126
-
127
- /**
128
- * Output file
129
- */
130
- const parts = path.parse(destination)
131
- const extension = get(templateConfig, 'destination.extension', 'html')
132
- const finalDestination = `${parts.dir}/${parts.name}.${extension}`
133
-
134
- await fs.outputFile(finalDestination, html)
135
- .then(async () => {
136
- /**
137
- * Remove original file if its path is different
138
- * from the final destination path.
139
- *
140
- * This ensures non-HTML files do not pollute
141
- * the build destination folder.
142
- */
143
- if (finalDestination !== file) {
144
- await fs.remove(file)
145
- }
146
-
147
- // Keep track of handled files
148
- files.push(file)
149
- parsed.push(file)
150
- })
151
- })
152
- .catch(error => {
153
- switch (config.build.fail) {
154
- case 'silent':
155
- spinner.warn(`Failed to compile template: ${path.basename(file)}`)
156
- break
157
- case 'verbose':
158
- spinner.warn(`Failed to compile template: ${path.basename(file)}`)
159
- console.error(error)
160
- break
161
- default:
162
- spinner.fail(`Failed to compile template: ${path.basename(file)}`)
163
- throw error
164
- }
101
+ try {
102
+ const compiled = await render(html, {
103
+ maizzle: {
104
+ ...config,
105
+ env
106
+ },
107
+ tailwind: {
108
+ compiled: css
109
+ },
110
+ ...config.events
165
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
+ }
166
174
  })
167
175
 
168
176
  const assets = {source: '', destination: 'assets', ...get(templateConfig, 'assets')}
@@ -1,52 +1,49 @@
1
- const path = require('path')
2
- const {get} = require('lodash')
3
- const {stripHtml} = require('string-strip-html')
4
-
5
- module.exports.generate = async (html, destination, config) => {
6
- const options = get(config, 'build.currentTemplates.plaintext', get(config, 'plaintext', {}))
7
- const configDestinationPath = get(options, 'destination.path')
8
- const extension = get(options, 'destination.extension', 'txt')
9
- const plaintext = stripHtml(html, {
10
- dumpLinkHrefsNearby: {
11
- enabled: true
12
- },
13
- ...options
14
- }).result
15
-
16
- // If we set plaintext.destination.path in config/fm
17
- if (configDestinationPath) {
18
- // Naive path checking, this does not support dot folders
19
- const isFilePath = Boolean(path.parse(configDestinationPath).ext)
20
-
21
- /**
22
- * Using a file path will generate a single plaintext file,
23
- * no matter how many templates there are.
24
- *
25
- * It will be based on the last-processed template.
26
- */
27
- if (isFilePath) {
28
- destination = configDestinationPath
29
-
30
- return {plaintext, destination}
31
- }
32
-
33
- /**
34
- * Using a directory-like path for plaintext.destination.path
35
- */
36
- destination = path.join(configDestinationPath, path.basename(config.filepath, path.extname(config.filepath)) + '.' + extension)
37
-
38
- return {plaintext, destination}
39
- }
40
-
41
- /**
42
- * Use template's `permalink` Front Matter key,
43
- * fall back to the original `destination`.
44
- */
45
- destination = get(config, 'permalink', destination)
46
-
47
- if (typeof destination === 'string') {
48
- destination = path.join(path.dirname(destination), path.basename(destination, path.extname(destination)) + '.' + extension)
49
- }
50
-
51
- return {plaintext, destination}
52
- }
1
+ const path = require('path')
2
+ const {get} = require('lodash')
3
+ const {stripHtml} = require('string-strip-html')
4
+
5
+ module.exports.generate = async (html, destination, config) => {
6
+ const configDestinationPath = get(config, 'destination.path')
7
+ const extension = get(config, 'destination.extension', 'txt')
8
+
9
+ const plaintext = stripHtml(html, {
10
+ dumpLinkHrefsNearby: {
11
+ enabled: true
12
+ },
13
+ ...get(config, 'options', {})
14
+ }).result
15
+
16
+ // If we set plaintext.destination.path in config/fm
17
+ if (configDestinationPath) {
18
+ /**
19
+ * Using a file path will generate a single plaintext file,
20
+ * no matter how many templates there are.
21
+ *
22
+ * It will be based on the last-processed template.
23
+ */
24
+ if (path.extname(configDestinationPath)) {
25
+ destination = configDestinationPath
26
+
27
+ return {plaintext, destination}
28
+ }
29
+
30
+ /**
31
+ * Using a directory-like path for plaintext.destination.path
32
+ */
33
+ destination = path.join(configDestinationPath, path.basename(config.filepath, path.extname(config.filepath)) + '.' + extension)
34
+
35
+ return {plaintext, destination}
36
+ }
37
+
38
+ /**
39
+ * Use template's `permalink` Front Matter key,
40
+ * fall back to the original `destination`.
41
+ */
42
+ destination = get(config, 'permalink', destination)
43
+
44
+ if (typeof destination === 'string') {
45
+ destination = path.join(path.dirname(destination), path.basename(destination, path.extname(destination)) + '.' + extension)
46
+ }
47
+
48
+ return {plaintext, destination}
49
+ }
@@ -1,61 +1,61 @@
1
- const fm = require('front-matter')
2
- const posthtml = require('posthtml')
3
- const {get, merge} = require('lodash')
4
- const fetch = require('posthtml-fetch')
5
- const layouts = require('posthtml-extend')
6
- const modules = require('posthtml-modules')
7
- const expressions = require('posthtml-expressions')
8
-
9
- module.exports = async (html, config) => {
10
- const layoutsOptions = get(config, 'build.layouts', {})
11
-
12
- const fetchOptions = get(config, 'build.posthtml.fetch', {})
13
- const fetchPlugin = fetch({...fetchOptions})
14
-
15
- const modulesOptions = get(config, 'build.components', {})
16
- // Fake `from` option so we can reference modules relatively
17
- const modulesRoot = modulesOptions.root || './'
18
- const modulesFrom = modulesOptions.from || `${modulesRoot}/fake`
19
-
20
- const posthtmlOptions = get(config, 'build.posthtml.options', {})
21
- const posthtmlPlugins = get(config, 'build.posthtml.plugins', [])
22
-
23
- const expressionsOptions = merge({strictMode: false}, get(config, 'build.posthtml.expressions', {}))
24
-
25
- const locals = merge(
26
- get(expressionsOptions, 'locals', {}),
27
- get(config, 'locals', {}),
28
- {page: config}
29
- )
30
-
31
- return posthtml([
32
- fetchPlugin,
33
- layouts(
34
- merge(
35
- {
36
- strict: false,
37
- plugins: [
38
- expressions({...expressionsOptions, locals})
39
- ]
40
- },
41
- layoutsOptions
42
- )
43
- ),
44
- modules({
45
- parser: posthtmlOptions,
46
- attributeAsLocals: true,
47
- from: modulesFrom,
48
- root: modulesRoot,
49
- tag: 'component',
50
- attribute: 'src',
51
- plugins: [
52
- fetchPlugin
53
- ],
54
- locals,
55
- ...modulesOptions
56
- }),
57
- ...posthtmlPlugins
58
- ])
59
- .process(html, {...posthtmlOptions})
60
- .then(result => fm(result.html).body)
61
- }
1
+ const fm = require('front-matter')
2
+ const posthtml = require('posthtml')
3
+ const {get, merge} = require('lodash')
4
+ const fetch = require('posthtml-fetch')
5
+ const layouts = require('posthtml-extend')
6
+ const modules = require('posthtml-modules')
7
+ const expressions = require('posthtml-expressions')
8
+
9
+ module.exports = async (html, config) => {
10
+ const layoutsOptions = get(config, 'build.layouts', {})
11
+
12
+ const fetchOptions = get(config, 'build.posthtml.fetch', {})
13
+ const fetchPlugin = fetch({...fetchOptions})
14
+
15
+ const modulesOptions = get(config, 'build.components', {})
16
+ // Fake `from` option so we can reference modules relatively
17
+ const modulesRoot = modulesOptions.root || './'
18
+ const modulesFrom = modulesOptions.from || `${modulesRoot}/fake`
19
+
20
+ const posthtmlOptions = get(config, 'build.posthtml.options', {})
21
+ const posthtmlPlugins = get(config, 'build.posthtml.plugins', [])
22
+
23
+ const expressionsOptions = merge({strictMode: false}, get(config, 'build.posthtml.expressions', {}))
24
+
25
+ const locals = merge(
26
+ get(expressionsOptions, 'locals', {}),
27
+ get(config, 'locals', {}),
28
+ {page: config}
29
+ )
30
+
31
+ return posthtml([
32
+ fetchPlugin,
33
+ layouts(
34
+ merge(
35
+ {
36
+ strict: false,
37
+ plugins: [
38
+ expressions({...expressionsOptions, locals})
39
+ ]
40
+ },
41
+ layoutsOptions
42
+ )
43
+ ),
44
+ modules({
45
+ parser: posthtmlOptions,
46
+ attributeAsLocals: true,
47
+ from: modulesFrom,
48
+ root: modulesRoot,
49
+ tag: 'component',
50
+ attribute: 'src',
51
+ plugins: [
52
+ fetchPlugin
53
+ ],
54
+ locals,
55
+ ...modulesOptions
56
+ }),
57
+ ...posthtmlPlugins
58
+ ])
59
+ .process(html, {...posthtmlOptions})
60
+ .then(result => fm(result.html).body)
61
+ }
@@ -73,6 +73,13 @@ module.exports = {
73
73
  return {raw: '', extension: 'html'}
74
74
  }
75
75
 
76
+ // Support single-file sources i.e. src/templates/index.html
77
+ if (typeof source === 'string' && Boolean(path.extname(source))) {
78
+ config.content.files.push(source)
79
+
80
+ return {raw: '', extension: 'html'}
81
+ }
82
+
76
83
  return `${source}/**/*.*`
77
84
  })
78
85
 
@@ -83,7 +90,9 @@ module.exports = {
83
90
  const userFileExists = await fs.pathExists(userFilePath)
84
91
 
85
92
  if (userFileExists) {
86
- css = await fs.readFile(path.resolve(userFilePath), 'utf8') + css
93
+ css += await fs.readFile(path.resolve(userFilePath), 'utf8')
94
+ } else {
95
+ css = '@tailwind components; @tailwind utilities;' + css
87
96
  }
88
97
 
89
98
  return postcss([