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

Build fails if <script> src path doesn't start with a slash #7818

Closed
7 tasks done
ngustavo opened this issue Apr 19, 2022 · 1 comment · Fixed by #8122
Closed
7 tasks done

Build fails if <script> src path doesn't start with a slash #7818

ngustavo opened this issue Apr 19, 2022 · 1 comment · Fixed by #8122
Labels
feat: html has workaround inconsistency Inconsistency between dev & build p2-nice-to-have Not breaking anything but nice to have (priority)

Comments

@ngustavo
Copy link

Describe the bug

If you move files into an assets folder or any other kind of structure, you must refer to main.js as /assets/main.js. Though, CSS and favicon can be referred as just assets/main.css and assets/favicon.svg.

The issue is that Vite throws this:

The following dependencies are imported but could not be resolved:
    assets/main.js
Are they installed?

But it actually "works" on npm run dev and the script is loaded by the browser as expected. But if you do npm run build, it throws:

[vite]: Rollup failed to resolve import "assets/main.js" from "index.html".

I wonder if this inconsistency happens because Rollup looks into project folder instead of index.html location.

Reproduction

https://stackblitz.com/edit/vitejs-vite-jchmw5?file=index.html&terminal=dev

System Info

System:
  OS: Linux 5.10 Ubuntu 20.04.4 LTS (Focal Fossa)
  CPU: (12) x64 AMD Ryzen 5 1600 Six-Core Processor
  Memory: 11.39 GB / 12.44 GB
  Container: Yes
  Shell: 5.0.17 - /bin/bash
Binaries:
  Node: 17.6.0 - ~/.asdf/installs/nodejs/17.6.0/bin/node
  npm: 8.5.1 - ~/.asdf/plugins/nodejs/shims/npm
npmPackages:
  vite: ^2.9.2 => 2.9.5

Used Package Manager

npm

Logs

No response

Validations

@sapphi-red sapphi-red added the inconsistency Inconsistency between dev & build label Apr 20, 2022
@sapphi-red
Copy link
Member

But it actually "works" on npm run dev and the script is loaded by the browser as expected.

To describe more specifically, it only works when you access / or /foo and it does not work with /foo/bar.

Looks like it is supported for relative paths which starts with ..

} else if (
url.startsWith('.') &&
originalUrl &&
originalUrl !== '/' &&
htmlPath === '/index.html'
) {
// #3230 if some request url (localhost:3000/a/b) return to fallback html, the relative assets
// path will add `/a/` prefix, it will caused 404.
// rewrite before `./index.js` -> `localhost:3000/a/index.js`.
// rewrite after `../index.js` -> `localhost:3000/index.js`.
s.overwrite(
node.value!.loc.start.offset,
node.value!.loc.end.offset,
`"${path.posix.join(
path.posix.relative(originalUrl, '/'),
url.slice(1)
)}"`,
{ contentOnly: true }
)
}


For a workaround use ./assets/main.js instead of assets/main.js.

@poyoho poyoho mentioned this issue May 11, 2022
9 tasks
@sapphi-red sapphi-red added feat: html p2-nice-to-have Not breaking anything but nice to have (priority) and removed pending triage labels May 12, 2022
@github-actions github-actions bot locked and limited conversation to collaborators May 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feat: html has workaround inconsistency Inconsistency between dev & build p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants