- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(CLI-Bundler, Aliases): improve alias support
Define multiple aliases when they can be applied for a path. Closes #1093
1 parent
5d39d85
commit 19ea1ec
Showing
4 changed files
with
132 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' } | ||
]); | ||
}); | ||
}); | ||
}); |