Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 35 additions & 20 deletions packages/alias/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { Alias, ResolverFunction, RollupAliasOptions } from '../types';
const VOLUME = /^([A-Z]:)/i;
const IS_WINDOWS = platform() === 'win32';

// Helper functions
const noop = () => null;

function matches(pattern: string | RegExp, importee: string) {
if (pattern instanceof RegExp) {
return pattern.test(importee);
Expand Down Expand Up @@ -48,10 +48,28 @@ function getEntries({ entries }: RollupAliasOptions): Alias[] {
});
}

function getCustomResolver(
{ customResolver }: Alias,
options: RollupAliasOptions
): ResolverFunction | null {
if (typeof customResolver === 'function') {
return customResolver;
}
if (customResolver && typeof customResolver.resolveId === 'function') {
return customResolver.resolveId;
}
if (typeof options.customResolver === 'function') {
return options.customResolver;
}
if (options.customResolver && typeof options.customResolver.resolveId === 'function') {
return options.customResolver.resolveId;
}
return null;
}

export default function alias(options: RollupAliasOptions = {}): Plugin {
const entries = getEntries(options);

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

return {
name: 'alias',
buildStart(inputOptions) {
return Promise.all(
[...entries, options].map(
({ customResolver }) =>
customResolver &&
typeof customResolver === 'object' &&
typeof customResolver.buildStart === 'function' &&
customResolver.buildStart.call(this, inputOptions)
)
).then(() => {
// enforce void return value
});
},
resolveId(importee, importer) {
const importeeId = normalizeId(importee);
const importerId = normalizeId(importer);
Expand All @@ -75,25 +106,9 @@ export default function alias(options: RollupAliasOptions = {}): Plugin {
importeeId.replace(matchedEntry.find, matchedEntry.replacement)
);

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

const customResolver = getCustomResolver(matchedEntry, options);
if (customResolver) {
return customResolver(updatedId, importerId);
return customResolver.call(this, updatedId, importerId);
}

return this.resolve(updatedId, importer, { skipSelf: true }).then((resolved) => {
Expand Down
30 changes: 27 additions & 3 deletions packages/alias/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,33 @@ test('Local customResolver plugin-like object', (t) => {
).then((result) => t.deepEqual(result, [localCustomResult]));
});

/** @TODO
* Needs to be modified after rollup-plugin-node-resolve would became a part of rollup-plugins monorepo
*/
test('supports node-resolve as a custom resolver', (t) =>
resolveAliasWithRollup(
{
entries: [
{
find: 'local-resolver',
replacement: path.resolve(DIRNAME, 'fixtures'),
customResolver: nodeResolvePlugin()
},
{
find: 'global-resolver',
replacement: path.resolve(DIRNAME, 'fixtures', 'folder')
}
],
customResolver: nodeResolvePlugin()
},
[
{ source: 'local-resolver', importer: posix.resolve(DIRNAME, './files/index.js') },
{ source: 'global-resolver', importer: posix.resolve(DIRNAME, './files/index.js') }
]
).then((result) =>
t.deepEqual(result, [
path.resolve(DIRNAME, 'fixtures', 'index.js'),
path.resolve(DIRNAME, 'fixtures', 'folder', 'index.js')
])
));

test('Alias + rollup-plugin-node-resolve', (t) =>
rollup({
input: './test/fixtures/index.js',
Expand Down
8 changes: 3 additions & 5 deletions packages/alias/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { Plugin, ResolveIdResult } from 'rollup';
import { Plugin, PluginHooks } from 'rollup';

export interface Alias {
find: string | RegExp;
replacement: string;
customResolver?: ResolverFunction | ResolverObject | null;
}

export type ResolverFunction = (
source: string,
importer: string | undefined
) => Promise<ResolveIdResult> | ResolveIdResult;
export type ResolverFunction = PluginHooks['resolveId'];

export interface ResolverObject {
buildStart?: PluginHooks['buildStart'];
resolveId: ResolverFunction;
}

Expand Down