Skip to content

Commit

Permalink
small edits
Browse files Browse the repository at this point in the history
  • Loading branch information
asanehisa committed Nov 1, 2023
1 parent a48b89e commit f9e6aa1
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 67 deletions.
26 changes: 14 additions & 12 deletions packages/pages/src/common/src/template/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ export const makeClientFiles = async (projectStructure: ProjectStructure) => {
f !== "_client17.tsx" &&
f !== "_client.tsx" &&
f !== "_server.tsx" &&
!f.includes(".client.")
!f.includes("client.tsx")
)
.forEach(async (template) => {
const relativePath = path.join(templatePath.path, template);
fs.writeFileSync(formatClientPath(relativePath), "");
await generateAndSaveClientHydrationTemplates(relativePath);
const templeFilePath = path.join(templatePath.path, template);
await generateAndSaveClientHydrationTemplates(templeFilePath);
});
}
} catch (err) {
Expand All @@ -44,22 +43,25 @@ export const makeClientFiles = async (projectStructure: ProjectStructure) => {
* @param path src/templates/<templateName>.tsx
*/
const generateAndSaveClientHydrationTemplates = async (path: string) => {
const newPath = getClientPath(path);
fs.writeFileSync(newPath, "");
const sfp = new SourceFileParser(path, createTsMorphProject());
const newSfp = new SourceFileParser(
formatClientPath(path),
createTsMorphProject()
);
const newSfp = new SourceFileParser(newPath, createTsMorphProject());
const templateParser = new TemplateParser(sfp).makeClientTemplateFromSfp(
newSfp
);
templateParser.sourceFile.save();
};

const formatClientPath = (clientPath: string): string => {
const templatePath = path.parse(clientPath);
/**
* @param templatePath /src/templates/location.tsx
* @returns clientPath /src/templates/location.client.tsx
*/
const getClientPath = (templatePath: string): string => {
const parsedPath = path.parse(templatePath);
return path.join(
templatePath.dir,
templatePath.name + ".client" + templatePath.ext
parsedPath.dir,
parsedPath.name + ".client" + parsedPath.ext
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const getTemplateFilepathsFromProjectStructure = (
const globalClientRenderFilename17 = "_client17.tsx";
const globalClientRenderFilename = "_client.tsx";
const globalServerRenderFilename = "_server.tsx";
const globalHydrationClientFilename = ".client.";
const globalHydrationClientFilename = "client.tsx";

/**
* Determines the client and server rendering templates to use. It first looks for a _client/server.tsx file in the scoped
Expand Down
2 changes: 1 addition & 1 deletion packages/pages/src/vite-plugin/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const discoverInputs = async (
)
.forEach((template) => {
const parsedPath = parse(template);
const bundlePath = template.includes(".client.")
const bundlePath = template.includes("client.tsx")
? projectStructure.config.subfolders.clientBundle
: projectStructure.config.subfolders.serverBundle;
const outputPath = `${bundlePath}/${parsedPath.name.replace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const baseTemplateModule: TemplateModuleInternal<any, any> = {
templateName: "template",
config: {
name: "name",
hydrate: true,
hydrate: false,
},
getPath: () => {
return "path";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,9 @@ export const reactWrapper = async <T extends TemplateRenderProps>(

let clientHydrationString;
if (hydrate) {
const templatePath =
Object.keys(manifest.clientPaths).length === 0
? path.join(
projectStructure.config.subfolders.assets,
templateModuleInternal.path.replace("..", "")
)
: manifest.clientPaths[templateModuleInternal.templateName];
clientHydrationString = getHydrationTemplate(
pluginRenderTemplates.client,
templatePath,
manifest.clientPaths[templateModuleInternal.templateName],
props
);
}
Expand Down
68 changes: 24 additions & 44 deletions packages/pages/src/vite-plugin/build/closeBundle/manifest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from "fs-extra";
import path from "path";
import path from "node:path";
import { ProjectStructure } from "../../../common/src/project/structure.js";
import { convertToPosixPath } from "../../../common/src/template/paths.js";
import { Manifest } from "../../../common/src/template/types.js";
Expand Down Expand Up @@ -27,22 +27,24 @@ export const generateManifestFile = async (
([name, path]) => [name, convertToPosixPath(distPath.getRelativePath(path))]
);

// Add the renderPaths to the manifest. This defines the _client and _server entries.
const renderPaths = glob.sync(
convertToPosixPath(
path.resolve(
projectStructure.config.rootFolders.dist,
projectStructure.config.subfolders.assets,
projectStructure.config.subfolders.renderBundle,
"**/*.js"
//Scans for paths in dist/assets/<assetPath> and finds the paths and file names.
async function getAssetBundlePaths(assetPath: string): Promise<string[][]> {
const distPath = new Path(projectStructure.config.rootFolders.dist);
const filePaths = glob.sync(
convertToPosixPath(
path.resolve(
projectStructure.config.rootFolders.dist,
projectStructure.config.subfolders.assets,
assetPath,
"**/*.js"
)
)
)
);

const relativeRenderPaths = renderPaths.map((filepath) => [
path.parse(filepath).name.split(".")[0], // get the name of the file without the hash or extension
convertToPosixPath(distPath.getRelativePath(filepath)),
]);
);
return filePaths.map((filepath) => [
path.parse(filepath).name.split(".")[0],
convertToPosixPath(distPath.getRelativePath(filepath)),
]);
}

let bundlerManifest = Buffer.from("{}");
if (fs.existsSync(path.join(distPath.path, "manifest.json"))) {
Expand All @@ -52,8 +54,12 @@ export const generateManifestFile = async (
}
const manifest: Manifest = {
serverPaths: Object.fromEntries(relativeBundlePaths),
clientPaths: Object.fromEntries(await getClientPaths(projectStructure)),
renderPaths: Object.fromEntries(relativeRenderPaths),
clientPaths: Object.fromEntries(
await getAssetBundlePaths(projectStructure.config.subfolders.clientBundle)
),
renderPaths: Object.fromEntries(
await getAssetBundlePaths(projectStructure.config.subfolders.renderBundle)
),
projectStructure: projectStructure.config,
bundlerManifest: JSON.parse(bundlerManifest.toString()),
};
Expand All @@ -66,32 +72,6 @@ export const generateManifestFile = async (
fs.remove(path.resolve(distPath.path, "manifest.json"));
};

/**
* Scans for paths in dist/assets/client/ and finds the client template's
* path and feature name.
* @param projectStructure
* @returns Promise<string[][]> with the feature name and path
*/
const getClientPaths = async (
projectStructure: ProjectStructure
): Promise<string[][]> => {
const distPath = new Path(projectStructure.config.rootFolders.dist);
const filePaths = glob.sync(
convertToPosixPath(
path.resolve(
projectStructure.config.rootFolders.dist,
projectStructure.config.subfolders.assets,
projectStructure.config.subfolders.clientBundle,
"**/*.js"
)
)
);
return filePaths.map((filepath) => [
path.parse(filepath).name.split(".")[0],
convertToPosixPath(distPath.getRelativePath(filepath)),
]);
};

// writeFile ensures that the directory of the filepath exists before writing the file.
const writeFile = (filepath: string, contents: string) => {
fs.mkdirSync(path.dirname(filepath), { recursive: true });
Expand Down

0 comments on commit f9e6aa1

Please sign in to comment.