FIxes #188 - Performance: Assume *.marko.js files are up-to-date in production

This commit is contained in:
Patrick Steele-Idem 2016-02-10 10:53:07 -07:00
parent 15c837f45d
commit cf53fe02c9
2 changed files with 32 additions and 2 deletions

View File

@ -17,6 +17,7 @@
var extend = require('raptor-util/extend'); var extend = require('raptor-util/extend');
var req = require; // Fool code inspectors used by client-side bundles var req = require; // Fool code inspectors used by client-side bundles
var nodePath = require('path'); var nodePath = require('path');
var NODE_ENV = process.env.NODE_ENV;
var defaultOptions = { var defaultOptions = {
/** /**
@ -62,7 +63,12 @@ var defaultOptions = {
* compiled templates will not be written to disk (i.e., no `.marko.js` file will * compiled templates will not be written to disk (i.e., no `.marko.js` file will
* be generated) * be generated)
*/ */
writeToDisk: true writeToDisk: true,
/**
* If true, then the compiled template on disk will assumed to be up-to-date if it exists.
*/
assumeUpToDate: NODE_ENV == null ? false : (NODE_ENV !== 'development' && NODE_ENV !== 'dev')
}; };
if (process.env.MARKO_CLEAN === '' || process.env.MARKO_CLEAN === 'true') { if (process.env.MARKO_CLEAN === '' || process.env.MARKO_CLEAN === 'true') {

View File

@ -45,12 +45,36 @@ function loadSource(templatePath, compiledSrc) {
} }
function loadFile(templatePath, options) { function loadFile(templatePath, options) {
var targetFile = templatePath + '.js';
// Short-circuit loading if the template has already been cached in the Node.js require cache
var cached = require.cache[targetFile];
if (cached) {
return cached.exports;
}
templatePath = nodePath.resolve(cwd, templatePath); templatePath = nodePath.resolve(cwd, templatePath);
var targetDir = nodePath.dirname(templatePath); var targetDir = nodePath.dirname(templatePath);
var targetFile = templatePath + '.js'; targetFile = templatePath + '.js';
// Check the require cache again after fully resolving the path
cached = require.cache[targetFile];
if (cached) {
return cached.exports;
}
// If the `assumeUpToDate` option is true then we just assume that the compiled template on disk is up-to-date
// if it exists
if (markoCompiler.defaultOptions.assumeUpToDate) {
if (fs.existsSync(targetFile)) {
return require(targetFile);
}
}
var compiler = markoCompiler.createCompiler(templatePath, options); var compiler = markoCompiler.createCompiler(templatePath, options);
var isUpToDate = compiler.checkUpToDate(targetFile); var isUpToDate = compiler.checkUpToDate(targetFile);
if (isUpToDate) { if (isUpToDate) {
return require(targetFile); return require(targetFile);
} }