Skip to content

Commit

Permalink
New mode added: -B --big-mode to compile in one file
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Ocoka committed Mar 7, 2021
1 parent 0eb8d37 commit aaadf6d
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ program
.option('-b, --basedir <path>', '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 <path>', 'create all template functions in one file (works only with directory as source, implies --client and --name-after-file)')
.option('-n, --name <str>', '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')
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
});
Expand All @@ -204,7 +213,7 @@ function errorToString(e) {
*
* This is used in watch mode.
*/
function tryRender(path, rootPath) {
function tryRender(path, rootPatр) {
try {
renderFile(path, rootPath);
} catch (e) {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit aaadf6d

Please sign in to comment.