Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ssg generate htmls should apply regions #6303

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading