mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Simplified checkUpToDate() and added getLastModified()
This commit is contained in:
parent
2843f29c81
commit
e49d0f31cb
@ -30,6 +30,7 @@ var ElementNode = require('./ElementNode');
|
|||||||
var TextNode = require('./TextNode');
|
var TextNode = require('./TextNode');
|
||||||
var TagHandlerNode = require('../taglibs/core/TagHandlerNode');
|
var TagHandlerNode = require('../taglibs/core/TagHandlerNode');
|
||||||
var raptorModulesResolver = require('raptor-modules/resolver');
|
var raptorModulesResolver = require('raptor-modules/resolver');
|
||||||
|
var fs = require('fs');
|
||||||
|
|
||||||
function TemplateCompiler(path, options) {
|
function TemplateCompiler(path, options) {
|
||||||
this.dirname = nodePath.dirname(path);
|
this.dirname = nodePath.dirname(path);
|
||||||
@ -54,7 +55,7 @@ TemplateCompiler.prototype = {
|
|||||||
function transformTreeHelper(node) {
|
function transformTreeHelper(node) {
|
||||||
try {
|
try {
|
||||||
_this.taglibs.forEachNodeTransformer(node, function (transformer) {
|
_this.taglibs.forEachNodeTransformer(node, function (transformer) {
|
||||||
|
|
||||||
if (!node.isTransformerApplied(transformer)) {
|
if (!node.isTransformerApplied(transformer)) {
|
||||||
//Check to make sure a transformer of a certain type is only applied once to a node
|
//Check to make sure a transformer of a certain type is only applied once to a node
|
||||||
node.setTransformerApplied(transformer);
|
node.setTransformerApplied(transformer);
|
||||||
@ -122,7 +123,7 @@ TemplateCompiler.prototype = {
|
|||||||
var err = createError(new Error('An error occurred while trying to compile template at path "' + filePath + '". Exception: ' + (e.stack || e)), e);
|
var err = createError(new Error('An error occurred while trying to compile template at path "' + filePath + '". Exception: ' + (e.stack || e)), e);
|
||||||
return returnError(err);
|
return returnError(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
/*
|
/*
|
||||||
* The tree has been transformed and we can now generate
|
* The tree has been transformed and we can now generate
|
||||||
@ -132,7 +133,7 @@ TemplateCompiler.prototype = {
|
|||||||
var err = createError(new Error('An error occurred while trying to compile template at path "' + filePath + '". Exception: ' + e), e);
|
var err = createError(new Error('An error occurred while trying to compile template at path "' + filePath + '". Exception: ' + e), e);
|
||||||
return returnError(err);
|
return returnError(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.hasErrors()) {
|
if (this.hasErrors()) {
|
||||||
var message = 'An error occurred while trying to compile template at path "' + filePath + '". Error(s) in template:\n';
|
var message = 'An error occurred while trying to compile template at path "' + filePath + '". Error(s) in template:\n';
|
||||||
var errors = _this.getErrors();
|
var errors = _this.getErrors();
|
||||||
@ -226,7 +227,7 @@ TemplateCompiler.prototype = {
|
|||||||
var tagName = Ctor;
|
var tagName = Ctor;
|
||||||
Ctor = this.getNodeClass(tagName);
|
Ctor = this.getNodeClass(tagName);
|
||||||
}
|
}
|
||||||
|
|
||||||
ok(Ctor != null, 'Ctor is required');
|
ok(Ctor != null, 'Ctor is required');
|
||||||
ok(typeof Ctor === 'function', 'Ctor should be a function');
|
ok(typeof Ctor === 'function', 'Ctor should be a function');
|
||||||
|
|
||||||
@ -239,14 +240,38 @@ TemplateCompiler.prototype = {
|
|||||||
return new TextNode(text, escapeXml);
|
return new TextNode(text, escapeXml);
|
||||||
},
|
},
|
||||||
|
|
||||||
checkUpToDate: function(sourceFile, targetFile) {
|
getLastModified: function() {
|
||||||
|
var sourceFile = this.path;
|
||||||
|
|
||||||
|
var statSource = fs.statSync(sourceFile);
|
||||||
|
|
||||||
|
var lastModifiedTime = statSource.mtime.getTime();
|
||||||
|
|
||||||
|
var taglibFiles = this.taglibs.getInputFiles();
|
||||||
|
var len = taglibFiles.length;
|
||||||
|
for (var i=0; i<len; i++) {
|
||||||
|
var taglibFileStat;
|
||||||
|
var taglibFile = taglibFiles[i];
|
||||||
|
|
||||||
|
try {
|
||||||
|
taglibFileStat = fs.statSync(taglibFile);
|
||||||
|
} catch(e) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
lastModifiedTime = Math.max(lastModifiedTime, taglibFileStat.mtime.getTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
return lastModifiedTime;
|
||||||
|
},
|
||||||
|
|
||||||
|
checkUpToDate: function(targetFile) {
|
||||||
if (this.options.checkUpToDate === false) {
|
if (this.options.checkUpToDate === false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var fs = require('fs');
|
|
||||||
|
|
||||||
|
var sourceFile = this.path;
|
||||||
|
|
||||||
var statTarget;
|
var statTarget;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -262,7 +287,7 @@ TemplateCompiler.prototype = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Now check if any of the taglib files have been modified after the target file was generated
|
// Now check if any of the taglib files have been modified after the target file was generated
|
||||||
|
|
||||||
var taglibFiles = this.taglibs.getInputFiles();
|
var taglibFiles = this.taglibs.getInputFiles();
|
||||||
var len = taglibFiles.length;
|
var len = taglibFiles.length;
|
||||||
for (var i=0; i<len; i++) {
|
for (var i=0; i<len; i++) {
|
||||||
@ -287,4 +312,4 @@ TemplateCompiler.prototype = {
|
|||||||
return raptorModulesResolver.deresolve(targetModuleFile, this.dirname);
|
return raptorModulesResolver.deresolve(targetModuleFile, this.dirname);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
module.exports = TemplateCompiler;
|
module.exports = TemplateCompiler;
|
||||||
|
|||||||
@ -84,6 +84,16 @@ extend(exports, {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
getLastModified: function(path, options, callback) {
|
||||||
|
if (typeof options === 'function') {
|
||||||
|
callback = options;
|
||||||
|
options = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var compiler = this.createCompiler(path, options);
|
||||||
|
callback(null, compiler.getLastModified());
|
||||||
|
},
|
||||||
|
|
||||||
Node: require('./Node'),
|
Node: require('./Node'),
|
||||||
ElementNode: require('./ElementNode'),
|
ElementNode: require('./ElementNode'),
|
||||||
TextNode: require('./TextNode'),
|
TextNode: require('./TextNode'),
|
||||||
|
|||||||
@ -21,10 +21,10 @@ function loadSource(templatePath, compiledSrc) {
|
|||||||
|
|
||||||
module.exports = function load(templatePath) {
|
module.exports = function load(templatePath) {
|
||||||
templatePath = nodePath.resolve(cwd, templatePath);
|
templatePath = nodePath.resolve(cwd, templatePath);
|
||||||
|
|
||||||
var targetFile = templatePath + '.js';
|
var targetFile = templatePath + '.js';
|
||||||
var compiler = raptorTemplatesCompiler.createCompiler(templatePath);
|
var compiler = raptorTemplatesCompiler.createCompiler(templatePath);
|
||||||
var isUpToDate = compiler.checkUpToDate(templatePath, targetFile);
|
var isUpToDate = compiler.checkUpToDate(targetFile);
|
||||||
if (isUpToDate) {
|
if (isUpToDate) {
|
||||||
return require(targetFile);
|
return require(targetFile);
|
||||||
}
|
}
|
||||||
@ -33,10 +33,10 @@ module.exports = function load(templatePath) {
|
|||||||
var compiledSrc = compiler.compile(templateSrc);
|
var compiledSrc = compiler.compile(templateSrc);
|
||||||
|
|
||||||
// console.log('Compiled code for "' + templatePath + '":\n' + compiledSrc);
|
// console.log('Compiled code for "' + templatePath + '":\n' + compiledSrc);
|
||||||
|
|
||||||
fs.writeFileSync(targetFile, compiledSrc, {encoding: 'utf8'});
|
fs.writeFileSync(targetFile, compiledSrc, {encoding: 'utf8'});
|
||||||
|
|
||||||
return require(targetFile);
|
return require(targetFile);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.loadSource = loadSource;
|
module.exports.loadSource = loadSource;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user