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: option to filter style tags from callback #1783

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

Conversation

Kapcash
Copy link

@Kapcash Kapcash commented Dec 29, 2020

I needed a way to write multiple <style> tags on my components, but to only apply (i.e. bundle) them conditionally.

The feature I was looking for

This is a dummy example, just here to illustrate.

Let's say the style depends on the current season:

<template>
  <p>Hello vue-loader team!</p>
</template>

<style scoped theme="winter">
  p { color: cyan; }
</style>

<style scoped theme="corporate">
  p { color: orange; } 
</style>

And we select the correct style tag depending on an environment variable (process.env.theme === 'summer').

I know there are other way to achieve the same thing, but it looked more elegant this way, and it may reduce the bundle size at the end, compared to the others solutions I came up with:

  • using css preprocessor conditions: painful to maintain, bad code readability
  • css variables: not fully supported by IE
  • Load different css file according the condition result: can't use scoped style, so overriding css is painful to write

Usage example

{
   test: /\.vue$/,
   loader: 'vue-loader',
   options: {
      filterStyleTag: ({ theme }) => {
         return theme === process.env.theme
      }
   }
}

We can declare a new option with a callback that take the inner "query" parsed from the style tag.
It is supposed to be used like the Array.prototype.filter method: return true to keep the style tag. false will ignore the style tag.

Let me know what you think!
Is it a bad idea to do something like this? Will it break anything I didn't see? Can my feature be improved?

@@ -17,7 +17,8 @@ declare namespace VueLoader {
cacheDirectory?: string
cacheIdentifier?: string
prettify?: boolean
exposeFilename?: boolean
exposeFilename?: boolean,
filterStyleTag?: (styleTagAttributes: Object) => boolean,
Copy link
Author

Choose a reason for hiding this comment

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

I typed it as an Object even though it is a ParsedUrlQuery internally (users don't need to know its real interface in my opinion, but it is poorly typed with just object).
I'm opened to types suggestions here!

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.

None yet

1 participant