Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize compiler filesystem caching #16

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 74 additions & 18 deletions src/transpileCodeblocks/compiler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ts from 'typescript';
import { normalize } from 'node:path';
import { normalize, dirname, join } from 'node:path';

import type { VirtualFiles, VirtualFile } from './plugin.js';

Expand Down Expand Up @@ -53,14 +53,13 @@ export class Compiler {
}

public compile(files: VirtualFiles) {
// console.log(compilerOptions)

this.compilerHost.setScriptFileNames([]);
for (let [fileName, { code }] of Object.entries(files)) {
code = code.replace(/^$/gm, '//__NEWLINE__');
this.compilerHost.writeFile(fileName, code);
}
this.compilerHost.setScriptFileNames(Object.keys(files));
const filenames = Object.keys(files);
this.compilerHost.setScriptFileNames(filenames);
Comment on lines +61 to +62
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's a bunch of these going on for logging purposes - could you undo them so we have the diff a little bit smaller?


const returnFiles: TranspiledFiles = {};

Expand Down Expand Up @@ -101,31 +100,57 @@ export class Compiler {
}
}

function createCompilerHost(
compilerOptions: ts.CompilerOptions,
externalResolutions: CompilerSettings['externalResolutions']
): ts.LanguageServiceHost &
type ModifiedCompilerHost = ts.LanguageServiceHost &
ts.ModuleResolutionHost &
Required<Pick<ts.LanguageServiceHost, 'writeFile'>> & {
setScriptFileNames(files: string[]): void;
} {
};

function createCompilerHost(
compilerOptions: ts.CompilerOptions,
externalResolutions: CompilerSettings['externalResolutions']
): ModifiedCompilerHost {
const virtualFiles: Record<string, { contents: string; version: number }> =
{};
let scriptFileNames: string[] = [];

return {
let resolvedModules: Record<string, ts.ResolvedModuleFull | undefined> = {};

let cachedFiles: Record<string, string | undefined> = {};

const host: ModifiedCompilerHost = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could also stay a direct return?

...ts.createCompilerHost(compilerOptions),
getCompilationSettings() {
return compilerOptions;
},
fileExists(fileName) {
// console.log('fileExists', fileName)
return !!virtualFiles[normalize(fileName)] || ts.sys.fileExists(fileName);
const result =
!!virtualFiles[normalize(fileName)] || ts.sys.fileExists(fileName);
return result;
Comment on lines +127 to +129
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also pulled apart for logging purposes I guess?

},
readFile(fileName: string) {
// console.log('readFile', fileName)
const virtual = virtualFiles[normalize(fileName)];
return virtual ? virtual.contents : ts.sys.readFile(fileName);
const normalized = normalize(fileName);
const virtual = virtualFiles[normalized];

if (virtual) {
return virtual.contents;
}

if (normalized in cachedFiles) {
const cacheResult = cachedFiles[fileName];

if (cacheResult) {
return cacheResult;
}
}

const contents = ts.sys.readFile(fileName);

if (contents) {
cachedFiles[normalized] = contents;
}
Comment on lines +139 to +151
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you verify that this doesn't cause problems in watch mode?
If it does, we might need a flag to swap behaviours here.


return contents;
},
writeFile(fileName, contents) {
fileName = normalize(fileName);
Expand All @@ -147,7 +172,6 @@ function createCompilerHost(
},
setScriptFileNames(files) {
scriptFileNames = files.map(normalize);
// console.log({ virtualFiles, scriptFileNames })
},
getScriptFileNames() {
return scriptFileNames;
Expand All @@ -166,7 +190,7 @@ function createCompilerHost(
);
},
resolveModuleNames(moduleNames, containingFile) {
return moduleNames.map((moduleName) => {
const mappedModules = moduleNames.map((moduleName) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also stay a direct return and was only pulled apart for logging I think?

if (moduleName in externalResolutions) {
const resolved = externalResolutions[moduleName];

Expand All @@ -185,13 +209,45 @@ function createCompilerHost(
};
}

return ts.resolveModuleName(
// Files that are local to the code block, like './api', should be resolved
// more directly rather than triggering a full module resolution search.
if (
moduleName.startsWith('.') &&
containingFile.includes('codeBlock')
) {
const containingDir = dirname(containingFile);
const newModuleName = join(containingDir, moduleName);
const resolvedModule = ts.resolveModuleName(
newModuleName,
containingFile,
compilerOptions,
this
).resolvedModule;

return resolvedModule;
Comment on lines +220 to +227
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const resolvedModule = ts.resolveModuleName(
newModuleName,
containingFile,
compilerOptions,
this
).resolvedModule;
return resolvedModule;
return ts.resolveModuleName(
newModuleName,
containingFile,
compilerOptions,
this
).resolvedModule;

}

const key = `${containingFile}|${moduleName}`;

if (key in resolvedModules) {
return resolvedModules[key];
}

const resolved = ts.resolveModuleName(
moduleName,
containingFile,
compilerOptions,
this
).resolvedModule;

resolvedModules[key] = resolved;

return resolved;
});

return mappedModules;
},
};

return host;
}