From 06f5d77f8bd5877ec104106318e246fd047da231 Mon Sep 17 00:00:00 2001 From: guybedford Date: Fri, 21 Aug 2015 18:50:33 +0200 Subject: [PATCH] basePath implementation --- lib/package.js | 40 ++++++++++++++----- test/tests/testpkg.json | 1 + test/tests/testpkg/{ => lib}/dir/index.js | 0 test/tests/testpkg/{ => lib}/dir2/index.json | 0 .../testpkg/{ => lib}/env-module-browser.js | 0 test/tests/testpkg/{ => lib}/env-module.js | 0 test/tests/testpkg/{ => lib}/json.js | 0 test/tests/testpkg/{ => lib}/json.json | 0 test/tests/testpkg/{ => lib}/self.js | 0 test/tests/testpkg/{ => lib}/test.ts | 0 10 files changed, 30 insertions(+), 11 deletions(-) rename test/tests/testpkg/{ => lib}/dir/index.js (100%) rename test/tests/testpkg/{ => lib}/dir2/index.json (100%) rename test/tests/testpkg/{ => lib}/env-module-browser.js (100%) rename test/tests/testpkg/{ => lib}/env-module.js (100%) rename test/tests/testpkg/{ => lib}/json.js (100%) rename test/tests/testpkg/{ => lib}/json.json (100%) rename test/tests/testpkg/{ => lib}/self.js (100%) rename test/tests/testpkg/{ => lib}/test.ts (100%) diff --git a/lib/package.js b/lib/package.js index 5b3cd68e..4c953ce8 100644 --- a/lib/package.js +++ b/lib/package.js @@ -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 diff --git a/test/tests/testpkg.json b/test/tests/testpkg.json index f08bc18e..5d40be28 100644 --- a/test/tests/testpkg.json +++ b/test/tests/testpkg.json @@ -1,5 +1,6 @@ { "main": "wrong-main", + "basePath": "lib", "format": "cjs", "defaultExtension": "js", "meta": { diff --git a/test/tests/testpkg/dir/index.js b/test/tests/testpkg/lib/dir/index.js similarity index 100% rename from test/tests/testpkg/dir/index.js rename to test/tests/testpkg/lib/dir/index.js diff --git a/test/tests/testpkg/dir2/index.json b/test/tests/testpkg/lib/dir2/index.json similarity index 100% rename from test/tests/testpkg/dir2/index.json rename to test/tests/testpkg/lib/dir2/index.json diff --git a/test/tests/testpkg/env-module-browser.js b/test/tests/testpkg/lib/env-module-browser.js similarity index 100% rename from test/tests/testpkg/env-module-browser.js rename to test/tests/testpkg/lib/env-module-browser.js diff --git a/test/tests/testpkg/env-module.js b/test/tests/testpkg/lib/env-module.js similarity index 100% rename from test/tests/testpkg/env-module.js rename to test/tests/testpkg/lib/env-module.js diff --git a/test/tests/testpkg/json.js b/test/tests/testpkg/lib/json.js similarity index 100% rename from test/tests/testpkg/json.js rename to test/tests/testpkg/lib/json.js diff --git a/test/tests/testpkg/json.json b/test/tests/testpkg/lib/json.json similarity index 100% rename from test/tests/testpkg/json.json rename to test/tests/testpkg/lib/json.json diff --git a/test/tests/testpkg/self.js b/test/tests/testpkg/lib/self.js similarity index 100% rename from test/tests/testpkg/self.js rename to test/tests/testpkg/lib/self.js diff --git a/test/tests/testpkg/test.ts b/test/tests/testpkg/lib/test.ts similarity index 100% rename from test/tests/testpkg/test.ts rename to test/tests/testpkg/lib/test.ts