Skip to content

Commit

Permalink
fix(vite): broken imports from some third-party libs (#1203)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anber committed Feb 7, 2023
1 parent 08c49f1 commit 917db44
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/bright-files-beg.md
@@ -0,0 +1,6 @@
---
'@linaria/babel-preset': patch
'@linaria/vite': patch
---

A workaround for an issue with Vite and imports from some third-party libs.
22 changes: 22 additions & 0 deletions packages/babel/src/module.ts
Expand Up @@ -78,6 +78,14 @@ const NOOP = () => {};
const padStart = (num: number, len: number) =>
num.toString(10).padStart(len, '0');

const hasKey = <TKey extends string | symbol>(
obj: unknown,
key: TKey
): obj is Record<TKey, unknown> =>
(typeof obj === 'object' || typeof obj === 'function') &&
obj !== null &&
key in obj;

class Module {
static invalidate: () => void;

Expand Down Expand Up @@ -197,6 +205,20 @@ class Module {
// e.g `exports.hasOwnProperty`
value = Reflect.get(target, key);
}

if (value === undefined && this.#lazyValues.has('default')) {
const defaultValue = this.#lazyValues.get('default')?.();
if (hasKey(defaultValue, key)) {
this.debug(
'evaluated',
'⚠️ %s has been found in `default`. It indicates that ESM to CJS conversion of %s went wrong.',
key,
filename
);
value = defaultValue[key];
}
}

this.debug('evaluated', 'get %s: %o', key, value);
return value;
},
Expand Down

0 comments on commit 917db44

Please sign in to comment.