Config
Default configuration
When you install the module, the default Tailwind configuration for your project would be equivalent to this:
{
"theme": { "extend": {} },
"content": [
// all directories and extensions will correspond to your Nuxt config
"{srcDir}/components/**/*.{vue,js,jsx,mjs,ts,tsx}",
"{srcDir}/layouts/**/*.{vue,js,jsx,mjs,ts,tsx}",
"{srcDir}/pages/**/*.{vue,js,jsx,mjs,ts,tsx}",
"{srcDir}/plugins/**/*.{js,ts,mjs}",
"{srcDir}/composables/**/*.{js,ts,mjs}",
"{srcDir}/utils/**/*.{js,ts,mjs}",
"{srcDir}/{A,a}pp.{vue,js,jsx,mjs,ts,tsx}",
"{srcDir}/{E,e}rror.{vue,js,jsx,mjs,ts,tsx}",
"{srcDir}/app.config.{js,ts,mjs}",
"{srcDir}/app/spa-loading-template.html"
],
"plugins": []
}
You can learn more about the Tailwind configuration and the content configuration in Tailwind docs.
Overwriting the configuration
You can extend the default configuration:
- with a tailwind.config file
- using the config option
- using hooks, such as the
tailwindcss:config
tailwind.config
If a tailwind.config
file is present, it will be imported and used to overwrite the default configuration. All of the following file extensions will work by default: .js
, .cjs
, .mjs
, and .ts
. You can configure the path with the configPath
option.
config
option.import colors from 'tailwindcss/colors'
export default {
theme: {
extend: {
colors: {
primary: colors.green
}
}
}
}
Learn more about the Tailwind config in their docs.
config
option
You can also use your nuxt.config
file to set your Tailwind config with the tailwindcss.config
property:
import colors from 'tailwindcss/colors'
export default defineNuxtConfig({
// ...
tailwindcss: {
config: {
theme: {
extend: {
colors: { primary: colors.green }
}
}
}
}
})
tailwind.config
file(s).Hooks
tailwindcss:loadConfig
Passes any Tailwind configuration read by the module for each (extended) layer/path before merging all of them.
tailwindcss:config
Passes the resolved vanilla configuration read from all layers and paths with merging using defu.
tailwindcss:resolvedConfig
Passes the complete resolved configuration with all defaults from the full Tailwind config using resolveConfig
.
Usage
You can use a Nuxt hook to manipulate the Tailwind configuration.
// ~/modules/nuxt-tailwind-mod/index.ts
import { defineNuxtModule, addTemplate } from '@nuxt/kit'
export default defineNuxtModule({
setup (options, nuxt) {
nuxt.hook('tailwindcss:config', function (tailwindConfig) {
tailwindConfig.theme.colors.blue = '#fff'
})
nuxt.hook('tailwindcss:resolvedConfig', function (resolvedConfig) {
console.log('This is the resulting config', JSON.stringify(resolvedConfig))
})
}
})
Learn more about Nuxt modules.
async/await
) and are called after merging the configurations.Referencing in the application
It can often be useful to reference Tailwind configuration values at runtime, e.g. to access some of your theme values when dynamically applying inline styles in a component.
If you need resolved Tailwind config at runtime, you can enable the exposeConfig option:
export default defineNuxtConfig({
// ...
tailwindcss: {
exposeConfig: true
}
})
Then, import where needed from #tailwind-config
:
// Import fully resolved config
import tailwindConfig from '#tailwind-config'
// Import only part which is required to allow tree-shaking
import { theme } from '#tailwind-config'
Please be aware this adds ~19.5KB
(~3.5KB
) to the client bundle size. If you want to only import really specific parts of your tailwind config, you can enable imports for each property in the config:
export default defineNuxtConfig({
// ...
tailwindcss: {
exposeConfig: {
level: 4,
alias: '#twcss' // if you want to change alias
}
}
})
// Import within properties for further tree-shaking
import screens from '#twcss/theme/screens' // default import
import { _neutral } from '#twcss/theme/colors' // named (with _ prefix)
import { _800 as slate800 } from '#twcss/theme/colors/slate' // alias
level
to ever be over 4 - the usual depth of a Tailwind config. A higher value is also likely to increase boot-time and disk space in dev. Refer to the Nuxt Virtual File System to see generated files._
(_colors
, _900
, _2xl
) to ensure safe variable names. You can use default imports to provide any identifier or rename named imports using as
. Properties with unsafe variable names (spacing['1.5']
, height['1/2']
, keyframes.ping['75%, 100%']
) do not get exported individually.Tailwind CSS version
If you wish to use another version of Tailwind CSS (i.e., different from the one that would be installed with this module), you can simply do so using your package manager like so:
yarn add -D tailwindcss@3.2.0