diff --git a/packages/alias/src/index.ts b/packages/alias/src/index.ts index 7758afacb..dae492f37 100755 --- a/packages/alias/src/index.ts +++ b/packages/alias/src/index.ts @@ -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); @@ -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', @@ -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); @@ -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) => { diff --git a/packages/alias/test/test.js b/packages/alias/test/test.js index 8adeb2c58..9f69f6700 100755 --- a/packages/alias/test/test.js +++ b/packages/alias/test/test.js @@ -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', diff --git a/packages/alias/types/index.d.ts b/packages/alias/types/index.d.ts index d826f8f35..9302df16c 100644 --- a/packages/alias/types/index.d.ts +++ b/packages/alias/types/index.d.ts @@ -1,4 +1,4 @@ -import { Plugin, ResolveIdResult } from 'rollup'; +import { Plugin, PluginHooks } from 'rollup'; export interface Alias { find: string | RegExp; @@ -6,12 +6,10 @@ export interface Alias { customResolver?: ResolverFunction | ResolverObject | null; } -export type ResolverFunction = ( - source: string, - importer: string | undefined -) => Promise | ResolveIdResult; +export type ResolverFunction = PluginHooks['resolveId']; export interface ResolverObject { + buildStart?: PluginHooks['buildStart']; resolveId: ResolverFunction; }