Skip to content

Commit

Permalink
fix(resolve): compat for babel 7.13 helper resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Feb 24, 2021
1 parent 5095e04 commit 39820b9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
28 changes: 18 additions & 10 deletions packages/vite/src/node/optimizer/esbuildDepPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import path from 'path'
import { Loader, Plugin } from 'esbuild'
import { KNOWN_ASSET_TYPES } from '../constants'
import { ResolvedConfig } from '..'
import {
isRunningWithYarnPnp,
flattenId,
normalizePath,
isBuiltin
} from '../utils'
import { isRunningWithYarnPnp, flattenId, normalizePath } from '../utils'
import { browserExternalId } from '../plugins/resolve'
import { ExportsData } from '.'

Expand Down Expand Up @@ -102,15 +97,28 @@ export function esbuildDepPlugin(
return entry
}

// externalize node built-ins
if (isBuiltin(id)) {
const resolved = await resolve(id, importer)
if (resolved && resolved.startsWith(browserExternalId)) {
// #2199 @babel/runtime 7.13 now has mjs fallback for non-esm helpers
// it can break if we use Vite's resolver here since we don't know if
// the user is importing or requiring it. Fallback to esbuild resolver
// here.
// Hopefully esbuild will expose that information in the future
// https://github.com/evanw/esbuild/issues/879
if (id.startsWith('@babel/runtime')) {
return
}

// use vite's own resolver
const resolved = await resolve(id, importer)
if (resolved) {
if (resolved.startsWith(browserExternalId)) {
return {
path: id,
namespace: 'browser-external'
}
}
return {
path: path.resolve(resolved)
}
}
}
)
Expand Down
29 changes: 22 additions & 7 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,15 @@ export interface InternalResolveOptions extends ResolveOptions {
tryIndex?: boolean
tryPrefix?: string
preferRelative?: boolean
isRequire?: boolean
}

export function resolvePlugin(options: InternalResolveOptions): Plugin {
const { root, isProduction, asSrc, preferRelative = false } = options
export function resolvePlugin(baseOptions: InternalResolveOptions): Plugin {
const { root, isProduction, asSrc, preferRelative = false } = baseOptions
const requireOptions: InternalResolveOptions = {
...baseOptions,
isRequire: true
}
let server: ViteDevServer | undefined

return {
Expand All @@ -72,7 +77,7 @@ export function resolvePlugin(options: InternalResolveOptions): Plugin {
server = _server
},

resolveId(id, importer, _, ssr) {
resolveId(id, importer, resolveOpts, ssr) {
if (id.startsWith(browserExternalId)) {
return id
}
Expand All @@ -82,6 +87,15 @@ export function resolvePlugin(options: InternalResolveOptions): Plugin {
return
}

// this is passed by @rollup/plugin-commonjs
const isRequire =
resolveOpts &&
resolveOpts.custom &&
resolveOpts.custom['node-resolve'] &&
resolveOpts.custom['node-resolve'].isRequire

const options = isRequire ? requireOptions : baseOptions

let res

// explicit fs paths that starts with /@fs/*
Expand Down Expand Up @@ -540,15 +554,16 @@ function resolveExports(
key: string,
options: InternalResolveOptions
) {
const conditions = [
'module',
options.isProduction ? 'production' : 'development'
]
const conditions = [options.isProduction ? 'production' : 'development']
if (!options.isRequire) {
conditions.push('module')
}
if (options.conditions) {
conditions.push(...options.conditions)
}
return _resolveExports(pkg, key, {
browser: true,
require: options.isRequire,
conditions
})
}
Expand Down

0 comments on commit 39820b9

Please sign in to comment.