From a40ed56c29f23463a68d1189f1dfc024ace408f4 Mon Sep 17 00:00:00 2001 From: Ocoka Date: Sun, 7 Mar 2021 10:02:45 +0300 Subject: [PATCH 1/2] New mode added: -B --big-mode to compile in one file For case where HTML rendering should occur on client, it is convinient to build one big file, with all generated template functions. This mode assumes that directory should be given as source --- index.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 84cbe42..633d687 100644 --- a/index.js +++ b/index.js @@ -34,6 +34,7 @@ program .option('-b, --basedir ', 'path used as root directory to resolve absolute includes') .option('-P, --pretty', 'compile pretty HTML output') .option('-c, --client', 'compile function for client-side') + .option('-B, --big ', 'create all template functions in one file (works only with directory as source, implies --client and --name-after-file)') .option('-n, --name ', 'the name of the compiled template (requires --client)') .option('-D, --no-debug', 'compile without debugging (smaller functions)') .option('-w, --watch', 'watch files for changes and automatically re-render') @@ -136,6 +137,13 @@ var watchList = {}; // function for rendering var render = program.watch ? tryRender : renderFile; +var bigMode = files.length && files.every(_ => fs.lstatSync(_).isDirectory()) && !!program.big; +var bigModeFirstFileWrote = false; +if (bigMode) { + options.client = true; + program.nameAfterFile = true; +} + // compile files if (files.length) { @@ -185,6 +193,7 @@ function watchFile(path, base, rootPath) { if (curr.mtime.getTime() === 0) return; // istanbul ignore if if (curr.mtime.getTime() === prev.mtime.getTime()) return; + bigModeFirstFileWrote = false; watchList[path].forEach(function(file) { tryRender(file, rootPath); }); @@ -286,8 +295,15 @@ function renderFile(path, rootPath) { var dir = resolve(dirname(path)); mkdirp.sync(dir); var output = options.client ? fn : fn(options); - fs.writeFileSync(path, output); - consoleLog(' ' + chalk.gray('rendered') + ' ' + chalk.cyan('%s'), normalize(path)); + if (bigMode) { + + fs[!bigModeFirstFileWrote ? 'writeFileSync' : 'appendFileSync'](program.big, output); + bigModeFirstFileWrote = true; + consoleLog(' ' + chalk.gray(`appended ${path} to `) + ' ' + chalk.cyan('%s'), normalize(program.big)); + } else { + fs.writeFileSync(path, output); + consoleLog(' ' + chalk.gray('rendered') + ' ' + chalk.cyan('%s'), normalize(path)); + } // Found directory } else if (stat.isDirectory()) { var files = fs.readdirSync(path); From 876801c2f99df9f1af5fa74a780a8584b44e6ca5 Mon Sep 17 00:00:00 2001 From: Ocoka Date: Sun, 7 Mar 2021 10:32:37 +0300 Subject: [PATCH 2/2] "big" and "wathc" mode together fix --- index.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 633d687..f785de7 100644 --- a/index.js +++ b/index.js @@ -193,13 +193,23 @@ function watchFile(path, base, rootPath) { if (curr.mtime.getTime() === 0) return; // istanbul ignore if if (curr.mtime.getTime() === prev.mtime.getTime()) return; - bigModeFirstFileWrote = false; + if (bigMode) { + bigModeReRenderAll(); + return; + } watchList[path].forEach(function(file) { tryRender(file, rootPath); }); }); } +function bigModeReRenderAll() { + bigModeFirstFileWrote = false; + files.forEach(function (file) { + render(file); + }); +} + /** * Convert error to string */ @@ -296,10 +306,9 @@ function renderFile(path, rootPath) { mkdirp.sync(dir); var output = options.client ? fn : fn(options); if (bigMode) { - fs[!bigModeFirstFileWrote ? 'writeFileSync' : 'appendFileSync'](program.big, output); + consoleLog(' ' + chalk.gray(`${bigModeFirstFileWrote ? 'appended' : 'writed'} ${path} to `) + ' ' + chalk.cyan('%s'), normalize(program.big)); bigModeFirstFileWrote = true; - consoleLog(' ' + chalk.gray(`appended ${path} to `) + ' ' + chalk.cyan('%s'), normalize(program.big)); } else { fs.writeFileSync(path, output); consoleLog(' ' + chalk.gray('rendered') + ' ' + chalk.cyan('%s'), normalize(path));