mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Additional fix for #188 - Improvements to the Node.js require extension
This commit is contained in:
parent
4f150a34e5
commit
864c168368
@ -35,10 +35,16 @@ function compile(templatePath, markoCompiler, compilerOptions) {
|
|||||||
templateSrc = fs.readFileSync(templatePath, fsReadOptions);
|
templateSrc = fs.readFileSync(templatePath, fsReadOptions);
|
||||||
compiledSrc = markoCompiler.compile(templateSrc, templatePath);
|
compiledSrc = markoCompiler.compile(templateSrc, templatePath);
|
||||||
} else {
|
} else {
|
||||||
var targetDir = path.dirname(templatePath);
|
|
||||||
|
|
||||||
var targetFile = templatePath + '.js';
|
var targetFile = templatePath + '.js';
|
||||||
|
|
||||||
|
if (markoCompiler.defaultOptions.assumeUpToDate && fs.existsSync(targetFile)) {
|
||||||
|
// If the target file already exists and "assumeUpToDate" then just use the previously
|
||||||
|
// compiled template.
|
||||||
|
return fs.readFileSync(targetFile, fsReadOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
var targetDir = path.dirname(templatePath);
|
||||||
|
|
||||||
var isUpToDate = markoCompiler.checkUpToDate(targetFile);
|
var isUpToDate = markoCompiler.checkUpToDate(targetFile);
|
||||||
|
|
||||||
if (isUpToDate) {
|
if (isUpToDate) {
|
||||||
@ -48,7 +54,7 @@ function compile(templatePath, markoCompiler, compilerOptions) {
|
|||||||
compiledSrc = markoCompiler.compile(templateSrc, templatePath);
|
compiledSrc = markoCompiler.compile(templateSrc, templatePath);
|
||||||
|
|
||||||
// Write to a temporary file and move it into place to avoid problems
|
// Write to a temporary file and move it into place to avoid problems
|
||||||
// assocatiated with multiple processes write to teh same file. We only
|
// assocatiated with multiple processes write to the same file. We only
|
||||||
// write the compiled source code to disk so that stack traces will
|
// write the compiled source code to disk so that stack traces will
|
||||||
// be accurate.
|
// be accurate.
|
||||||
var filename = path.basename(targetFile);
|
var filename = path.basename(targetFile);
|
||||||
@ -78,6 +84,14 @@ exports.install = function(options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
require.extensions[extension] = function markoExtension(module, filename) {
|
require.extensions[extension] = function markoExtension(module, filename) {
|
||||||
|
var targetFile = filename + '.js';
|
||||||
|
var loaded = require.cache[targetFile];
|
||||||
|
if (loaded) {
|
||||||
|
// The template has already been loaded so use the exports of the already loaded template
|
||||||
|
module.exports = loaded.exports;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Resolve the appropriate compiler relative to the location of the
|
// Resolve the appropriate compiler relative to the location of the
|
||||||
// marko template file on disk using the "resolve-from" module.
|
// marko template file on disk using the "resolve-from" module.
|
||||||
var dirname = path.dirname(filename);
|
var dirname = path.dirname(filename);
|
||||||
@ -90,6 +104,6 @@ exports.install = function(options) {
|
|||||||
|
|
||||||
// Append ".js" to the filename since that is where we write the compiled
|
// Append ".js" to the filename since that is where we write the compiled
|
||||||
// source code that is being loaded. This allows stack traces to match up.
|
// source code that is being loaded. This allows stack traces to match up.
|
||||||
module._compile(compiledSrc, filename + '.js');
|
module._compile(compiledSrc, targetFile);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user