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

refactor: resolveExports #10917

Merged
merged 2 commits into from Apr 21, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 21 additions & 36 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -580,7 +580,6 @@ function tryResolveFile(
export type InternalResolveOptionsWithOverrideConditions =
InternalResolveOptions & {
/**
* @deprecated In future, `conditions` will work like this.
* @internal
*/
overrideConditions?: string[]
Expand Down Expand Up @@ -1037,50 +1036,36 @@ function packageEntryFailure(id: string, details?: string) {
)
}

const conditionalConditions = new Set(['production', 'development', 'module'])

function resolveExports(
pkg: PackageData['data'],
key: string,
options: InternalResolveOptionsWithOverrideConditions,
targetWeb: boolean
) {
const overrideConditions = options.overrideConditions
? new Set(options.overrideConditions)
: undefined
const additionalConditions = new Set(
options.overrideConditions || [
'production',
'development',
'module',
...options.conditions
]
)

const conditions = []
if (
(!overrideConditions || overrideConditions.has('production')) &&
options.isProduction
) {
conditions.push('production')
}
if (
(!overrideConditions || overrideConditions.has('development')) &&
!options.isProduction
) {
conditions.push('development')
}
if (
(!overrideConditions || overrideConditions.has('module')) &&
!options.isRequire
) {
conditions.push('module')
}
if (options.overrideConditions) {
conditions.push(
...options.overrideConditions.filter((condition) =>
conditionalConditions.has(condition)
)
)
} else if (options.conditions.length > 0) {
conditions.push(...options.conditions)
}
const conditions = [...additionalConditions].filter((condition) => {
switch (condition) {
case 'production':
return options.isProduction
case 'development':
return !options.isProduction
case 'module':
return !options.isRequire
}
return true
})

return _resolveExports(pkg, key, {
browser: targetWeb && !conditions.includes('node'),
require: options.isRequire && !conditions.includes('import'),
browser: targetWeb && !additionalConditions.has('node'),
require: options.isRequire && !additionalConditions.has('import'),
conditions
Copy link
Member Author

Choose a reason for hiding this comment

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

  const conditions = [...additionalConditions].filter((condition) => {
    switch (condition) {
      case 'production':
        return options.isProduction
      case 'development':
        return !options.isProduction
      case 'import':
      case 'module':
        return !options.isRequire
      case 'require':
        return options.isRequire
    }
    return true
  })

  return _resolveExports(pkg, key, {
    conditions,
    unsafe: true

This is what I was going to propose.

})
}
Expand Down