Skip to content

Commit

Permalink
Merge pull request #7019 from QwikDev/v2-fix-repl
Browse files Browse the repository at this point in the history
fix(repl): make it work again
  • Loading branch information
wmertens authored Oct 30, 2024
2 parents 4f2815a + 9b20911 commit 80f6fbd
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 36 deletions.
6 changes: 6 additions & 0 deletions packages/docs/src/repl/bundled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import qServerCjs from '../../node_modules/@qwik.dev/core/dist/server.cjs?raw-so
import qServerDts from '../../node_modules/@qwik.dev/core/dist/server.d.ts?raw-source';

export const QWIK_PKG_NAME = '@qwik.dev/core';
export const QWIK_PKG_NAME_V1 = '@builder.io/qwik';
const ROLLUP_VERSION = '2.75.6';

export const getNpmCdnUrl = (
Expand All @@ -41,6 +42,11 @@ export const getNpmCdnUrl = (
pkgVersion = pkgName === QWIK_PKG_NAME ? qwikVersion.split('-dev')[0] : '';
}
}
if (pkgName === QWIK_PKG_NAME) {
if (pkgVersion < '2') {
pkgName = QWIK_PKG_NAME_V1;
}
}
return `https://cdn.jsdelivr.net/npm/${pkgName}${pkgVersion ? '@' + pkgVersion : ''}${pkgPath}`;
};

