-
Notifications
You must be signed in to change notification settings - Fork 54
/
gulpfile.babel.js
49 lines (43 loc) · 1.38 KB
/
gulpfile.babel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import gulp from 'gulp'
import babel from 'gulp-babel'
import watch from 'gulp-watch'
import log from 'fancy-log'
import fs from 'fs'
import path from 'path'
import mjml2html from 'mjml'
import { registerComponent } from 'mjml-core'
const walkSync = (dir, filelist = []) => {
fs.readdirSync(dir).forEach(file => {
filelist = fs.statSync(path.join(dir, file)).isDirectory()
? walkSync(path.join(dir, file), filelist)
: filelist.concat(path.join(dir, file))
})
return filelist
}
const watchedComponents = walkSync('./components')
const compile = () => {
return gulp
.src(path.normalize('components/**/*.js'))
.pipe(babel({
presets: ['@babel/preset-env'],
}))
.on('error', log)
.pipe(gulp.dest('lib'))
.on('end', () => {
watchedComponents.forEach(compPath => {
const fullPath = path.join(process.cwd(), compPath.replace(/^components/, 'lib'))
delete require.cache[fullPath]
registerComponent(require(fullPath).default)
})
fs.readFile(path.normalize('./index.mjml'), 'utf8', (err, data) => {
if (err) throw err
const result = mjml2html(data)
fs.writeFileSync(path.normalize('./index.html'), result.html)
})
})
}
gulp.task('build', compile)
gulp.task('watch', () => {
compile()
return watch([path.normalize('components/**/*.js'), path.normalize('index.mjml')], compile)
})