Configuring Astro
هذا المحتوى غير متوفر بلغتك بعد.
Customize how Astro works by adding an astro.config.mjs
file in your project. This is a common file in Astro projects, and all official example templates and themes ship with one by default.
The Astro Config File
Section titled The Astro Config FileA valid Astro config file exports its configuration using the default
export, using the recommended defineConfig
helper:
Using defineConfig()
is recommended for automatic type hints in your IDE, but it is also optional. An absolutely bare-minimum, valid configuration file would look like this:
Supported Config File Types
Section titled Supported Config File TypesAstro supports several file formats for its JavaScript configuration file: astro.config.js
, astro.config.mjs
, astro.config.cjs
and astro.config.ts
. We recommend using .mjs
in most cases or .ts
if you want to write TypeScript in your config file.
TypeScript config file loading is handled using tsm
and will respect your project tsconfig options.
Config File Resolving
Section titled Config File ResolvingAstro will automatically try to resolve a config file named astro.config.mjs
inside project root. If no config file is found in your project root, Astro’s default options will be used.
You can explicitly set a config file to use with the --config
CLI flag. This CLI flag always resolves relative to the current working directory where you ran the astro
CLI command.
Config IntelliSense
Section titled Config IntelliSenseAstro recommends using the defineConfig()
helper in your configuration file. defineConfig()
provides automatic IntelliSense in your IDE. Editors like VSCode are able to read Astro’s TypeScript type definitions and provide automatic jsdoc type hints, even if your configuration file isn’t written in TypeScript.
You can also provide type definitions manually to VSCode, using this JSDoc notation:
Referencing Relative Files
Section titled Referencing Relative FilesIf you provide a relative path to root
or the --root
CLI flag, Astro will resolve it against the current working directory where you ran the astro
CLI command.
Astro will resolve all other relative file and directory strings as relative to the project root:
To reference a file or directory relative to the configuration file, use import.meta.url
(unless you are writing a common.js astro.config.cjs
file).
Vite-specific import.meta
properties, like import.meta.env
or import.meta.glob
, are not accessible from your configuration file. We recommend alternatives like dotenv or fast-glob for these respective use cases. In addition, tsconfig path aliases will not be resolved. Use relative paths for module imports in this file.
Customising Output Filenames
Section titled Customising Output FilenamesFor code that Astro processes, like imported JavaScript or CSS files, you can customise output filenames using entryFileNames
, chunkFileNames
, and assetFileNames
in a vite.build.rollupOptions
entry in your astro.config.*
file.
This can be helpful if you have scripts with names that might be impacted by ad blockers (e.g. ads.js
or google-tag-manager.js
).
Environment Variables
Section titled Environment VariablesAstro evaluates configuration files before it loads your other files. As such, you can’t use import.meta.env
to access environment variables that were set in .env
files.
You can use process.env
in a configuration file to access other environment variables, like those set by the CLI.
You can also use Vite’s loadEnv
helper to manually load .env
files.
pnpm
does not allow you to import modules that are not directly installed in your project. If you are using pnpm
, you will need to install vite
to use the loadEnv
helper.