Skip to content

Commit

Permalink
feat: error when failed to resolve aliased import (#14973)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Nov 13, 2023
1 parent dc58222 commit 6a564fa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/importAnalysis.ts
Expand Up @@ -305,7 +305,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {

const resolved = await this.resolve(url, importerFile)

if (!resolved) {
if (!resolved || resolved.meta?.['vite:alias']?.noResolved) {
// in ssr, we should let node handle the missing modules
if (ssr) {
return [url, url]
Expand Down
18 changes: 16 additions & 2 deletions packages/vite/src/node/plugins/index.ts
@@ -1,4 +1,4 @@
import aliasPlugin from '@rollup/plugin-alias'
import aliasPlugin, { type ResolverFunction } from '@rollup/plugin-alias'
import type { ObjectHook } from 'rollup'
import type { PluginHookUtils, ResolvedConfig } from '../config'
import { isDepsOptimizerEnabled } from '../config'
Expand Down Expand Up @@ -51,7 +51,10 @@ export async function resolvePlugins(
isBuild ? metadataPlugin() : null,
!isWorker ? watchPackageDataPlugin(config.packageCache) : null,
preAliasPlugin(config),
aliasPlugin({ entries: config.resolve.alias }),
aliasPlugin({
entries: config.resolve.alias,
customResolver: viteAliasCustomResolver,
}),
...prePlugins,
modulePreload !== false && modulePreload.polyfill
? modulePreloadPolyfillPlugin(config)
Expand Down Expand Up @@ -161,3 +164,14 @@ export function getHookHandler<T extends ObjectHook<Function>>(
): HookHandler<T> {
return (typeof hook === 'object' ? hook.handler : hook) as HookHandler<T>
}

// Same as `@rollup/plugin-alias` default resolver, but we attach additional meta
// if we can't resolve to something, which will error in `importAnalysis`
export const viteAliasCustomResolver: ResolverFunction = async function (
id,
importer,
options,
) {
const resolved = await this.resolve(id, importer, options)
return resolved || { id, meta: { 'vite:alias': { noResolved: true } } }
}

0 comments on commit 6a564fa

Please sign in to comment.