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

Adding content to tailwind in a nuxt module #852

Closed
guirak opened this issue May 26, 2024 · 2 comments
Closed

Adding content to tailwind in a nuxt module #852

guirak opened this issue May 26, 2024 · 2 comments
Labels
question Further information is requested

Comments

@guirak
Copy link

guirak commented May 26, 2024

Hello,

There is a change that occurs between nuxt/ui 2.15.1 and 2.16.0.

In 2.15.1, I was able to specify paths in the tailwindConfig.content.files doing the following in my module :

export const configureTailwind = (tailwindConfig: Partial<Config>, srcResolver: Resolver) => {
  const runtimeDir = srcResolver.resolve("./runtime");

  tailwindConfig.content.files.push(srcResolver.resolve(runtimeDir, "components/**/*.{vue,mjs,ts}"));
  tailwindConfig.content.files.push(srcResolver.resolve(runtimeDir, "config/**/*.{vue,mjs,ts}"));

};

This function is called on the module.ts on the hook tailwindcss:config :

    nuxt.hook("tailwindcss:config",
      (tailwindConfig: Partial<Config>) => configureTailwind(tailwindConfig, srcResolver));

In the last version, I am having the tailwindConfig.content.files undefined sometimes when calling the pnpm run dev:prepare

I think I am not using the good way to do that.

My project is a mono repo with severals nuxt module that depends of nuxt/ui and can have dependencies between them.

Please, can you give me the clean way to provide the content files to tailwind css from nuxt modules ?

Thank you

@guirak guirak added the question Further information is requested label May 26, 2024
@ineshbose
Copy link
Collaborator

Hi, thanks for opening an issue on this repository - it's a good question.

Content configuration could be a bit varied, so hooks just need minor if-conditions to extend this. See how the FormKit module does this. In your case, this is what we'll do:

export const configureTailwind = (tailwindConfig: Partial<Config>, srcResolver: Resolver) => {
  const runtimeDir = srcResolver.resolve("./runtime");

  // in case `content: undefined` which is highly unlikely, we provide default value
  tailwindConfig.content = tailwindConfig.content ?? { files: [] }

  if (Array.isArray(tailwindConfig.content)) {
     // content: string[]
     tailwindConfig.content.push(...)
  } else {     
    tailwindConfig.content.files.push(srcResolver.resolve(runtimeDir, "components/**/*.{vue,mjs,ts}"));
    tailwindConfig.content.files.push(srcResolver.resolve(runtimeDir, "config/**/*.{vue,mjs,ts}"));
  }
};

It's likely that in your hook, tailwindConfig.content was a simple array of paths, so Array.prototype.files was undefined. I do think of extending this module to always pass content as an object as a future improvement, but devs should also ensure safer hooks to support different Tailwind configurations. 🙂

@guirak
Copy link
Author

guirak commented Jun 3, 2024

Hi Ineshbose,

Thank you, I will try to test it next week and come back here with a feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants