mirror of
https://github.com/marko-js/marko.git
synced 2026-01-25 15:03:04 +00:00
77 lines
1.7 KiB
JavaScript
77 lines
1.7 KiB
JavaScript
var path = require('path');
|
|
var resolveFrom = require('resolve-from');
|
|
var Template = require('./html/Template');
|
|
|
|
function getDeps(template) {
|
|
if (!template.meta && template.template) {
|
|
template = template.template;
|
|
}
|
|
|
|
if (!(template instanceof Template)) {
|
|
return [];
|
|
}
|
|
|
|
if (false && template.deps) {
|
|
return template.deps;
|
|
}
|
|
|
|
if (!template.meta) {
|
|
console.error('Metadata not set for template at ', template.path);
|
|
return [];
|
|
}
|
|
|
|
var meta = template.meta;
|
|
var root = path.dirname(template.path);
|
|
var deps = [];
|
|
|
|
if (meta.tags) {
|
|
meta.tags.forEach(tagPath => {
|
|
var tag = resolveFrom(root, tagPath);
|
|
var tagDeps = getDeps(require(tag));
|
|
deps.push.apply(deps, tagDeps);
|
|
});
|
|
}
|
|
|
|
if (meta.deps) {
|
|
deps.push.apply(deps, meta.deps.map(d => resolveDep(d, root)));
|
|
}
|
|
|
|
deps = dedupeDeps(deps);
|
|
|
|
template.deps = deps;
|
|
|
|
return deps;
|
|
}
|
|
|
|
function resolveDep(dep, root) {
|
|
if (typeof dep === 'string') {
|
|
dep = parseDependencyString(dep);
|
|
}
|
|
if (dep.path) {
|
|
return Object.assign({}, dep, {
|
|
path: resolveFrom(root, dep.path)
|
|
});
|
|
} else if (dep.virtualPath) {
|
|
return Object.assign({}, dep, {
|
|
virtualPath: path.resolve(root, dep.virtualPath)
|
|
});
|
|
} else {
|
|
return dep;
|
|
}
|
|
}
|
|
|
|
function parseDependencyString(string) {
|
|
var match = /^(?:([\w-]+)(?:\:\s*|\s+))?(.*?(?:\.(\w+))?)$/.exec(string);
|
|
return {
|
|
type: match[1] || match[3],
|
|
path: match[2]
|
|
};
|
|
}
|
|
|
|
function dedupeDeps(deps) {
|
|
return deps;
|
|
}
|
|
|
|
Template.prototype.getDependencies = function() {
|
|
return getDeps(this);
|
|
}; |