|
1 | | -import aliasPlugin from '@rollup/plugin-alias' |
| 1 | +import aliasPlugin, { type ResolverFunction } from '@rollup/plugin-alias' |
2 | 2 | import type { ObjectHook } from 'rollup' |
3 | 3 | import type { PluginHookUtils, ResolvedConfig } from '../config' |
4 | 4 | import { isDepsOptimizerEnabled } from '../config' |
@@ -51,7 +51,10 @@ export async function resolvePlugins( |
51 | 51 | isBuild ? metadataPlugin() : null, |
52 | 52 | !isWorker ? watchPackageDataPlugin(config.packageCache) : null, |
53 | 53 | preAliasPlugin(config), |
54 | | - aliasPlugin({ entries: config.resolve.alias }), |
| 54 | + aliasPlugin({ |
| 55 | + entries: config.resolve.alias, |
| 56 | + customResolver: viteAliasCustomResolver, |
| 57 | + }), |
55 | 58 | ...prePlugins, |
56 | 59 | modulePreload !== false && modulePreload.polyfill |
57 | 60 | ? modulePreloadPolyfillPlugin(config) |
@@ -161,3 +164,14 @@ export function getHookHandler<T extends ObjectHook<Function>>( |
161 | 164 | ): HookHandler<T> { |
162 | 165 | return (typeof hook === 'object' ? hook.handler : hook) as HookHandler<T> |
163 | 166 | } |
| 167 | + |
| 168 | +// Same as `@rollup/plugin-alias` default resolver, but we attach additional meta |
| 169 | +// if we can't resolve to something, which will error in `importAnalysis` |
| 170 | +export const viteAliasCustomResolver: ResolverFunction = async function ( |
| 171 | + id, |
| 172 | + importer, |
| 173 | + options, |
| 174 | +) { |
| 175 | + const resolved = await this.resolve(id, importer, options) |
| 176 | + return resolved || { id, meta: { 'vite:alias': { noResolved: true } } } |
| 177 | +} |
0 commit comments