Remove dynamic imports

This commit is contained in:
Gregory Schier
2025-05-15 12:29:45 -07:00
parent b52bf7cd56
commit 3184c1b79e
5 changed files with 77 additions and 81 deletions

View File

@@ -4,7 +4,7 @@ import type { GenericCompletionOption } from '@yaakapp-internal/plugins';
import classNames from 'classnames'; import classNames from 'classnames';
import { atom, useAtomValue } from 'jotai'; import { atom, useAtomValue } from 'jotai';
import type { CSSProperties } from 'react'; import type { CSSProperties } from 'react';
import React, { lazy, useCallback, useMemo, useState } from 'react'; import React, { useCallback, useMemo, useState } from 'react';
import { activeRequestIdAtom } from '../hooks/useActiveRequestId'; import { activeRequestIdAtom } from '../hooks/useActiveRequestId';
import { useCancelHttpResponse } from '../hooks/useCancelHttpResponse'; import { useCancelHttpResponse } from '../hooks/useCancelHttpResponse';
import { useHttpAuthenticationSummaries } from '../hooks/useHttpAuthentication'; import { useHttpAuthenticationSummaries } from '../hooks/useHttpAuthentication';
@@ -46,15 +46,10 @@ import { FormMultipartEditor } from './FormMultipartEditor';
import { FormUrlencodedEditor } from './FormUrlencodedEditor'; import { FormUrlencodedEditor } from './FormUrlencodedEditor';
import { HeadersEditor } from './HeadersEditor'; import { HeadersEditor } from './HeadersEditor';
import { HttpAuthenticationEditor } from './HttpAuthenticationEditor'; import { HttpAuthenticationEditor } from './HttpAuthenticationEditor';
import { MarkdownEditor } from './MarkdownEditor';
import { UrlBar } from './UrlBar'; import { UrlBar } from './UrlBar';
import { UrlParametersEditor } from './UrlParameterEditor'; import { UrlParametersEditor } from './UrlParameterEditor';
import { GraphQLEditor } from './GraphQLEditor';
const GraphQLEditor = lazy(() =>
import('./GraphQLEditor').then((m) => ({ default: m.GraphQLEditor })),
);
const MarkdownEditor = lazy(() =>
import('./MarkdownEditor').then((m) => ({ default: m.MarkdownEditor })),
);
interface Props { interface Props {
style: CSSProperties; style: CSSProperties;

View File

@@ -1,7 +1,7 @@
import type { HttpResponse } from '@yaakapp-internal/models'; import type { HttpResponse } from '@yaakapp-internal/models';
import classNames from 'classnames'; import classNames from 'classnames';
import type { CSSProperties, ReactNode } from 'react'; import type { CSSProperties, ReactNode } from 'react';
import React, { lazy , useCallback, useMemo } from 'react'; import React, { useCallback, useMemo } from 'react';
import { useLocalStorage } from 'react-use'; import { useLocalStorage } from 'react-use';
import { usePinnedHttpResponse } from '../hooks/usePinnedHttpResponse'; import { usePinnedHttpResponse } from '../hooks/usePinnedHttpResponse';
import { useResponseViewMode } from '../hooks/useResponseViewMode'; import { useResponseViewMode } from '../hooks/useResponseViewMode';
@@ -27,10 +27,7 @@ import { CsvViewer } from './responseViewers/CsvViewer';
import { EventStreamViewer } from './responseViewers/EventStreamViewer'; import { EventStreamViewer } from './responseViewers/EventStreamViewer';
import { HTMLOrTextViewer } from './responseViewers/HTMLOrTextViewer'; import { HTMLOrTextViewer } from './responseViewers/HTMLOrTextViewer';
import { ImageViewer } from './responseViewers/ImageViewer'; import { ImageViewer } from './responseViewers/ImageViewer';
import { PdfViewer } from './responseViewers/PdfViewer';
const PdfViewer = lazy(() =>
import('./responseViewers/PdfViewer').then((m) => ({ default: m.PdfViewer })),
);
import { SvgViewer } from './responseViewers/SvgViewer'; import { SvgViewer } from './responseViewers/SvgViewer';
import { VideoViewer } from './responseViewers/VideoViewer'; import { VideoViewer } from './responseViewers/VideoViewer';
import { ErrorBoundary } from './ErrorBoundary'; import { ErrorBoundary } from './ErrorBoundary';

View File

@@ -32,6 +32,7 @@ export function RedirectToLatestWorkspace() {
request_id: requestId, request_id: requestId,
}; };
console.log("Redirecting to workspace", params, search);
await router.navigate({ to: '/workspaces/$workspaceId', params, search }); await router.navigate({ to: '/workspaces/$workspaceId', params, search });
})(); })();
}, [recentWorkspaces, workspaces, workspaces.length]); }, [recentWorkspaces, workspaces, workspaces.length]);

View File

