Skip to content

Commit cce10bc

Browse files
author
福晋
committed
chore: split integration file
1 parent 5019805 commit cce10bc

30 files changed

Lines changed: 1635 additions & 1156 deletions

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ The public configuration covers:
251251
- home content, feature cards, and controlled Astro component slots;
252252
- CSS token overrides for consumer-defined branding and theme customization;
253253
- footer content;
254-
- Demo height and output directory.
254+
- Demo height.
255255

256256
Unknown keys are rejected. Removed features therefore fail fast instead of being silently ignored.
257257

‎demos/basic-site/astro.base.config.mjs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ import { siteConfig } from "./site.config.mjs";
66

77
export default defineConfig({
88
base: "/platform/",
9+
outDir: "./dist-base",
910
integrations: [
10-
antvSite({
11-
...siteConfig,
12-
output: "./dist-base",
13-
}),
11+
antvSite(siteConfig),
1412
mdx(),
1513
sitemap(),
1614
],

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"build": "pnpm run clean && tsc -p tsconfig.build.json && node scripts/copy-theme.mjs",
4444
"check:types": "tsc -p tsconfig.build.json --noEmit",
4545
"check:theme": "astro check --root demos/basic-site",
46-
"test:unit": "node --test test/config.test.mjs test/content.test.mjs test/package-boundary.test.mjs test/qa-adapters.test.mjs test/qa-protocol.test.mjs test/integration-config.test.mjs",
46+
"test:unit": "node --test test/architecture-boundaries.test.mjs test/config.test.mjs test/content.test.mjs test/package-boundary.test.mjs test/qa-adapters.test.mjs test/qa-protocol.test.mjs test/integration-config.test.mjs",
4747
"test:integration": "node --test test/integration-build.test.mjs test/development-search.test.mjs",
4848
"test": "pnpm run build && node --test test/*.test.mjs",
4949
"check": "pnpm run test",

‎src/astro/adapter.ts‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import { fileURLToPath } from "node:url";
2+
import type { AstroIntegration } from "astro";
3+
import type { AntVSiteConfig, ResolvedSiteConfig } from "../compiler/config.js";
4+
import { resolveConfig } from "../compiler/config.js";
5+
import { prepareQaIntegration } from "../integration/qa.js";
6+
import {
7+
prepareSearchIntegration,
8+
writeProductionSearch,
9+
} from "../integration/search.js";
10+
import {
11+
prepareThemeIntegration,
12+
setupThemeDevServer,
13+
} from "../integration/theme.js";
14+
15+
export function antvSite(input: AntVSiteConfig): AstroIntegration {
16+
let config: ResolvedSiteConfig;
17+
let themeRestartRoots: string[] = [];
18+
19+
return {
20+
name: "@antv/site",
21+
hooks: {
22+
async "astro:config:setup"({
23+
addWatchFile,
24+
command,
25+
config: astroConfig,
26+
injectRoute,
27+
updateConfig,
28+
}) {
29+
const consumerRoot = fileURLToPath(astroConfig.root);
30+
if (astroConfig.output !== "static") {
31+
throw new Error(
32+
"@antv/site supports Astro static output only. Remove the server output or adapter configuration.",
33+
);
34+
}
35+
36+
config = await resolveConfig(input, consumerRoot, {
37+
outDir: fileURLToPath(astroConfig.outDir),
38+
srcDir: fileURLToPath(astroConfig.srcDir),
39+
publicDir: fileURLToPath(astroConfig.publicDir),
40+
cacheDir: fileURLToPath(astroConfig.cacheDir),
41+
});
42+
43+
const qa = await prepareQaIntegration(config);
44+
const theme = await prepareThemeIntegration({
45+
config,
46+
markdownProcessor: astroConfig.markdown.processor,
47+
getBase: () => astroConfig.base,
48+
});
49+
const search = prepareSearchIntegration({
50+
config,
51+
command,
52+
});
53+
themeRestartRoots = theme.restartRoots;
54+
55+
for (const path of [...theme.watchFiles, ...qa.watchFiles]) {
56+
addWatchFile(path);
57+
}
58+
for (const route of [
59+
...qa.routes,
60+
...theme.routes,
61+
...search.routes,
62+
]) {
63+
injectRoute(route);
64+
}
65+
66+
updateConfig({
67+
site: config.site.origin,
68+
trailingSlash: "always",
69+
build: { format: "directory", assets: "_assets" },
70+
markdown: { processor: theme.markdownProcessor },
71+
vite: {
72+
define: {
73+
...search.define,
74+
"import.meta.env.ANTV_SITE_DEVELOPMENT": JSON.stringify(
75+
command === "dev",
76+
),
77+
},
78+
plugins: [...theme.plugins, ...qa.plugins],
79+
server: {
80+
fs: {
81+
allow: [config.root, ...theme.fsAllow, ...qa.fsAllow],
82+
},
83+
},
84+
},
85+
});
86+
},
87+
"astro:server:setup"({ server }) {
88+
setupThemeDevServer(server, themeRestartRoots);
89+
},
90+
async "astro:build:done"({ dir }) {
91+
await writeProductionSearch(config, dir);
92+
},
93+
},
94+
};
95+
}

‎src/compiler/config.ts‎

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,15 @@ export interface AntVSiteConfig {
119119
demo?: {
120120
height?: number;
121121
};
122-
output?: string;
122+
/** @deprecated Configure Astro's standard `outDir` instead. */
123+
output?: never;
124+
}
125+
126+
export interface SiteWorkspacePaths {
127+
outDir?: string;
128+
srcDir?: string;
129+
publicDir?: string;
130+
cacheDir?: string;
123131
}
124132

125133
export interface ResolvedSiteConfig {
@@ -195,7 +203,6 @@ export interface ResolvedSiteConfig {
195203
demo: {
196204
height: number;
197205
};
198-
output: string;
199206
}
200207

201208
const localizedTextSchema = z.strictObject({
@@ -428,7 +435,12 @@ const configSchema = z
428435
.default({
429436
height: 480,
430437
}),
431-
output: z.string().min(1).default("./dist"),
438+
output: z
439+
.never({
440+
error:
441+
"output was removed. Configure Astro's standard outDir instead.",
442+
})
443+
.optional(),
432444
})
433445
.superRefine((config, context) => {
434446
if (!config.site.locales.includes(config.site.defaultLocale)) {
@@ -529,7 +541,8 @@ async function validateOutputPath(
529541
root: string,
530542
output: string,
531543
content: Pick<ResolvedSiteConfig["content"], "docs" | "examples">,
532-
): Promise<string> {
544+
workspace: SiteWorkspacePaths,
545+
): Promise<void> {
533546
const physicalRoot = await resolvePhysicalPath(root);
534547
const physicalOutput = await resolvePhysicalPath(output);
535548
if (
@@ -544,7 +557,9 @@ async function validateOutputPath(
544557
const protectedPaths = [
545558
content.docs,
546559
content.examples,
547-
resolve(physicalRoot, "public"),
560+
workspace.srcDir ?? resolve(physicalRoot, "src"),
561+
workspace.publicDir ?? resolve(physicalRoot, "public"),
562+
workspace.cacheDir ?? resolve(physicalRoot, ".astro"),
548563
resolve(physicalRoot, ".antv-site"),
549564
resolve(physicalRoot, ".git"),
550565
resolve(physicalRoot, "node_modules"),
@@ -557,17 +572,16 @@ async function validateOutputPath(
557572
isWithin(physicalProtectedPath, physicalOutput)
558573
) {
559574
throw new Error(
560-
`Site output overlaps a protected input or workspace directory: ${physicalOutput}`,
575+
`Site output overlaps a protected input or workspace directory: ${physicalOutput} (protected: ${physicalProtectedPath})`,
561576
);
562577
}
563578
}
564-
565-
return physicalOutput;
566579
}
567580

568581
export async function resolveConfig(
569582
input: AntVSiteConfig,
570583
consumerRoot: string,
584+
workspace: SiteWorkspacePaths = {},
571585
): Promise<ResolvedSiteConfig> {
572586
const config = configSchema.parse(input);
573587
const root = await resolvePhysicalPath(consumerRoot);
@@ -578,10 +592,11 @@ export async function resolveConfig(
578592
? resolveFromRoot(root, config.content.examples)
579593
: null,
580594
};
581-
const output = await validateOutputPath(
595+
await validateOutputPath(
582596
root,
583-
resolveFromRoot(root, config.output),
597+
workspace.outDir ?? resolve(root, "dist"),
584598
content,
599+
workspace,
585600
);
586601

587602
return {
@@ -615,6 +630,5 @@ export async function resolveConfig(
615630
]),
616631
) as Record<HomeSlotName, string[]>,
617632
},
618-
output,
619633
} as ResolvedSiteConfig;
620634
}

‎src/compiler/content.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { readFile, readdir } from "node:fs/promises";
22
import { createRequire } from "node:module";
33
import { dirname, relative, resolve, sep } from "node:path";
44
import { z } from "zod";
5-
import { assertWithin, pathKey } from "../util.js";
5+
import { assertRealpathWithin, assertWithin, pathKey } from "../util.js";
66
import { localize as localized } from "./localization.js";
77
import type {
88
LocalizedText,
@@ -263,6 +263,7 @@ export async function scanSite(
263263
for (const item of meta.demos) {
264264
const sourcePath = resolve(demoDirectory, item.filename);
265265
assertWithin(demoDirectory, sourcePath);
266+
await assertRealpathWithin(demoDirectory, sourcePath);
266267
const source = await readFile(sourcePath, "utf8");
267268
const slug = item.filename
268269
.replace(demoExtensionPattern, "")

0 commit comments

Comments
 (0)