Skip to content

Commit

Permalink
fix(ssr): avoid using tryNodeResolve on absolute paths (#6488)
Browse files Browse the repository at this point in the history
Co-authored-by: Alec Larson <1925840+aleclarson@users.noreply.github.com>
  • Loading branch information
bluwy and aleclarson committed Jan 13, 2022
1 parent b2d972e commit f346d89
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/playground/ssr-deps/package.json
Expand Up @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions packages/playground/ssr-deps/require-absolute/foo.js
@@ -0,0 +1 @@
module.exports.hello = 'Hello World!'
3 changes: 3 additions & 0 deletions packages/playground/ssr-deps/require-absolute/index.js
@@ -0,0 +1,3 @@
const path = require('path')

module.exports.hello = () => require(path.resolve(__dirname, './foo.js')).hello
5 changes: 5 additions & 0 deletions packages/playground/ssr-deps/require-absolute/package.json
@@ -0,0 +1,5 @@
{
"name": "require-absolute",
"version": "0.0.0",
"private": true
}
4 changes: 4 additions & 0 deletions packages/playground/ssr-deps/src/app.js
Expand Up @@ -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 = ''
Expand Down Expand Up @@ -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'
}
7 changes: 4 additions & 3 deletions packages/vite/src/node/ssr/ssrModuleLoader.ts
Expand Up @@ -2,6 +2,7 @@ import path from 'path'
import { pathToFileURL } from 'url'
import type { ViteDevServer } from '../server'
import {
bareImportRE,
dynamicImport,
isBuiltin,
unwrapId,
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f346d89

Please sign in to comment.