Skip to content

Commit

Permalink
fix: refactored to use native new Function
Browse files Browse the repository at this point in the history
  • Loading branch information
niftylettuce committed Jan 24, 2019
1 parent 1a9c11b commit 60e60b7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 70 deletions.
75 changes: 12 additions & 63 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
const pug = require('pug');
Expand All @@ -15,7 +14,7 @@ class CachePugTemplates {
this.config = {
app: false,
views: false,
concurrency: 2,
concurrency: 1,
...config
};

Expand Down Expand Up @@ -74,75 +73,25 @@ class CachePugTemplates {
(filename, fn) => {
this.writeCache(filename, fn);
},
() => {
err => {
if (err) console.error(err);
debug('done with queue');
}
);
}

writeCache(filename, fn) {
debug(`compiling template located at ${filename}`);

const cat = spawn('node_modules/.bin/shx', ['cat', filename], {
cwd: __dirname
});

const cli = spawn(
'node_modules/.bin/pug',
['--return-function-only', '-p', filename],
{
cwd: __dirname
fs.readFile(filename, 'utf8', (err, str) => {
if (err) return fn(err);
try {
const options = { cache: true, filename };
if (pug.cache[filename]) return fn();
pug.cache[filename] = pug.compile(str, options);
fn();
} catch (err2) {
fn(err2);
}
);

let tmpl = '';

cat.on('close', code => {
debug(`child worker for cat ${filename} exited with code ${code}`);
cli.stdin.end();
});

cat.on('error', err => {
debug(`child worker for cat ${filename} had error ${err}`);
console.error(err);
});

cat.stdout.on('data', data => {
cli.stdin.write(data);
});

cli.on('close', code => {
debug(`child worker for pug ${filename} exited with code ${code}`);
(function() {
try {
tmpl = tmpl.trim();
if (tmpl) {
tmpl = tmpl.replace('function template(locals) {', '');
tmpl = tmpl.replace('return pug_html;}', 'return pug_html;');
// eslint-disable-next-line no-new-func
pug.cache[filename] = new Function('locals', tmpl);
}
} catch (err) {
err.message = `${filename}: ${err.message}`;
console.error(err);
}
})();

delete this.queuedFiles[filename];
fn();
});

cli.on('error', err => {
debug(`child worker for pug ${filename} had error ${err}`);
console.error(err);
});

cli.stdout.on('data', data => {
tmpl += data.toString();
});

cli.stderr.on('data', data => {
debug(`child worker for pug ${filename} had an error ${data}`);
});
}

Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
],
"dependencies": {
"async": "^2.6.1",
"debug": "^4.1.1",
"pug-cli": "git+https://github.com/niftylettuce/pug-cli.git",
"shx": "^0.3.2"
"debug": "^4.1.1"
},
"devDependencies": {
"ava": "^1.1.0",
Expand Down
8 changes: 4 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test.cb('rendering works', t => {
t.log(`str 2 ${pug.compileFile(home)()}`);
t.is(pug.cache[home](), pug.compileFile(home)());
t.end();
}, 15000);
}, 3000);
});
});

Expand All @@ -36,7 +36,7 @@ test.cb('email-templates', t => {
setTimeout(() => {
t.is(Object.keys(pug.cache).length, 3);
t.end();
}, 15000);
}, 3000);
});
});

Expand All @@ -55,7 +55,7 @@ test.cb('koa', t => {
setTimeout(() => {
t.is(Object.keys(pug.cache).length, 3);
t.end();
}, 15000);
}, 3000);
});
});
});
Expand All @@ -71,7 +71,7 @@ test.cb('express', t => {
setTimeout(() => {
t.is(Object.keys(pug.cache).length, 3);
t.end();
}, 15000);
}, 3000);
});
});
});
Expand Down

0 comments on commit 60e60b7

Please sign in to comment.