Skip to content

Commit eb73b0d

Browse files
authored
fix(alias): properly initialize custom resolvers (#426)
* fix(alias): properly initialize custom resolvers * chore(commonjs): add helpful comment
1 parent 6446657 commit eb73b0d

File tree

3 files changed

+65
-28
lines changed

3 files changed

+65
-28
lines changed

packages/alias/src/index.ts

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { Alias, ResolverFunction, RollupAliasOptions } from '../types';
88
const VOLUME = /^([A-Z]:)/i;
99
const IS_WINDOWS = platform() === 'win32';
1010

11-
// Helper functions
1211
const noop = () => null;
12+
1313
function matches(pattern: string | RegExp, importee: string) {
1414
if (pattern instanceof RegExp) {
1515
return pattern.test(importee);
@@ -48,10 +48,28 @@ function getEntries({ entries }: RollupAliasOptions): Alias[] {
4848
});
4949
}
5050

51+
function getCustomResolver(
52+
{ customResolver }: Alias,
53+
options: RollupAliasOptions
54+
): ResolverFunction | null {
55+
if (typeof customResolver === 'function') {
56+
return customResolver;
57+
}
58+
if (customResolver && typeof customResolver.resolveId === 'function') {
59+
return customResolver.resolveId;
60+
}
61+
if (typeof options.customResolver === 'function') {
62+
return options.customResolver;
63+
}
64+
if (options.customResolver && typeof options.customResolver.resolveId === 'function') {
65+
return options.customResolver.resolveId;
66+
}
67+
return null;
68+
}
69+
5170
export default function alias(options: RollupAliasOptions = {}): Plugin {
5271
const entries = getEntries(options);
5372

54-
// No aliases?
5573
if (entries.length === 0) {
5674
return {
5775
name: 'alias',
@@ -61,6 +79,19 @@ export default function alias(options: RollupAliasOptions = {}): Plugin {
6179

6280
return {
6381
name: 'alias',
82+
buildStart(inputOptions) {
83+
return Promise.all(
84+
[...entries, options].map(
85+
({ customResolver }) =>
86+
customResolver &&
87+
typeof customResolver === 'object' &&
88+
typeof customResolver.buildStart === 'function' &&
89+
customResolver.buildStart.call(this, inputOptions)
90+
)
91+
).then(() => {
92+
// enforce void return value
93+
});
94+
},
6495
resolveId(importee, importer) {
6596
const importeeId = normalizeId(importee);
6697
const importerId = normalizeId(importer);
@@ -75,25 +106,9 @@ export default function alias(options: RollupAliasOptions = {}): Plugin {
75106
importeeId.replace(matchedEntry.find, matchedEntry.replacement)
76107
);
77108

78-
let customResolver: ResolverFunction | null = null;
79-
if (typeof matchedEntry.customResolver === 'function') {
80-
({ customResolver } = matchedEntry);
81-
} else if (
82-
typeof matchedEntry.customResolver === 'object' &&
83-
typeof matchedEntry.customResolver!.resolveId === 'function'
84-
) {
85-
customResolver = matchedEntry.customResolver!.resolveId;
86-
} else if (typeof options.customResolver === 'function') {
87-
({ customResolver } = options);
88-
} else if (
89-
typeof options.customResolver === 'object' &&
90-
typeof options.customResolver!.resolveId === 'function'
91-
) {
92-
customResolver = options.customResolver!.resolveId;
93-
}
94-
109+
const customResolver = getCustomResolver(matchedEntry, options);
95110
if (customResolver) {
96-
return customResolver(updatedId, importerId);
111+
return customResolver.call(this, updatedId, importerId);
97112
}
98113

99114
return this.resolve(updatedId, importer, { skipSelf: true }).then((resolved) => {

packages/alias/test/test.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,33 @@ test('Local customResolver plugin-like object', (t) => {
311311
).then((result) => t.deepEqual(result, [localCustomResult]));
312312
});
313313

314-
/** @TODO
315-
* Needs to be modified after rollup-plugin-node-resolve would became a part of rollup-plugins monorepo
316-
*/
314+
test('supports node-resolve as a custom resolver', (t) =>
315+
resolveAliasWithRollup(
316+
{
317+
entries: [
318+
{
319+
find: 'local-resolver',
320+
replacement: path.resolve(DIRNAME, 'fixtures'),
321+
customResolver: nodeResolvePlugin()
322+
},
323+
{
324+
find: 'global-resolver',
325+
replacement: path.resolve(DIRNAME, 'fixtures', 'folder')
326+
}
327+
],
328+
customResolver: nodeResolvePlugin()
329+
},
330+
[
331+
{ source: 'local-resolver', importer: posix.resolve(DIRNAME, './files/index.js') },
332+
{ source: 'global-resolver', importer: posix.resolve(DIRNAME, './files/index.js') }
333+
]
334+
).then((result) =>
335+
t.deepEqual(result, [
336+
path.resolve(DIRNAME, 'fixtures', 'index.js'),
337+
path.resolve(DIRNAME, 'fixtures', 'folder', 'index.js')
338+
])
339+
));
340+
317341
test('Alias + rollup-plugin-node-resolve', (t) =>
318342
rollup({
319343
input: './test/fixtures/index.js',

packages/alias/types/index.d.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
import { Plugin, ResolveIdResult } from 'rollup';
1+
import { Plugin, PluginHooks } from 'rollup';
22

33
export interface Alias {
44
find: string | RegExp;
55
replacement: string;
66
customResolver?: ResolverFunction | ResolverObject | null;
77
}
88

9-
export type ResolverFunction = (
10-
source: string,
11-
importer: string | undefined
12-
) => Promise<ResolveIdResult> | ResolveIdResult;
9+
export type ResolverFunction = PluginHooks['resolveId'];
1310

1411
export interface ResolverObject {
12+
buildStart?: PluginHooks['buildStart'];
1513
resolveId: ResolverFunction;
1614
}
1715

0 commit comments

Comments
 (0)