mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Added code comments
This commit is contained in:
parent
e34d957f85
commit
c0f497443e
@ -17,12 +17,21 @@ function compile(templatePath, markoCompiler, compilerOptions) {
|
|||||||
} else {
|
} else {
|
||||||
var templateSrc = fs.readFileSync(templatePath, fsReadOptions);
|
var templateSrc = fs.readFileSync(templatePath, fsReadOptions);
|
||||||
compiledSrc = compiler.compile(templateSrc);
|
compiledSrc = compiler.compile(templateSrc);
|
||||||
|
|
||||||
|
// Write to a temporary file and move it into place to avoid problems
|
||||||
|
// assocatiated with multiple processes write to teh same file. We only
|
||||||
|
// write the compiled source code to disk so that stack traces will
|
||||||
|
// be accurate.
|
||||||
var filename = path.basename(targetFile);
|
var filename = path.basename(targetFile);
|
||||||
var tempFile = path.join(targetDir, '.' + process.pid + '.' + Date.now() + '.' + filename);
|
var tempFile = path.join(targetDir, '.' + process.pid + '.' + Date.now() + '.' + filename);
|
||||||
fs.writeFileSync(tempFile, compiledSrc, fsReadOptions);
|
fs.writeFileSync(tempFile, compiledSrc, fsReadOptions);
|
||||||
fs.renameSync(tempFile, targetFile);
|
fs.renameSync(tempFile, targetFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The compiled output is for a CommonJS module. The code that we want to load should
|
||||||
|
// actually export a loaded template instance so we append some additional code to
|
||||||
|
// load the compiled template and assign it to `module.exports`. We also attach a
|
||||||
|
// path to the compiled template so that hot reloading will work.
|
||||||
return compiledSrc + '\nexports.path=__filename;\nmodule.exports = require("marko").load(exports);';
|
return compiledSrc + '\nexports.path=__filename;\nmodule.exports = require("marko").load(exports);';
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,10 +51,18 @@ exports.install = function(options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
require.extensions[extension] = function markoExtension(module, filename) {
|
require.extensions[extension] = function markoExtension(module, filename) {
|
||||||
|
// Resolve the appropriate compiler relative to the location of the
|
||||||
|
// marko template file on disk using the "resolve-from" module.
|
||||||
var dirname = path.dirname(filename);
|
var dirname = path.dirname(filename);
|
||||||
var markoCompilerModulePath = resolveFrom(dirname, 'marko/compiler');
|
var markoCompilerModulePath = resolveFrom(dirname, 'marko/compiler');
|
||||||
var markoCompiler = require(markoCompilerModulePath);
|
var markoCompiler = require(markoCompilerModulePath);
|
||||||
|
|
||||||
|
// Now use the appropriate Marko compiler to compile the Marko template
|
||||||
|
// file to JavaScript source code:
|
||||||
var compiledSrc = compile(filename, markoCompiler, compilerOptions);
|
var compiledSrc = compile(filename, markoCompiler, compilerOptions);
|
||||||
|
|
||||||
|
// 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.
|
||||||
module._compile(compiledSrc, filename + '.js');
|
module._compile(compiledSrc, filename + '.js');
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user