Skip to content

Commit a40ed56

Browse files
author
Ocoka
committed
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
1 parent 0eb8d37 commit a40ed56

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

index.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ program
3434
.option('-b, --basedir <path>', 'path used as root directory to resolve absolute includes')
3535
.option('-P, --pretty', 'compile pretty HTML output')
3636
.option('-c, --client', 'compile function for client-side')
37+
.option('-B, --big <path>', 'create all template functions in one file (works only with directory as source, implies --client and --name-after-file)')
3738
.option('-n, --name <str>', 'the name of the compiled template (requires --client)')
3839
.option('-D, --no-debug', 'compile without debugging (smaller functions)')
3940
.option('-w, --watch', 'watch files for changes and automatically re-render')
@@ -136,6 +137,13 @@ var watchList = {};
136137
// function for rendering
137138
var render = program.watch ? tryRender : renderFile;
138139

140+
var bigMode = files.length && files.every(_ => fs.lstatSync(_).isDirectory()) && !!program.big;
141+
var bigModeFirstFileWrote = false;
142+
if (bigMode) {
143+
options.client = true;
144+
program.nameAfterFile = true;
145+
}
146+
139147
// compile files
140148

141149
if (files.length) {
@@ -185,6 +193,7 @@ function watchFile(path, base, rootPath) {
185193
if (curr.mtime.getTime() === 0) return;
186194
// istanbul ignore if
187195
if (curr.mtime.getTime() === prev.mtime.getTime()) return;
196+
bigModeFirstFileWrote = false;
188197
watchList[path].forEach(function(file) {
189198
tryRender(file, rootPath);
190199
});
@@ -286,8 +295,15 @@ function renderFile(path, rootPath) {
286295
var dir = resolve(dirname(path));
287296
mkdirp.sync(dir);
288297
var output = options.client ? fn : fn(options);
289-
fs.writeFileSync(path, output);
290-
consoleLog(' ' + chalk.gray('rendered') + ' ' + chalk.cyan('%s'), normalize(path));
298+
if (bigMode) {
299+
300+
fs[!bigModeFirstFileWrote ? 'writeFileSync' : 'appendFileSync'](program.big, output);
301+
bigModeFirstFileWrote = true;
302+
consoleLog(' ' + chalk.gray(`appended ${path} to `) + ' ' + chalk.cyan('%s'), normalize(program.big));
303+
} else {
304+
fs.writeFileSync(path, output);
305+
consoleLog(' ' + chalk.gray('rendered') + ' ' + chalk.cyan('%s'), normalize(path));
306+
}
291307
// Found directory
292308
} else if (stat.isDirectory()) {
293309
var files = fs.readdirSync(path);

0 commit comments

Comments
 (0)