Installing a Vite or Rollup plugin
Questi contenuti non sono ancora disponibili nella tua lingua.
Astro builds on top of Vite, and supports both Vite and Rollup plugins. This recipe uses a Rollup plugin to add the ability to import a YAML (.yml) file in Astro.
Recipe
Install
@rollup/plugin-yaml:npm install @rollup/plugin-yaml --save-devpnpm add @rollup/plugin-yaml --save-devyarn add @rollup/plugin-yaml --devImport the plugin in your
astro.config.mjsand add it to the Vite plugins array:import { defineConfig } from 'astro/config'; import yaml from '@rollup/plugin-yaml'; export default defineConfig({ vite: { plugins: [yaml()] } });Finally, you can import YAML data using an
importstatement:import yml from './data.yml';:::noteWhile you can now import YAML data in your Astro project, your editor will not provide types for the imported data. To add types, create or find an existing
*.d.tsfile in thesrcdirectory of your project and add the following:// Specify the file extension you want to import declare module "*.yml" { const value: any; // Add type definitions here if desired export default value; }This will allow your editor to provide type hints for your YAML data.:::