Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.

Commit afca32c

Browse files
committed
Add support for object syntax
1 parent fe3113b commit afca32c

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,25 @@ export default {
4343
{find:'somelibrary-1.0.0', replacement: './mylocallibrary-1.5.0'}, //remap a library with a specific version
4444
{find:/^i18n\!(.*)/, replacement: '$1.js'}, //remove something in front of the import and append an extension (e.g. loaders, for files that were previously transpiled via the AMD module, to properly handle them in rollup as internals now)
4545
//for whatever reason, replace all .js extensions with .wasm
46-
{find:/^(.*)\.js$/, replacement: '$1.wasm'}
46+
{find:/^(.*)\.js$/, replacement: '$1.wasm'}
4747
]
4848
})
4949
],
5050
};
51+
52+
// or with object syntax
53+
export default {
54+
input: './src/index.js',
55+
plugins: [
56+
alias({
57+
resolve: ['.jsx', '.js'],
58+
entries: {
59+
'something': '../../../something',
60+
'somelibrary-1.0.0': './mylocallibrary-1.5.0',
61+
}
62+
})
63+
],
64+
};
5165
```
5266
The order of the entries is important, in that the first rules are applied first.
5367

src/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,24 @@ const normalizeId = (id) => {
4040
return id;
4141
};
4242

43+
const getEntries = ({ entries }) => {
44+
if (!entries) {
45+
return [];
46+
}
47+
48+
if (Array.isArray(entries)) {
49+
return entries;
50+
}
51+
52+
return Object.keys(entries).map(key => ({ find: key, replacement: entries[key] }));
53+
};
54+
4355
export default function alias(options = {}) {
4456
const resolve = Array.isArray(options.resolve) ? options.resolve : ['.js'];
45-
const entries = options.entries?options.entries:[];
57+
const entries = getEntries(options);
4658

4759
// No aliases?
48-
if (!entries || entries.length === 0) {
60+
if (entries.length === 0) {
4961
return {
5062
resolveId: noop,
5163
};

test/index.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ test('defaults', (t) => {
2323
t.is(typeof result.resolveId, 'function');
2424
});
2525

26-
test('Simple aliasing', (t) => {
26+
test('Simple aliasing (array)', (t) => {
2727
const result = alias({
2828
entries: [
2929
{find:'foo', replacement:'bar'},
@@ -41,6 +41,24 @@ test('Simple aliasing', (t) => {
4141
t.is(resolved3, 'global');
4242
});
4343

44+
test('Simple aliasing (object)', (t) => {
45+
const result = alias({
46+
entries: {
47+
foo: 'bar',
48+
pony: 'paradise',
49+
'./local': 'global'
50+
}
51+
});
52+
53+
const resolved = result.resolveId('foo', '/src/importer.js');
54+
const resolved2 = result.resolveId('pony', '/src/importer.js');
55+
const resolved3 = result.resolveId('./local', '/src/importer.js');
56+
57+
t.is(resolved, 'bar');
58+
t.is(resolved2, 'paradise');
59+
t.is(resolved3, 'global');
60+
});
61+
4462
test('RegExp aliasing', (t) => {
4563
const result = alias({
4664
entries: [

0 commit comments

Comments
 (0)