@@ -4,24 +4,32 @@ import watch from 'gulp-watch'
44import log from 'fancy-log'
55import fs from 'fs'
66import path from 'path'
7- import { exec } from 'child_process'
87import mjml2html from 'mjml'
98import { 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
1921const 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 ( / ^ c o m p o n e n t s / , '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)
3543gulp . 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