Skip to content

Commit

Permalink
fix build output (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
baked-dev authored Dec 7, 2022
1 parent 6ce1e20 commit de10814
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "whop-sdk-ts",
"version": "0.0.2",
"version": "0.0.3-canary.0",
"private": true,
"workspaces": [
"apps/*",
Expand Down
34 changes: 30 additions & 4 deletions packages/repo-utils/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,27 @@ class Buildr {
]);
};

private fixESM = (code: string) => {
return code.replace(
/(?:(?:^|\s)import\("(\.\.?\/.*)"\)|(?:import|export) (?:[a-zA-Z0-9]|\n|{|}|\s|,)* from "(\.\.?\/.*)";)/gm,
(match, dynamicImport, staticImport) => {
return match.replace(
`"${staticImport || dynamicImport}"`,
`"${staticImport || dynamicImport}.mjs"`
);
}
);
};

private fixCJS = (code: string) => {
return code.replace(
/(?:^|\s)require\("(\.\.?\/.*)"\)/gm,
(match, required) => {
return match.replace(`"${required}"`, `"${required}.cjs"`);
}
);
};

private buildESM = async (file: string, outBase: string) => {
await this.writeOutput(
await transformFile(file, {
Expand All @@ -97,7 +118,8 @@ class Buildr {
},
}),
outBase,
".mjs"
".mjs",
this.fixESM
);
};

Expand All @@ -110,17 +132,21 @@ class Buildr {
},
}),
outBase,
".cjs"
".cjs",
this.fixCJS
);
};

private writeOutput = async (
{ code, map }: Output,
outBase: string,
extension: string
extension: string,
fix: (code: string) => string = (code) => code
) => {
if (this.destroyed) return;
const promises: Promise<any>[] = [outputFile(outBase + extension, code)];
const promises: Promise<any>[] = [
outputFile(outBase + extension, fix(code)),
];
if (map) promises.push(outputFile(outBase + extension + ".map", map));
await Promise.all(promises);
};
Expand Down

0 comments on commit de10814

Please sign in to comment.