Skip to content

fix: root import#7224

Closed
poyoho wants to merge 1 commit intovitejs:mainfrom
poyoho:fix/root-import
Closed

fix: root import#7224
poyoho wants to merge 1 commit intovitejs:mainfrom
poyoho:fix/root-import

Conversation

@poyoho
Copy link
Copy Markdown
Member

@poyoho poyoho commented Mar 8, 2022

Description

fix: vitejs/vite-plugin-vue#25

Additional context

If set the base URL to root on Linux and use vue plugin, vue plugin will import css by absolute path. And vite will replace base to /, and absolute path startWith base path will got a error.
this PR analy req.url(absolute path) include root will replace it to emtry strings and then it will be a <root>/[relative path]


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 vitejs/vite#123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

const parsed = parseUrl(url)
const path = parsed.pathname || '/'

if (path.startsWith(root)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should check for path.startsWith(root + base), then replace to req.url = url.replace(root + base, '/').

If we have root as /home/xxx/, and base equals to /home/, the current implementation will fail, no?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

root + base = /home/xxx/home/ ?

In my test case, path will be <root>/[relative path from root]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, gotcha, you are actually trying to fix the other way around.
I thought the bug was that /home/xxx/home/path was converted into /xxx/home/path, that seems to be an issue currently.
I don't understand your case, I think that absolute paths will be covered by the rule I proposed. And paths relative to the project root will also work.
The only case that won't be covered is is when you are trying to have an absolute path out of root, but in that case Vite will use /@fs/, no?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I update the desc.

If set the base URL to root on Linux and use vue plugin, vue plugin will import css by absolute path. And vite will replace base to /, and absolute path startWith base path will got a error.

Copy link
Copy Markdown
Member Author

@poyoho poyoho Mar 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@patak-dev the case is

config with

{
  base: "/home/"
}
  • had absolute path /home/xxx
  • root is /home/xxx/project

so the request must be /home/xxx/project/assets (because the request will call by the vue plugin)

and then replace root to be /assets

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the bug was that /home/xxx/home/path was converted into /xxx/home/path, that seems to be an issue currently.

Is there a way to reproduce this possible? I've been thinking about it for a long time and can't figure it out. 😂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Vue style import will conflict with the base url config if base url is the prefix of project's full path

3 participants