Skip to content

Commit 3e4a7aa

Browse files
committed
feat (vs-code-ext): preview updates when any changes to the document are done, even without saving
1 parent e5cd988 commit 3e4a7aa

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

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

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

7171
updatePreviewPanelContent();
7272

73-
vscode.workspace.onDidSaveTextDocument((ev) => {
74-
if (ev.fileName === vscode.window.activeTextEditor?.document.fileName) {
73+
vscode.workspace.onDidChangeTextDocument((ev) => {
74+
if (ev.document.fileName === vscode.window.activeTextEditor?.document.fileName) {
7575
updatePreviewPanelContent();
7676
}
7777
});

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

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

33
import { basename, join } from "path";
4-
import { rm } from "fs/promises";
4+
import { rm, unlink, writeFile } from "fs/promises";
55
import * as esbuild from "esbuild";
66

77
import { render } from "@react-email/render";
@@ -29,27 +29,37 @@ export async function renderOpenEmailFile(
2929
return { valid: false };
3030
}
3131

32+
3233
const currentlyOpenTabFilePath = activeEditor.document.fileName; // actually a path not the name of the file
33-
const emailsDirectory = join(currentlyOpenTabFilePath, "..");
34+
const currentlyOpenTabFilename = basename(currentlyOpenTabFilePath, ".tsx");
3435

36+
const emailsDirectory = join(currentlyOpenTabFilePath, "..");
3537
const previewDirectory = join(emailsDirectory, extensionPreviewFolder);
38+
39+
// this is necessary so that we can still build things in a stable way
40+
// and have a up-to date version of the email preview on the extension
41+
const currentlyOpenTabFilesPathWithCurrentContents = join(emailsDirectory, `${currentlyOpenTabFilename}.vscpreview.tsx`);
42+
const currentContents = activeEditor.document.getText();
43+
await writeFile(currentlyOpenTabFilesPathWithCurrentContents, currentContents);
44+
45+
const builtFileWithCurrentContents = join(previewDirectory, `${currentlyOpenTabFilename}.js`);
46+
3647
try {
3748
await esbuild.build({
3849
bundle: true,
39-
entryPoints: [currentlyOpenTabFilePath],
50+
entryPoints: [currentlyOpenTabFilesPathWithCurrentContents],
4051
platform: "node",
4152
write: true,
42-
outdir: previewDirectory,
53+
outfile: builtFileWithCurrentContents,
4354
});
4455

45-
const filename = basename(currentlyOpenTabFilePath, ".tsx");
46-
const templatePath = `${previewDirectory}/${filename}.js`;
56+
await unlink(currentlyOpenTabFilesPathWithCurrentContents); // unlink the temporary file after building it
4757

48-
delete require.cache[templatePath];
58+
delete require.cache[builtFileWithCurrentContents];
4959
// we need to use require since it has a way to programatically invalidate its cache
50-
const email = require(templatePath);
60+
const email = require(builtFileWithCurrentContents);
5161

52-
if (typeof email.default === "undefined") {
62+
if (typeof email.default === "undefined") { // this means there is no "export default ..." in the file
5363
return { valid: false };
5464
}
5565

@@ -63,7 +73,7 @@ export async function renderOpenEmailFile(
6373
await rm(previewDirectory, { recursive: true });
6474

6575
return {
66-
filename,
76+
filename: currentlyOpenTabFilename,
6777
html: emailAsHTML,
6878
text: emailAsText,
6979
valid: true,

0 commit comments

Comments
 (0)