Skip to content

Commit

Permalink
fix(ssr): allow virtual paths on node modules (#9405)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Jul 29, 2022
1 parent 156a3a4 commit e60368f
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 16 deletions.
33 changes: 19 additions & 14 deletions packages/vite/src/node/ssr/ssrExternal.ts
Expand Up @@ -129,20 +129,25 @@ export function createIsConfiguredAsSsrExternal(
if (!bareImportRE.test(id) || id.includes('\0')) {
return false
}
return !!tryNodeResolve(
id,
undefined,
resolveOptions,
ssr?.target === 'webworker',
undefined,
true,
// try to externalize, will return undefined or an object without
// a external flag if it isn't externalizable
true,
// Allow linked packages to be externalized if they are explicitly
// configured as external
!!configuredAsExternal
)?.external
try {
return !!tryNodeResolve(
id,
undefined,
resolveOptions,
ssr?.target === 'webworker',
undefined,
true,
// try to externalize, will return undefined or an object without
// a external flag if it isn't externalizable
true,
// Allow linked packages to be externalized if they are explicitly
// configured as external
!!configuredAsExternal
)?.external
} catch (e) {
// may be an invalid import that's resolved by a plugin
return false
}
}

// Returns true if it is configured as external, false if it is filtered
Expand Down
5 changes: 5 additions & 0 deletions playground/ssr-deps/__tests__/ssr-deps.spec.ts
Expand Up @@ -103,3 +103,8 @@ test('msg from linked no external', async () => {
await page.goto(url)
expect(await page.textContent('.linked-no-external')).toMatch('Hello World!')
})

test('msg from linked no external', async () => {
await page.goto(url)
expect(await page.textContent('.dep-virtual')).toMatch('[success]')
})
3 changes: 2 additions & 1 deletion playground/ssr-deps/package.json
Expand Up @@ -27,7 +27,8 @@
"optimized-cjs-with-nested-external": "file:./optimized-with-nested-external",
"external-using-external-entry": "file:./external-using-external-entry",
"external-entry": "file:./external-entry",
"linked-no-external": "link:./linked-no-external"
"linked-no-external": "link:./linked-no-external",
"pkg-exports": "file:./pkg-exports"
},
"devDependencies": {
"cross-env": "^7.0.3",
Expand Down
1 change: 1 addition & 0 deletions playground/ssr-deps/pkg-exports/index.js
@@ -0,0 +1 @@
export default undefined
9 changes: 9 additions & 0 deletions playground/ssr-deps/pkg-exports/package.json
@@ -0,0 +1,9 @@
{
"name": "pkg-exports",
"private": true,
"version": "0.0.0",
"exports": {
".": "./index.js"
},
"type": "module"
}
18 changes: 17 additions & 1 deletion playground/ssr-deps/server.js
Expand Up @@ -45,7 +45,23 @@ export async function createServer(root = process.cwd(), hmrPort) {
optimizeDeps: {
disabled: 'build'
}
}
},
plugins: [
{
name: 'dep-virtual',
enforce: 'pre',
resolveId(id) {
if (id === 'pkg-exports/virtual') {
return 'pkg-exports/virtual'
}
},
load(id) {
if (id === 'pkg-exports/virtual') {
return 'export default "[success]"'
}
}
}
]
})
// use vite's connect instance as middleware
app.use(vite.middlewares)
Expand Down
3 changes: 3 additions & 0 deletions playground/ssr-deps/src/app.js
Expand Up @@ -12,6 +12,7 @@ import requireAbsolute from 'require-absolute'
import noExternalCjs from 'no-external-cjs'
import importBuiltinCjs from 'import-builtin-cjs'
import { hello as linkedNoExternal } from 'linked-no-external'
import virtualMessage from 'pkg-exports/virtual'

// This import will set a 'Hello World!" message in the nested-external non-entry dependency
import 'non-optimized-with-nested-external'
Expand Down Expand Up @@ -79,5 +80,7 @@ export async function render(url, rootDir) {
const linkedNoExternalMessage = linkedNoExternal()
html += `\n<p class="linked-no-external">message from linked-no-external: ${linkedNoExternalMessage}</p>`

html += `\n<p class="dep-virtual">message from dep-virtual: ${virtualMessage}</p>`

return html + '\n'
}
11 changes: 11 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 e60368f

Please sign in to comment.