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

whitelist: transition classes #34

Closed
MarvinMiles opened this issue Feb 3, 2019 · 18 comments
Closed

whitelist: transition classes #34

MarvinMiles opened this issue Feb 3, 2019 · 18 comments
Labels
enhancement New feature or request

Comments

@MarvinMiles
Copy link

Hey!

Currently I have to manually write white list like this:
whitelist: [..., 'page-enter-active', 'page-leave-active', 'page-enter', 'page-leave-to', 'smooth-enter-active', 'smooth-leave-active', 'smooth-enter', 'smooth-leave-to', 'fade-enter-active', 'fade-leave-active', 'fade-enter', 'fade-leave-to' ...]

Is it possible to configure module for auto whitelisting transition classes which nuxt uses? If not, it would be super cool to get this functionality in the future)

@TheAlexLichter TheAlexLichter added the enhancement New feature or request label Feb 3, 2019
@TheAlexLichter
Copy link
Member

@MarvinMiles Hey 👋

Interesting idea! Automatically grabbing the classes might be possible with a custom extractor.

Besides that you could always use whitelist patterns to whitelist /-leave/ and /-enter/ though that will whitelist all transition classes.

@koresar
Copy link

koresar commented May 10, 2019

@manniL this article has a better default extractor configuration: https://medium.com/@kyis/vue-tailwind-purgecss-the-right-way-c70d04461475

It takes care about transitions, vue-router classes, etc. Please, take a look.

@koresar
Copy link

koresar commented May 10, 2019

I had to drop using the nuxt-purgecss. Here is my build.postcss.plugins value in nuxt.config.js

[
    require("postcss-preset-env")({ stage: 0 }),
    require("tailwindcss")(require("path").join(__dirname, "tailwind.js")),

    process.env.NODE_ENV === "production" &&
        require("@fullhuman/postcss-purgecss")({
            defaultExtractor(content) {
                return content.match(/[A-Za-z0-9-_/:]*[A-Za-z0-9-_/]+/g) || [];
            },
            content: ["components/**/*.vue", "layouts/**/*.vue", "pages/**/*.vue"],
            styleExtensions: [".css"],
            whitelist: ["body", "html", "nuxt-progress"],
            whitelistPatterns: [
                /-(leave|enter|appear)(|-(to|from|active))$/,
                /^(?!(|.*?:)cursor-move).+-move$/,
                /^router-link(|-exact)-active$/
            ]
        }),

    // autoperfixer does not work automatically even though both Nuxt and Tailwind depend on it.
    require("autoprefixer")
].filter(Boolean);

Good news. After applying this configuration my production CSS halved. :) Was 12KB gzipped, become 6KB.

@TheAlexLichter
Copy link
Member

TheAlexLichter commented May 11, 2019

@koresar Why exactly did you drop nuxt-purgecss? 🤔
The same setup is achievable with the package ☺️

@TheAlexLichter
Copy link
Member

TheAlexLichter commented May 11, 2019

PS: I'd accept a PR that adds transition and link classes to the whitelist patterns by default.

@koresar
Copy link

koresar commented May 11, 2019

I dropped it because I didn't understand from the README how to do it.

@curtisbelt
Copy link

curtisbelt commented May 11, 2019

@koresar

Thank you for sharing that article, I am actually updating my config on a project now based on your post and that article!

I'm keeping nuxt-purgecss, I believe the config below is equivalent to what you posted. Note I updated router-link to nuxt-link.

// nuxt.config.js

module.exports = {
  purgeCSS: {
    mode: 'postcss',
    extractors: [
      {
        extractor: class {
          static extract(content) {
            return content.match(/[A-Za-z0-9-_/:]*[A-Za-z0-9-_/]+/g) || [];
          }
        }
      }
    ],
    whitelistPatterns: [
      /-(leave|enter|appear)(|-(to|from|active))$/,
      /^(?!(|.*?:)cursor-move).+-move$/,
      /^nuxt-link(|-exact)-active$/
    ]
  },
  build: {
    postcss: {
      preset: {
        stage: 0
      },
      plugins: {
        tailwindcss: './tailwind.js',
        autoprefixer: {}
      }
    }
  }
}

@TheAlexLichter
Copy link
Member

@koresar thanks for the insights

Hope the answer from @curtisbelt helped you but I try to improve the readme.

For v1 of the module, I think the settings provided in the previous comment are sensible defaults in addition to current ones ☺️

@marcfilleul
Copy link

marcfilleul commented May 12, 2019

Thanks @curtisbelt, I was just wondering how to add the extended extractor part.
I was not sure how to add the "style" part of kyis Medium post:

defaultExtractor: (content) => {
    const contentWithoutStyleBlocks = content.replace(/<style[^]+?<\/style>/gi, '')
    return contentWithoutStyleBlocks.match(/[A-Za-z0-9-_/:]*[A-Za-z0-9-_/]+/g) || []
  }

Then I realized that if styles in a SFC are not used, they shouldn't be here in the first place ;)

@koresar: As far as I'm concerned, autoprefixer works well without adding it in the conf.
How did you come to the conclusion it wasn't working automatically ?

@TheAlexLichter
Copy link
Member

@willdante If you are using a postcss.config.js, autoprefixer won't work as expected, so that can happen (see nuxt/nuxt#5419)

@koresar
Copy link

koresar commented May 13, 2019

Oh! That's why! Thanks

Side note: the IDEA IDE from Jetbrains have just got support for .nuxtignore
JetBrains/idea-gitignore#576 (comment)

@marcfilleul
Copy link

@willdante If you are using a postcss.config.js, autoprefixer won't work as expected, so that can happen (see nuxt/nuxt.js#5419)

@manniL Just to be sure, the issue happens only if wa have a separate postcss.config.js file and not if we use postcss mode and setup build.postcss.plugins values in nuxt.config.js, is that right ?

@TheAlexLichter
Copy link
Member

@willdante Correct.

@TheAlexLichter
Copy link
Member

Already resolved through #59

@zernonia
Copy link

zernonia commented Jun 19, 2020

Already resolved through #59

My main.css has

.page-enter-active {
  transition: all .5s;
  transform: translate(00px,0);
}
.page-leave-active {
  transition: all .5s;
  transform: translate(-100px,0);
}
.page-enter, .page-leave-to {
  opacity: 0;
}

yet they are still being purged.

"devDependencies": {
    "@nuxtjs/tailwindcss": "^1.0.0"
  }

@TheAlexLichter
Copy link
Member

@zernonia "@nuxtjs/tailwindcss": "^1.0.0" doesn't include nuxt-purgecss version ^2.0.0. That's why.

Feel free to upgrade the module ☺️

@zernonia
Copy link

@zernonia "@nuxtjs/tailwindcss": "^1.0.0" doesn't include nuxt-purgecss version ^2.0.0. That's why.

Feel free to upgrade the module ☺️

THAKN YOU SO MUCH for replying fast.. I didnt realised that this module wasnt included in @nuxtjs/tailwindcss..

@TheAlexLichter
Copy link
Member

@zernonia "@nuxtjs/tailwindcss": "^1.0.0" doesn't include nuxt-purgecss version ^2.0.0. That's why.
Feel free to upgrade the module ☺️

THAKN YOU SO MUCH for replying fast.. I didnt realised that this module wasnt included in @nuxtjs/tailwindcss..

No problem ☺️
It's included, but not the newest version. And tailwind comes with it's own purgecss process now btw, so updating the tailwind module + using tailwindcss.config.js purge options should be enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants