From 9ea434d63b0ecbc0ac377f4f2511c6e57c1ddc57 Mon Sep 17 00:00:00 2001 From: Vladislav Date: Tue, 5 Nov 2019 19:54:08 +0300 Subject: [PATCH] feat(alias): customResolver instead of built-in resolving algorithm (#21) * 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 --- .gitignore | 1 + packages/alias/README.md | 45 +++++++++++++++++++++++++++++++++++++ packages/alias/src/index.js | 21 +++++++++++++++++ packages/alias/test/test.js | 38 +++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+) diff --git a/.gitignore b/.gitignore index b4f3cc927..b96767f48 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ output/ .eslintcache coverage.lcov pnpm-debug.log +.idea diff --git a/packages/alias/README.md b/packages/alias/README.md index e65e2dc06..97b5a0f30 100644 --- a/packages/alias/README.md +++ b/packages/alias/README.md @@ -60,6 +60,13 @@ Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#comma ## Options +### `customResolver` + +Type: `Function | Object`
+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]`
@@ -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) diff --git a/packages/alias/src/index.js b/packages/alias/src/index.js index 92f530fa5..5d696b86a 100755 --- a/packages/alias/src/index.js +++ b/packages/alias/src/index.js @@ -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); diff --git a/packages/alias/test/test.js b/packages/alias/test/test.js index 973dbc133..4993dede7 100755 --- a/packages/alias/test/test.js +++ b/packages/alias/test/test.js @@ -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); +});