Next.js
// TODO: link agli esempi
Iniziare con UnoCSS e Next.js.
Configurazione
Installazione
bash
pnpm add -D unocss @unocss/webpackbash
yarn add -D unocss @unocss/webpackbash
npm install -D unocss @unocss/webpackbash
bun add -D unocss @unocss/webpackConfigurazione
Crea uno.config.ts nella root del tuo progetto.
ts
import {
defineConfig,
presetAttributify,
presetIcons,
presetWebFonts,
presetWind3
} from 'unocss'
export default defineConfig({
presets: [
presetWind3(),
// ...
],
})Aggiungi plugin
Poi aggiungi UnoCSS come plugin a webpack tramite la tua next.config.js.
js
// next.config.js
const UnoCSS = require('@unocss/webpack').default
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
webpack: (config) => {
config.plugins.push(
UnoCSS(),
)
return config
},
}
module.exports = nextConfigImporta stylesheet
Poi importa uno.css in _app.tsx.
tsx
import type { AppProps } from 'next/app'
// _app.tsx
import '@unocss/reset/tailwind.css'
import 'uno.css'
function MyApp({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />
}
export default MyAppUtilizzo
Stilizza i tuoi componenti con unocss!
tsx
/* index.tsx */
const Home: NextPage = () => {
return (
<>
<main className="py-20 px-12 text-center flex flex-col items-center gap-20px">
<span text="blue 5xl hover:red" cursor="default">Nextjs</span>
<div className="i-carbon-car inline-block" text="4xl" />
<button className="btn w-10rem">Button</button>
</main>
</>
)
}Hot Module Reload
Per supportare HMR devi disabilitare la cache di webpack.
js
// next.config.js
const nextConfig = {
reactStrictMode: true,
webpack: (config) => {
+ config.cache = false
config.plugins.push(UnoCSS())
return config
}
}Risoluzione dei problemi
Errore relativo al modulo virtuale
bash
Error: ENOENT: no such file or directory, open '.../_virtual_/__uno.css'Prova a eliminare la directory .next e riavvia il server di sviluppo.
Altro
Potresti dover aumentare il target almeno a es2015 nel tuo tsconfig.json per costruire il tuo progetto.
I file con estensione .js non sono supportati per impostazione predefinita. Cambia le estensioni dei file in .jsx o prova a includere i file js nella tua configurazione con include: /\.js$/. Scopri di più.