tailwindcss icon indicating copy to clipboard operation
tailwindcss copied to clipboard

Saving Tailwind config written using 'export default' with 'exposeConfig: true' causes malformed generated config and types

Open OpenSnack opened this issue 1 year ago • 2 comments

Environment


  • Operating System: Linux
  • Node Version: v18.20.3 (on StackBlitz - v20 on my machine)
  • Nuxt Version: 3.16.0
  • CLI Version: 3.23.0
  • Nitro Version: 2.11.6
  • Package Manager: [email protected]
  • Builder: -
  • User Config: compatibilityDate, devtools, modules, tailwindcss, typescript
  • Runtime Modules: @nuxtjs/[email protected]
  • Build Modules: -

Reproduction

https://stackblitz.com/edit/github-3urdppax?file=tailwind.config.js

Describe the bug

When tailwindcss.exposeConfig is enabled alongside typescript.typeCheck, the type checker catches an error in the module's type generation:

  • Start the dev server
  • Make any change to tailwind.config.js
  • The following types are generated:
// .nuxt/tailwind/expose/index.mjs

import default from "#build/tailwind/expose/default.mjs"
const config = { theme, corePlugins, plugins, content, presets, darkMode, prefix, important, separator, safelist, blocklist, default }
export { config as default, theme, corePlugins, plugins, content, presets, darkMode, prefix, important, separator, safelist, blocklist, default }

// .nuxt/types/tailwind.config.d.ts

declare module "#tailwind-config/default" { export const _content: typeof import("#tailwind-config/default/content")["default"]; export const _theme: typeof import("#tailwind-config/default/theme")["default"]; export const _plugins: typeof import("#tailwind-config/default/plugins")["default"]; const defaultExport: { "content": typeof _content, "theme": typeof _theme, "plugins": typeof _plugins }; export default defaultExport; }
declare module "#tailwind-config" { export const theme: typeof import("#tailwind-config/theme")["default"]; export const corePlugins: typeof import("#tailwind-config/corePlugins")["default"]; export const plugins: typeof import("#tailwind-config/plugins")["default"]; export const content: typeof import("#tailwind-config/content")["default"]; export const presets: typeof import("#tailwind-config/presets")["default"]; export const darkMode: typeof import("#tailwind-config/darkMode")["default"]; export const prefix: typeof import("#tailwind-config/prefix")["default"]; export const important: typeof import("#tailwind-config/important")["default"]; export const separator: typeof import("#tailwind-config/separator")["default"]; export const safelist: typeof import("#tailwind-config/safelist")["default"]; export const blocklist: typeof import("#tailwind-config/blocklist")["default"]; export const default: typeof import("#tailwind-config/default")["default"]; const defaultExport: { "theme": typeof theme,"corePlugins": typeof corePlugins,"plugins": typeof plugins,"content": typeof content,"presets": typeof presets,"darkMode": typeof darkMode,"prefix": typeof prefix,"important": typeof important,"separator": typeof separator,"safelist": typeof safelist,"blocklist": typeof blocklist,"default": typeof default }; export default defaultExport; }

Note the import default from "#build/tailwind/expose/default.mjs" and export const default: typeof import("#tailwind-config/default")["default"]; which are malformed. This is flagged by the type checker, and also breaks Tailwind CSS Intellisense, occasionally causes HMR errors, etc.

Additional context

As I understand it: in the builder:watch hook, the updated Tailwind config _config gets resolved with resolveTWConfig:

https://github.com/nuxt-modules/tailwindcss/blob/d40ed3f15cc0d095c735d8296aa05414feb61d1a/src/internal-context/load.ts#L264-L274

In the case where the config is written as export default { ... }, this _config object is formed as { default: { ... } } which I think is why we wrongly get import default generated, for example.

In my testing, using resolveTWConfig(_config.default ?? _config) solves the problem, though I don't know if this would be the preferred way to fix it.

Logs


OpenSnack avatar Mar 19 '25 05:03 OpenSnack

Oh okay wow - very good catch. Thank you, will investigate and fix this in the next release!

ineshbose avatar Mar 19 '25 22:03 ineshbose

Fixing with https://github.com/nuxt-modules/tailwindcss/commit/d3e4de719d5e4fdea174c8c0a73030351eee2c4d. Will release in next stable 👍

ineshbose avatar Apr 08 '25 10:04 ineshbose