Skip to content

Commit 177e9b4

Browse files
authored
Merge pull request #15 from mjmlio/fix-14
fix #14 watch on components, remove the need to register them manually
2 parents 5abbad4 + 7965c8d commit 177e9b4

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

gulpfile.babel.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,32 @@ import watch from 'gulp-watch'
44
import log from 'fancy-log'
55
import fs from 'fs'
66
import path from 'path'
7-
import { exec } from 'child_process'
87
import mjml2html from 'mjml'
98
import { registerComponent } from 'mjml-core'
10-
import MjLayout from './components/MjLayout'
11-
import MjImageText from './components/MjImageText'
12-
import MjBasicComponent from './components/MjBasicComponent'
13-
registerComponent(MjBasicComponent)
14-
registerComponent(MjImageText)
15-
registerComponent(MjLayout)
169

17-
// Import and register your components here
10+
const walkSync = (dir, filelist = []) => {
11+
fs.readdirSync(dir).forEach(file => {
12+
filelist = fs.statSync(path.join(dir, file)).isDirectory()
13+
? walkSync(path.join(dir, file), filelist)
14+
: filelist.concat(path.join(dir, file))
15+
})
16+
return filelist
17+
}
18+
19+
const watchedComponents = walkSync('./components')
1820

1921
const compile = () => {
20-
gulp.src(path.normalize('components/**.js'))
22+
gulp.src(path.normalize('components/**/*.js'))
2123
.pipe(babel())
2224
.on('error', log)
2325
.pipe(gulp.dest('lib'))
2426
.on('end', () => {
27+
watchedComponents.forEach(compPath => {
28+
const fullPath = path.join(process.cwd(), compPath.replace(/^components/, 'lib'))
29+
delete require.cache[fullPath]
30+
registerComponent(require(fullPath).default)
31+
})
32+
2533
fs.readFile(path.normalize('./index.mjml'), 'utf8', (err, data) => {
2634
if (err) throw err
2735
const result = mjml2html(data)
@@ -35,7 +43,7 @@ gulp.task('build', compile)
3543
gulp.task('watch', () => {
3644
compile()
3745
return watch([
38-
path.normalize('components/*.js'),
46+
path.normalize('components/**/*.js'),
3947
path.normalize('index.mjml'),
4048
], compile)
4149
})

0 commit comments

Comments
 (0)