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(ssr): allow virtual paths on node modules #9405

Merged
merged 1 commit into from Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.