Skip to content

Commit

Permalink
Add support for URL object serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Fryuni committed Feb 7, 2024
1 parent 0692002 commit d430acc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/eleven-shrimps-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@inox-tools/inline-mod": minor
---

Add support for serializing URL objects
3 changes: 3 additions & 0 deletions packages/inline-mod/src/closure/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ type EntryMap = {
// A simple expression to use to represent this instance. For example "global.Number";
expr: string;

// A simple expression to use to construct an instance. The reference of the instance is relevant.
refExpr: string;

// A placeholder for a pending entry
pending: never;
};
Expand Down
7 changes: 7 additions & 0 deletions packages/inline-mod/src/closure/inspectCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,13 @@ class Inspector {
};
}

if (value instanceof URL) {
return {
type: 'refExpr',
value: `new URL(${JSON.stringify(value.toString())})`,
};
}

if (Array.isArray(value)) {
log('Inspecting array value');
const array: Entry[] = [];
Expand Down
5 changes: 5 additions & 0 deletions packages/inline-mod/src/closure/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ class ModuleSerializer {
return this.emitSymbol(envEntry, varName);
case 'expr':
return envEntry.value;
case 'refExpr': {
const name = this.createEnvVarName(varName, false);
this.emitCode(envEntry, `const ${name} = ${envEntry.value};\n`);
return name;
}
case 'pending':
return '';
}
Expand Down

0 comments on commit d430acc

Please sign in to comment.