Skip to content
Permalink
Browse files

fix(CLI-Bundler, Aliases): improve alias support

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 19ea1ec06ce1901e147ae1feaec16b79e83de1e4
Showing with 132 additions and 71 deletions.
  1. +4 −32 lib/build/bundled-source.js
  2. +27 −0 lib/build/module-id-processor.js
  3. +39 −39 package-lock.json
  4. +62 −0 spec/lib/build/module-id-processor.spec.js
@@ -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) {
@@ -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}`);

@@ -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;

@@ -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('/');
}
@@ -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 };

0 comments on commit 19ea1ec

Please sign in to comment.
You can’t perform that action at this time.