Expand Down
9 changes: 5 additions & 4 deletions packages/docs/src/repl/monaco.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { isServer } from '@qwik.dev/core/build';
import type { Diagnostic } from '@qwik.dev/core/optimizer';
import type MonacoTypes from 'monaco-editor';
import { getColorPreference } from '../components/theme-toggle/theme-toggle';
import { bundled, getNpmCdnUrl } from './bundled';
import { QWIK_PKG_NAME, QWIK_PKG_NAME_V1, bundled, getNpmCdnUrl } from './bundled';
import type { EditorProps, EditorStore } from './editor';
import type { ReplStore } from './types';
// We cannot use this, it causes the repl to use imports
Expand Down Expand Up @@ -223,28 +223,29 @@ export const addQwikLibs = async (version: string) => {

const loadDeps = async (qwikVersion: string) => {
const [M, m, p] = qwikVersion.split('-')[0].split('.').map(Number);
const isV1 = M < 2;
const prefix =
qwikVersion === 'bundled' || M > 1 || (M == 1 && (m > 7 || (m == 7 && p >= 2)))
? '/dist/'
: '/';
const deps: NodeModuleDep[] = [
// qwik
{
pkgName: '@qwik.dev/core',
pkgName: isV1 ? QWIK_PKG_NAME_V1 : QWIK_PKG_NAME,
pkgVersion: qwikVersion,
pkgPath: `${prefix}core.d.ts`,
import: '',
},
// server API
{
pkgName: '@qwik.dev/core',
pkgName: isV1 ? QWIK_PKG_NAME_V1 : QWIK_PKG_NAME,
pkgVersion: qwikVersion,
pkgPath: `${prefix}server.d.ts`,
import: '/server',
},
// build constants
{
pkgName: '@qwik.dev/core',
pkgName: isV1 ? QWIK_PKG_NAME_V1 : QWIK_PKG_NAME,
pkgVersion: qwikVersion,
pkgPath: `${prefix}build/index.d.ts`,
import: '/build',
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/src/repl/repl-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export const getReplVersion = async (version: string | undefined, offline: boole
});

versions.unshift('bundled');
if (!hasVersion || !version) {
if ((!offline && !hasVersion) || !version) {
version = 'bundled';
}

Expand Down
19 changes: 10 additions & 9 deletions packages/docs/src/repl/repl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
useVisibleTask$,
} from '@qwik.dev/core';
import { isServer } from '@qwik.dev/core/build';
import { QWIK_PKG_NAME, bundled, getNpmCdnUrl } from './bundled';
import { QWIK_PKG_NAME, QWIK_PKG_NAME_V1, bundled, getNpmCdnUrl } from './bundled';
import { ReplDetailPanel } from './repl-detail-panel';
import { ReplInputPanel } from './repl-input-panel';
import { ReplOutputPanel } from './repl-output-panel';
Expand Down Expand Up @@ -80,10 +80,6 @@ export const Repl = component$((props: ReplProps) => {

useVisibleTask$(
async () => {
if (isServer) {
return;
}
// only run on the client
// Get the version asap, most likely it will be cached.
const v = await getReplVersion(input.version, true);
store.versions = v.versions;
Expand Down Expand Up @@ -170,9 +166,14 @@ const getDependencies = (input: ReplAppInput) => {
if (input.version !== 'bundled') {
const [M, m, p] = input.version.split('-')[0].split('.').map(Number);
const prefix = M > 1 || (M == 1 && (m > 7 || (m == 7 && p >= 2))) ? '/dist/' : '/';
out[QWIK_PKG_NAME] = {
version: input.version,
};
// we must always provide the qwik.dev package so the worker can find the version
out[QWIK_PKG_NAME] = { version: input.version };
let bundles;
if (M < 2) {
bundles = out[QWIK_PKG_NAME_V1] = { version: input.version };
} else {
bundles = out[QWIK_PKG_NAME];
}
for (const p of [
`${prefix}core.cjs`,
`${prefix}core.mjs`,
Expand All @@ -182,7 +183,7 @@ const getDependencies = (input: ReplAppInput) => {
`/bindings/qwik.wasm.cjs`,
`/bindings/qwik_wasm_bg.wasm`,
]) {
out[QWIK_PKG_NAME][p] = getNpmCdnUrl(bundled, QWIK_PKG_NAME, input.version, p);
bundles[p] = getNpmCdnUrl(bundled, QWIK_PKG_NAME, input.version, p);
}
}
return out;
Expand Down
1 change: 1 addition & 0 deletions packages/docs/src/repl/worker/repl-constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const QWIK_PKG_NAME = '@qwik.dev/core';
export const QWIK_PKG_NAME_V1 = '@builder.io/qwik';

export const QWIK_REPL_DEPS_CACHE = 'QwikReplDeps';
export const QWIK_REPL_RESULT_CACHE = 'QwikReplResults';
30 changes: 17 additions & 13 deletions packages/docs/src/repl/worker/repl-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
/* eslint-disable no-console */
import type { ReplInputOptions } from '../types';
import { QWIK_PKG_NAME, QWIK_REPL_DEPS_CACHE } from './repl-constants';
import { QWIK_PKG_NAME, QWIK_PKG_NAME_V1, QWIK_REPL_DEPS_CACHE } from './repl-constants';
import type { QwikWorkerGlobal } from './repl-service-worker';

let options: ReplInputOptions;
let cache: Cache;

export const depResponse = async (pkgName: string, pkgPath: string) => {
let dep = options.deps[pkgName];
if (pkgName === QWIK_PKG_NAME) {
const version = options.deps[pkgName].version;
const version = dep.version;
const [M, m, p] = version.split('-')[0].split('.').map(Number);
if (!pkgPath.startsWith('/bindings')) {
const [M, m, p] = version.split('-')[0].split('.').map(Number);
if (M > 1 || (M == 1 && (m > 7 || (m == 7 && p >= 2)))) {
pkgPath = `/dist${pkgPath}`;
}
if (version < '2') {
pkgName = '@builder.io/qwik';
}
}
if (version < '2') {
pkgName = QWIK_PKG_NAME_V1;
dep = options.deps[pkgName];
}
}
const url = options.deps[pkgName][pkgPath];
const url = dep[pkgPath];
if (!url) {
throw new Error(`No URL given for dep: ${pkgName}${pkgPath}`);
}
Expand Down Expand Up @@ -78,27 +80,29 @@ const _loadDependencies = async (replOptions: ReplInputOptions) => {
if (!isSameQwikVersion(self.qwikCore?.version)) {
await exec(QWIK_PKG_NAME, '/core.cjs');
if (self.qwikCore) {
console.debug(`Loaded @qwik.dev/core: ${self.qwikCore.version}`);
console.debug(`Loaded ${QWIK_PKG_NAME}: ${self.qwikCore.version}`);
} else {
throw new Error(`Unable to load @qwik.dev/core ${qwikVersion}`);
throw new Error(`Unable to load ${QWIK_PKG_NAME} ${qwikVersion}`);
}
}

if (!isSameQwikVersion(self.qwikOptimizer?.versions.qwik)) {
await exec(QWIK_PKG_NAME, '/optimizer.cjs');
if (self.qwikOptimizer) {
console.debug(`Loaded @qwik.dev/core/optimizer: ${self.qwikOptimizer.versions.qwik}`);
console.debug(`Loaded ${QWIK_PKG_NAME}/optimizer: ${self.qwikOptimizer.versions.qwik}`);
} else {
throw new Error(`Unable to load @qwik.dev/core/optimizer ${qwikVersion}`);
throw new Error(`Unable to load ${QWIK_PKG_NAME}/optimizer ${qwikVersion}`);
}
}

if (!isSameQwikVersion(self.qwikServer?.versions.qwik)) {
// clear out the require shim that is version specific
(globalThis as any).require = undefined;
await exec(QWIK_PKG_NAME, '/server.cjs');
if (self.qwikServer) {
console.debug(`Loaded @qwik.dev/core/server: ${self.qwikServer.versions.qwik}`);
console.debug(`Loaded ${QWIK_PKG_NAME}/server: ${self.qwikServer.versions.qwik}`);
} else {
throw new Error(`Unable to load @qwik.dev/core/server ${qwikVersion}`);
throw new Error(`Unable to load ${QWIK_PKG_NAME}/server ${qwikVersion}`);
}
}

Expand Down
18 changes: 9 additions & 9 deletions packages/docs/src/repl/worker/repl-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ export const replResolver = (options: ReplInputOptions, buildMode: 'client' | 's
if (!importer) {
return id;
}
if (
id === '@qwik.dev/core' ||
id === '@qwik.dev/core/jsx-runtime' ||
id === '@qwik.dev/core/jsx-dev-runtime'
) {
return '\0qwikCore';
}
if (id === '@builder.io/qwik/server' || id === '@qwik.dev/core/server') {
return '\0qwikServer';
const match = id.match(/(@builder\.io\/qwik|@qwik\.dev\/core)(.*)/);
if (match) {
const pkgPath = match[2];
if (pkgPath === '/server') {
return '\0qwikServer';
}
if (/^(|jsx(-dev)?-runtime)$/.test(pkgPath)) {
return '\0qwikCore';
}
}
// Simple relative file resolution
if (id.startsWith('./')) {
Expand Down

0 comments on commit 80f6fbd

Please sign in to comment.