Skip to content

Commit

Permalink
feat: handle non-relative paths by Vite's resolver for enableNativePl…
Browse files Browse the repository at this point in the history
…ugin
  • Loading branch information
sapphi-red committed Sep 19, 2024
1 parent 097670c commit 259bd2a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
14 changes: 13 additions & 1 deletion packages/vite/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
32 changes: 31 additions & 1 deletion packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -180,6 +180,36 @@ export interface ResolvePluginOptionsWithOverrides
extends ResolveOptions,
ResolvePluginOptions {}

export function filteredResolvePlugin(
resolveOptions: ResolvePluginOptionsWithOverrides,
environmentsOptions?: Record<string, ResolvedEnvironmentOptions>,
): 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,
/**
Expand Down
3 changes: 2 additions & 1 deletion playground/resolve/browser-field/relative.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

0 comments on commit 259bd2a

Please sign in to comment.