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

feat(html,asset): improve asset resolution #7643

Closed
wants to merge 2 commits into from
Closed

feat(html,asset): improve asset resolution #7643

wants to merge 2 commits into from

Conversation

grievouz
Copy link

@grievouz grievouz commented Apr 7, 2022

Description


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.

@grievouz
Copy link
Author

grievouz commented Apr 7, 2022

  • Ideally, include relevant tests that fail without this PR but pass with it.

I didn't look further into it but if needed/wanted I can implement tests to cover the tagged issues.

Comment on lines 197 to 246
filter: (attribute, value, node) => {
const relProp = findProp(node, 'rel')
if (
relProp &&
relProp.type === NodeTypes.ATTRIBUTE &&
relProp.value?.content &&
[
'stylesheet',
'icon',
'shortcut icon',
'mask-icon',
'apple-touch-icon',
'apple-touch-icon-precomposed',
'apple-touch-startup-image',
'manifest',
'prefetch',
'preload'
].includes(relProp.value.content)
) {
return true
}

const itempropProp = findProp(node, 'itemprop')
if (
itempropProp &&
itempropProp.type === NodeTypes.ATTRIBUTE &&
itempropProp.value?.content &&
[
'image',
'logo',
'screenshot',
'thumbnailurl',
'contenturl',
'downloadurl',
'duringmedia',
'embedurl',
'installurl',
'layoutimage '
].includes(itempropProp.value.content)
) {
return true
}

return false
}
Copy link
Member

Choose a reason for hiding this comment

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

I think this can also be configured here. I don't know why it should be turned into a method. I would appreciate it if you could tell me

{
  rel: ['stylesheet','icon','shortcut icon','mask-icon','apple-touch-icon','apple-touch-icon-precomposed','apple-touch-startup-image','manifest','prefetch','preload'],
  itemprop: ['image','logo','screenshot','thumbnailurl','contenturl','downloadurl','duringmedia','embedurl','installurl','layoutimage']
}

Copy link
Author

Choose a reason for hiding this comment

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

I didn't give much thought to how it should be configured. I basically replicated the API from html-loader, as proposed by #7362 (comment). Furthermore, the approach is most likely a matter of personal preference. If the array-based configuration fits better into vites config schema then that's how it should be done, so as not to create unnecessary complexity where it isn't required.

Copy link
Member

Choose a reason for hiding this comment

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

Since the processing I've seen is all the same, I'm guessing it can be done this way. 🫣 This reduces a lot of code.

Copy link
Member

Choose a reason for hiding this comment

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

I would also prefer we simplify the code. But the functional form would allow for a more flexible configuration point since the PR is exposing build.htmlAssetSources. I'm not sure we would like to expose this complexity it is better to leave it to userland. Maybe we could only correct the list as a first step. Added the issue for discussion in a future team meeting.

Copy link
Member

Choose a reason for hiding this comment

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

From my experience building svelte-preprocess-import-assets, this is actually the only way to ensure correctness (with a filter). I tried simplifying it but it isn't quite achievable if we follow the html spec.

Copy link
Member

Choose a reason for hiding this comment

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

since the PR is exposing build.htmlAssetSources

It doesn't look like the PR expose it now (maybe I've missed a discussion) 🤔

Copy link
Member

Choose a reason for hiding this comment

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

Ya, the change to remove it was force pushed. We can still discuss it in the meeting, the PR was exposing build.htmlAssetSources and letting you define whatever you want instead of the default defaultHtmlAssetSources array. The API was exactly the one of that array.

Copy link
Author

Choose a reason for hiding this comment

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

Sorry, my mistake. I discovered some old incorrectly merged changes while rebasing the branch earlier. However, I have the changes that expose build.htmlAssetSources saved locally. I'll commit them later so that the discussion has a point of reference.

packages/vite/src/node/plugins/html.ts Outdated Show resolved Hide resolved
packages/vite/src/node/plugins/html.ts Show resolved Hide resolved
@bluwy bluwy added p2-nice-to-have Not breaking anything but nice to have (priority) feat: html labels Apr 12, 2022
Add logic for selecting source attributes and extend build options to allow for configuration.
}
}

export const defaultHtmlAssetSources: HtmlAssetSource[] = [
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be great to extract this and it's related types/functions as it's own file as this is HUGE. Maybe we can put it in the src/node folder

@floriangosse
Copy link

Is there any update on this? Do you need some support for this?

@patak-dev patak-dev added this to the 4.0 milestone Oct 7, 2022
@benmccann
Copy link
Collaborator

There's a merge conflict for this PR that would need to be resolved

@aleclarson
Copy link
Member

This looks great. Are you still willing to carry this forward, @grievouz? We would love to ship this in Vite 4.

@bluwy
Copy link
Member

bluwy commented Nov 17, 2022

Looks like we had discussed this in a couple meetings ago and forgot to give an update. We think this feature is great for Vite 4, but should be shipped without an option first.

@bluwy
Copy link
Member

bluwy commented Dec 7, 2022

Closing this one for now in favour of #11138

@bluwy bluwy closed this Dec 7, 2022
@bluwy bluwy removed this from the 4.0 milestone Dec 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: html needs test p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Add import directive for html attributes Custom html attributes resolving
8 participants