Improved how tags are discovered and merged

This commit is contained in:
Patrick Steele-Idem 2017-02-08 14:58:02 -08:00
parent 41b2c0878a
commit 6e17698a5c
8 changed files with 66 additions and 39 deletions

View File

@ -2,14 +2,13 @@
var taglibLoader = require('../taglib-loader');
var nodePath = require('path');
var fs = require('fs');
var lassoPackageRoot = require('lasso-package-root');
var resolveFrom = require('resolve-from');
var scanTagsDir = require('../taglib-loader/scanTagsDir');
var Taglib = require('../taglib-loader/Taglib');
var DependencyChain = require('../taglib-loader/DependencyChain');
var lassoCachingFS = require('lasso-caching-fs');
var existsCache = {};
var findCache = {};
var excludedDirs = {};
var excludedPackages = {};
@ -20,7 +19,7 @@ var taglibsForNodeModulesDirCache = {};
* was added for testing purposes.
*/
function reset() {
existsCache = {};
lassoCachingFS.clearCaches();
findCache = {};
excludedDirs = {};
excludedPackages = {};
@ -28,12 +27,7 @@ function reset() {
}
function existsCached(path) {
var exists = existsCache[path];
if (exists === undefined) {
exists = fs.existsSync(path);
existsCache[path] = exists;
}
return exists;
return lassoCachingFS.existsSync(path);
}
function getModuleRootPackage(dirname) {
@ -105,17 +99,23 @@ function find(dirname, registeredTaglibs) {
while(true) {
if(!excludedDirs[curDirname]) {
let taglibPath = nodePath.join(curDirname, 'marko.json');
let taglib;
if (existsCached(taglibPath)) {
let taglib = taglibLoader.load(taglibPath);
taglib = taglibLoader.load(taglibPath);
helper.addTaglib(taglib);
} else {
let componentPath = nodePath.join(curDirname, 'components');
if (existsCached(componentPath) && !excludedDirs[componentPath] && !helper.alreadyAdded(componentPath)) {
let taglib = new Taglib(componentPath);
scanTagsDir(componentPath, nodePath.dirname(componentPath), './components', taglib, new DependencyChain([componentPath]));
}
if (!taglib || taglib.tagsDir === undefined) {
let componentsPath = nodePath.join(curDirname, 'components');
if (existsCached(componentsPath) && !excludedDirs[componentsPath] && !helper.alreadyAdded(componentsPath)) {
let taglib = new Taglib(componentsPath);
scanTagsDir(componentsPath, nodePath.dirname(componentsPath), './components', taglib, new DependencyChain([componentsPath]));
helper.addTaglib(taglib);
}
}
}
if (curDirname === rootDirname) {
@ -150,7 +150,7 @@ function find(dirname, registeredTaglibs) {
}
function clearCache() {
existsCache = {};
lassoCachingFS.clearCaches();
findCache = {};
taglibsForNodeModulesDirCache = {};
}

View File

@ -12,27 +12,33 @@ function createCustomTagNodeFactory(tagDef) {
class Tag{
constructor() {
ok(arguments.length === 0);
this.taglibId = null;
this.taglibPath = null;
this.name = undefined;
this.renderer = null;
this.codeGeneratorModulePath = null;
this.nodeFactoryPath = null;
this.template = null;
this.attributes = {};
this.transformers = {};
this.nestedVariables = null;
this.importedVariables = null;
this.patternAttributes = [];
this.bodyFunction = null;
this.nestedTags = null;
this.isRepeated = null;
this.isNestedTag = false;
this.parentTagName = null;
this.openTagOnly = null;
this.body = null;
this.type = null; // Only applicable for nested tags
this._nodeFactory = undefined;
// NOTE: We don't set this properties since
// it breaks merging of tags when the same
// tag is declared at multiple levels
// this.taglibId = null;
// this.taglibPath = null;
// this.name = undefined;
// this.renderer = null;
// this.codeGeneratorModulePath = null;
// this.nodeFactoryPath = null;
// this.template = null;
// this.nestedVariables = null;
// this.importedVariables = null;
// this.bodyFunction = null;
// this.nestedTags = null;
// this.isRepeated = null;
// this.isNestedTag = false;
// this.parentTagName = null;
// this.openTagOnly = null;
// this.body = null;
// this.type = null; // Only applicable for nested tags
// this._nodeFactory = undefined;
}
forEachVariable(callback, thisObj) {

View File

@ -188,12 +188,16 @@ class TaglibLoader {
var path = this.filePath;
var dirname = this.dirname;
if (Array.isArray(dir)) {
for (var i = 0; i < dir.length; i++) {
scanTagsDir(path, dirname, dir[i], taglib, this.dependencyChain.append(`tags-dir[${i}]`));
taglib.tagsDir = dir;
if (dir != null) {
if (Array.isArray(dir)) {
for (var i = 0; i < dir.length; i++) {
scanTagsDir(path, dirname, dir[i], taglib, this.dependencyChain.append(`tags-dir[${i}]`));
}
} else {
scanTagsDir(path, dirname, dir, taglib, this.dependencyChain.append(`tags-dir`));
}
} else {
scanTagsDir(path, dirname, dir, taglib, this.dependencyChain.append(`tags-dir`));
}
}

View File

@ -61,6 +61,7 @@
"events-light": "^1.0.0",
"he": "^1.1.0",
"htmljs-parser": "^2.3.0",
"lasso-caching-fs": "^1.0.1",
"lasso-modules-client": "^2.0.3",
"lasso-package-root": "^1.0.0",
"listener-tracker": "^2.0.0",

View File

@ -0,0 +1 @@
<div>test-hello</div>

View File

@ -0,0 +1,3 @@
{
"tags-dir": null
}

View File

@ -0,0 +1 @@
<test-hello/>

View File

@ -0,0 +1,11 @@
var expect = require('chai').expect;
exports.templateData = {};
exports.checkError = function(e) {
//includes the tag it broke on
expect(e.message).to.contain('Unrecognized tag: test-hello');
//includes the line number of the template
expect(e.message).to.contain('template.marko:1:0');
};