Skip to content

Commit

Permalink
fix(babel): don't run async prepareForEval if target file is processi…
Browse files Browse the repository at this point in the history
…ng (fixes #1054) (#1086)
  • Loading branch information
Anber committed Oct 18, 2022
1 parent c2fa865 commit 9111b4e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tasty-falcons-crash.md
@@ -0,0 +1,5 @@
---
'@linaria/babel-preset': patch
---

Fix an issue with async resolvers that sometimes leads to attempts to evaluate unprepared code. Fixes #1054.
15 changes: 14 additions & 1 deletion packages/babel/src/transform-stages/1-prepare-for-eval.ts
Expand Up @@ -317,6 +317,8 @@ export function prepareForEvalSync(
return Array.from(results);
}

const mutexes = new Map<string, Promise<void>>();

/**
* Parses the specified file and recursively all its dependencies,
* finds tags, applies eval-time replacements, removes dead code.
Expand All @@ -335,6 +337,11 @@ export default async function prepareForEval(
stack: string[] = []
): Promise<ITransformFileResult[] | undefined> {
const resolvedFile = await file;
const mutex = resolvedFile ? mutexes.get(resolvedFile.name) : null;
if (mutex) {
await mutex;
}

const processed = processQueueItem(babel, resolvedFile, codeCache, options);
if (!processed) return undefined;

Expand Down Expand Up @@ -397,7 +404,13 @@ export default async function prepareForEval(
);
});

await Promise.all(promises);
const promise = Promise.all(promises).then(() => {});

mutexes.set(name, promise);

await promise;

mutexes.delete(name);

return Array.from(results);
}

0 comments on commit 9111b4e

Please sign in to comment.