diff --git a/.changeset/eleven-shrimps-rest.md b/.changeset/eleven-shrimps-rest.md new file mode 100644 index 00000000..50ffaf96 --- /dev/null +++ b/.changeset/eleven-shrimps-rest.md @@ -0,0 +1,5 @@ +--- +"@inox-tools/inline-mod": minor +--- + +Add support for serializing URL objects diff --git a/packages/inline-mod/src/closure/entry.ts b/packages/inline-mod/src/closure/entry.ts index 189d38a1..9f85644c 100644 --- a/packages/inline-mod/src/closure/entry.ts +++ b/packages/inline-mod/src/closure/entry.ts @@ -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; }; diff --git a/packages/inline-mod/src/closure/inspectCode.ts b/packages/inline-mod/src/closure/inspectCode.ts index d9bc9eac..bcc02f7c 100644 --- a/packages/inline-mod/src/closure/inspectCode.ts +++ b/packages/inline-mod/src/closure/inspectCode.ts @@ -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[] = []; diff --git a/packages/inline-mod/src/closure/serialization.ts b/packages/inline-mod/src/closure/serialization.ts index 682b6e88..0c1c1c75 100644 --- a/packages/inline-mod/src/closure/serialization.ts +++ b/packages/inline-mod/src/closure/serialization.ts @@ -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 ''; }