mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Cleanup support for scoped packages
This commit is contained in:
parent
be298e867d
commit
a55f3bb82b
@ -87,39 +87,52 @@ function tryNodeModules(parent, helper) {
|
|||||||
|
|
||||||
if ((nodeModulesDir = realpathCached(nodeModulesDir))) {
|
if ((nodeModulesDir = realpathCached(nodeModulesDir))) {
|
||||||
taglibsForNodeModulesDir = [];
|
taglibsForNodeModulesDir = [];
|
||||||
var children = fs.readdirSync(nodeModulesDir);
|
|
||||||
// Add support for npm scoped packages
|
var handlePackageDir = function(packageName) {
|
||||||
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) {
|
|
||||||
// Fixes https://github.com/marko-js/marko/issues/140
|
// Fixes https://github.com/marko-js/marko/issues/140
|
||||||
// If the same node_module is found multiple times then only load the first one.
|
// 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
|
// Only the package name (that is: node_modules/<module_name>) matters and the
|
||||||
// package version does not matter.
|
// package version does not matter.
|
||||||
if (helper.foundTaglibPackages[moduleDirBasename]) {
|
if (helper.foundTaglibPackages[packageName]) {
|
||||||
return;
|
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');
|
var taglibPath = nodePath.join(moduleDir, 'marko-taglib.json');
|
||||||
|
|
||||||
if (existsCached(taglibPath)) {
|
if (existsCached(taglibPath)) {
|
||||||
taglibPath = fs.realpathSync(taglibPath);
|
taglibPath = fs.realpathSync(taglibPath);
|
||||||
|
|
||||||
var taglib = taglibLoader.load(taglibPath);
|
var taglib = taglibLoader.load(taglibPath);
|
||||||
taglib.moduleName = moduleDirBasename;
|
taglib.moduleName = packageName;
|
||||||
taglibsForNodeModulesDir.push(taglib);
|
taglibsForNodeModulesDir.push(taglib);
|
||||||
helper.addTaglib(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;
|
taglibsForNodeModulesDirCache[nodeModulesDir] = taglibsForNodeModulesDir.length ? taglibsForNodeModulesDir : null;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user