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): check root import extension for external #9494

Merged
merged 2 commits into from Aug 9, 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
16 changes: 10 additions & 6 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -657,18 +657,22 @@ export function tryNodeResolve(
if (!externalize) {
return resolved
}
// dont external symlink packages
// don't external symlink packages
if (!allowLinkedExternal && !resolved.id.includes('node_modules')) {
return resolved
}
const resolvedExt = path.extname(resolved.id)
// don't external non-js imports
if (
resolvedExt &&
resolvedExt !== '.js' &&
resolvedExt !== '.mjs' &&
resolvedExt !== '.cjs'
) {
return resolved
}
let resolvedId = id
if (isDeepImport) {
// check ext before externalizing - only externalize
// extension-less imports and explicit .js imports
if (resolvedExt && !resolved.id.match(/(.js|.mjs|.cjs)$/)) {
return resolved
}
if (!pkg?.data.exports && path.extname(id) !== resolvedExt) {
resolvedId += resolvedExt
}
Expand Down
7 changes: 6 additions & 1 deletion playground/ssr-deps/__tests__/ssr-deps.spec.ts
@@ -1,5 +1,5 @@
import { port } from './serve'
import { page } from '~utils'
import { getColor, page } from '~utils'

const url = `http://localhost:${port}`

Expand Down Expand Up @@ -108,3 +108,8 @@ test('msg from linked no external', async () => {
await page.goto(url)
expect(await page.textContent('.dep-virtual')).toMatch('[success]')
})

test('import css library', async () => {
await page.goto(url)
expect(await getColor('.css-lib')).toBe('blue')
})
3 changes: 3 additions & 0 deletions playground/ssr-deps/css-lib/index.css
@@ -0,0 +1,3 @@
.css-lib {
color: blue;
}
6 changes: 6 additions & 0 deletions playground/ssr-deps/css-lib/package.json
@@ -0,0 +1,6 @@
{
"name": "@vitejs/css-lib",
"private": true,
"version": "0.0.0",
"main": "./index.css"
}
4 changes: 4 additions & 0 deletions playground/ssr-deps/index.html
Expand Up @@ -8,5 +8,9 @@
<body>
<h1>SSR Dependencies</h1>
<div><!--app-html--></div>
<script type="module">
// hydration scripts
import '@vitejs/css-lib'
</script>
</body>
</html>
1 change: 1 addition & 0 deletions playground/ssr-deps/package.json
Expand Up @@ -9,6 +9,7 @@
"debug": "node --inspect-brk server"
},
"dependencies": {
"@vitejs/css-lib": "file:./css-lib",
"bcrypt": "^5.0.1",
"define-properties-exports": "file:./define-properties-exports",
"define-property-exports": "file:./define-property-exports",
Expand Down
3 changes: 3 additions & 0 deletions playground/ssr-deps/src/app.js
Expand Up @@ -13,6 +13,7 @@ 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'
import '@vitejs/css-lib'

// 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 @@ -82,5 +83,7 @@ export async function render(url, rootDir) {

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

html += `\n<p class="css-lib">I should be blue</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.