Skip to content

Commit

Permalink
Merge branch 'main' into scaffold
Browse files Browse the repository at this point in the history
  • Loading branch information
asanehisa authored Jul 30, 2024
2 parents 46bf905 + b239cbb commit 2b1834f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 44 deletions.
11 changes: 10 additions & 1 deletion packages/pages/src/common/src/template/hydration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HeadConfig, renderHeadConfigToString } from "./head.js";
import { convertToPosixPath } from "./paths.js";
import { TemplateRenderProps } from "./types.js";
import { FeaturesConfig } from "../feature/features.js";

/**
* Imports the custom hydration template and entrypoint template as modules and calls
Expand Down Expand Up @@ -134,21 +135,29 @@ const getCommonInjectedIndexHtml = (
* @param clientHydrationString
* @param indexHtml
* @param appLanguage
* @param templatesConfig
* @param headConfig
* @returns the server template to render in the Vite dev environment
*/
export const getIndexTemplateDev = (
clientHydrationString: string | undefined,
indexHtml: string,
appLanguage: string,
templatesConfig: FeaturesConfig,
headConfig?: HeadConfig
): string => {
return getCommonInjectedIndexHtml(
let commonIndex = getCommonInjectedIndexHtml(
clientHydrationString,
indexHtml,
appLanguage,
headConfig
);
commonIndex = injectIntoEndOfHead(
commonIndex,
`<script>const pageSets = ${JSON.stringify(templatesConfig, null)}</script>`
);

return commonIndex;
};

/**
Expand Down
13 changes: 13 additions & 0 deletions packages/pages/src/dev/server/middleware/sendAppHTML.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import {
getHydrationTemplateDev,
getIndexTemplateDev,
} from "../../../common/src/template/hydration.js";
import {
getTemplateModules,
getTemplatesConfig,
} from "../../../generate/templates/createTemplatesJson.js";
import { FeaturesConfig } from "../../../common/src/feature/features.js";

/**
* Renders the HTML for a given {@link TemplateModuleInternal}
Expand Down Expand Up @@ -53,6 +58,13 @@ export default async function sendAppHTML(
clientServerRenderTemplates.serverRenderTemplatePath
)) as ServerRenderTemplate;

const { templateModules, redirectModules } =
await getTemplateModules(projectStructure);
const templatesConfig: FeaturesConfig = getTemplatesConfig(
templateModules,
redirectModules
);

const clientInjectedIndexHtml = getIndexTemplateDev(
clientHydrationString,
serverRenderTemplateModule.getIndexHtml
Expand All @@ -62,6 +74,7 @@ export default async function sendAppHTML(
})
: serverRenderTemplateModule.indexHtml,
getLang(headConfig, props),
templatesConfig,
headConfig
);

Expand Down
15 changes: 1 addition & 14 deletions packages/pages/src/generate/features/features.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
import { ProjectStructure } from "../../common/src/project/structure.js";
import { getTemplateFilepaths } from "../../common/src/template/internal/getTemplateFilepaths.js";
import { Command } from "commander";
import { createTemplatesJson } from "../templates/createTemplatesJson.js";
import { logErrorAndExit } from "../../util/logError.js";
import { getRedirectFilePaths } from "../../common/src/redirect/internal/getRedirectFilepaths.js";

const handler = async ({ scope }: { scope: string }): Promise<void> => {
const projectStructure = await ProjectStructure.init({ scope });
const templateFilepaths = getTemplateFilepaths(
projectStructure.getTemplatePaths()
);
const redirectFilepaths = getRedirectFilePaths(
projectStructure.getRedirectPaths()
);

try {
await createTemplatesJson(
templateFilepaths,
redirectFilepaths,
projectStructure,
"FEATURES"
);
await createTemplatesJson(projectStructure, "FEATURES");
} catch (error) {
logErrorAndExit(error);
}
Expand Down
48 changes: 33 additions & 15 deletions packages/pages/src/generate/templates/createTemplatesJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,19 @@ import {
loadRedirectModules,
RedirectModuleCollection,
} from "../../common/src/redirect/loader/loader.js";
import { getTemplateFilepaths } from "../../common/src/template/internal/getTemplateFilepaths.js";
import { getRedirectFilePaths } from "../../common/src/redirect/internal/getRedirectFilepaths.js";

/**
* Loads the templates as modules and generates a templates.json or
* features.json from the templates.
*/
export const createTemplatesJson = async (
templateFilepaths: string[],
redirectFilepaths: string[],
projectStructure: ProjectStructure,
type: "FEATURES" | "TEMPLATES"
): Promise<void> => {
const templateModules = await loadTemplateModules(
templateFilepaths,
true,
false,
projectStructure
);

const redirectModules = await loadRedirectModules(
redirectFilepaths,
true,
false,
projectStructure
);
const { templateModules, redirectModules } =
await getTemplateModules(projectStructure);

return createTemplatesJsonFromModule(
templateModules,
Expand Down Expand Up @@ -114,6 +103,35 @@ export const createTemplatesJsonFromModule = async (
);
};

/**
* Helper to get the template modules from the project structure
* @param projectStructure
*/
export const getTemplateModules = async (
projectStructure: ProjectStructure
) => {
const templateFilepaths = getTemplateFilepaths(
projectStructure.getTemplatePaths()
);
const redirectFilepaths = getRedirectFilePaths(
projectStructure.getRedirectPaths()
);
const templateModules = await loadTemplateModules(
templateFilepaths,
true,
false,
projectStructure
);

const redirectModules = await loadRedirectModules(
redirectFilepaths,
true,
false,
projectStructure
);
return { templateModules, redirectModules };
};

export const getTemplatesConfig = (
templateModules: TemplateModuleCollection,
redirectModules?: RedirectModuleCollection
Expand Down
15 changes: 1 addition & 14 deletions packages/pages/src/generate/templates/templates.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
import { ProjectStructure } from "../../common/src/project/structure.js";
import { getTemplateFilepaths } from "../../common/src/template/internal/getTemplateFilepaths.js";
import { createTemplatesJson } from "./createTemplatesJson.js";
import { Command } from "commander";
import { getRedirectFilePaths } from "../../common/src/redirect/internal/getRedirectFilepaths.js";

export const templatesHandler = async ({
scope,
}: {
scope: string;
}): Promise<void> => {
const projectStructure = await ProjectStructure.init({ scope });
const templateFilepaths = getTemplateFilepaths(
projectStructure.getTemplatePaths()
);
const redirectFilepaths = getRedirectFilePaths(
projectStructure.getRedirectPaths()
);

await createTemplatesJson(
templateFilepaths,
redirectFilepaths,
projectStructure,
"TEMPLATES"
);
await createTemplatesJson(projectStructure, "TEMPLATES");
};

export const templatesCommand = (program: Command) => {
Expand Down

0 comments on commit 2b1834f

Please sign in to comment.