diff --git a/compiler/marko-compiler.js b/compiler/marko-compiler.js index 7ff5dc44b..593d03a9f 100644 --- a/compiler/marko-compiler.js +++ b/compiler/marko-compiler.js @@ -17,6 +17,7 @@ var extend = require('raptor-util/extend'); var req = require; // Fool code inspectors used by client-side bundles var nodePath = require('path'); +var NODE_ENV = process.env.NODE_ENV; var defaultOptions = { /** @@ -62,7 +63,12 @@ var defaultOptions = { * compiled templates will not be written to disk (i.e., no `.marko.js` file will * 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') { diff --git a/runtime/loader.js b/runtime/loader.js index 98b0b95e5..22656945a 100644 --- a/runtime/loader.js +++ b/runtime/loader.js @@ -45,12 +45,36 @@ function loadSource(templatePath, compiledSrc) { } 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); 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 isUpToDate = compiler.checkUpToDate(targetFile); + if (isUpToDate) { return require(targetFile); }