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
michaelw85 committed Apr 26, 2019

Verified

This commit was signed with the committer’s verified signature.
michaelw85 Michael Withagen
1 parent 5d39d85 commit 19ea1ec
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
@@ -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('/');
}
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 };
78 changes: 39 additions & 39 deletions package-lock.json
62 changes: 62 additions & 0 deletions spec/lib/build/module-id-processor.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const { toDotDot, fromDotDot, getAliases } = require('../../../lib/build/module-id-processor');

describe('module-id-processor', () => {
const moduleId = '../src/elements/hello-world.ts';
const escapedModuleId = '__dot_dot__/src/elements/hello-world.ts';
const paths = {
'resources': '../src',
'elements': '../src/elements'
};

describe('toDotDot', () => {
it('should replace ../ in module id', () => {
expect(toDotDot(moduleId)).toEqual(escapedModuleId);
});

it('should replace multiple ../ in module id', () => {
expect(toDotDot('../' + moduleId)).toEqual('__dot_dot__/' + escapedModuleId);
});
});

describe('fromDotDot', () => {
it('should convert moduleId to original path', () => {
expect(fromDotDot(escapedModuleId)).toEqual(moduleId);
});

it('should replace multiple ../ in moduleId', () => {
expect(fromDotDot('__dot_dot__/' + escapedModuleId)).toEqual('../' + moduleId);
});
});

describe('getAliases', () => {
it('should return a single match', () => {
expect(getAliases('../src/hello-world.ts', paths)).toEqual([
{ fromId: 'resources/hello-world.ts', toId: '__dot_dot__/src/hello-world.ts' }
]);
});

it('should return an empty array when no match is found', () => {
expect(getAliases('no/match/hello-world.ts', paths)).toEqual([]);
});

it('should return multiple matches', () => {
expect(getAliases(moduleId, paths)).toEqual([
{ fromId: 'resources/elements/hello-world.ts', toId: '__dot_dot__/src/elements/hello-world.ts' },
{ fromId: 'elements/hello-world.ts', toId: '__dot_dot__/src/elements/hello-world.ts' }
]);
});

it('should support different aliases with same paths', () => {
const duplicatePaths = {
...paths,
'@resources': '../src'
};

expect(getAliases(moduleId, duplicatePaths)).toEqual([
{ fromId: 'resources/elements/hello-world.ts', toId: '__dot_dot__/src/elements/hello-world.ts' },
{ fromId: 'elements/hello-world.ts', toId: '__dot_dot__/src/elements/hello-world.ts' },
{ fromId: '@resources/elements/hello-world.ts', toId: '__dot_dot__/src/elements/hello-world.ts' }
]);
});
});
});

0 comments on commit 19ea1ec

Please sign in to comment.