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: relative path html #8122

merged 9 commits into from May 12, 2022


Copy link

@poyoho poyoho commented May 11, 2022


fix: #7818

Additional context

isExternalUrl is truth? html support ftp / file protocol?

this PR make script style src attr relative path



./asset/main.js // add ./
./asset/style.js // add ./
http://a.a/asset/style.js // no change
https://a.a/asset/style.js // no change

What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

Copy link

I don't know if we could support this without a breaking change. Could we import an npm module directly before? 🤔

<script src="module-with-side-effect" type="module"></script>

Another issue is what happens with nested HTML, where does assets/file.js points for them? Is it ok to also point them relative to the HTML itself?

Maybe we should ask people to keep using relative paths instead?

Copy link
Member Author

poyoho commented May 11, 2022

I think no base path(aaset/main.js) is relative path too? 👀

I don't know what is nested HTML

Copy link

Nested html as in an MPA where you have:


Copy link
Member Author

poyoho commented May 11, 2022

emm... asset/main.js seems to equal with ./asset/main.js 🤔

the other way is call waring like style no resolved and suggest user add ./ by hand?

Copy link
Member Author

poyoho commented May 11, 2022

add the test to try it 👀

Copy link

I have tested a bit.
npm i normalize.css ./pkg

<link rel="stylesheet" href="normalize.css" /> <!-- this does not resolve to node_modules/normalize.css/normalize.css -->
<link rel="stylesheet" href="normalize.css/normalize.css" /> <!-- this does not resolve to node_modules/normalize.css/normalize.css -->
<script type="module" src="pkg"></script> <!-- this **does** resolve to node_modules/pkg/index.js -->

So it would be a breaking change?

Copy link

bluwy commented May 12, 2022

I think we should use the preferRelative feature for HTML files only. Adding a importer?.endsWith('.html') on this line:

if (id.startsWith('.') || (preferRelative && /^\w/.test(id))) {

Fixed the issue for me, and should avoid the breaking change. What do y'all think?

Copy link
Member Author

poyoho commented May 12, 2022

@sapphi-red now I know this use case <script src="module-with-side-effect" type="module"></script>. we make all the module script / link href to js import. we can use this case. My resolution is actually break change. 😂
thanks @bluwy good idea to avoid change it.

thanks again @patak-dev @sapphi-red @bluwy help for me. please /cc the new change.

bluwy previously approved these changes May 12, 2022
@bluwy bluwy added feat: html p2-nice-to-have 🍰 Not breaking anything but nice to have (priority) labels May 12, 2022
@patak-dev patak-dev merged commit d0deac0 into vitejs:main May 12, 2022
@poyoho poyoho deleted the fix/relative-path-html branch May 13, 2022 01:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
feat: html p2-nice-to-have 🍰 Not breaking anything but nice to have (priority)
None yet

Successfully merging this pull request may close these issues.

Build fails if <script> src path doesn't start with a slash
5 participants