@@ -4,6 +4,7 @@ import type { EditorStateConfig, Extension } from '@codemirror/state';
import { Compartment, EditorState } from '@codemirror/state'; import { Compartment, EditorState } from '@codemirror/state';
import { EditorView, keymap, placeholder as placeholderExt, tooltips } from '@codemirror/view'; import { EditorView, keymap, placeholder as placeholderExt, tooltips } from '@codemirror/view';
import { emacs } from '@replit/codemirror-emacs'; import { emacs } from '@replit/codemirror-emacs';
import { vim } from '@replit/codemirror-vim';
import { vscodeKeymap } from '@replit/codemirror-vscode-keymap'; import { vscodeKeymap } from '@replit/codemirror-vscode-keymap';
import type { EditorKeymap, EnvironmentVariable } from '@yaakapp-internal/models'; import type { EditorKeymap, EnvironmentVariable } from '@yaakapp-internal/models';
@@ -47,8 +48,6 @@ import {
import type { GenericCompletionConfig } from './genericCompletion'; import type { GenericCompletionConfig } from './genericCompletion';
import { singleLineExtensions } from './singleLine'; import { singleLineExtensions } from './singleLine';
const { vim } = await import('@replit/codemirror-vim');
// VSCode's Tab actions mess with the single-line editor tab actions, so remove it. // VSCode's Tab actions mess with the single-line editor tab actions, so remove it.
const vsCodeWithoutTab = vscodeKeymap.filter((k) => k.key !== 'Tab'); const vsCodeWithoutTab = vscodeKeymap.filter((k) => k.key !== 'Tab');
@@ -366,7 +365,7 @@ export const Editor = forwardRef<EditorView | undefined, EditorProps>(function E
useEffect(() => { useEffect(() => {
if (cm.current === null) return; if (cm.current === null) return;
const { view, languageCompartment } = cm.current; const { view, languageCompartment } = cm.current;
getLanguageExtension({ const ext = getLanguageExtension({
useTemplating, useTemplating,
language, language,
environmentVariables, environmentVariables,
@@ -375,9 +374,8 @@ export const Editor = forwardRef<EditorView | undefined, EditorProps>(function E
onClickVariable, onClickVariable,
onClickMissingVariable, onClickMissingVariable,
onClickPathParameter, onClickPathParameter,
}).then((ext) => {
view.dispatch({ effects: languageCompartment.reconfigure(ext) });
}); });
view.dispatch({ effects: languageCompartment.reconfigure(ext) });
}, [ }, [
language, language,
autocomplete, autocomplete,
@@ -401,7 +399,7 @@ export const Editor = forwardRef<EditorView | undefined, EditorProps>(function E
try { try {
const languageCompartment = new Compartment(); const languageCompartment = new Compartment();
getLanguageExtension({ const langExt = getLanguageExtension({
useTemplating, useTemplating,
language, language,
completionOptions, completionOptions,
@@ -410,57 +408,56 @@ export const Editor = forwardRef<EditorView | undefined, EditorProps>(function E
onClickVariable, onClickVariable,
onClickMissingVariable, onClickMissingVariable,
onClickPathParameter, onClickPathParameter,
}).then((langExt) => {
const extensions = [
languageCompartment.of(langExt),
placeholderCompartment.current.of(placeholderExt(placeholderElFromText(placeholder))),
wrapLinesCompartment.current.of(wrapLines ? EditorView.lineWrapping : emptyExtension),
tabIndentCompartment.current.of(
!disableTabIndent ? keymap.of([indentWithTab]) : emptyExtension,
),
keymapCompartment.current.of(
keymapExtensions[settings.editorKeymap] ?? keymapExtensions['default'],
),
...getExtensions({
container,
readOnly,
singleLine,
hideGutter,
stateKey,
onChange: handleChange,
onPaste: handlePaste,
onPasteOverwrite: handlePasteOverwrite,
onFocus: handleFocus,
onBlur: handleBlur,
onKeyDown: handleKeyDown,
}),
...(extraExtensions ?? []),
];
const cachedJsonState = getCachedEditorState(defaultValue ?? '', stateKey);
const doc = `${defaultValue ?? ''}`;
const config: EditorStateConfig = { extensions, doc };
const state = cachedJsonState
? EditorState.fromJSON(cachedJsonState, config, stateFields)
: EditorState.create(config);
const view = new EditorView({ state, parent: container });
// For large documents, the parser may parse the max number of lines and fail to add
// things like fold markers because of it.
// This forces it to parse more but keeps the timeout to the default of 100 ms.
forceParsing(view, 9e6, 100);
cm.current = { view, languageCompartment };
if (autoFocus) {
view.focus();
}
if (autoSelect) {
view.dispatch({ selection: { anchor: 0, head: view.state.doc.length } });
}
}); });
const extensions = [
languageCompartment.of(langExt),
placeholderCompartment.current.of(placeholderExt(placeholderElFromText(placeholder))),
wrapLinesCompartment.current.of(wrapLines ? EditorView.lineWrapping : emptyExtension),
tabIndentCompartment.current.of(
!disableTabIndent ? keymap.of([indentWithTab]) : emptyExtension,
),
keymapCompartment.current.of(
keymapExtensions[settings.editorKeymap] ?? keymapExtensions['default'],
),
...getExtensions({
container,
readOnly,
singleLine,
hideGutter,
stateKey,
onChange: handleChange,
onPaste: handlePaste,
onPasteOverwrite: handlePasteOverwrite,
onFocus: handleFocus,
onBlur: handleBlur,
onKeyDown: handleKeyDown,
}),
...(extraExtensions ?? []),
];
const cachedJsonState = getCachedEditorState(defaultValue ?? '', stateKey);
const doc = `${defaultValue ?? ''}`;
const config: EditorStateConfig = { extensions, doc };
const state = cachedJsonState
? EditorState.fromJSON(cachedJsonState, config, stateFields)
: EditorState.create(config);
const view = new EditorView({ state, parent: container });
// For large documents, the parser may parse the max number of lines and fail to add
// things like fold markers because of it.
// This forces it to parse more but keeps the timeout to the default of 100 ms.
forceParsing(view, 9e6, 100);
cm.current = { view, languageCompartment };
if (autoFocus) {
view.focus();
}
if (autoSelect) {
view.dispatch({ selection: { anchor: 0, head: view.state.doc.length } });
}
} catch (e) { } catch (e) {
console.log('Failed to initialize Codemirror', e); console.log('Failed to initialize Codemirror', e);
} }

View File

@@ -5,6 +5,8 @@ import {
completionKeymap, completionKeymap,
} from '@codemirror/autocomplete'; } from '@codemirror/autocomplete';
import { history, historyKeymap } from '@codemirror/commands'; import { history, historyKeymap } from '@codemirror/commands';
import { javascript } from '@codemirror/lang-javascript';
import { markdown } from '@codemirror/lang-markdown';
import type { LanguageSupport } from '@codemirror/language'; import type { LanguageSupport } from '@codemirror/language';
import { import {
codeFolding, codeFolding,
@@ -37,7 +39,12 @@ import { pluralizeCount } from '../../../lib/pluralize';
import type { EditorProps } from './Editor'; import type { EditorProps } from './Editor';
import { text } from './text/extension'; import { text } from './text/extension';
import type { TwigCompletionOption } from './twig/completion'; import type { TwigCompletionOption } from './twig/completion';
import { twig } from './twig/extension';
import { pathParametersPlugin } from './twig/pathParameters'; import { pathParametersPlugin } from './twig/pathParameters';
import { json } from '@codemirror/lang-json';
import { xml } from '@codemirror/lang-xml';
import { pairs } from './pairs/extension';
import { url } from './url/extension';
export const syntaxHighlightStyle = HighlightStyle.define([ export const syntaxHighlightStyle = HighlightStyle.define([
{ {
@@ -70,23 +77,23 @@ const closeBracketsExtensions: Extension = [closeBrackets(), keymap.of([...close
const syntaxExtensions: Record< const syntaxExtensions: Record<
NonNullable<EditorProps['language']>, NonNullable<EditorProps['language']>,
null | (() => Promise<LanguageSupport>) null | (() => LanguageSupport)
> = { > = {
graphql: null, graphql: null,
json: () => import('@codemirror/lang-json').then((m) => m.json()), json: json,
javascript: () => import('@codemirror/lang-javascript').then((m) => m.javascript()), javascript: javascript,
// HTML as XML because HTML is oddly slow // HTML as XML because HTML is oddly slow
html: () => import('@codemirror/lang-xml').then((m) => m.xml()), html: xml,
xml: () => import('@codemirror/lang-xml').then((m) => m.xml()), xml: xml,
url: () => import('./url/extension').then((m) => m.url()), url: url,
pairs: () => import('./pairs/extension').then((m) => m.pairs()), pairs: pairs,
text: () => import('./text/extension').then((m) => m.text()), text: text,
markdown: () => import('@codemirror/lang-markdown').then((m) => m.markdown()), markdown: markdown,
}; };
const closeBracketsFor: (keyof typeof syntaxExtensions)[] = ['json', 'javascript', 'graphql']; const closeBracketsFor: (keyof typeof syntaxExtensions)[] = ['json', 'javascript', 'graphql'];
export async function getLanguageExtension({ export function getLanguageExtension({
useTemplating, useTemplating,
language = 'text', language = 'text',
environmentVariables, environmentVariables,
@@ -120,13 +127,12 @@ export async function getLanguageExtension({
} }
const base_ = syntaxExtensions[language ?? 'text'] ?? text(); const base_ = syntaxExtensions[language ?? 'text'] ?? text();
const base = typeof base_ === 'function' ? await base_() : text(); const base = typeof base_ === 'function' ? base_() : text();
if (!useTemplating) { if (!useTemplating) {
return [base, extraExtensions]; return [base, extraExtensions];
} }
const { twig } = await import('./twig/extension');
return twig({ return twig({
base, base,
environmentVariables, environmentVariables,