Skip to content

Commit

Permalink
fix: ESM temp files not cleaned up
Browse files Browse the repository at this point in the history
  • Loading branch information
nonara committed Jun 13, 2023
1 parent a188059 commit 8802054
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions projects/patch/src/plugin/esm-intercept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ namespace tsp {

export function registerEsmIntercept(registerConfig: RegisterConfig): () => void {
const originalRequire = Module.prototype.require;
const originalReadFileSync = fs.readFileSync;
const builtFiles = new Map<string, string>();

const getHash = () => {
Expand All @@ -47,7 +46,7 @@ namespace tsp {
for (const { 1: filePath } of builtFiles) {
delete require.cache[filePath];
try {
fs.readFileSync(filePath, 'utf8');
fs.rmSync(filePath, { force: true, maxRetries: 3 });
} catch (e) {
if (process.env.NODE_ENV !== 'production')
console.warn(`[ts-patch] Warning: Failed to delete temporary esm cache file: ${filePath}.`);
Expand Down Expand Up @@ -83,7 +82,7 @@ namespace tsp {
let targetFilePath: string;
if (tsExtensions.includes(resolvedPathExt)) {
if (!builtFiles.has(resolvedPath)) {
const tsCode = originalReadFileSync(resolvedPath, 'utf8');
const tsCode = fs.readFileSync(resolvedPath, 'utf8');
const jsCode = registerConfig.tsNodeInstance!.compile(tsCode, resolvedPath);
const outputFileName = getHash() + '.mjs';
const outputFilePath = path.join(getTmpDir('esm'), outputFileName);
Expand Down

0 comments on commit 8802054

Please sign in to comment.