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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| const gulp = require('gulp'); const babel = require('gulp-babel'); const watch = require('gulp-watch'); const fs = require('fs'); const path = require('path');
const mjml2html = require('mjml'); const { registerComponent } = require('mjml-core'); const glob = require('glob'); const fsPath = require('fs-path'); const componentPath = path.normalize('comp/**/*.ts'); const templatePath = 'tmpl/**/*.mjml';
function handleError(err) { console.log(err.toString()); process.exit(-1); }
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('./comp');
const compile = () => gulp .src(componentPath) .pipe( babel({ presets: ['@babel/preset-env'], }) ) .on('error', handleError) .pipe(gulp.dest('lib')) .on('end', () => { watchedComponents.forEach((compPath) => { const distPath = compPath.substr(0, compPath.lastIndexOf('.')) + '.js'; const fullPath = path.join(process.cwd(), distPath.replace(/^comp/, 'lib')); delete require.cache[fullPath]; registerComponent(require(fullPath).default); }); glob(templatePath, (err, files) => { if (err) throw err; files.forEach((file) => { const templateName = file.split('/')[2]; fs.readFile(file, 'utf8', (error, data) => { if (error) throw error; const result = mjml2html(data, { beautify: true }); fsPath.writeFileSync(path.normalize(`dist/${templateName}.html`), result.html); }); }); }); }); gulp.task('build', compile); gulp.task('watch', () => { compile(); return watch([componentPath, path.normalize(templatePath)], compile); });
|