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

Safelist causing build failures #838

Closed
paulvonber opened this issue Apr 18, 2024 · 17 comments · Fixed by #839
Closed

Safelist causing build failures #838

paulvonber opened this issue Apr 18, 2024 · 17 comments · Fixed by #839
Labels
bug Something isn't working

Comments

@paulvonber
Copy link

Version

@nuxtjs/tailwindcss: 6.12.0
@nuxt/ui: 2.15.2
nuxt: 3.11.2

Having a safelist array in tailwind.config.{js,ts} messing up the build

image
@paulvonber paulvonber added the bug Something isn't working label Apr 18, 2024
@ineshbose
Copy link
Collaborator

Hi, thanks for opening an issue - this bug makes sense. Can you share your Tailwind configuration, or is this coming from nuxt/ui?

@paulvonber
Copy link
Author

That's my tailwind.config.js so everything is ok unless I add safelist

image

@ineshbose
Copy link
Collaborator

Config here for reference:

export default {
  safelist: [
    {
      pattern: /^(bg|border|text)-(neutral|gray)-(50|100|200)$/,
      variants: ['md', 'lg', 'hover', 'group-hover']
    }
  ]
}

@ineshbose
Copy link
Collaborator

Sorry, I'm not able to replicate it. Do you think you can create a reproduction?

Use https://stackblitz.com/github/nuxt/starter/tree/v3-stackblitz or https://stackblitz.com/github/nuxt-modules/tailwindcss as a starter.

@ineshbose ineshbose added the need-repro Issue needs reproduction label Apr 18, 2024
@stevenhurth
Copy link

stevenhurth commented Apr 19, 2024

Using NuxtUI, we had the exact same issue yesterday, with the safelist pattern/variants defined in our nuxt.config.ts file.

To keep development moving along, we were able to get rid of the safelist entirely by reworking some dynamic widths, but the WARN message regarding functional plugins in the tailwindcss.config still resides, and now intellisense no longer works.

For reference, this was our config before nixing the safelist entirely.

tailwindcss: {
    config: {
        safelist: [
            {
                pattern: /^w-(\d+\/\d+|full)$/,
                variants: ["sm", "md", "lg", "xl"]
            }
        ]
    }
}

@ineshbose
Copy link
Collaborator

Using NuxtUI, we had the exact same issue yesterday, with the safelist pattern/variants defined in our nuxt.config.ts file.

To keep development moving along, we were able to get rid of the safelist entirely by reworking some dynamic widths, but the WARN message regarding functional plugins in the tailwindcss.config still resides, and now intellisense no longer works.

For reference, this was our config before nixing the safelist entirely.

tailwindcss: {

    config: {

        safelist: [

            {

                pattern: /^w-(\d+\/\d+|full)$/,

                variants: ["sm", "md", "lg", "xl"]

            }

        ]

    }

}

Thanks for chiming in @stevenhurth. There's a PR open in NuxtUI to support this latest feature. With your config however (thanks for reference), the regex in safelist would need to move to a tailwind.config file as its non-serialisable. Perhaps the module isn't detecting this and putting you back to the old version so the build is failing - I'll provide a fix for backwards compatibility but can you confirm if moving this to a separate config file helps?

@stevenhurth
Copy link

@ineshbose

I gave it a shot, but it's still a no-go. The error gets logged x73, even when in it's own tailwind.config.

export default {
  safelist: [
    {
      pattern: /^w-(\d+\/\d+|full)$/,
      variants: ["sm", "md", "lg", "xl"]
    }
  ]
}

@ineshbose
Copy link
Collaborator

Okay I was testing usage of safelist on project without nuxt/ui, but I can confirm this issue.

I traced it back to this code causing the issue:
https://github.com/nuxt/ui/blob/8d9d9736ba6a0f25d8cd0b60e1ca50b072a3d176/src/module.ts#L149

and this should be solved by nuxt/ui#1665 🙂

@fasterv410
Copy link

fasterv410 commented Apr 24, 2024

I also got this problem while npm run build
https://stackblitz.com/edit/nuxt-starter-urujx1?file=tailwind.config.ts

"@nuxt/ui": "^2.15.2",
"nuxt": "^3.11.2"
Screenshot 2567-04-24 at 14 55 54

@ineshbose ineshbose removed the need-repro Issue needs reproduction label Apr 24, 2024
@ineshbose
Copy link
Collaborator

Going to provide a fix for this in 6.12.1 🙂

@ineshbose
Copy link
Collaborator

It should not crash (but give a soft warning) on nightly now if you would like to confirm. Will release accordingly 🙂

@ineshbose
Copy link
Collaborator

