Skip to content

Commit

Permalink
Cleanup support for scoped packages
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-steele-idem committed Dec 10, 2015
1 parent be298e8 commit a55f3bb
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions compiler/taglibs/taglib-finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,39 +87,52 @@ function tryNodeModules(parent, helper) {

if ((nodeModulesDir = realpathCached(nodeModulesDir))) {
taglibsForNodeModulesDir = [];
var children = fs.readdirSync(nodeModulesDir);
// Add support for npm scoped packages
children.forEach(function(scope, index, arr) {
if (/^\@\w+$/.test(scope)) {
var scoped = fs.readdirSync(nodePath.join(nodeModulesDir, scope));
var splice = [index, 1];
scoped.forEach(function(s) { splice.push(scope + '/' + s) });
Array.prototype.splice.apply(arr, splice);
}
});
children.forEach(function(moduleDirBasename) {

var handlePackageDir = function(packageName) {
// Fixes https://github.com/marko-js/marko/issues/140
// If the same node_module is found multiple times then only load the first one.
// Only the package name (that is: node_modules/<module_name>) matters and the
// package version does not matter.
if (helper.foundTaglibPackages[moduleDirBasename]) {
if (helper.foundTaglibPackages[packageName]) {
return;
}

helper.foundTaglibPackages[moduleDirBasename] = true;
helper.foundTaglibPackages[packageName] = true;

var moduleDir = nodePath.join(nodeModulesDir, moduleDirBasename);
var moduleDir = nodePath.join(nodeModulesDir, packageName);
var taglibPath = nodePath.join(moduleDir, 'marko-taglib.json');

if (existsCached(taglibPath)) {
taglibPath = fs.realpathSync(taglibPath);

var taglib = taglibLoader.load(taglibPath);
taglib.moduleName = moduleDirBasename;
taglib.moduleName = packageName;
taglibsForNodeModulesDir.push(taglib);
helper.addTaglib(taglib);
}
});
};

fs.readdirSync(nodeModulesDir)
.forEach(function(packageName) {
if (packageName.charAt(0) === '@') {
// Add support for npm scoped packages. Scoped packages
// get instaled into subdirectories organized by user.
// For example:
// node_modules/@foo/my-package
// node_modules/@foo/another-package

var scope = packageName; // e.g. scope = '@foo'

// We need to loop over the nested directory to automatically
// discover taglibs exported by scoped packages.
fs.readdirSync(nodePath.join(nodeModulesDir, scope))
.forEach(function(packageName) {
handlePackageDir(scope + '/' + packageName); // @foo/my-package
});
} else {
handlePackageDir(packageName);
}
});

taglibsForNodeModulesDirCache[nodeModulesDir] = taglibsForNodeModulesDir.length ? taglibsForNodeModulesDir : null;
} else {
Expand Down

0 comments on commit a55f3bb

Please sign in to comment.