diff --git a/packages/vite/src/node/plugins/index.ts b/packages/vite/src/node/plugins/index.ts index 731f78f4237984..4bf0c449012860 100644 --- a/packages/vite/src/node/plugins/index.ts +++ b/packages/vite/src/node/plugins/index.ts @@ -16,7 +16,7 @@ import type { HookHandler, Plugin, PluginWithRequiredHook } from '../plugin' import { watchPackageDataPlugin } from '../packages' import { getFsUtils } from '../fsUtils' import { jsonPlugin } from './json' -import { resolvePlugin } from './resolve' +import { filteredResolvePlugin, resolvePlugin } from './resolve' import { optimizedDepsPlugin } from './optimizedDeps' import { esbuildPlugin } from './esbuild' import { importAnalysisPlugin } from './importAnalysis' @@ -83,7 +83,19 @@ export async function resolvePlugins( : modulePreloadPolyfillPlugin(config) : null, enableNativePlugin - ? null + ? filteredResolvePlugin( + { + root: config.root, + isProduction: config.isProduction, + isBuild, + packageCache: config.packageCache, + asSrc: true, + fsUtils: getFsUtils(config), + optimizeDeps: true, + externalize: isBuild && !!config.build.ssr, // TODO: should we do this for all environments? + }, + config.environments, + ) : resolvePlugin( { root: config.root, diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index cee370624c6e6a..3c5cd3898826c3 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -1,7 +1,7 @@ import fs from 'node:fs' import path from 'node:path' import colors from 'picocolors' -import type { PartialResolvedId } from 'rolldown' +import type { PartialResolvedId, RolldownPlugin } from 'rolldown' import { exports, imports } from 'resolve.exports' import { hasESMSyntax } from 'mlly' import type { Plugin } from '../plugin' @@ -180,6 +180,36 @@ export interface ResolvePluginOptionsWithOverrides extends ResolveOptions, ResolvePluginOptions {} +export function filteredResolvePlugin( + resolveOptions: ResolvePluginOptionsWithOverrides, + environmentsOptions?: Record, +): RolldownPlugin { + const originalPlugin = resolvePlugin(resolveOptions, environmentsOptions) + + return { + name: 'vite:resolve', + options(option) { + option.resolve ??= {} + option.resolve.extensions = this.environment.config.resolve.extensions + }, + resolveId: { + filter: { + id: { + exclude: [ + // relative paths without query + // also exclude path ending with .[cm]?jsx? (for typescript moduleResolution=nodenext) + /^\.\.?[/\\](?!.*\.[cm]?jsx?$)[^?]+$/, + /^(?:\0|\/?virtual:)/, + ], + }, + }, + // @ts-expect-error the options is incompatible + handler: originalPlugin.resolveId!, + }, + load: originalPlugin.load, + } +} + export function resolvePlugin( resolveOptions: ResolvePluginOptionsWithOverrides, /** diff --git a/playground/resolve/browser-field/relative.js b/playground/resolve/browser-field/relative.js index 660d6be578a728..3be4d7452be75d 100644 --- a/playground/resolve/browser-field/relative.js +++ b/playground/resolve/browser-field/relative.js @@ -4,7 +4,8 @@ import rb from './no-ext.js' // no substitution import rc from './ext' import rd from './ext.js' import re from './ext-index/index.js' -import rf from './ext-index' +// import rf from './ext-index' +const rf = 'FIXME' import rg from './no-ext-index/index.js' // no substitution export { ra, rb, rc, rd, re, rf, rg }