Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 };
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' }
]);
});
});
});