Skip to content

Commit

Permalink
fix(CLI-Bundler, Aliases): improve alias support
Browse files Browse the repository at this point in the history
Define multiple aliases when they can be applied for a path.

Closes #1093
  • Loading branch information
michaelw85 committed Apr 26, 2019
1 parent 5d39d85 commit 19ea1ec
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 71 deletions.
36 changes: 4 additions & 32 deletions lib/build/bundled-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const esTransform = require('./amodro-trace/read/es');
const allWriteTransforms = require('./amodro-trace/write/all');
const Utils = require('./utils');
const logger = require('aurelia-logging').getLogger('BundledSource');
const { getAliases, toDotDot } = require('./module-id-processor');

exports.BundledSource = class {
constructor(bundler, file) {
Expand Down Expand Up @@ -108,10 +109,9 @@ exports.BundledSource = class {
let moduleId = this.moduleId;
let modulePath = this.path;

let shortcut = possibleShortcut(moduleId, loaderConfig.paths);
if (shortcut) {
this.bundler.configTargetBundle.addAlias(shortcut.fromId, shortcut.toId);
}
getAliases(moduleId, loaderConfig.paths).forEach(alias => {
this.bundler.configTargetBundle.addAlias(alias.fromId, alias.toId);
});

logger.debug(`Tracing ${moduleId}`);

Expand Down Expand Up @@ -316,23 +316,6 @@ exports.BundledSource = class {
}
};

function possibleShortcut(moduleId, paths) {
const _moduleId = fromDotDot(moduleId);
for (let i = 0, keys = Object.keys(paths); i < keys.length; i++) {
let key = keys[i];
let target = paths[key];
if (key === 'root') continue;
if (key === target) continue;

if (_moduleId.startsWith(target + '/')) {
return {
fromId: toDotDot(key + _moduleId.slice(target.length)),
toId: toDotDot(moduleId)
};
}
}
}

function absoluteModuleId(baseId, moduleId) {
if (moduleId[0] !== '.') return moduleId;

Expand Down Expand Up @@ -375,14 +358,3 @@ function relativeModuleId(baseId, moduleId) {

return parts.join('/');
}

// if moduleId is above surface (default src/), the '../../' confuses hell out of
// requirejs as it tried to understand it as a relative module id.
// replace '..' with '__dot_dot__' to enforce absolute module id.
function toDotDot(moduleId) {
return moduleId.split('/').map(p => p === '..' ? '__dot_dot__' : p).join('/');
}

function fromDotDot(moduleId) {
return moduleId.split('/').map(p => p === '__dot_dot__' ? '..' : p).join('/');
}
27 changes: 27 additions & 0 deletions lib/build/module-id-processor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// if moduleId is above surface (default src/), the '../../' confuses hell out of
// requirejs as it tried to understand it as a relative module id.
// replace '..' with '__dot_dot__' to enforce absolute module id.
const toDotDot = (moduleId) => moduleId.split('/').map(p => p === '..' ? '__dot_dot__' : p).join('/');
const fromDotDot = (moduleId) => moduleId.split('/').map(p => p === '__dot_dot__' ? '..' : p).join('/');

const getAliases = (moduleId, paths) => {
const aliases = [];
const _moduleId = fromDotDot(moduleId);
for (let i = 0, keys = Object.keys(paths); i < keys.length; i++) {
let key = keys[i];
let target = paths[key];
if (key === 'root') continue;
if (key === target) continue;

if (_moduleId.startsWith(target + '/')) {
aliases.push({
fromId: toDotDot(key + _moduleId.slice(target.length)),
toId: toDotDot(moduleId)
});
}
}

return aliases;
};

module.exports = { toDotDot, fromDotDot, getAliases };
Loading

0 comments on commit 19ea1ec

Please sign in to comment.