Skip to content

Commit 4ce91a1

Browse files
committed
feat: queueMicrotask polyfill
1 parent 46e8dc8 commit 4ce91a1

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

src/config/config-schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export const ConfigSchema = DataType.RecordOf({
8080
base64: OptionalField(DataType.Boolean),
8181
fetch: OptionalField(DataType.Boolean),
8282
WebSocket: OptionalField(DataType.Boolean),
83+
queueMicrotask: OptionalField(DataType.Boolean),
8384
node: OptionalField(
8485
DataType.RecordOf({
8586
path: OptionalField(DataType.Boolean),

src/polyfills/queue-microtask.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { registerPolyfills } from "./shared/polyfill-global";
2+
3+
registerPolyfills("queueMicrotask")(() => {
4+
let onNextMicrotask: Array<() => void> | undefined;
5+
6+
function queueMicrotask(task: () => void) {
7+
if (onNextMicrotask) {
8+
onNextMicrotask.push(task);
9+
}
10+
11+
onNextMicrotask = [task];
12+
13+
imports.mainloop.idle_add(() => {
14+
if (!onNextMicrotask) return;
15+
const tasks = onNextMicrotask;
16+
onNextMicrotask = undefined;
17+
for (const task of tasks!) {
18+
try {
19+
task();
20+
} catch (e) {
21+
console.error(e);
22+
}
23+
}
24+
}, -2000);
25+
}
26+
27+
return { queueMicrotask };
28+
});

src/utils/get-polyfills.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ export const getGlobalPolyfills = (program: Program): Promise<Polyfills> => {
5757
polyFilepaths.push("./polyfills/esm/websocket.mjs");
5858
}
5959

60+
if (polyfills?.queueMicrotask) {
61+
polyFilepaths.push("./polyfills/esm/queue-microtask.mjs");
62+
}
63+
6064
const customPolyfills: string[] = [];
6165

6266
if (program.config.customPolyfills) {

src/utils/parse-env-var-config.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,12 @@ export const parseEnvVarConfig = (program: Program) => {
102102
}
103103

104104
// Add env file vars
105-
const cwdFiles = fs.readdirSync(program.cwd);
106105
const envFileName = envVars?.envFilePath ?? ".env";
107106
const failIfNoEnvFile = envVars?.envFilePath !== undefined;
108107

109-
if (cwdFiles.includes(envFileName)) {
110-
const envFileData = fs.readFileSync(
111-
path.resolve(program.cwd, envFileName),
112-
"utf-8",
113-
);
108+
const envFilepath = path.resolve(program.cwd, envFileName);
109+
if (fs.existsSync(envFilepath)) {
110+
const envFileData = fs.readFileSync(envFilepath, "utf-8");
114111

115112
for (const envVarEntry of parseDotEnv(envFileData)) {
116113
if (vars.some(([k]) => k === envVarEntry[0])) {

0 commit comments

Comments
 (0)