Next.js
// TODO: link to examples
Komma igång med UnoCSS och Next.js.
Setup
Installation
bash
pnpm add -D unocss @unocss/webpackbash
yarn add -D unocss @unocss/webpackbash
npm install -D unocss @unocss/webpackbash
bun add -D unocss @unocss/webpackKonfiguration
Skapa uno.config.ts i rotkatalogen för ditt projekt.
ts
import {
defineConfig,
presetAttributify,
presetIcons,
presetWebFonts,
presetWind3
} from 'unocss'
export default defineConfig({
presets: [
presetWind3(),
// ...
],
})Lägg till plugin
Lägg sedan till UnoCSS som en plugin till webpack genom din 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 = nextConfigImportera stylesheets
Importera sedan uno.css i _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 MyAppAnvändning
Styla dina komponenter med 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
För att stödja HMR måste du välja bort webpack:s cachelagring.
js
// next.config.js
const nextConfig = {
reactStrictMode: true,
webpack: (config) => {
+ config.cache = false
config.plugins.push(UnoCSS())
return config
}
}Felsökning
Fel gällande virtuell modul
bash
Error: ENOENT: no such file or directory, open '.../_virtual_/__uno.css'Försök att radera .next-katalogen och starta om dev-servern.
Annat
Du kan behöva uppgradera ditt mål till minst es2015 i din tsconfig.json för att bygga ditt projekt.
Filer med .js extension stöds inte som standard. Ändra dina filextensioner till .jsx eller försök att inkludera js-filer i din konfiguration med include: /\.js$/. Läs mer.