Would someone like to confirm if nightly works with their project? :)

@SergkeiM
Copy link

SergkeiM commented May 30, 2024

Hi @ineshbose

I can confirm is working now.

My use case:

I have a NuxtModule that installs @nuxtjs/tailwindcss and sets some default for tailwind.

Some code:

import { installModule } from '@nuxt/kit'
export default defineNuxtModule<ModuleOptions>({
    meta: {
        name: 'myModule',
        configKey: 'myModule'
    },
    defaults(nuxt) {
        return {
            enabled: true,
            screens: {
                sm: '576px',
                md: '768px',
                lg: '992px',
                xl: '1200px',
                xxl: '1400px'
            },
        }
    },
    async setup (options, nuxt) {
          nuxt.hook('tailwindcss:config', function (tailwindConfig) {
                 tailwindConfig.theme.screens = options.screens
                 
                 tailwindConfig.safelist = [
                       ...(tailwindConfig.safelist || []),
                       {
                           pattern: /^col-([1-9]|1[0-2])$/,
                           variants: ['sm', 'md', 'lg', 'xl', 'xxl'],
                       }
                 ]
          })
          await installModule('@nuxtjs/tailwindcss')
    }

In version v6.12.0 this doesn't work as it strips out the regex, in nightly build (6.12.1-1715798143.63a8a8e) this works, but it gives this warnings

[nuxt:tailwindcss 2:18:06 PM]  WARN  A hook has injected a non-serializable value in config["plugins"], so the Tailwind Config cannot be serialized. Falling back to providing the loaded configuration inlined directly to PostCSS loader.. Please consider using a configuration file/template instead (specifying in configPath of the module options) to enable additional support for IntelliSense and HMR.

[nuxt:tailwindcss 2:18:06 PM]  WARN  A hook is injecting RegExp values in your configuration (check config["safelist"]) which may be unsafely serialized. Consider moving your safelist to a separate configuration file/template instead (specifying in configPath of the module options)

Can you point me with correct direction on how to to make this correctly so warnings will not appear? The warning message is not clear for me. Thank you.

@ineshbose
Copy link
Collaborator

ineshbose commented May 30, 2024

I can confirm is working now.

Many many thanks for confirming. 🙏

I have a NuxtModule that installs @nuxtjs/tailwindcss and sets some default for tailwind.
...
In version v6.12.0 this doesn't work as it strips out the regex, in nightly build (6.12.1-1715798143.63a8a8e) this works, but it gives this warnings
...
Can you point me with correct direction on how to to make this correctly so warnings will not appear? The warning message is not clear for me. Thank you.

Sure - it's the RegExp value being added by your hook that would be better to add via a template. I'm just working on #853 that adds an example to the docs on how you can author a module to extend the Tailwind configuration; if you see the Stackblitz example here, based on that, we can change your module code to this:

import { installModule, addTemplate } from '@nuxt/kit'
export default defineNuxtModule<ModuleOptions>({
    meta: {
        name: 'myModule',
        configKey: 'myModule'
    },
    defaults(nuxt) {
        return {
            enabled: true,
            screens: {
                sm: '576px',
                md: '768px',
                lg: '992px',
                xl: '1200px',
                xxl: '1400px'
            },
        }
    },
    async setup (options, nuxt) {
          nuxt.hook('tailwindcss:config', function (tailwindConfig) {
                 tailwindConfig.theme.screens = options.screens
          })

          const configTemplate = addTemplate({
            filename: 'myModule-tw.config.mjs',
            write: true,
            getContents: () => `export default { safelist: [{ pattern: /^col-([1-9]|1[0-2])$/, variants: ['sm', 'md', 'lg', 'xl', 'xxl'] }] }`
          })
          await installModule('@nuxtjs/tailwindcss', {
            configPath: [
              configTemplate.dst,
              join(nuxt.options.rootDir, 'tailwind.config')
            ]
         })
    }

(above code may have minor syntax issues as I'm on mobile). I appreciate that 6.12.0 requires module authors to write this little bit of code more, but it helps the module consumers to work with their configuration in a more safe and efficient way.

I hope this helps - let me know! 🙂

@SergkeiM
Copy link

Hi @ineshbose

Thank you a lot, works like a charm now.

module authors to write this little bit of code more

But is definetly better and cleaner. Great work thank you a lot.

@SergkeiM
Copy link

Hi @ineshbose, hope you are well, do you have an ETA for this to be released?

@ineshbose
Copy link
Collaborator

Hi @ineshbose, hope you are well, do you have an ETA for this to be released?

Targeting next week! Got some minor stuff to add in for the release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants