Repository: freeCodeCamp/freeCodeCamp
File: tools/client-plugins/browser-scripts/typescript-worker.ts (TypeScript)

Code before:
  error: string;
}

interface CheckIsReadyRequestEvent extends MessageEvent {
  data: {
    type: 'check-is-ready';
  };
}

interface CancelEvent extends MessageEvent {
  data: {
    type: 'cancel';
    value: number;
  };
}

const TS_VERSION = '5.7.3'; // hardcoding for now, in the future this may be dynamic. Must be fully specified version

let tsEnv: VirtualTypeScriptEnvironment | null = null;
let compilerHost: CompilerHost | null = null;
let cachedVersion: string | null = null;

// NOTE: vfs.globals must only be imported once, otherwise it will throw.
importScripts(
  'https://cdn.jsdelivr.net/npm/@typescript/vfs@1.6.0/dist/vfs.globals.js'
);

function importTS(version: string) {
  if (cachedVersion == version) return;
  importScripts(
    `https://cdnjs.cloudflare.com/ajax/libs/typescript/${version}/typescript.min.js`
  );
  cachedVersion = version;
}

async function setupTypeScript() {
  importTS(TS_VERSION);
  const compilerOptions: CompilerOptions = {
    target: ts.ScriptTarget.ES2015,
    skipLibCheck: true // TODO: look into why this is needed. Are we doing something wrong? Could it be that it's not "synced"  with this TS version?
    // from the docs: "Note: it's possible for this list to get out of
    // sync with TypeScript over time. It was last synced with TypeScript
    // 3.8.0-rc."
  };
  const fsMap = await createDefaultMapFromCDN(
    compilerOptions,
    ts.version,
    false, // TODO: cache this. It needs a store that's available to workers and implements https://github.com/microsoft/TypeScript-Website/blob/ac68b8b8e4a621113c4ee45c4051002fd55ede24/packages/typescript-vfs/src/index.ts#L11
    ts
  );


Review comment (suggestion):
My concern with jsdelivr is still https://github.com/freeCodeCamp/freeCodeCamp/pull/59291#issuecomment-2728701157.

I think we either need to wait for the cdnjs PR to go in and replace this with that URL, or we keep using unpkg.com for `@typescript/vfs` and update it whenever the replacement is available.

Code after:
  error: string;
}

interface CheckIsReadyRequestEvent extends MessageEvent {
  data: {
    type: 'check-is-ready';
  };
}

interface CancelEvent extends MessageEvent {
  data: {
    type: 'cancel';
    value: number;
  };
}

// Pin at the latest TS version available as cdnjs doesn't support version range.
const TS_VERSION = '5.7.3';

let tsEnv: VirtualTypeScriptEnvironment | null = null;
let compilerHost: CompilerHost | null = null;
let cachedVersion: string | null = null;

// NOTE: vfs.globals must only be imported once, otherwise it will throw.
importScripts(
  'https://cdn.jsdelivr.net/npm/@typescript/vfs@1.6.0/dist/vfs.globals.js'
);

function importTS(version: string) {
  if (cachedVersion == version) return;
  importScripts(
    `https://cdnjs.cloudflare.com/ajax/libs/typescript/${version}/typescript.min.js`
  );
  cachedVersion = version;
}

async function setupTypeScript() {
  importTS(TS_VERSION);
  const compilerOptions: CompilerOptions = {
    target: ts.ScriptTarget.ES2015,
    skipLibCheck: true // TODO: look into why this is needed. Are we doing something wrong? Could it be that it's not "synced"  with this TS version?
    // from the docs: "Note: it's possible for this list to get out of
    // sync with TypeScript over time. It was last synced with TypeScript
    // 3.8.0-rc."
  };
  const fsMap = await createDefaultMapFromCDN(
    compilerOptions,
    ts.version,
    false, // TODO: cache this. It needs a store that's available to workers and implements https://github.com/microsoft/TypeScript-Website/blob/ac68b8b8e4a621113c4ee45c4051002fd55ede24/packages/typescript-vfs/src/index.ts#L11
    ts
  );