@@ -2,10 +2,10 @@ import * as vscode from "vscode";
2
2
3
3
import * as crypto from "crypto" ;
4
4
import { basename , join } from "path" ;
5
- import { unlinkSync , writeFileSync } from "fs" ;
6
5
import * as esbuild from "esbuild" ;
7
6
8
7
import { render } from "@react-email/render" ;
8
+ import { unlink } from "fs/promises" ;
9
9
10
10
const extensionPreviewFolder = ".vscpreview" as const ;
11
11
@@ -18,9 +18,9 @@ export type BuiltEmail =
18
18
}
19
19
| { valid : false } ;
20
20
21
- export function renderOpenEmailFile (
21
+ export async function renderOpenEmailFile (
22
22
activeEditor : vscode . TextEditor | undefined ,
23
- ) : BuiltEmail | undefined {
23
+ ) : Promise < BuiltEmail | undefined > {
24
24
if ( typeof activeEditor !== "undefined" ) {
25
25
if (
26
26
typeof activeEditor . document . fileName === "undefined" ||
@@ -36,39 +36,26 @@ export function renderOpenEmailFile(
36
36
const emailsDirectory = join ( currentlyOpenTabFilePath , ".." ) ;
37
37
const previewDirectory = join ( emailsDirectory , extensionPreviewFolder ) ;
38
38
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
41
40
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
- ) ;
54
41
55
42
const builtFileWithCurrentContents = join (
56
43
previewDirectory ,
57
44
`${ currentlyOpenTabFilename } -${ renderingHash } .js` ,
58
45
) ;
59
46
60
47
try {
61
- esbuild . buildSync ( {
48
+ await esbuild . build ( {
62
49
bundle : true ,
63
- entryPoints : [ currentlyOpenTabFilesPathWithCurrentContents ] ,
50
+ entryPoints : [ currentlyOpenTabFilePath ] ,
64
51
platform : "node" ,
65
52
write : true ,
66
53
outfile : builtFileWithCurrentContents ,
67
54
} ) ;
68
55
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 ) ;
72
59
73
60
if ( typeof email . default === "undefined" ) {
74
61
// this means there is no "export default ..." in the file
@@ -83,8 +70,7 @@ export function renderOpenEmailFile(
83
70
84
71
const emailAsHTML = render ( comp , { pretty : false } ) ;
85
72
86
- unlinkSync ( builtFileWithCurrentContents ) ;
87
- unlinkSync ( currentlyOpenTabFilesPathWithCurrentContents ) ;
73
+ await unlink ( builtFileWithCurrentContents ) ;
88
74
89
75
return {
90
76
filename : currentlyOpenTabFilename ,
0 commit comments