diff --git a/lib/compile.js b/lib/compile.js index b837f32..48c8f64 100644 --- a/lib/compile.js +++ b/lib/compile.js @@ -378,8 +378,8 @@ function wrapSFXOutputs(loader, tree, outputs, entryPoints, compileOpts, cache) }); var externalDepIds = externalDeps.map(function(dep) { - if (compileOpts.format == 'global' || compileOpts.format == 'umd' && (compileOpts.globalName || - Object.keys(compileOpts.globalDeps).length > 0)) { + if (compileOpts.format == 'global' || + compileOpts.format == 'umd' && (compileOpts.globalName || Object.keys(compileOpts.globalDeps).length > 0)) { var alias = getAlias(loader, dep); var globalDep = compileOpts.globalDeps[dep] || compileOpts.globalDeps[alias]; if (!globalDep) @@ -400,10 +400,6 @@ function wrapSFXOutputs(loader, tree, outputs, entryPoints, compileOpts, cache) return dep; }); - if (compileOpts.globalName && globalDeps.length != externalDeps.length) { - throw new Error('not enough globalDeps'); - } - // next wrap with the core code return asp(fs.readFile)(path.resolve(__dirname, (allRegister ? '../templates/sfx-core-register.min.js' : '../templates/sfx-core.min.js'))) .then(function(sfxcore) { diff --git a/lib/utils.js b/lib/utils.js index 8be9b30..bb664f0 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -72,20 +72,31 @@ function getAlias(loader, canonicalName) { if (canonicalName.match(/\#[\:\{\?]/)) throw new Error('Unable to alias the conditional dependency "' + canonicalName + '".'); - var bestAlias; + var bestAliasLength = 0; + var bestAliasSubpath; + Object.keys(loader.map).forEach(function(alias) { + if (alias.split('/').length <= bestAliasLength) + return; - function getBestAlias(mapped) { - return canonicalName.substr(0, mapped.length) == mapped && - (canonicalName.length == mapped.length || canonicalName[mapped.length] == '/'); - } + // get mapped without defaultJSExtension + var mapped = normalizePath(loader, loader.map[alias], true); - Object.keys(loader.map).forEach(function(alias) { - if (getBestAlias(loader.map[alias])) + // do matching with defaultJSExtension checking + if (loader.defaultJSExtensions && canonicalName == mapped + '.js') { bestAlias = alias; + bestAliasSubpath = ''; + bestAliasLength = alias.split('/').length; + } + else if (canonicalName.substr(0, mapped.length) == mapped && + (canonicalName.length == mapped.length || canonicalName[mapped.length] == '/')) { + bestAlias = alias; + bestAliasSubpath = canonicalName.substr(alias.length); + bestAliasLength = alias.split('/').length; + } }); if (bestAlias) - return bestAlias + canonicalName.substr(loader.map[bestAlias].length); + return bestAlias + bestAliasSubpath; return canonicalName; } @@ -161,7 +172,7 @@ function getCanonicalNamePlain(loader, normalized, isPlugin) { if (loader.paths[p].indexOf('*') != -1) continue; - var curPath = normalizePath(loader, p, isPlugin); + var curPath = normalizePath(loader, loader.paths[p], isPlugin); // always stop on first exact match if (normalized === curPath) @@ -181,7 +192,7 @@ function getCanonicalNamePlain(loader, normalized, isPlugin) { continue; // normalize the output path - var curPath = normalizePath(loader, p, true); + var curPath = normalizePath(loader, loader.paths[p], true); // do reverse match var wIndex = curPath.indexOf('*'); @@ -297,10 +308,10 @@ function getPackage(packages, name) { var absURLRegEx = /^[^\/]+:\/\//; function normalizePath(loader, path, skipExtension) { var curPath; - if (loader.paths[path][0] == '.') - curPath = decodeURI(url.resolve(toFileURL(process.cwd()) + '/', loader.paths[path])); + if (path[0] == '.') + curPath = decodeURI(url.resolve(toFileURL(process.cwd()) + '/', path)); else - curPath = decodeURI(url.resolve(loader.baseURL, loader.paths[path])); + curPath = decodeURI(url.resolve(loader.baseURL, path)); if (loader.defaultJSExtensions && !skipExtension && curPath.substr(curPath.length - 3, 3) != '.js') curPath += '.js'; return curPath;