Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(resolve): support subpath imports #10929

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
"postcss-import": "^15.0.0",
"postcss-load-config": "^4.0.1",
"postcss-modules": "^5.0.0",
"resolve.exports": "^1.1.0",
"resolve.exports": "npm:@alloc/resolve.exports@^1.1.0",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need to change this to use a fork. the current resolve.exports version used by Vite supports subpath imports

"sirv": "^2.0.2",
"source-map-js": "^1.0.2",
"source-map-support": "^0.5.21",
Expand Down
10 changes: 3 additions & 7 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ import {
DEFAULT_MAIN_FIELDS,
ENV_ENTRY
} from './constants'
import type {
InternalResolveOptions,
InternalResolveOptionsWithOverrideConditions,
ResolveOptions
} from './plugins/resolve'
import type { InternalResolveOptions, ResolveOptions } from './plugins/resolve'
import { resolvePlugin, tryNodeResolve } from './plugins/resolve'
import type { LogLevel, Logger } from './logger'
import { createLogger } from './logger'
Expand Down Expand Up @@ -958,7 +954,7 @@ async function bundleConfigFile(
{
name: 'externalize-deps',
setup(build) {
const options: InternalResolveOptionsWithOverrideConditions = {
const options: InternalResolveOptions = {
root: path.dirname(fileName),
isBuild: true,
isProduction: true,
Expand All @@ -968,7 +964,7 @@ async function bundleConfigFile(
mainFields: [],
browserField: false,
conditions: [],
overrideConditions: ['node'],
overrideConditions: ['node', 'require'],
dedupe: [],
extensions: DEFAULT_EXTENSIONS,
preserveSymlinks: false
Expand Down
60 changes: 59 additions & 1 deletion packages/vite/src/node/packages.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'node:fs'
import path from 'node:path'
import { createDebugger, createFilter, resolveFrom } from './utils'
import { createDebugger, createFilter, lookupFile, resolveFrom } from './utils'
import type { ResolvedConfig } from './config'
import type { Plugin } from './plugin'

Expand All @@ -27,6 +27,7 @@ export interface PackageData {
main: string
module: string
browser: string | Record<string, string | false>
imports: Record<string, any>
exports: string | Record<string, any> | string[]
dependencies: Record<string, string>
}
Expand Down Expand Up @@ -131,6 +132,26 @@ export function loadPackageData(
return pkg
}

export function loadNearestPackageData(
startDir: string,
options?: { preserveSymlinks?: boolean },
predicate?: (pkg: PackageData) => boolean
): PackageData | null {
let importerPkg: PackageData | undefined
lookupFile(startDir, ['package.json'], {
pathOnly: true,
predicate(pkgPath) {
importerPkg = loadPackageData(pkgPath, options?.preserveSymlinks)
return !predicate || predicate(importerPkg)
}
})
return importerPkg || null
}

export function isNamedPackage(pkg: PackageData): boolean {
return !!pkg.data.name
}

export function watchPackageDataPlugin(config: ResolvedConfig): Plugin {
const watchQueue = new Set<string>()
let watchFile = (id: string) => {
Expand Down Expand Up @@ -163,3 +184,40 @@ export function watchPackageDataPlugin(config: ResolvedConfig): Plugin {
}
}
}

export function findPackageJson(dir: string): string | null {
// Stop looking at node_modules directory.
if (path.basename(dir) === 'node_modules') {
return null
}
const pkgPath = path.join(dir, 'package.json')
if (fs.existsSync(pkgPath)) {
return pkgPath
}
const parentDir = path.dirname(dir)
return parentDir !== dir ? findPackageJson(parentDir) : null
}

const workspaceRootFiles = ['lerna.json', 'pnpm-workspace.yaml', '.git']

export function isWorkspaceRoot(
dir: string,
preserveSymlinks?: boolean,
packageCache?: PackageCache
): boolean {
const files = fs.readdirSync(dir)
if (files.some((file) => workspaceRootFiles.includes(file))) {
return true // Found a lerna/pnpm workspace or git repository.
}
if (files.includes('package.json')) {
const workspacePkg = loadPackageData(
path.join(dir, 'package.json'),
preserveSymlinks,
packageCache
)
if (workspacePkg?.data.workspaces) {
return true // Found a npm/yarn workspace.
}
}
return false
}
Loading