Skip to content

Commit

Permalink
feat(alias): customResolver instead of built-in resolving algorithm (#21
Browse files Browse the repository at this point in the history
)

* ignore Intellij IDEA dir

* updated pnpm-lock.yaml

According to pnpm actual version and included packages

* @rollup/plugin-alias: customResolver option

Allow to use customResolver instead of built-in resolving algorithm in

* @rollup/plugin-alias: updated readme

customResolver option

* (alias) updated README

customResolver description

* docs: rework custom resolver options and section
  • Loading branch information
Acionyx authored and shellscape committed Nov 5, 2019
1 parent cec516f commit 9ea434d
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ output/
.eslintcache
coverage.lcov
pnpm-debug.log
.idea
45 changes: 45 additions & 0 deletions packages/alias/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#comma

## Options

### `customResolver`

Type: `Function | Object`<br>
Default: `null`

Instructs the plugin to use an alternative resolving algorithm, rather than the built-in resolver. Please refer to the [Rollup documentation](https://rollupjs.org/guide/en/#hooks) for more information about the `resolveId` hook. For a detailed example, see: [Custom Resolvers](#custom-resolvers).

### `entries`

Type: `Object | Array[Object]`<br>
Expand Down Expand Up @@ -122,6 +129,44 @@ To replace extensions with another, a pattern like the following might be used:

This would replace the file extension for all imports ending with `.js` to `.wasm`.

## Custom Resolvers

The `customResolver` option can be leveraged to provide separate module resolution for an invidudual alias.

Example:
```javascript
// rollup.config.js
import alias from "@rollup/plugin-alias";
import resolve from "rollup-plugin-node-resolve";

const customResolver = resolve({
extensions: [".mjs", ".js", ".jsx", ".json", ".sass", ".scss"]
});
const projectRootDir = path.resolve(__dirname);

export default {
// ...
plugins: [
alias(
{
entries: [
{
find: "src",
replacement: path.resolve(projectRootDir, "src")
// OR place `customResolver` here. See explanation below.
}
],
customResolver
}
),
resolve()
]
};
```

In the example above the alias `src` is used, which uses the `node-resolve` algorithm for files _aliased_ with `src`, by passing the `customResolver` option. The `resolve()` plugin is kept separate in the plugins list for other files which are not _aliased_ with `src`. The `customResolver` option can be passed inside each `entries` item for granular control over resolving allowing each alias a preferred resolver.


## Meta

[CONTRIBUTING](/.github/CONTRIBUTING.md)
Expand Down
21 changes: 21 additions & 0 deletions packages/alias/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,27 @@ export default function alias(options = {}) {

let updatedId = normalizeId(importeeId.replace(matchedEntry.find, matchedEntry.replacement));

let customResolver = null;
if (typeof matchedEntry.customResolver === 'function') {
customResolver = matchedEntry.customResolver;
} else if (
typeof matchedEntry.customResolver === 'object' &&
typeof matchedEntry.customResolver.resolveId === 'function'
) {
customResolver = options.customResolver.resolveId;
} else if (typeof options.customResolver === 'function') {
customResolver = options.customResolver;
} else if (
typeof options.customResolver === 'object' &&
typeof options.customResolver.resolveId === 'function'
) {
customResolver = options.customResolver.resolveId;
}

if (customResolver) {
return customResolver(updatedId, importerId);
}

if (isFilePath(updatedId)) {
const directory = posix.dirname(importerId);

Expand Down
38 changes: 38 additions & 0 deletions packages/alias/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,41 @@ test('Works in rollup', (t) =>
)
);
}));

test('Global customResolver function', (t) => {
const customResult = 'customResult';
const result = alias({
entries: [
{
find: 'test',
replacement: path.resolve('./test/files/folder/hipster.jsx')
}
],
resolve: ['.js', '.jsx'],
customResolver: () => customResult
});

const resolved = result.resolveId('test', posix.resolve(DIRNAME, './files/index.js'));

t.is(resolved, customResult);
});

test('Local customResolver function', (t) => {
const customResult = 'customResult';
const localCustomResult = 'localCustomResult';
const result = alias({
entries: [
{
find: 'test',
replacement: path.resolve('./test/files/folder/hipster.jsx'),
customResolver: () => localCustomResult
}
],
resolve: ['.js', '.jsx'],
customResolver: () => customResult
});

const resolved = result.resolveId('test', posix.resolve(DIRNAME, './files/index.js'));

t.is(resolved, localCustomResult);
});

0 comments on commit 9ea434d

Please sign in to comment.