Skip to content

Commit

Permalink
fix: ssg generate htmls should apply regions
Browse files Browse the repository at this point in the history
  • Loading branch information
GiveMe-A-Name committed Sep 29, 2024
1 parent c92b8f3 commit 1d6b2c9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/smart-boats-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/plugin-ssg': patch
---

fix: ssg generate htmls should apply regions
fix: ssg 生成 html 应该应用于 deploy.regions
26 changes: 24 additions & 2 deletions packages/cli/plugin-ssg/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppTools> => ({
name: '@modern-js/plugin-ssg',

Expand All @@ -40,14 +44,30 @@ export const ssgPlugin = (): CliPlugin<AppTools> => ({
const appContext = api.useAppContext();

const { appDirectory, entrypoints } = appContext;
const { output, server } = resolvedConfig;
const { output, server, deploy } = resolvedConfig;
const {
ssg,
distPath: { root: outputPath } = {},
} = output;

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);
Expand Down Expand Up @@ -187,7 +207,9 @@ export const ssgPlugin = (): CliPlugin<AppTools> => ({
);

// 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);
Expand Down

0 comments on commit 1d6b2c9

Please sign in to comment.