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 unplugin path resolution (#774) #786

Merged
merged 4 commits into from
Oct 27, 2023

Conversation

Mokshit06
Copy link
Contributor

This PR should fix #774 and also make #785 not required. path.resolve would not consider the parent path if the child path starts with /, which made paths like /src/main.civet be considered as system-absolute path instead of project-absolute. path.join should take them into account always, and make this work.
The only issue this would face is not supporting system-absolute paths for civet, though I'm not sure if that is a feature that is actually used that much

@Mokshit06 Mokshit06 temporarily deployed to build October 27, 2023 14:35 — with GitHub Actions Inactive
@edemaine
Copy link
Collaborator

I tested this on Windows and Linux, and found vite build works now, when either index.html or src/main.civet uses absolute paths to refer to other Civet files.

However, vite dev still doesn't work: loading http://localhost:5173/src/main.civet in the <script> tag still gives me the raw Civet code, with no MIME type. So maybe #785 is still necessary? Did you find yarn dev worked for you somehow?

@@ -178,7 +178,7 @@ const civetUnplugin = createUnplugin((options: PluginOptions = {}) => {

const relativeId = path.relative(
process.cwd(),
path.resolve(path.dirname(importer ?? ''), id)
path.join(path.dirname(importer ?? ''), id)
Copy link
Collaborator

@edemaine edemaine Oct 27, 2023

Choose a reason for hiding this comment

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

Actually, I wonder whether this is correct... I would have guessed:

  • If the path is relative, use path.join with importer, as you have it here
  • If the path is absolute, use path.join with the project's root setting (which probably also needs to be joined with process.cwd() because it too can be relative).

This is somewhat confirmed because I find src/main.civet can successfully import /module.civet, but I don't think it should be able to; it should have to import /src/module.civet I believe.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops yeah you're correct, I overlooked that case.

@Mokshit06
Copy link
Contributor Author

Mokshit06 commented Oct 27, 2023

I am very sure that yarn dev worked after rebuilding with this change, but it doesn't actually. I think this might be because vite was reusing the cache in node_modules/.vite 🤔 But yeah then #785 is indeed still required

@Mokshit06 Mokshit06 temporarily deployed to build October 27, 2023 17:42 — with GitHub Actions Inactive
@edemaine edemaine temporarily deployed to build October 27, 2023 19:09 — with GitHub Actions Inactive

const absolutePath = path.isAbsolute(id)
? path.join(path.join(process.cwd(), viteConfig.root ?? ''), id)
: path.join(path.dirname(importer ?? ''), id);
Copy link
Collaborator

@edemaine edemaine Oct 27, 2023

Choose a reason for hiding this comment

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

I'm pretty sure the latter case should use path.resolve, not path.join.

I pushed a new commit that cleans up this code. Could you check it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The result should be the same with path.resolve and path.join as we're already handling the case where id is an absolute path which is the only case where resolve and join differ

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Rest of the changes look good 👍

@edemaine edemaine merged commit 2938bdd into DanielXMoore:main Oct 27, 2023
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Vite unplugin doesn't support project-absolute paths
2 participants