Skip to content

Commit

Permalink
docs: fix repl @builder.io/qwik/build types (#633)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley authored Jun 17, 2022
1 parent f287fe3 commit 389cb6a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
35 changes: 29 additions & 6 deletions packages/docs/src/components/repl/monaco.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Diagnostic } from '@builder.io/qwik/optimizer';
import type MonacoTypes from 'monaco-editor';
import type { EditorProps, EditorStore } from './editor';
import type { ReplStore } from './types';
import { QWIK_REPL_DEPS_CACHE } from './worker/repl-constants';

export const initMonacoEditor = async (
containerElm: any,
Expand Down Expand Up @@ -220,8 +221,16 @@ const loadDeps = async (qwikVersion: string) => {
pkgPath: '/server.d.ts',
path: '/node_modules/@types/builder.io__qwik/server.d.ts',
},
{
pkgName: '@builder.io/qwik',
pkgVersion: qwikVersion,
pkgPath: '/build/index.d.ts',
path: '/node_modules/@types/builder.io__qwik/build/index.d.ts',
},
];

const cache = await caches.open(QWIK_REPL_DEPS_CACHE);

await Promise.all(
deps.map(async (dep) => {
let storedDep = monacoCtx.deps.find(
Expand All @@ -238,12 +247,9 @@ const loadDeps = async (qwikVersion: string) => {
monacoCtx.deps.push(storedDep);

storedDep.promise = new Promise<void>((resolve, reject) => {
const url = getCdnUrl(dep.pkgName, dep.pkgVersion, dep.pkgPath);
fetch(url).then((rsp) => {
rsp.text().then((code) => {
storedDep!.code = code;
resolve();
}, reject);
fetchDep(cache, dep).then((code) => {
storedDep!.code = code;
resolve();
}, reject);
});
}
Expand All @@ -254,6 +260,23 @@ const loadDeps = async (qwikVersion: string) => {
return monacoCtx.deps;
};

const fetchDep = async (cache: Cache, dep: NodeModuleDep) => {
const url = getCdnUrl(dep.pkgName, dep.pkgVersion, dep.pkgPath);
const req = new Request(url);
const cachedRes = await cache.match(req);
if (cachedRes) {
return cachedRes.clone().text();
}
const fetchRes = await fetch(req);
if (fetchRes.ok) {
if (!req.url.includes('localhost')) {
await cache.put(req, fetchRes.clone());
}
return fetchRes.clone().text();
}
throw new Error(`Unable to fetch: ${url}`);
};

const getMonaco = async (): Promise<Monaco> => {
if (!monacoCtx.loader) {
// lazy-load the monaco AMD script ol' school
Expand Down
6 changes: 3 additions & 3 deletions packages/docs/src/components/repl/worker/repl-constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const QWIK_PKG_NAME = '@builder.io/qwik';
export const ROLLUP_VERSION = '2.75.4';
export const PRETTIER_VERSION = '2.6.2';
export const TERSER_VERSION = '5.14.0';
export const ROLLUP_VERSION = '2.75.6';
export const PRETTIER_VERSION = '2.7.1';
export const TERSER_VERSION = '5.14.1';

export const QWIK_REPL_DEPS_CACHE = 'QwikReplDeps';
export const QWIK_REPL_RESULT_CACHE = 'QwikReplResults';
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export const depResponse = async (
}
const fetchRes = await fetch(req);
if (fetchRes.ok) {
await cache.put(req, fetchRes.clone());
if (!req.url.includes('localhost')) {
await cache.put(req, fetchRes.clone());
}
return fetchRes;
}
};
Expand Down

0 comments on commit 389cb6a

Please sign in to comment.