Skip to content

Commit

Permalink
Use worse wrapper for __filename and __dirname
Browse files Browse the repository at this point in the history
import.meta.url is not supported in prominent build tools yet, so we use a different approach:
vitejs/vite#8427
evanw/esbuild#1492
  • Loading branch information
atjn committed Sep 10, 2023
1 parent 71d5ee9 commit f5b9ca1
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 21 deletions.
4 changes: 1 addition & 3 deletions lib/crashed.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ export default async function (__usable_environment) {
memoryUsage: () => {},
};
const __usable_dirname =
__usable_environment.environmentHint !== "server"
? undefined
: decodeURI(new URL(".", import.meta.url).pathname);
__usable_environment.environmentHint !== "server" ? undefined : "lib";

/* BEGIN WRAPPED GUN CODE */

Expand Down
4 changes: 1 addition & 3 deletions lib/fsrm.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ export default function (__usable_environment) {
if (__usable_isActivated) return __usable_module.exports;
__usable_isActivated = true;
const __usable_dirname =
__usable_environment.environmentHint !== "server"
? undefined
: decodeURI(new URL(".", import.meta.url).pathname);
__usable_environment.environmentHint !== "server" ? undefined : "lib";

/* BEGIN WRAPPED GUN CODE */

Expand Down
4 changes: 1 addition & 3 deletions lib/reboot.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ export default function (__usable_environment) {
},
);
const __usable_dirname =
__usable_environment.environmentHint !== "server"
? undefined
: decodeURI(new URL(".", import.meta.url).pathname);
__usable_environment.environmentHint !== "server" ? undefined : "lib";

/* BEGIN WRAPPED GUN CODE */

Expand Down
4 changes: 1 addition & 3 deletions lib/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ export default function (__usable_environment) {
},
);
const __usable_dirname =
__usable_environment.environmentHint !== "server"
? undefined
: decodeURI(new URL(".", import.meta.url).pathname);
__usable_environment.environmentHint !== "server" ? undefined : "lib";

/* BEGIN WRAPPED GUN CODE */

Expand Down
4 changes: 1 addition & 3 deletions lib/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ export default function (__usable_environment) {
);
};
const __usable_dirname =
__usable_environment.environmentHint !== "server"
? undefined
: decodeURI(new URL(".", import.meta.url).pathname);
__usable_environment.environmentHint !== "server" ? undefined : "lib";

/* BEGIN WRAPPED GUN CODE */

Expand Down
4 changes: 1 addition & 3 deletions lib/wsp.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ export default function (__usable_environment) {
},
);
const __usable_dirname =
__usable_environment.environmentHint !== "server"
? undefined
: decodeURI(new URL(".", import.meta.url).pathname);
__usable_environment.environmentHint !== "server" ? undefined : "lib";

/* BEGIN WRAPPED GUN CODE */

Expand Down
10 changes: 9 additions & 1 deletion rebuild/wrapCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ function newReport(){
return report;
}

function removeUrlStuff(path){
if(path.startsWith("./")) path = path.slice(2);
if(path.startsWith(".")) path = path.slice(1);
return path;
}

console.log("Run string replacers, uprade requires, wrap globals, expand names");
await Promise.all(paths.map(async path => {
const link = new Link(path);
Expand All @@ -166,7 +172,9 @@ await Promise.all(paths.map(async path => {
code = wrapperCode.replace("/* usable_insert_code */", code);
const exportMemberPaths = link.getRelativePath(rebuiltDir).replaceAll("-", "_").split(".")[0].split("/").map(p => p === "src" ? "gun" : p);
if (exportMemberPaths.length === 1) exportMemberPaths.unshift("default");
code = code.replaceAll("/* usable_insert_export_members */", `.${exportMemberPaths.join(".")}`);
code = code.replace("/* usable_insert_filename */", removeUrlStuff(`.${link.getRelativePath(rebuiltDir)}`));
code = code.replace("/* usable_insert_dirname */", removeUrlStuff(`.${pathJoin(link.getRelativePath(rebuiltDir), "..")}`));
code = code.replace("/* usable_insert_export_members */", `.${exportMemberPaths.join(".")}`);

let output;

Expand Down
4 changes: 2 additions & 2 deletions rebuild/wrapper/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ export default function (__usable_environment, __usable_MODULE){
const __usable_require = __usable_environment.environmentHint !== "server" ? undefined : () => {
throw new Error("It should not be possible to call this function, please open an issue in `usable-gun` to have this fixed.");
}
const __usable_filename = __usable_environment.environmentHint !== "server" ? undefined : decodeURI(new URL("", import.meta.url).pathname);
const __usable_dirname = __usable_environment.environmentHint !== "server" ? undefined : decodeURI(new URL(".", import.meta.url).pathname);
const __usable_filename = __usable_environment.environmentHint !== "server" ? undefined : "/* usable_insert_filename */";
const __usable_dirname = __usable_environment.environmentHint !== "server" ? undefined : "/* usable_insert_dirname */";

/* BEGIN WRAPPED GUN CODE */

Expand Down

0 comments on commit f5b9ca1

Please sign in to comment.