fix(ssr): avoid using tryNodeResolve on absolute paths (#6488)

Co-authored-by: Alec Larson <1925840+aleclarson@users.noreply.github.com>
This commit is contained in:
Bjorn Lu 2022-01-14 05:45:38 +08:00 committed by GitHub
parent b2d972e53b
commit f346d89741
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 23 additions and 3 deletions

View File

@ -17,6 +17,7 @@
"only-object-assigned-exports": "file:./only-object-assigned-exports",
"primitive-export": "file:./primitive-export",
"read-file-content": "file:./read-file-content",
"require-absolute": "file:./require-absolute",
"ts-transpiled-exports": "file:./ts-transpiled-exports"
},
"devDependencies": {

View File

@ -0,0 +1 @@
module.exports.hello = 'Hello World!'

View File

@ -0,0 +1,3 @@
const path = require('path')
module.exports.hello = () => require(path.resolve(__dirname, './foo.js')).hello

View File

@ -0,0 +1,5 @@
{
"name": "require-absolute",
"version": "0.0.0",
"private": true
}

View File

@ -8,6 +8,7 @@ import bcrypt from 'bcrypt'
import definePropertiesExports from 'define-properties-exports'
import definePropertyExports from 'define-property-exports'
import onlyObjectAssignedExports from 'only-object-assigned-exports'
import requireAbsolute from 'require-absolute'
export async function render(url, rootDir) {
let html = ''
@ -41,5 +42,8 @@ export async function render(url, rootDir) {
const onlyObjectAssignedExportsMessage = onlyObjectAssignedExports.hello()
html += `\n<p class="only-object-assigned-exports-msg">message from only-object-assigned-exports: ${onlyObjectAssignedExportsMessage}</p>`
const requireAbsoluteMessage = requireAbsolute.hello()
html += `\n<p class="require-absolute-msg">message from require-absolute: ${requireAbsoluteMessage}</p>`
return html + '\n'
}

View File

@ -2,6 +2,7 @@ import path from 'path'
import { pathToFileURL } from 'url'
import type { ViteDevServer } from '../server'
import {
bareImportRE,
dynamicImport,
isBuiltin,
unwrapId,
@ -232,9 +233,9 @@ async function nodeImport(
// When an ESM module imports an ESM dependency, this hook is *not* used.
const unhookNodeResolve = hookNodeResolve(
(nodeResolve) => (id, parent, isMain, options) => {
// Fix #5709, use require to resolve files with the '.node' file extension.
// See detail, https://nodejs.org/api/addons.html#addons_loading_addons_using_require
if (id[0] === '.' || isBuiltin(id) || id.endsWith('.node')) {
// Use the Vite resolver only for bare imports while skipping
// any built-in modules and binary modules.
if (!bareImportRE.test(id) || isBuiltin(id) || id.endsWith('.node')) {
return nodeResolve(id, parent, isMain, options)
}
if (parent) {

View File

@ -463,6 +463,7 @@ importers:
only-object-assigned-exports: file:./only-object-assigned-exports
primitive-export: file:./primitive-export
read-file-content: file:./read-file-content
require-absolute: file:./require-absolute
ts-transpiled-exports: file:./ts-transpiled-exports
dependencies:
bcrypt: 5.0.1
@ -473,6 +474,7 @@ importers:
only-object-assigned-exports: link:only-object-assigned-exports
primitive-export: link:primitive-export
read-file-content: link:read-file-content
require-absolute: link:require-absolute
ts-transpiled-exports: link:ts-transpiled-exports
devDependencies:
cross-env: 7.0.3
@ -499,6 +501,9 @@ importers:
packages/playground/ssr-deps/read-file-content:
specifiers: {}
packages/playground/ssr-deps/require-absolute:
specifiers: {}
packages/playground/ssr-deps/ts-transpiled-exports:
specifiers: {}