@maizzle/framework 4.0.0-alpha.4 → 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,208 +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('
|
|
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
|
-
}
|
|
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('', '', {}, 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
|
+
}
|
|
@@ -15,8 +15,8 @@ module.exports = async (html, config = {}, direct = false) => {
|
|
|
15
15
|
|
|
16
16
|
const compileCss = css => Tailwind.compile(css, html, tailwindConfig, maizzleConfig)
|
|
17
17
|
|
|
18
|
-
replacements.tailwindcss = css => compileCss(
|
|
19
|
-
replacements.postcss = css => compileCss(
|
|
18
|
+
replacements.tailwindcss = css => compileCss(css)
|
|
19
|
+
replacements.postcss = css => compileCss(css)
|
|
20
20
|
|
|
21
21
|
return posthtml([posthtmlContent(replacements)]).process(html, posthtmlOptions).then(result => result.html)
|
|
22
22
|
}
|
package/test/test-tailwind.js
CHANGED
|
@@ -113,5 +113,5 @@ test('uses custom postcss plugins from the maizzle config', async t => {
|
|
|
113
113
|
const css = await Tailwind.compile('.test {transform: scale(0.5)}', '<div class="test">Test</a>', {}, maizzleConfig)
|
|
114
114
|
|
|
115
115
|
t.not(css, undefined)
|
|
116
|
-
t.is(css.trim(), '.test {-webkit-transform: scale(0.5);transform: scale(0.5)}')
|
|
116
|
+
t.is(css.trim(), '.inline {display: inline !important} .table {display: table !important} .contents {display: contents !important} .transform {-webkit-transform: translate(var(--tw-translate-x), var(--tw-translate-y)) rotate(var(--tw-rotate)) skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x)) scaleY(var(--tw-scale-y)) !important;transform: translate(var(--tw-translate-x), var(--tw-translate-y)) rotate(var(--tw-rotate)) skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x)) scaleY(var(--tw-scale-y)) !important} .test {-webkit-transform: scale(0.5);transform: scale(0.5)}')
|
|
117
117
|
})
|