Skip to content

Commit 4e41744

Browse files
committed
fix (vs-code-ext): update the preview on save
this is because it just became to coomplicated when trying to update on text change but there are still a few commits before this that are trying to make this work, couldn't make it work well though
1 parent efddc7f commit 4e41744

File tree

2 files changed

+12
-26
lines changed

2 files changed

+12
-26
lines changed

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

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

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

41-
vscode.workspace.onDidChangeTextDocument((ev) => {
41+
vscode.workspace.onDidSaveTextDocument((ev) => {
4242
if (
43-
ev.document.fileName ===
43+
ev.fileName ===
4444
vscode.window.activeTextEditor?.document.fileName
4545
) {
4646
updatePreiewPanel(previewPanel);

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

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

33
import * as crypto from "crypto";
44
import { basename, join } from "path";
5-
import { unlinkSync, writeFileSync } from "fs";
65
import * as esbuild from "esbuild";
76

87
import { render } from "@react-email/render";
8+
import { unlink } from "fs/promises";
99

1010
const extensionPreviewFolder = ".vscpreview" as const;
1111

@@ -18,9 +18,9 @@ export type BuiltEmail =
1818
}
1919
| { valid: false };
2020

21-
export function renderOpenEmailFile(
21+
export async function renderOpenEmailFile(
2222
activeEditor: vscode.TextEditor | undefined,
23-
): BuiltEmail | undefined {
23+
): Promise<BuiltEmail | undefined> {
2424
if (typeof activeEditor !== "undefined") {
2525
if (
2626
typeof activeEditor.document.fileName === "undefined" ||
@@ -36,39 +36,26 @@ export function renderOpenEmailFile(
3636
const emailsDirectory = join(currentlyOpenTabFilePath, "..");
3737
const previewDirectory = join(emailsDirectory, extensionPreviewFolder);
3838

39-
// this hash is needed so the temporary files don't get mixed up
40-
// when changing the email very fast
39+
// this hash is needed so the the import doesn't get from its cache
4140
const renderingHash = crypto.randomBytes(20).toString('hex');
42-
43-
// this is necessary so that we can still build things in a stable way
44-
// and have a up-to date version of the email preview on the extension
45-
const currentlyOpenTabFilesPathWithCurrentContents = join(
46-
emailsDirectory,
47-
`${currentlyOpenTabFilename}-${renderingHash}.vscpreview.tsx`,
48-
);
49-
const currentContents = activeEditor.document.getText();
50-
writeFileSync(
51-
currentlyOpenTabFilesPathWithCurrentContents,
52-
currentContents,
53-
);
5441

5542
const builtFileWithCurrentContents = join(
5643
previewDirectory,
5744
`${currentlyOpenTabFilename}-${renderingHash}.js`,
5845
);
5946

6047
try {
61-
esbuild.buildSync({
48+
await esbuild.build({
6249
bundle: true,
63-
entryPoints: [currentlyOpenTabFilesPathWithCurrentContents],
50+
entryPoints: [currentlyOpenTabFilePath],
6451
platform: "node",
6552
write: true,
6653
outfile: builtFileWithCurrentContents,
6754
});
6855

69-
delete require.cache[builtFileWithCurrentContents];
70-
// we need to use require since it has a way to programatically invalidate its cache
71-
const email = require(builtFileWithCurrentContents);
56+
// for future people debugging this: if this doesnt update the preview, might be because import keeps a cache
57+
// and the hash is not being unique (unlikely though)
58+
const email = await import(builtFileWithCurrentContents);
7259

7360
if (typeof email.default === "undefined") {
7461
// this means there is no "export default ..." in the file
@@ -83,8 +70,7 @@ export function renderOpenEmailFile(
8370

8471
const emailAsHTML = render(comp, { pretty: false });
8572

86-
unlinkSync(builtFileWithCurrentContents);
87-
unlinkSync(currentlyOpenTabFilesPathWithCurrentContents);
73+
await unlink(builtFileWithCurrentContents);
8874

8975
return {
9076
filename: currentlyOpenTabFilename,

0 commit comments

Comments
 (0)