Skip to content

Commit

Permalink
Fix walkObject to work with module namespace objects
Browse files Browse the repository at this point in the history
  • Loading branch information
askoufis committed Mar 21, 2024
1 parent ccb4957 commit c56cfe5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 19 additions & 0 deletions .changeset/chilly-hairs-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
'@vanilla-extract/private': patch
---

**walkObject**: Use an empty object instead of calling the input object's `constructor` when mapping over it

This allows `walkObject` to be used on module namespace objects:

```ts
import * as ns from './foo';

// Would sometimes cause a runtime error
walkObject(ns, myMappingFunction);
```

The previous implementation did not work with these objects because [they do not have a `constructor` function][es6 spec].
`esbuild` seems to have papered over this issue by providing a `constructor` function on these objects, but this seems to not be the case with `vite-node`, hence the need for this fix.

[es6 spec]: https://262.ecma-international.org/6.0/#sec-module-namespace-objects
2 changes: 1 addition & 1 deletion packages/private/src/walkObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function walkObject<T extends Walkable, MapTo>(
fn: (value: Primitive, path: Array<string>) => MapTo,
path: Array<string> = [],
): MapLeafNodes<T, MapTo> {
const clone = obj.constructor();
const clone = {} as any;

for (let key in obj) {
const value = obj[key];
Expand Down

0 comments on commit c56cfe5

Please sign in to comment.