Skip to content

Commit 3090564

Browse files
authoredNov 28, 2022
feat(ssr)!: remove dedupe and mode support for CJS (#11101)
1 parent 2a4ffb7 commit 3090564

File tree

4 files changed

+14
-168
lines changed

4 files changed

+14
-168
lines changed
 

‎packages/vite/src/node/plugins/index.ts

-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { modulePreloadPolyfillPlugin } from './modulePreloadPolyfill'
1818
import { webWorkerPlugin } from './worker'
1919
import { preAliasPlugin } from './preAlias'
2020
import { definePlugin } from './define'
21-
import { ssrRequireHookPlugin } from './ssrRequireHook'
2221
import { workerImportMetaUrlPlugin } from './workerImportMetaUrl'
2322
import { assetImportMetaUrlPlugin } from './assetImportMetaUrl'
2423
import { ensureWatchPlugin } from './ensureWatch'
@@ -88,7 +87,6 @@ export async function resolvePlugins(
8887
wasmFallbackPlugin(),
8988
definePlugin(config),
9089
cssPostPlugin(config),
91-
isBuild && config.build.ssr ? ssrRequireHookPlugin(config) : null,
9290
isBuild && buildHtmlPlugin(config),
9391
workerImportMetaUrlPlugin(config),
9492
assetImportMetaUrlPlugin(config),

‎packages/vite/src/node/plugins/resolve.ts

-3
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ export interface InternalResolveOptions extends Required<ResolveOptions> {
105105
// Resolve using esbuild deps optimization
106106
getDepsOptimizer?: (ssr: boolean) => DepsOptimizer | undefined
107107
shouldExternalize?: (id: string) => boolean | undefined
108-
// Check this resolve is called from `hookNodeResolve` in SSR
109-
isHookNodeResolve?: boolean
110108
}
111109

112110
export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
@@ -689,7 +687,6 @@ export function tryNodeResolve(
689687
// if import can't be found, check if it's an optional peer dep.
690688
// if so, we can resolve to a special id that errors only when imported.
691689
if (
692-
!options.isHookNodeResolve &&
693690
basedir !== root && // root has no peer dep
694691
!isBuiltin(nestedPath) &&
695692
!nestedPath.includes('\0') &&

‎packages/vite/src/node/plugins/ssrRequireHook.ts

-90
This file was deleted.

‎packages/vite/src/node/ssr/ssrModuleLoader.ts

+14-73
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import path from 'node:path'
22
import { pathToFileURL } from 'node:url'
33
import type { ViteDevServer } from '../server'
44
import {
5-
bareImportRE,
65
dynamicImport,
76
isBuiltin,
87
unwrapId,
@@ -11,7 +10,6 @@ import {
1110
import { transformRequest } from '../server/transformRequest'
1211
import type { InternalResolveOptions } from '../plugins/resolve'
1312
import { tryNodeResolve } from '../plugins/resolve'
14-
import { hookNodeResolve } from '../plugins/ssrRequireHook'
1513
import {
1614
ssrDynamicImportKey,
1715
ssrExportAllKey,
@@ -114,21 +112,16 @@ async function instantiateModule(
114112
root
115113
} = server.config
116114

117-
// The `extensions` and `mainFields` options are used to ensure that
118-
// CommonJS modules are preferred. We want to avoid ESM->ESM imports
119-
// whenever possible, because `hookNodeResolve` can't intercept them.
120115
const resolveOptions: InternalResolveOptions = {
121116
mainFields: ['main'],
122117
browserField: true,
123118
conditions: [],
124119
extensions: ['.js', '.cjs', '.json'],
125120
dedupe,
126121
preserveSymlinks,
127-
isBuild: true,
122+
isBuild: false,
128123
isProduction,
129-
isRequire: true,
130-
root,
131-
isHookNodeResolve: true
124+
root
132125
}
133126

134127
// Since dynamic imports can happen in parallel, we need to
@@ -227,96 +220,44 @@ async function instantiateModule(
227220
return Object.freeze(ssrModule)
228221
}
229222

230-
// `nodeImport` may run in parallel on multiple `ssrLoadModule` calls.
231-
// We keep track of the current importing count so that the first import
232-
// would `hookNodeResolve`, and the last import would `unhookNodeResolve`.
233-
let importingCount = 0
234-
let unhookNodeResolve: ReturnType<typeof hookNodeResolve> | undefined
235-
236223
// In node@12+ we can use dynamic import to load CJS and ESM
237224
async function nodeImport(
238225
id: string,
239226
importer: string,
240227
resolveOptions: InternalResolveOptions
241228
) {
242-
// Node's module resolution is hi-jacked so Vite can ensure the
243-
// configured `resolve.dedupe` and `mode` options are respected.
244-
const viteResolve = (
245-
id: string,
246-
importer: string,
247-
options = resolveOptions
248-
) => {
249-
const resolved = tryNodeResolve(id, importer, options, false)
250-
if (!resolved) {
251-
const err: any = new Error(
252-
`Cannot find module '${id}' imported from '${importer}'`
253-
)
254-
err.code = 'ERR_MODULE_NOT_FOUND'
255-
throw err
256-
}
257-
return resolved.id
258-
}
259-
260-
if (importingCount === 0) {
261-
// When an ESM module imports an ESM dependency, this hook is *not* used.
262-
unhookNodeResolve = hookNodeResolve(
263-
(nodeResolve) => (id, parent, isMain, options) => {
264-
// Use the Vite resolver only for bare imports while skipping
265-
// any absolute paths, built-in modules and binary modules.
266-
if (
267-
!bareImportRE.test(id) ||
268-
path.isAbsolute(id) ||
269-
isBuiltin(id) ||
270-
id.endsWith('.node')
271-
) {
272-
return nodeResolve(id, parent, isMain, options)
273-
}
274-
if (parent) {
275-
let resolved = viteResolve(id, parent.id)
276-
if (resolved) {
277-
// hookNodeResolve must use platform-specific path.normalize
278-
// to be compatible with dynamicImport (#6080)
279-
resolved = path.normalize(resolved)
280-
}
281-
return resolved
282-
}
283-
// Importing a CJS module from an ESM module. In this case, the import
284-
// specifier is already an absolute path, so this is a no-op.
285-
// Options like `resolve.dedupe` and `mode` are not respected.
286-
return id
287-
}
288-
)
289-
}
290-
291229
let url: string
292230
if (id.startsWith('node:') || isBuiltin(id)) {
293231
url = id
294232
} else {
295-
url = viteResolve(
233+
const resolved = tryNodeResolve(
296234
id,
297235
importer,
298236
// Non-external modules can import ESM-only modules, but only outside
299237
// of test runs, because we use Node `require` in Jest to avoid segfault.
300238
// @ts-expect-error
301239
typeof jest === 'undefined'
302240
? { ...resolveOptions, tryEsmOnly: true }
303-
: resolveOptions
241+
: resolveOptions,
242+
false
304243
)
244+
if (!resolved) {
245+
const err: any = new Error(
246+
`Cannot find module '${id}' imported from '${importer}'`
247+
)
248+
err.code = 'ERR_MODULE_NOT_FOUND'
249+
throw err
250+
}
251+
url = resolved.id
305252
if (usingDynamicImport) {
306253
url = pathToFileURL(url).toString()
307254
}
308255
}
309256

310257
try {
311-
importingCount++
312258
const mod = await dynamicImport(url)
313259
return proxyESM(mod)
314-
} finally {
315-
importingCount--
316-
if (importingCount === 0) {
317-
unhookNodeResolve?.()
318-
}
319-
}
260+
} catch {}
320261
}
321262

322263
// rollup-style default import interop for cjs

0 commit comments

Comments
 (0)
Please sign in to comment.