Skip to content

Commit

Permalink
basePath implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Aug 21, 2015
1 parent 3c1511a commit 06f5d77
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 11 deletions.
40 changes: 29 additions & 11 deletions lib/package.js
Expand Up @@ -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',
Expand Down Expand Up @@ -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 = '';
Expand All @@ -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);

Expand All @@ -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;
});
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/tests/testpkg.json
@@ -1,5 +1,6 @@
{
"main": "wrong-main",
"basePath": "lib",
"format": "cjs",
"defaultExtension": "js",
"meta": {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 06f5d77

Please sign in to comment.