Repository navigation
Description
Describe the bug
Using vite-plugin-ssr with Vue, running build with an asset referenced from the root path (e.g. /myimage.jpg) results in a Rollup error. This doesn't occur during dev.
Also, the issue doesn't occur when used with a generic Vue SPA.
However, @brillout noted that vite-plugin-ssr doesn't do anything about the public directory
Here's a detailed explanation of the scenarios:
Scenario A: VPS with Vue using npm init vite-plugin-ssr@latest
"vite": "^3.0.9",
"vite-plugin-ssr": "^0.4.32",
"@vitejs/plugin-vue": "^3.0.3"
myimage.jpg can be found in public directory, inside the root component in the template:
this breaks:
<img src="/myimage.jpg" />this works:
<img src="http://localhost:3000/myimage.jpg" />Scenario B: Vue 3 project using npm init vue@latest
"vite": "^3.0.9",
"@vitejs/plugin-vue": "^3.0.3"
myimage.jpg can be found in public directory, inside the root component in the template:
this works:
<img src="/myimage.jpg" />this works:
<img src="http://localhost:3000/myimage.jpg" />Reproduction
https://github.com/truumahn/vps-public-image-repro
System Info
System:
OS: Linux 5.10 Ubuntu 20.04.2 LTS (Focal Fossa)
CPU: (12) x64 AMD Ryzen 5 4600H with Radeon Graphics
Memory: 5.76 GB / 15.31 GB
Container: Yes
Shell: 5.8 - /usr/bin/zsh
Binaries:
Node: 16.16.0 - /mnt/wslg/runtime-dir/fnm_multishells/31022_1662972639084/bin/node
npm: 8.11.0 - /mnt/wslg/runtime-dir/fnm_multishells/31022_1662972639084/bin/npm
Browsers:
Chrome: 96.0.4664.45
npmPackages:
@vitejs/plugin-vue: ^3.0.3 => 3.1.0
vite: ^3.0.9 => 3.1.0Used Package Manager
pnpm
Logs
Click to expand!
[vite]: Rollup failed to resolve import "/politecat.jpg" from "renderer/PageShell.vue?vue&type=script&setup=true&lang.ts".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
[vite-plugin-ssr:autoFullBuild] [vite]: Rollup failed to resolve import "/politecat.jpg" from "renderer/PageShell.vue?vue&type=script&setup=true&lang.ts".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
error during build:
Error: [vite]: Rollup failed to resolve import "/politecat.jpg" from "renderer/PageShell.vue?vue&type=script&setup=true&lang.ts".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
at onRollupWarning (file:///path/to/project/vite-ssr-project/node_modules/.pnpm/vite@3.1.0/node_modules/vite/dist/node/chunks/dep-665b0112.js:45824:19)
at onwarn (file:///path/to/project/vite-ssr-project/node_modules/.pnpm/vite@3.1.0/node_modules/vite/dist/node/chunks/dep-665b0112.js:45622:13)
at Object.onwarn (file:///path/to/project/vite-ssr-project/node_modules/.pnpm/rollup@2.78.1/node_modules/rollup/dist/es/shared/rollup.js:23225:13)
at ModuleLoader.handleResolveId (file:///path/to/project/vite-ssr-project/node_modules/.pnpm/rollup@2.78.1/node_modules/rollup/dist/es/shared/rollup.js:22352:26)
at file:///path/to/project/vite-ssr-project/node_modules/.pnpm/rollup@2.78.1/node_modules/rollup/dist/es/shared/rollup.js:22313:26
ELIFECYCLE Command failed with exit code 1.Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Activity
brillout commentedon Sep 12, 2022
Vite-plugin-ssr doesn't do anything with
public/assets, so it's likely a Vite issue during SSR building.sapphi-red commentedon Sep 12, 2022
It seems it does not reproduce with a simple setup.
https://stackblitz.com/edit/github-marmex?file=src%2FApp.vue
And it happens with vite-plugin-ssr + react too.
https://stackblitz.com/edit/github-6pyqmj?file=renderer%2FPageShell.jsx
truuman commentedon Sep 12, 2022
Only because you imported the images, not used them with the root path as
/logo.svgdirectly at thesrcattribute of theimgelement. It successfully builds like this: https://stackblitz.com/edit/github-6pyqmj-wrxz1m?file=renderer/PageShell.jsxThe same doesn't work with Vue though.
sapphi-red commentedon Sep 12, 2022
plugin-vue transforms
<img src="/logo.svg">intoSo something is not working around resolving
/logo.svg.And since it does not happen with Vite only, I guess something in vite-plugin-ssr is interacting it.
truuman commentedon Sep 12, 2022
It builds successfully with the following
vite.config.ts:https://stackblitz.com/edit/vitejs-vite-nsfvwb?file=vite.config.ts
So it is indeed the Vue plugin that causes this. I'm just checking the documentation: https://github.com/vitejs/vite/tree/main/packages/plugin-vue#asset-url-handling
This behaviour was introduced in
3.x.brillout commentedon Sep 13, 2022
It seems that
@vitejs/plugin-vuetries to resolvesrcif it doesn't match apublic/asset.Vite-plugin-ssr 0.4 changed Vite's config
publicDirtofalsefor SSR, which explains the problem.I reverted that change and released a new version
vite-plugin-ssr@0.4.33which should fix the problem.@truumahn Let me know if you run into any other issues. We can close this in the meantime.
@sapphi-red The proper fix would be to stop separating the client-side and server-side building process into two isolated processes. FYI, this is one of many reasons why I believe the decision the Vite team took here #9496 (comment) was a mistake. Separating both builds is asking for trouble.
brillout commentedon Sep 13, 2022
I created a discussion #10097 - Unify client-side and server-side build steps about this.
sapphi-red commentedon Sep 13, 2022
Yes. This is because of #10082 (comment).
What is the purpose of changing this? Avoiding the public directory to be copied? I think it's correct not to resolve public assets when
publicDir: falseis set.brillout commentedon Sep 13, 2022
Yes, to avoid the public directory being copied twice at
dist/client/assets/anddist/server/assets/. If the user has a couple of gigabytes of images, that's significant.truuman commentedon Sep 16, 2022
I'm closing this as the original issue has been resolved. The discussion can be continued in #10097