Skip to content
This repository has been archived by the owner on Oct 9, 2020. It is now read-only.

Commit

Permalink
comprehensive aliasing (#1551)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Feb 24, 2016
1 parent 6caf885 commit 9b1d18c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
8 changes: 2 additions & 6 deletions lib/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
37 changes: 24 additions & 13 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
Expand All @@ -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('*');
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 9b1d18c

Please sign in to comment.