basePath implementation

This commit is contained in:
guybedford 2015-08-21 18:50:33 +02:00
parent 3c1511a776
commit 06f5d77f8b
10 changed files with 30 additions and 11 deletions

View File

@ -5,6 +5,7 @@
*
* System.packages = {
* jquery: {
* basePath: 'lib', // optionally only use a subdirectory within the package
* main: 'index.js', // when not set, package name is requested directly
* format: 'amd',
* defaultExtension: 'js',
@ -138,16 +139,28 @@
}
}
function getBasePath(pkg) {
// sanitize basePath
var basePath = pkg.basePath && pkg.basePath != '.' ? pkg.basePath : '';
if (basePath) {
if (basePath.substr(0, 2) == './')
basePath = basePath.substr(2);
if (basePath[basePath.length - 1] != '/')
basePath += '/';
}
return basePath;
}
function applyPackageConfig(normalized, pkgName, sync, defaultJSExtension) {
var loader = this;
var pkg = loader.packages[pkgName];
var basePath = getBasePath(pkg);
// main
if (pkgName === normalized && pkg.main)
normalized += '/' + (pkg.main.substr(0, 2) == './' ? pkg.main.substr(2) : pkg.main);
if (normalized.substr(pkgName.length) == '/')
return normalized;
// defaultExtension & defaultJSExtension
// if we have meta for this package, don't do defaultExtensions
var defaultExtension = '';
@ -164,9 +177,13 @@
}
}
// no submap if name is package itself
if (normalized.length == pkgName.length)
return normalized + defaultExtension;
// sync normalize does not apply package map
if (sync || !pkg.map)
return normalized + defaultExtension;
return pkgName + '/' + basePath + normalized.substr(pkgName.length + 1) + defaultExtension;
var subPath = '.' + normalized.substr(pkgName.length);
@ -186,14 +203,13 @@
normalized = loader.normalizeSync(pkgName);
// internal package map
else if (mapped.substr(0, 2) == './')
normalized = pkgName + mapped.substr(1);
normalized = pkgName + '/' + basePath + mapped.substr(2);
// global package map
else
normalized = normalize.call(loader, mapped);
}
else {
normalized += defaultExtension;
}
else
normalized = pkgName + '/' + basePath + normalized.substr(pkgName.length + 1) + defaultExtension;
return normalized;
});
}
@ -309,6 +325,8 @@
var pkgName = getPackage.call(loader, load.name);
if (pkgName) {
var pkg = loader.packages[pkgName];
var basePath = getBasePath(pkg);
// format
if (pkg.format)
@ -333,7 +351,7 @@
if (wildcardIndex === -1)
continue;
if (module.substr(0, wildcardIndex) === load.name.substr(0, wildcardIndex)
if (basePath + module.substr(0, wildcardIndex) === load.name.substr(pkgName.length + 1, wildcardIndex + basePath.length)
&& module.substr(wildcardIndex + 1) === load.name.substr(load.name.length - module.length + wildcardIndex + 1)) {
var depth = module.split('/').length;
if (depth > bestDepth)
@ -342,9 +360,9 @@
}
}
// exact meta
var metaName = load.name.substr(pkgName.length + 1);
var metaName = load.name.substr(pkgName.length + basePath.length + 1);
var exactMeta = pkg.meta[metaName] || pkg.meta['./' + metaName];
if (exactMeta)
if (exactMeta && load.name.substr(pkgName.length + 1, basePath.length) == basePath)
extendMeta(meta, exactMeta);
// allow alias and loader to be package-relative

View File

@ -1,5 +1,6 @@
{
"main": "wrong-main",
"basePath": "lib",
"format": "cjs",
"defaultExtension": "js",
"meta": {