Skip to content

Commit 0a5c95f

Browse files
committed
refactor: normalize path and use better names
1 parent 8aad4c9 commit 0a5c95f

File tree

5 files changed

+78
-50
lines changed

5 files changed

+78
-50
lines changed
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
export const GET = () => {
2+
return true;
3+
};
14

5+
export const POST = () => {
6+
return true;
7+
};
28

3-
// export const GET = create
9+
export const PUT = () => {
10+
return true;
11+
};
12+
13+
export const DELETE = () => {
14+
return true;
15+
};

packages/pranx/src/build/bundle/browser.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import { OUTPUT_BUNDLE_BROWSER_DIR, SOURCE_DIR, SOURCE_PAGES_DIR } from "../cons
99

1010
export async function bundle_browser(options: { optimize: boolean; user_config: PranxConfig }) {
1111
const browser_entries = await glob(
12-
[join(SOURCE_PAGES_DIR, "**/*page.{js,ts,tsx,jsx}"), join(SOURCE_DIR, "entry-client.tsx")],
12+
[
13+
join(SOURCE_PAGES_DIR, "**/*page.{js,ts,tsx,jsx}"),
14+
join(SOURCE_DIR, "entry-client.{tsx,jsx}"),
15+
],
1316
{
1417
nodir: true,
1518
absolute: true,

packages/pranx/src/build/bundle/server.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import { OUTPUT_BUNDLE_SERVER_DIR, SOURCE_DIR, SOURCE_PAGES_DIR } from "../const
88

99
export async function bundle_server(options: { optimize: boolean; user_config: PranxConfig }) {
1010
const server_entries = await glob(
11-
[join(SOURCE_PAGES_DIR, "**/*page.{js,ts,tsx,jsx}"), join(SOURCE_DIR, "entry-server.tsx")],
11+
[
12+
join(SOURCE_PAGES_DIR, "**/*{page,route}.{js,ts,tsx,jsx}"),
13+
join(SOURCE_DIR, "entry-server.{tsx,jsx}"),
14+
],
1215
{
1316
nodir: true,
1417
absolute: true,
@@ -59,7 +62,7 @@ export async function bundle_server(options: { optimize: boolean; user_config: P
5962
".jsx": "jsx",
6063
".ts": "tsx",
6164
".tsx": "tsx",
62-
".module.css": "local-css",
65+
".module.css": "empty",
6366
".css": "empty",
6467
".json": "json",
6568
},

packages/pranx/src/cmd/build.ts

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export async function build() {
4343
user_config: await get_user_pranx_config(),
4444
});
4545

46-
const browser_bundle_metafile = await bundle_browser({
46+
const browser_bundle_result = await bundle_browser({
4747
optimize: true,
4848
user_config: await get_user_pranx_config(),
4949
});
@@ -58,81 +58,90 @@ export async function build() {
5858

5959
server_entry_module = (await import(server_site_manifest.entry_server)) as ServerEntryModule;
6060

61-
const pranx_bundle_replace_path = join(".pranx", "browser");
61+
const pranx_browser_base_path = join(".pranx", "browser");
6262

63-
const css_output: {
63+
type CSS_OUTPUT = {
6464
entry: string;
6565
[key: string]: string;
66-
} = {
66+
};
67+
68+
const css_output: CSS_OUTPUT = {
6769
entry: "",
6870
};
6971

7072
// Calculating css files
71-
for (const [file, _output] of Object.entries(browser_bundle_metafile.metafile.outputs)) {
73+
for (const [file, _output] of Object.entries(browser_bundle_result.metafile.outputs)) {
74+
if (!file.endsWith(".css")) continue;
75+
7276
if (file.endsWith("entry-client.css")) {
73-
css_output.entry = file.replace(pranx_bundle_replace_path, "");
77+
css_output.entry = file.replace(pranx_browser_base_path, "");
7478
continue;
7579
}
7680

77-
if (file.endsWith(".css")) {
78-
const pages_relative_path = file.replace(pranx_bundle_replace_path, "");
81+
const css_file_relative = file.replace(pranx_browser_base_path, "");
7982

80-
const path_splitted = pages_relative_path.replace("page.css", "").split("/");
81-
path_splitted.pop();
82-
const final_path = path_splitted.join("/") || "/";
83+
const path_normalized = `/${css_file_relative
84+
.replace("page.css", "")
85+
.split("/")
86+
.filter(Boolean)
87+
.join("/")}`;
8388

84-
css_output[final_path] = pages_relative_path;
85-
}
89+
css_output[path_normalized] = css_file_relative;
8690
}
8791

88-
// Generating Manifest and generating static pages
89-
for (const [file, _output] of Object.entries(browser_bundle_metafile.metafile.outputs)) {
92+
// Generating Manifest and generating static pages data
93+
for (const [file, _output] of Object.entries(browser_bundle_result.metafile.outputs)) {
9094
if (!file.endsWith("page.js")) continue;
9195

92-
const pages_relative_path = file.replace(pranx_bundle_replace_path, "");
96+
const pages_relative_path = file.replace(pranx_browser_base_path, "");
9397

94-
const path_splitted = pages_relative_path.replace("page.js", "").split("/");
95-
path_splitted.pop();
96-
const final_path = path_splitted.join("/") || "/";
98+
const final_path_normalized = `/${pages_relative_path
99+
.replace("page.js", "")
100+
.split("/")
101+
.filter(Boolean)
102+
.join("/")}`;
97103

98104
const module_path = join(OUTPUT_BUNDLE_SERVER_DIR, "pages", pages_relative_path);
105+
99106
const {
100-
// default: PageComponent,
101-
getServerSideProps,
102-
getStaticProps,
103-
getStaticPaths,
104-
// meta: metadata,
107+
getServerSideProps = undefined,
108+
getStaticProps = undefined,
109+
getStaticPaths = undefined,
105110
} = (await import(module_path)) as PageModule;
106111

107112
if (getServerSideProps && (getStaticProps || getStaticPaths)) {
108113
logger.error(`
109-
msg: "Only one can be present: getServerSideProps or getStaticProps/getStaticPaths"
110-
file: ${module_path}
111-
path: ${final_path}`);
114+
msg: "Only one can be present: getServerSideProps or getStaticProps/getStaticPaths"
115+
file: ${module_path}
116+
path: ${final_path_normalized}
117+
`);
112118
process.exit(1);
113119
}
114120

115121
const isStatic = !getServerSideProps;
116-
const isUrlDynamic = final_path.includes("[");
122+
const isUrlDynamic = final_path_normalized.includes("[");
117123
const dynamic_params = !isUrlDynamic
118124
? []
119-
: path_splitted
125+
: final_path_normalized
126+
.split("/")
120127
.filter((i) => i.startsWith("[") && i.endsWith("]"))
121128
.map((i) => i.replace("[", "").replace("]", ""));
122129

123130
if (isStatic && isUrlDynamic && !getStaticPaths) {
124131
logger.error(`
125-
msg: "getStaticPaths must be present on static pages with dynamic params"
126-
file: ${module_path}
127-
path: ${final_path}`);
132+
msg: "getStaticPaths must be present on static pages with dynamic params"
133+
file: ${module_path}
134+
path: ${final_path_normalized}
135+
`);
128136
process.exit(1);
129137
}
130138

131139
if (!isStatic && isUrlDynamic && !getServerSideProps) {
132140
logger.error(`
133-
msg: "getServerSideProps must be present on server pages with dynamic params"
134-
file: ${module_path}
135-
path: ${final_path}`);
141+
msg: "getServerSideProps must be present on server pages with dynamic params"
142+
file: ${module_path}
143+
path: ${final_path_normalized}
144+
`);
136145
process.exit(1);
137146
}
138147

@@ -143,17 +152,17 @@ path: ${final_path}`);
143152

144153
if (isStatic && isUrlDynamic && getStaticPaths) {
145154
const static_paths_result = await getStaticPaths();
146-
const new_final_path = final_path;
155+
const new_final_path = final_path_normalized;
147156

148157
server_site_manifest.routes.push({
149-
path: final_path,
158+
path: final_path_normalized,
150159
module: pages_relative_path,
151160
props: statics_fn_result.props,
152161
rendering_kind: "static",
153162
revalidate: statics_fn_result.revalidate || -1,
154163
is_dynamic: isUrlDynamic,
155164
dynamic_params: dynamic_params,
156-
css: [css_output.entry, css_output[final_path] || ""].filter(Boolean),
165+
css: [css_output.entry, css_output[final_path_normalized] || ""].filter(Boolean),
157166
static_generated_routes: [],
158167
absolute_module_path: module_path,
159168
});
@@ -168,10 +177,11 @@ path: ${final_path}`);
168177

169178
if (replaced_path.includes("[")) {
170179
logger.error(`
171-
msg: "getStaticPaths did not return all the necessary params"
172-
file: ${module_path}
173-
path: ${final_path}
174-
params returned by getStaticPaths: ${JSON.stringify(static_path.params)}`);
180+
msg: "getStaticPaths did not return all the necessary params"
181+
file: ${module_path}
182+
path: ${final_path_normalized}
183+
params returned by getStaticPaths: ${JSON.stringify(static_path.params)}
184+
`);
175185
process.exit(1);
176186
}
177187

@@ -202,15 +212,15 @@ params returned by getStaticPaths: ${JSON.stringify(static_path.params)}`);
202212
}
203213

204214
server_site_manifest.routes.push({
205-
path: final_path,
215+
path: final_path_normalized,
206216
module: pages_relative_path,
207217
props: statics_fn_result.props,
208218
rendering_kind: isStatic ? "static" : "server-side",
209219
revalidate: statics_fn_result.revalidate || -1,
210220
static_generated_routes: [],
211221
is_dynamic: isUrlDynamic,
212222
dynamic_params: dynamic_params,
213-
css: [css_output.entry, css_output[final_path] || ""].filter(Boolean) as string[],
223+
css: [css_output.entry, css_output[final_path_normalized] || ""].filter(Boolean) as string[],
214224
absolute_module_path: module_path,
215225
});
216226
}
@@ -224,7 +234,7 @@ params returned by getStaticPaths: ${JSON.stringify(static_path.params)}`);
224234
rendering_kind: r.rendering_kind,
225235
css: r.css,
226236
is_dynamic: r.is_dynamic,
227-
path_parsed_for_routing: filePathToRoutingPath(r.path),
237+
path_parsed_for_routing: filePathToRoutingPath(r.path, false),
228238
static_generated_routes: r.static_generated_routes.map((r) => {
229239
return {
230240
path: r.path,

packages/pranx/types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface GetStaticPathsResult<Params extends Record<string, any>> {
1616
fallback: boolean | "blocking";
1717
}
1818

19-
export type GetStaticPathsFunction<Params extends Record<string, any>> = () => Promise<
19+
export type GetStaticPathsFunction<Params extends Record<string, any> = {}> = () => Promise<
2020
GetStaticPathsResult<Params>
2121
>;
2222

0 commit comments

Comments
 (0)