diff --git a/.changeset/smart-boats-fetch.md b/.changeset/smart-boats-fetch.md new file mode 100644 index 000000000000..2e1856593de7 --- /dev/null +++ b/.changeset/smart-boats-fetch.md @@ -0,0 +1,6 @@ +--- +'@modern-js/plugin-ssg': patch +--- + +fix: ssg generate htmls should apply regions +fix: ssg 生成 html 应该应用于 deploy.regions diff --git a/packages/cli/plugin-ssg/src/index.ts b/packages/cli/plugin-ssg/src/index.ts index d4d37017f0c4..9c959fd5d9c0 100644 --- a/packages/cli/plugin-ssg/src/index.ts +++ b/packages/cli/plugin-ssg/src/index.ts @@ -17,6 +17,10 @@ import { import { createServer } from './server'; import type { AgreedRouteMap, SSGConfig, SsgRoute } from './types'; +const getRegionDist = (dist: string, region: string) => { + return path.resolve(dist, region); +}; + export const ssgPlugin = (): CliPlugin => ({ name: '@modern-js/plugin-ssg', @@ -40,7 +44,7 @@ export const ssgPlugin = (): CliPlugin => ({ const appContext = api.useAppContext(); const { appDirectory, entrypoints } = appContext; - const { output, server } = resolvedConfig; + const { output, server, deploy } = resolvedConfig; const { ssg, distPath: { root: outputPath } = {}, @@ -48,6 +52,22 @@ export const ssgPlugin = (): CliPlugin => ({ const ssgOptions: SSGConfig = (Array.isArray(ssg) ? ssg.pop() : ssg) || true; + const regions = (deploy as { regions?: string[] })?.regions; + + const computedOutputDir = (region: string) => { + const { distDirectory } = api.useAppContext(); + return getRegionDist(distDirectory, region); + }; + + const outputDirs: string[] = []; + + if (regions) { + regions.forEach(region => { + outputDirs.push(computedOutputDir(region)); + }); + } else { + outputDirs.push(path.join(appDirectory, outputPath as string)); + } const buildDir = path.join(appDirectory, outputPath as string); const routes = readJSONSpec(buildDir); @@ -187,7 +207,9 @@ export const ssgPlugin = (): CliPlugin => ({ ); // write to dist file - writeHtmlFile(htmlAry, ssgRoutes, buildDir); + for (const outputDir of outputDirs) { + writeHtmlFile(htmlAry, ssgRoutes, outputDir); + } // format route info, side effect replaceRoute(ssgRoutes, pageRoutes);