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: add custom attrs to <style></style> #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ywmail
Copy link

@ywmail ywmail commented Mar 14, 2019

What kind of change does this PR introduce?

feature

Did you add tests for your changes?
Yes

If relevant, did you update the README?
Yes

Summary
with webpack configuration

        {
          test: /\.css$/,
          use: [{
            loader: 'vue-style-loader',
            options: {
              attributes: {
                'test-attr': 'foo'
              }
            }
          }, 'css-loader']
        },

will result in

<style type="text/css" test-attr="foo">
}
</style>

Does this PR introduce a breaking change?

No

UPDATE
change attrs to attributes for style-loader@1.x has been updated.

@yyx990803
Copy link
Member

What's the use case for this?

@ywmail
Copy link
Author

ywmail commented Mar 15, 2019

Our app has a dynamic route to load asynchronous components(thousands) which are built in a separated project. The async component will inject style tags to head when loaded, we want to clear the style tags injected by the async component when the route changes, so we need an identifier to know which tags are injected by async components.

The webpack style-loader now supports this feature. see:
https://github.com/webpack-contrib/style-loader#options

Name Type Default Description
attributes {Object} {} Add custom attributes to <style></style>

@yyx990803
Copy link
Member

Why do you need to remove the style tags? Is it causing performance issues?

@ywmail
Copy link
Author

ywmail commented Apr 9, 2019

Because some async components add global styles(without scoped), when the route changes, I want to remove the side effects(global styles) when loading another async component.

@slengyel-lnx
Copy link

This feature is also needed in one of the projects I am working on.

We have an app which is creating a sourceless Iframe. The styles are being injected to the root document head but we would need them to be injected inside the iframe since vue components lives inside of it.
By adding attrs: option to vue-style-loader it would be easy to identify the injected styles by class or other attributes.

Going further it would be even better to have an option insertInto just like webpack style-loader has it.

@transcranial
Copy link

This would also be useful for adding nonce for CSP: #33

@ywmail
Copy link
Author

ywmail commented Jan 7, 2020

I've updated the PR by changing attrs to attributes.

@hulkyhawk
Copy link

@yyx990803 i need to add attribute to target the style when changing theme colors

@mixaDev
Copy link

mixaDev commented Apr 8, 2020

I also need supported attributes.

I am writing a library with css variable support, using https://github.com/jhildenbiddle/css-vars-ponyfill

cssVars ({
   include: 'style[data-only-my-lib]'
});

@mrswylet
Copy link

I also need to be supported attributes.

I want to find marked styles in a head and inject the styles in shadowDom

@predam
Copy link

predam commented Jan 14, 2022

I also need this feature.

@RavishMan
Copy link

Please do add this

@mwahlhuetter
Copy link

Any news on this?

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.