Skip to content

配置导入 API 参考

Added in: astro@5.7.0

此虚拟模块 astro:config 公开了一个非详尽、可序列化、类型安全的 Astro 配置版本。有两个子模块用于访问配置值的不同子集:/client and /server

¥This virtual module astro:config exposes a non-exhaustive, serializable, type-safe version of the Astro configuration. There are two submodules for accessing different subsets of your configuration values: /client and /server.

可以从 astro:config/server 访问所有可用的配置值。但是,对于在客户端上执行的代码,只有 astro:config/client 公开的值才可用。这通过仅向客户端提供一些数据来保护你的信息。

¥All available config values can be accessed from astro:config/server. However, for code executed on the client, only those values exposed by astro:config/client will be available. This protects your information by only making some data available to the client.

astro:config/client 导入

标题部分 从 astro:config/client 导入

¥Imports from astro:config/client

import {
i18n,
trailingSlash,
base,
build,
site,
} from "astro:config/client";

使用此子模块编写客户端代码:

¥Use this submodule for client-side code:

src/utils.js
import { trailingSlash } from "astro:config/client";
function addForwardSlash(path) {
if (trailingSlash === "always") {
return path.endsWith("/") ? path : path + "/"
} else {
return path
}
}

有关 astro:config/client 中可用的配置导入的更多信息:

¥See more about the configuration imports available from astro:config/client:

astro:config/server 导入

标题部分 从 astro:config/server 导入

¥Imports from astro:config/server

import {
i18n,
trailingSlash,
base,
build,
site,
srcDir,
cacheDir,
outDir,
publicDir,
root,
} from "astro:config/client";

这些导入包含 astro:config/client 中的所有内容,以及有关文件系统配置的其他敏感信息,这些信息不宜安全地暴露给客户端。

¥These imports include everything available from astro:config/client as well as additional sensitive information about your file system configuration that is not safe to expose to the client.

使用此子模块编写服务器端代码:

¥Use this submodule for server side code:

astro.config.mjs
import { integration } from "./integration.mjs";
export default defineConfig({
integrations: [
integration(),
]
});
integration.mjs
import { outDir } from "astro:config/server";
import { writeFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
export default function() {
return {
name: "internal-integration",
hooks: {
"astro:build:done": () => {
let file = new URL("result.json", outDir);
// generate data from some operation
let data = JSON.stringify([]);
writeFileSync(fileURLToPath(path), data, "utf-8");
}
}
}
}

有关 astro:config/server 中可用的配置导入的更多信息:

¥See more about the configuration imports available from astro:config/server:

Astro v5.7 中文网 - 粤ICP备13048890号