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

fix(config): don't use module condition (fixes #10430) #10495

Closed
Closed
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
58 changes: 32 additions & 26 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
createDebugger,
createFilter,
dynamicImport,
isBuiltin,
isExternalUrl,
isObject,
lookupFile,
Expand All @@ -49,7 +48,7 @@ import {
ENV_ENTRY
} from './constants'
import type { InternalResolveOptions, ResolveOptions } from './plugins/resolve'
import { resolvePlugin, tryNodeResolve } from './plugins/resolve'
import { resolvePlugin } from './plugins/resolve'
import type { LogLevel, Logger } from './logger'
import { createLogger } from './logger'
import type { DepOptimizationConfig, DepOptimizationOptions } from './optimizer'
Expand Down Expand Up @@ -949,6 +948,7 @@ async function bundleConfigFile(
platform: 'node',
bundle: true,
format: isESM ? 'esm' : 'cjs',
mainFields: ['main'],
sourcemap: 'inline',
metafile: true,
define: {
Expand All @@ -960,38 +960,44 @@ async function bundleConfigFile(
{
name: 'externalize-deps',
setup(build) {
const options: InternalResolveOptions = {
root: path.dirname(fileName),
isBuild: true,
isProduction: true,
isRequire: !isESM,
preferRelative: false,
tryIndex: true,
mainFields: [],
browserField: false,
conditions: [],
dedupe: [],
extensions: DEFAULT_EXTENSIONS,
preserveSymlinks: false
}

build.onResolve({ filter: /.*/ }, ({ path: id, importer, kind }) => {
// externalize bare imports
if (id[0] !== '.' && !path.isAbsolute(id) && !isBuiltin(id)) {
// partial deno support as `npm:` does not work in `tryNodeResolve`
// externalize bare imports
build.onResolve(
{ filter: /^[^.].*/ },
async ({ path: id, importer, kind, resolveDir, pluginData }) => {
if (pluginData?.internalResolve) {
return
}
if (kind === 'entry-point' || path.isAbsolute(id)) {
return
}

// partial deno support as `npm:` does not work with esbuild
if (id.startsWith('npm:')) {
return { external: true }
}
let idFsPath = tryNodeResolve(id, importer, options, false)?.id
if (idFsPath && (isESM || kind === 'dynamic-import')) {
idFsPath = pathToFileURL(idFsPath).href
const result = await build.resolve(id, {
importer,
kind:
kind === 'import-statement' && !isESM ? 'require-call' : kind,
resolveDir,
pluginData: { internalResolve: true }
})
if (result.errors.length > 0) {
return { errors: result.errors }
}
if (result.external) {
return { path: result.path, external: true }
}
const absolutePath = path.resolve(importer, '..', result.path)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const absolutePath = path.resolve(importer, '..', result.path)
const absolutePath = path.resolve(path.dirname(importer), result.path)

I'm not sure if it makes a difference, but I usually use path.dirname. But the .. trick here seems to work too.

return {
path: idFsPath,
path:
isESM || kind === 'dynamic-import'
? pathToFileURL(absolutePath).href
: absolutePath,
external: true
}
}
})
)
}
},
{
Expand Down