feat(services): introduce DeesServiceLibLoader to lazy-load heavy client libraries from CDN and update components to use it

This commit is contained in:
2026-01-01 20:25:05 +00:00
parent 2a6457e192
commit d7f3594dd4
12 changed files with 410 additions and 39 deletions

View File

@@ -7,9 +7,10 @@ import {
css,
cssManager,
} from '@design.estate/dees-element';
import { Terminal } from 'xterm';
import { FitAddon } from 'xterm-addon-fit';
import type { Terminal } from 'xterm';
import type { FitAddon } from 'xterm-addon-fit';
import { themeDefaultStyles } from '../../00theme.js';
import { DeesServiceLibLoader } from '../../../services/index.js';
declare global {
interface HTMLElementTagNameMap {
@@ -305,8 +306,15 @@ export class DeesWorkspaceTerminalPreview extends DeesElement {
const domtoolsInstance = await this.domtoolsPromise;
const isBright = domtoolsInstance.themeManager.goBrightBoolean;
// Create xterm terminal in read-only mode
this.terminal = new Terminal({
// Load xterm from CDN
const libLoader = DeesServiceLibLoader.getInstance();
const [xtermBundle, fitAddonBundle] = await Promise.all([
libLoader.loadXterm(),
libLoader.loadXtermFitAddon(),
]);
// Create xterm terminal in read-only mode using CDN-loaded module
this.terminal = new xtermBundle.Terminal({
convertEol: true,
cursorBlink: false,
disableStdin: true,
@@ -323,7 +331,7 @@ export class DeesWorkspaceTerminalPreview extends DeesElement {
}
});
this.fitAddon = new FitAddon();
this.fitAddon = new fitAddonBundle.FitAddon();
this.terminal.loadAddon(this.fitAddon);
this.terminal.open(container);
this.fitAddon.fit();

View File

@@ -10,8 +10,7 @@ import {
} from '@design.estate/dees-element';
import * as domtools from '@design.estate/dees-domtools';
import { Terminal } from 'xterm';
import { FitAddon } from 'xterm-addon-fit';
import type { Terminal } from 'xterm';
import { themeDefaultStyles } from '../../00theme.js';
import type { IExecutionEnvironment } from '../../00group-runtime/index.js';
import { WebContainerEnvironment } from '../../00group-runtime/index.js';
@@ -24,6 +23,7 @@ import type {
ICreateTerminalTabOptions,
TTerminalTabType,
} from './interfaces.js';
import { DeesServiceLibLoader } from '../../../services/index.js';
declare global {
interface HTMLElementTagNameMap {
@@ -495,6 +495,16 @@ export class DeesWorkspaceTerminal extends DeesElement {
}
);
// Load xterm from CDN
const libLoader = DeesServiceLibLoader.getInstance();
const [xtermBundle, fitAddonBundle] = await Promise.all([
libLoader.loadXterm(),
libLoader.loadXtermFitAddon(),
]);
// Initialize tab manager with loaded modules
this.tabManager.setXtermModules(xtermBundle, fitAddonBundle);
// Create default shell tab
await this.createShellTab();
}

View File

@@ -1,6 +1,7 @@
import { Terminal } from 'xterm';
import { FitAddon } from 'xterm-addon-fit';
import type { Terminal, ITerminalOptions } from 'xterm';
import type { FitAddon } from 'xterm-addon-fit';
import type { ITerminalTab, ICreateTerminalTabOptions, TTerminalTabType } from './interfaces.js';
import type { IXtermBundle, IXtermFitAddonBundle } from '../../../services/index.js';
/**
* Manages terminal tabs lifecycle and state
@@ -8,6 +9,17 @@ import type { ITerminalTab, ICreateTerminalTabOptions, TTerminalTabType } from '
export class TerminalTabManager {
private tabs: Map<string, ITerminalTab> = new Map();
private tabCounter: number = 0;
private xtermBundle: IXtermBundle | null = null;
private xtermFitAddonBundle: IXtermFitAddonBundle | null = null;
/**
* Initialize the manager with loaded xterm modules.
* Must be called before creating tabs.
*/
public setXtermModules(xtermBundle: IXtermBundle, fitAddonBundle: IXtermFitAddonBundle): void {
this.xtermBundle = xtermBundle;
this.xtermFitAddonBundle = fitAddonBundle;
}
/**
* Generate unique tab ID
@@ -96,11 +108,15 @@ export class TerminalTabManager {
* Create a new tab instance
*/
createTab(options: ICreateTerminalTabOptions, isBright: boolean): ITerminalTab {
if (!this.xtermBundle || !this.xtermFitAddonBundle) {
throw new Error('TerminalTabManager: xterm modules not initialized. Call setXtermModules() first.');
}
const id = this.generateTabId();
const type = options.type;
// Create xterm.js Terminal instance
const terminal = new Terminal({
// Create xterm.js Terminal instance using CDN-loaded module
const terminal = new this.xtermBundle.Terminal({
convertEol: true,
cursorBlink: true,
theme: this.getTerminalTheme(isBright),
@@ -109,8 +125,8 @@ export class TerminalTabManager {
lineHeight: 1.2,
});
// Create FitAddon
const fitAddon = new FitAddon();
// Create FitAddon using CDN-loaded module
const fitAddon = new this.xtermFitAddonBundle.FitAddon();
terminal.loadAddon(fitAddon);
const tab: ITerminalTab = {