|
1 | | -import path from 'path'; |
| 1 | +import path from 'node:path'; |
2 | 2 |
|
3 | | -import type { Plugin } from 'rollup'; |
| 3 | +// Public types are exported directly from source so tsc can emit declarations |
| 4 | +import type { Plugin, PluginHooks } from 'rollup'; |
4 | 5 |
|
5 | | -import type { ResolvedAlias, ResolverFunction, ResolverObject, RollupAliasOptions } from '../types'; |
| 6 | +// Narrow to explicit callable form instead of the global `Function` type. |
| 7 | +type MapToFunction<T> = T extends (...args: any[]) => any ? T : never; |
| 8 | + |
| 9 | +export type ResolverFunction = MapToFunction<PluginHooks['resolveId']>; |
| 10 | + |
| 11 | +export interface ResolverObject { |
| 12 | + buildStart?: PluginHooks['buildStart']; |
| 13 | + resolveId: ResolverFunction; |
| 14 | +} |
| 15 | + |
| 16 | +export interface Alias { |
| 17 | + find: string | RegExp; |
| 18 | + replacement: string; |
| 19 | + customResolver?: ResolverFunction | ResolverObject | null; |
| 20 | +} |
| 21 | + |
| 22 | +export interface ResolvedAlias { |
| 23 | + find: string | RegExp; |
| 24 | + replacement: string; |
| 25 | + resolverFunction: ResolverFunction | null; |
| 26 | +} |
| 27 | + |
| 28 | +export interface RollupAliasOptions { |
| 29 | + /** |
| 30 | + * Instructs the plugin to use an alternative resolving algorithm, |
| 31 | + * rather than the Rollup's resolver. |
| 32 | + * @default null |
| 33 | + */ |
| 34 | + customResolver?: ResolverFunction | ResolverObject | null; |
| 35 | + |
| 36 | + /** |
| 37 | + * Specifies an `Object`, or an `Array` of `Object`, |
| 38 | + * which defines aliases used to replace values in `import` or `require` statements. |
| 39 | + * With either format, the order of the entries is important, |
| 40 | + * in that the first defined rules are applied first. |
| 41 | + */ |
| 42 | + entries?: readonly Alias[] | { [find: string]: string }; |
| 43 | +} |
6 | 44 |
|
7 | 45 | function matches(pattern: string | RegExp, importee: string) { |
8 | 46 | if (pattern instanceof RegExp) { |
@@ -40,7 +78,7 @@ function getEntries({ entries, customResolver }: RollupAliasOptions): readonly R |
40 | 78 | }); |
41 | 79 | } |
42 | 80 |
|
43 | | -function getHookFunction<T extends Function>(hook: T | { handler?: T }): T | null { |
| 81 | +function getHookFunction<T extends (...args: any[]) => any>(hook: T | { handler?: T }): T | null { |
44 | 82 | if (typeof hook === 'function') { |
45 | 83 | return hook; |
46 | 84 | } |
@@ -104,7 +142,7 @@ export default function alias(options: RollupAliasOptions = {}): Plugin { |
104 | 142 |
|
105 | 143 | if (!path.isAbsolute(updatedId)) { |
106 | 144 | this.warn( |
107 | | - `rewrote ${importee} to ${updatedId} but was not an abolute path and was not handled by other plugins. ` + |
| 145 | + `rewrote ${importee} to ${updatedId} but was not an absolute path and was not handled by other plugins. ` + |
108 | 146 | `This will lead to duplicated modules for the same path. ` + |
109 | 147 | `To avoid duplicating modules, you should resolve to an absolute path.` |
110 | 148 | ); |
|
0 commit comments