Integrations
ใน Next.js คุณขยาย framework ผ่าน next.config.js — webpack plugins, Babel transforms, rewrites, redirects และ third-party packages ที่ hook เข้า build pipeline Astro มีแนวคิดชั้นหนึ่งที่เรียกว่า integrations ที่ครอบคลุม surface area เดียวกัน แต่ชัดเจนกว่า: แต่ละ integration คือ npm package ที่ลงทะเบียนใน astro.config.mjs และสามารถแตะ build pipeline, dev server และ output ได้
astro.config.mjs vs next.config.js
หัวข้อที่มีชื่อว่า “astro.config.mjs vs next.config.js”// next.config.jsconst withBundleAnalyzer = require('@next/bundle-analyzer')({ enabled: process.env.ANALYZE === 'true',});
/** @type {import('next').NextConfig} */const nextConfig = { reactStrictMode: true, swcMinify: true, images: { domains: ['images.unsplash.com'], }, async redirects() { return [ { source: '/old', destination: '/new', permanent: true }, ]; },};
module.exports = withBundleAnalyzer(nextConfig);// astro.config.mjsimport { defineConfig } from 'astro/config';import react from '@astrojs/react';import tailwind from '@astrojs/tailwind';import sitemap from '@astrojs/sitemap';import vercel from '@astrojs/vercel/serverless';
export default defineConfig({ site: 'https://example.com', integrations: [ react(), tailwind(), sitemap(), ], adapter: vercel(), redirects: { '/old': '/new', },});helper defineConfig ให้ TypeScript autocomplete เต็มรูปแบบใน editor integration ทั้งหมดอยู่ใน array integrations: [] — ไม่มี pattern การ wrap/compose แบบ withBundleAnalyzer(nextConfig)
เพิ่ม integrations ด้วย astro add
หัวข้อที่มีชื่อว่า “เพิ่ม integrations ด้วย astro add”# UI framework integrationsnpx astro add react # @astrojs/react — เปิดใช้งาน .tsx islandsnpx astro add preact # @astrojs/preact — ทางเลือก React-compatible ที่เบากว่าnpx astro add svelte # @astrojs/sveltenpx astro add vue # @astrojs/vue
# Stylingnpx astro add tailwind # @astrojs/tailwind — inject Tailwind เข้าทุกหน้าอัตโนมัติ
# Site utilitiesnpx astro add sitemap # @astrojs/sitemap — สร้าง sitemap.xml ตอน buildnpx astro add mdx # @astrojs/mdx — รองรับ MDX สำหรับ content pages
# SSR adapters (เลือกหนึ่งอัน)npx astro add vercel # @astrojs/vercelnpx astro add netlify # @astrojs/netlifynpx astro add node # @astrojs/node (self-hosted Node.js)npx astro add cloudflare # @astrojs/cloudflareแต่ละคำสั่งติดตั้ง package และ แก้ไข astro.config.mjs ให้คุณ คุณสามารถแก้ไข config ด้วยตนเองภายหลังได้เสมอ
รูปแบบ integration
หัวข้อที่มีชื่อว่า “รูปแบบ integration”Astro integration คือเพียง function ที่ return config object นี่คือ custom integration แบบ minimal:
import { defineConfig } from 'astro/config';
function myPlugin() { return { name: 'my-plugin', hooks: { 'astro:build:done': ({ dir }) => { console.log('Build finished. Output:', dir.pathname); }, }, };}
export default defineConfig({ integrations: [myPlugin()],});คุณจะแทบไม่ต้องเขียน custom integrations เอง — package @astrojs/* อย่างเป็นทางการครอบคลุมกรณีทั่วไปแล้ว — แต่การเข้าใจรูปแบบจะช่วยเมื่อคุณอ่าน source code ของ integration
ส่ง options ให้ integrations
หัวข้อที่มีชื่อว่า “ส่ง options ให้ integrations”import { defineConfig } from 'astro/config';import react from '@astrojs/react';import tailwind from '@astrojs/tailwind';
export default defineConfig({ integrations: [ react({ include: ['**/interactive/**'], // hydrate เฉพาะ components ใน dir นี้ }), tailwind({ applyBaseStyles: false, // ข้าม Tailwind's base reset }), ],});