I am trying to use @apply on my custom class in Nuxt.js 2
nuxt.config.js
export default {
buildModules: [
'@nuxtjs/tailwindcss',
],
tailwindcss: {
cssPath: '~/assets/app.css',
exposeConfig: true
}
}
assets/app.css
@tailwind base;
@tailwind components;
@tailwind utilities;
@layer utilities {
.btn {
@apply border-2 p-2 font-bold;
}
}
in any vue-single-file or any other scss file
<style lang="scss">
.btn-lg {
@apply btn;
}
</style>

The
btnclass does not exist. If you're sure thatbtnexists, make sure that any@importstatements are being properly processed before Tailwind CSS sees your CSS, as@applycan only be used for classes in the same CSS tree
So, how to make my custom styles be seen by the Tailwind CSS before processing to make my custom classes work in @apply?
I've tried the solutions in the following questions and document
But none of them work
I am using:
Thanks a lot for any replies!
As I mentioned in the comments, add a mode: "jit" can solve this problem.
tailwind.config.js
module.exports = {
mode: 'jit'
}
It's a good vanilla solution.
However, if you are programming the project in a virtual machine(Homestead) just like me, this could cause a error that the node can't recoginze the changings of your css/scss/sass files.
so there's another two solutions:
use pure tailwindcss instead of @nuxtjs/tailwindcss
just follow the document: Install Tailwind CSS with Nuxt.js
use plugin() in your tailwind.config.css
const plugin = require('tailwindcss/plugin') const fs = require('fs') module.exports = { // ... purge, theme, variants, ... plugins: [ plugin(function({ addUtilities, postcss }) { const css = fs.readFileSync('./your-custom-style-file-path', 'utf-8') addUtilities(postcss.parse(css).nodes) }), ], }from github
But what's more, this solution can't recoginze the changings too. So add your css/scss/sass files in nuxt.config.js (I'm using nuxt-vite)
vite: {
plugins: [
{
name: 'watch-external', // https://stackoverflow.com/questions/63373804/rollup-watch-include-directory/63548394#63548394
async buildStart(){
const files = await fg(['assets/**/*']);
for(let file of files){
this.addWatchFile(file);
}
}
}
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With