Skip to content

Commit

Permalink
fix(sys): ensure typescript sys patched for initial load
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Jul 6, 2020
1 parent a6b0643 commit 90913df
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/compiler/sys/typescript/typescript-load.ts
Expand Up @@ -3,7 +3,7 @@ import { denoLoadTypeScript } from '../../../sys/deno/deno-load-typescript';
import { dependencies } from '../dependencies.json';
import { isFunction, IS_NODE_ENV, IS_BROWSER_ENV, IS_WEB_WORKER_ENV, IS_DENO_ENV } from '@utils';
import { nodeLoadTypeScript } from '../../../sys/node/node-load-typescript';
import { patchImportedTsSys as patchRemoteTsSys } from './typescript-patch';
import { patchRemoteTsSys } from './typescript-patch';
import ts from 'typescript';

export const loadTypescript = (sys: d.CompilerSystem, typescriptPath: string, sync: boolean): TypeScriptModule | Promise<TypeScriptModule> => {
Expand Down Expand Up @@ -33,7 +33,7 @@ export const loadTypescript = (sys: d.CompilerSystem, typescriptPath: string, sy
if (IS_WEB_WORKER_ENV) {
const webWorkerTs = getLoadedTs(webWorkerLoadTypeScript(tsUrl));
if (webWorkerTs) {
patchRemoteTsSys(webWorkerTs, tsUrl);
patchRemoteTsSys(tsUrl);
return webWorkerTs;
}
}
Expand Down Expand Up @@ -72,7 +72,8 @@ const browserMainLoadTypeScript = (tsUrl: string): any =>
scriptElm.onload = () => {
const browserTs = getLoadedTs((globalThis as any).ts);
if (browserTs) {
resolve(patchRemoteTsSys(browserTs, tsUrl));
patchRemoteTsSys(tsUrl);
resolve(browserTs);
} else {
reject(`Unable to load TypeScript via browser script`);
}
Expand All @@ -90,11 +91,10 @@ const getTsUrl = (sys: d.CompilerSystem, typeScriptPath: string) => {
return sys.getRemoteModuleUrl({ moduleId: typecriptDep.name, version: typecriptDep.version, path: typecriptDep.main });
};

const getLoadedTs = (loadedTs: TypeScriptModule) => {
const getLoadedTs = (loadedTs: TypeScriptModule): TypeScriptModule => {
if (loadedTs != null && isFunction(loadedTs.transpileModule)) {
loadedTs.__loaded = true;
Object.assign(ts, loadedTs);
return loadedTs;
return Object.assign(ts, loadedTs);
}
return null;
};
Expand Down
5 changes: 2 additions & 3 deletions src/compiler/sys/typescript/typescript-patch.ts
Expand Up @@ -31,9 +31,9 @@ const patchTypescriptModule = async (config: d.Config, diagnostics: d.Diagnostic
}
};

export const patchImportedTsSys = (importedTs: any, tsUrl: string) => {
export const patchRemoteTsSys = (tsUrl: string) => {
// patches just the bare minimum
const tsSys: ts.System = (importedTs.sys = importedTs.sys || ({} as any));
const tsSys: ts.System = (ts.sys = ts.sys || ({} as any));
tsSys.getExecutingFilePath = () => tsUrl;

if (!tsSys.getCurrentDirectory) {
Expand All @@ -57,5 +57,4 @@ export const patchImportedTsSys = (importedTs: any, tsUrl: string) => {
if (!tsSys.write) {
tsSys.write = noop;
}
return importedTs;
};

0 comments on commit 90913df

Please sign in to comment.