Skip to content

Commit efddc7f

Browse files
committed
feat (vs-code-ext): renderOpenEmailFile blocking
this is to avoid too many temp files being created
1 parent f185cdf commit efddc7f

File tree

2 files changed

+12
-25
lines changed

2 files changed

+12
-25
lines changed

packages/vs-code-extension/src/extension.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ export function activate(context: vscode.ExtensionContext) {
3838

3939
previewPanel.onDidDispose(() => (previewPanel = undefined));
4040

41-
vscode.workspace.onDidChangeTextDocument(async (ev) => {
41+
vscode.workspace.onDidChangeTextDocument((ev) => {
4242
if (
4343
ev.document.fileName ===
4444
vscode.window.activeTextEditor?.document.fileName
4545
) {
46-
await updatePreiewPanel(previewPanel);
46+
updatePreiewPanel(previewPanel);
4747
}
4848
});
4949

50-
vscode.window.onDidChangeActiveTextEditor(async () => {
51-
await updatePreiewPanel(previewPanel);
50+
vscode.window.onDidChangeActiveTextEditor(() => {
51+
updatePreiewPanel(previewPanel);
5252
});
5353
},
5454
);

packages/vs-code-extension/src/renderOpenEmailFile.ts

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as vscode from "vscode";
22

33
import * as crypto from "crypto";
44
import { basename, join } from "path";
5-
import { unlink, writeFile } from "fs/promises";
5+
import { unlinkSync, writeFileSync } from "fs";
66
import * as esbuild from "esbuild";
77

88
import { render } from "@react-email/render";
@@ -18,19 +18,15 @@ export type BuiltEmail =
1818
}
1919
| { valid: false };
2020

21-
let isBuilding = false;
22-
23-
export async function renderOpenEmailFile(
21+
export function renderOpenEmailFile(
2422
activeEditor: vscode.TextEditor | undefined,
25-
): Promise<BuiltEmail | undefined> {
26-
if (typeof activeEditor !== "undefined" && !isBuilding) {
27-
isBuilding = true;
23+
): BuiltEmail | undefined {
24+
if (typeof activeEditor !== "undefined") {
2825
if (
2926
typeof activeEditor.document.fileName === "undefined" ||
3027
activeEditor.document.fileName.length === 0 ||
3128
activeEditor.document.getText().length === 0
3229
) {
33-
isBuilding = false;
3430
return { valid: false };
3531
}
3632

@@ -51,7 +47,7 @@ export async function renderOpenEmailFile(
5147
`${currentlyOpenTabFilename}-${renderingHash}.vscpreview.tsx`,
5248
);
5349
const currentContents = activeEditor.document.getText();
54-
await writeFile(
50+
writeFileSync(
5551
currentlyOpenTabFilesPathWithCurrentContents,
5652
currentContents,
5753
);
@@ -62,23 +58,19 @@ export async function renderOpenEmailFile(
6258
);
6359

6460
try {
65-
await esbuild.build({
61+
esbuild.buildSync({
6662
bundle: true,
6763
entryPoints: [currentlyOpenTabFilesPathWithCurrentContents],
6864
platform: "node",
6965
write: true,
7066
outfile: builtFileWithCurrentContents,
7167
});
7268

73-
await unlink(currentlyOpenTabFilesPathWithCurrentContents);
74-
7569
delete require.cache[builtFileWithCurrentContents];
7670
// we need to use require since it has a way to programatically invalidate its cache
7771
const email = require(builtFileWithCurrentContents);
7872

7973
if (typeof email.default === "undefined") {
80-
isBuilding = false;
81-
8274
// this means there is no "export default ..." in the file
8375
return { valid: false };
8476
}
@@ -91,9 +83,8 @@ export async function renderOpenEmailFile(
9183

9284
const emailAsHTML = render(comp, { pretty: false });
9385

94-
await unlink(builtFileWithCurrentContents);
95-
96-
isBuilding = false;
86+
unlinkSync(builtFileWithCurrentContents);
87+
unlinkSync(currentlyOpenTabFilesPathWithCurrentContents);
9788

9889
return {
9990
filename: currentlyOpenTabFilename,
@@ -102,8 +93,6 @@ export async function renderOpenEmailFile(
10293
valid: true,
10394
};
10495
} catch (exception) {
105-
isBuilding = false;
106-
10796
console.warn(
10897
"Exception happenned on rendering or building of an email, but maybe its because it just was invalid anyways",
10998
exception,
@@ -113,7 +102,5 @@ export async function renderOpenEmailFile(
113102
}
114103
}
115104

116-
isBuilding = false;
117-
118105
return undefined;
119106
}

0 commit comments

Comments
 (0)