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

Commit

Permalink
Add support for object syntax (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist authored and lukastaegert committed Oct 18, 2019
1 parent fe3113b commit 90f753c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,25 @@ export default {
{find:'somelibrary-1.0.0', replacement: './mylocallibrary-1.5.0'}, //remap a library with a specific version
{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)
//for whatever reason, replace all .js extensions with .wasm
{find:/^(.*)\.js$/, replacement: '$1.wasm'}
{find:/^(.*)\.js$/, replacement: '$1.wasm'}
]
})
],
};

// or with object syntax
export default {
input: './src/index.js',
plugins: [
alias({
resolve: ['.jsx', '.js'],
entries: {
something: '../../../something',
'somelibrary-1.0.0': './mylocallibrary-1.5.0',
}
})
],
};
```
The order of the entries is important, in that the first rules are applied first.

Expand Down
16 changes: 14 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,24 @@ const normalizeId = (id) => {
return id;
};

const getEntries = ({ entries }) => {
if (!entries) {
return [];
}

if (Array.isArray(entries)) {
return entries;
}

return Object.keys(entries).map(key => ({ find: key, replacement: entries[key] }));
};

export default function alias(options = {}) {
const resolve = Array.isArray(options.resolve) ? options.resolve : ['.js'];
const entries = options.entries?options.entries:[];
const entries = getEntries(options);

// No aliases?
if (!entries || entries.length === 0) {
if (entries.length === 0) {
return {
resolveId: noop,
};
Expand Down
20 changes: 19 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test('defaults', (t) => {
t.is(typeof result.resolveId, 'function');
});

test('Simple aliasing', (t) => {
test('Simple aliasing (array)', (t) => {
const result = alias({
entries: [
{find:'foo', replacement:'bar'},
Expand All @@ -41,6 +41,24 @@ test('Simple aliasing', (t) => {
t.is(resolved3, 'global');
});

test('Simple aliasing (object)', (t) => {
const result = alias({
entries: {
foo: 'bar',
pony: 'paradise',
'./local': 'global'
}
});

const resolved = result.resolveId('foo', '/src/importer.js');
const resolved2 = result.resolveId('pony', '/src/importer.js');
const resolved3 = result.resolveId('./local', '/src/importer.js');

t.is(resolved, 'bar');
t.is(resolved2, 'paradise');
t.is(resolved3, 'global');
});

test('RegExp aliasing', (t) => {
const result = alias({
entries: [
Expand Down

0 comments on commit 90f753c

Please sign in to comment.