mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
56 lines
1.7 KiB
JavaScript
56 lines
1.7 KiB
JavaScript
var nodePath = require('path');
|
|
var fs = require('fs');
|
|
var Module = require('module').Module;
|
|
var markoCompiler = require('../compiler');
|
|
var cwd = process.cwd();
|
|
var fsReadOptions = {encoding: 'utf8'};
|
|
|
|
if (process.env.hasOwnProperty('MARKO_HOT_RELOAD')) {
|
|
require('../hot-reload').enable();
|
|
}
|
|
|
|
if (process.env.hasOwnProperty('MARKO_BROWSER_REFRESH')) {
|
|
require('../browser-refresh').enable();
|
|
}
|
|
|
|
function loadSource(templatePath, compiledSrc) {
|
|
var templateModulePath = templatePath + '.js';
|
|
|
|
var templateModule = new Module(templateModulePath, module);
|
|
templateModule.paths = Module._nodeModulePaths(nodePath.dirname(templateModulePath));
|
|
templateModule.filename = templateModulePath;
|
|
|
|
|
|
templateModule._compile(
|
|
compiledSrc,
|
|
templateModulePath);
|
|
|
|
return templateModule.exports;
|
|
}
|
|
|
|
module.exports = function load(templatePath) {
|
|
templatePath = nodePath.resolve(cwd, templatePath);
|
|
var targetDir = nodePath.dirname(templatePath);
|
|
|
|
var targetFile = templatePath + '.js';
|
|
var compiler = markoCompiler.createCompiler(templatePath);
|
|
var isUpToDate = compiler.checkUpToDate(targetFile);
|
|
if (isUpToDate) {
|
|
return require(targetFile);
|
|
}
|
|
|
|
var templateSrc = fs.readFileSync(templatePath, fsReadOptions);
|
|
var compiledSrc = compiler.compile(templateSrc);
|
|
|
|
// console.log('Compiled code for "' + templatePath + '":\n' + compiledSrc);
|
|
|
|
var filename = nodePath.basename(targetFile);
|
|
var tempFile = nodePath.join(targetDir, '.' + process.pid + '.' + Date.now() + '.' + filename);
|
|
fs.writeFileSync(tempFile, compiledSrc, fsReadOptions);
|
|
fs.renameSync(tempFile, targetFile);
|
|
|
|
return require(targetFile);
|
|
};
|
|
|
|
module.exports.loadSource = loadSource;
|