diff --git a/src/App.tsx b/src/App.tsx index bd3cd3eb..b2a25377 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -43,6 +43,7 @@ const defaultConfig: AppConfig = { theme: "system", relayUrl: "wss://relay.primal.net", deployServer: "shakespeare.wtf", + esmUrl: "https://esm.shakespeare.diy", }; const presetRelays = [ diff --git a/src/AppRouter.tsx b/src/AppRouter.tsx index 5558d2db..c143e500 100644 --- a/src/AppRouter.tsx +++ b/src/AppRouter.tsx @@ -9,6 +9,7 @@ import Preferences from "./pages/Preferences"; import NostrSettings from "./pages/NostrSettings"; import AISettings from "./pages/AISettings"; import GitSettings from "./pages/GitSettings"; +import SystemSettings from "./pages/SystemSettings"; import DataSettings from "./pages/DataSettings"; import AboutSettings from "./pages/AboutSettings"; @@ -33,6 +34,7 @@ export function AppRouter() { } /> } /> } /> + } /> } /> } /> diff --git a/src/components/AppProvider.tsx b/src/components/AppProvider.tsx index b293fa20..1e6837b0 100644 --- a/src/components/AppProvider.tsx +++ b/src/components/AppProvider.tsx @@ -19,6 +19,7 @@ const AppConfigSchema: z.ZodType = z.object({ theme: z.enum(['dark', 'light', 'system']), relayUrl: z.string().url(), deployServer: z.string().min(1), + esmUrl: z.string().url(), language: z.string().optional(), }); diff --git a/src/contexts/AppContext.ts b/src/contexts/AppContext.ts index 28e9cb39..11e037fa 100644 --- a/src/contexts/AppContext.ts +++ b/src/contexts/AppContext.ts @@ -9,6 +9,8 @@ export interface AppConfig { relayUrl: string; /** Deploy server domain (e.g., "shakespeare.wtf") */ deployServer: string; + /** ESM CDN URL for package imports */ + esmUrl: string; /** Selected language */ language?: string; } diff --git a/src/hooks/useBuildProject.ts b/src/hooks/useBuildProject.ts index 3fd705db..6cb96e2b 100644 --- a/src/hooks/useBuildProject.ts +++ b/src/hooks/useBuildProject.ts @@ -1,9 +1,11 @@ import { useMutation } from "@tanstack/react-query"; import { buildProject } from "@/lib/build"; import { useFS } from "./useFS"; +import { useAppContext } from "./useAppContext"; export function useBuildProject(projectId: string) { const { fs } = useFS(); + const { config } = useAppContext(); return useMutation({ mutationFn: async () => { @@ -11,6 +13,7 @@ export function useBuildProject(projectId: string) { fs, projectPath: `/projects/${projectId}`, domParser: new DOMParser(), + esmUrl: config.esmUrl, }); }, }); diff --git a/src/lib/build/esmPlugin.ts b/src/lib/build/esmPlugin.ts index 9c2da0d0..ffcd563e 100644 --- a/src/lib/build/esmPlugin.ts +++ b/src/lib/build/esmPlugin.ts @@ -23,10 +23,11 @@ interface EsmPluginOptions { packageJson: PackageJson; packageLock?: PackageLock; target?: string; + esmUrl: string; } export function esmPlugin(options: EsmPluginOptions): Plugin { - const { packageJson, packageLock, target } = options; + const { packageJson, packageLock, target, esmUrl } = options; /** Cache of URL redirects */ const redirectedURLs = new Map(); @@ -222,7 +223,7 @@ export function esmPlugin(options: EsmPluginOptions): Plugin { switch (moduleName) { case "buffer": return { - path: `https://esm.sh/${moduleName}?target=${target ?? "esnext"}`, + path: `${esmUrl}/${moduleName}?target=${target ?? "esnext"}`, namespace: "esm", }; default: @@ -323,7 +324,7 @@ export function esmPlugin(options: EsmPluginOptions): Plugin { ? `${cdnName}@${version}${packagePath}` : `${cdnName}${packagePath}`; - const url = new URL(`https://esm.sh/*${specifier}`); + const url = new URL(`${esmUrl}/*${specifier}`); if (target) url.searchParams.set("target", target); if (childLockPath) url.searchParams.set("lp", encodeURIComponent(childLockPath)); @@ -441,9 +442,9 @@ export function esmPlugin(options: EsmPluginOptions): Plugin { }; } -/** Extract package name from esm.sh-style URL */ -function extractPackageName(esmShURL: string): { pkg?: string; version?: string } { - const full = new URL(esmShURL) +/** Extract package name from ESM CDN-style URL */ +function extractPackageName(esmURL: string): { pkg?: string; version?: string } { + const full = new URL(esmURL) .pathname .slice(1) .replace(/^\*/, ""); diff --git a/src/lib/build/index.test.ts b/src/lib/build/index.test.ts index 865ece6f..f56a05ca 100644 --- a/src/lib/build/index.test.ts +++ b/src/lib/build/index.test.ts @@ -161,6 +161,7 @@ describe('buildProject', () => { fs: mockFS, projectPath: '/test/project', domParser: mockDOMParser, + esmUrl: 'https://esm.shakespeare.diy', }); // Should include built files with hashed names @@ -220,6 +221,7 @@ describe('buildProject', () => { fs: mockFS, projectPath: '/test/project', domParser: mockDOMParser, + esmUrl: 'https://esm.shakespeare.diy', }); // Should still include built files with hashed names @@ -278,6 +280,7 @@ describe('buildProject', () => { fs: mockFS, projectPath: '/test/project', domParser: mockDOMParser, + esmUrl: 'https://esm.shakespeare.diy', }); // Should include built files with hashed names @@ -348,6 +351,7 @@ react@^18.0.0: fs: mockFS, projectPath: '/test/project', domParser: mockDOMParser, + esmUrl: 'https://esm.shakespeare.diy', }); // Should successfully build with yarn.lock @@ -398,6 +402,7 @@ react@^18.0.0: fs: mockFS, projectPath: '/test/project', domParser: mockDOMParser, + esmUrl: 'https://esm.shakespeare.diy', }); // Should successfully build with empty packages object @@ -457,6 +462,7 @@ react@^18.0.0: fs: mockFS, projectPath: '/test/project', domParser: mockDOMParser, + esmUrl: 'https://esm.shakespeare.diy', }); // Should successfully build using package.json dependencies @@ -519,6 +525,7 @@ react@^18.0.0: fs: mockFS, projectPath: '/test/project', domParser: mockDOMParser, + esmUrl: 'https://esm.shakespeare.diy', }); // Should successfully build with tsconfig.json diff --git a/src/lib/build/index.ts b/src/lib/build/index.ts index 933c36f1..23ceea44 100644 --- a/src/lib/build/index.ts +++ b/src/lib/build/index.ts @@ -17,14 +17,14 @@ export interface BuildProjectOptions { domParser: DOMParser; target?: string; outputPath?: string; + esmUrl: string; } async function bundle( options: BuildProjectOptions, ): Promise> { - const esbuild = await getEsbuild(); - - const { fs, projectPath, domParser, target = "esnext" } = options; + const { fs, projectPath, domParser, target = "esnext", esmUrl } = options; + const esbuild = await getEsbuild(esmUrl); const indexHtmlText = await fs.readFile( `${projectPath}/index.html`, @@ -124,9 +124,9 @@ async function bundle( chunkNames: "[name]-[hash]", assetNames: "[name]-[hash]", plugins: [ - shakespearePlugin(), + shakespearePlugin({ esmUrl }), fsPlugin({ fs, cwd: projectPath, tsconfig }), - esmPlugin({ packageJson, packageLock, target }), + esmPlugin({ packageJson, packageLock, target, esmUrl }), ], define: { "import.meta.env": JSON.stringify({}), @@ -161,12 +161,12 @@ async function bundle( } } - // Parse CSP and ensure esm.sh is allowed for necessary directives + // Parse CSP and ensure ESM CDN is allowed for necessary directives const cspMeta = doc.querySelector("meta[http-equiv=\"content-security-policy\"]"); if (cspMeta) { const cspContent = cspMeta.getAttribute("content"); if (cspContent) { - const updatedCSP = updateCSPForEsmSh(cspContent); + const updatedCSP = updateCSPForEsmSh(cspContent, esmUrl); cspMeta.setAttribute("content", updatedCSP); } } @@ -261,16 +261,16 @@ async function fileExists(fs: JSRuntimeFS, path: string): Promise { } } -/** Updates CSP to allow esm.sh for scripts, assets, fonts, and CSS */ -export function updateCSPForEsmSh(csp: string): string { - const esmShDirectives = [ +/** Updates CSP to allow ESM CDN for scripts, assets, fonts, and CSS */ +export function updateCSPForEsmSh(csp: string, esmUrl: string): string { + const esmDirectives = [ 'script-src', // For JavaScript modules 'img-src', // For images 'media-src', // For video/audio 'font-src', // For fonts 'style-src', // For CSS - 'connect-src' // For fetch/XHR requests to esm.sh + 'connect-src' // For fetch/XHR requests to ESM CDN ]; - return addDomainToCSP(csp, 'https://esm.sh', esmShDirectives); + return addDomainToCSP(csp, esmUrl, esmDirectives); } \ No newline at end of file diff --git a/src/lib/build/shakespearePlugin.ts b/src/lib/build/shakespearePlugin.ts index ec272afc..bf447250 100644 --- a/src/lib/build/shakespearePlugin.ts +++ b/src/lib/build/shakespearePlugin.ts @@ -1,11 +1,15 @@ import type { Plugin } from "esbuild-wasm"; +interface ShakespearePluginOptions { + esmUrl: string; +} + /** * Shakespeare plugin for dependency polyfills and workarounds. * This plugin handles specific dependencies that need custom implementations * or dummy APIs to work correctly in the browser environment. */ -export function shakespearePlugin(): Plugin { +export function shakespearePlugin(options?: ShakespearePluginOptions): Plugin { return { name: "shakespeare", @@ -23,7 +27,7 @@ export function shakespearePlugin(): Plugin { return { contents: ` import tailwindConfig from "@/../${pathname}"; - import "https://esm.sh/tailwindcss-cdn@3"; + import "${options?.esmUrl}/tailwindcss-cdn@3"; tailwind.config = tailwindConfig; `, loader: "js", diff --git a/src/lib/esbuild.ts b/src/lib/esbuild.ts index 9ee838c4..b051c1e9 100644 --- a/src/lib/esbuild.ts +++ b/src/lib/esbuild.ts @@ -4,19 +4,19 @@ type ESBuild = typeof _ESBuild; let promise: Promise | undefined; -export function getEsbuild(): Promise { +export function getEsbuild(esmUrl: string): Promise { if (!promise) { - promise = loadEsbuild(); + promise = loadEsbuild(esmUrl); } return promise; } -async function loadEsbuild(): Promise { +async function loadEsbuild(esmUrl: string): Promise { const { default: esbuild } = await import("esbuild-wasm"); await esbuild.initialize({ worker: false, - wasmURL: "https://esm.sh/esbuild-wasm@0.25.8/esbuild.wasm", + wasmURL: `${esmUrl}/esbuild-wasm@0.25.8/esbuild.wasm`, }); return esbuild; diff --git a/src/lib/i18n.ts b/src/lib/i18n.ts index 6a0256d0..14cc92ee 100644 --- a/src/lib/i18n.ts +++ b/src/lib/i18n.ts @@ -27,6 +27,12 @@ const resources = { language: 'Language', languageDescription: 'Select your preferred language for the interface.', + // System Settings Section + systemSettings: 'System Settings', + systemSettingsDescription: 'Configure system-level settings and advanced options.', + esmUrl: 'ESM CDN URL', + esmUrlDescription: 'Configure the ESM CDN URL for package imports. Default: https://esm.shakespeare.diy', + // Language Options english: 'English', portuguese: 'Portuguese', @@ -338,6 +344,12 @@ const resources = { language: 'Idioma', languageDescription: 'Selecione seu idioma preferido para a interface.', + // System Settings Section + systemSettings: 'Configurações do Sistema', + systemSettingsDescription: 'Configure configurações de nível de sistema e opções avançadas.', + esmUrl: 'URL do CDN ESM', + esmUrlDescription: 'Configure a URL do CDN ESM para importações de pacotes. Padrão: https://esm.shakespeare.diy', + // Language Options english: 'Inglês', portuguese: 'Português', @@ -649,6 +661,12 @@ const resources = { language: '语言', languageDescription: '选择您的界面首选语言。', + // System Settings Section + systemSettings: '系统设置', + systemSettingsDescription: '配置系统级设置和高级选项。', + esmUrl: 'ESM CDN URL', + esmUrlDescription: '配置包导入的 ESM CDN URL。默认:https://esm.shakespeare.diy', + // Language Options english: 'English', portuguese: 'Português', @@ -960,6 +978,12 @@ const resources = { language: 'Harshe', languageDescription: 'Zaɓi harshen da kuke so don dubawa.', + // System Settings Section + systemSettings: 'Saitunan Tsarin', + systemSettingsDescription: 'Saita saitunan matakin tsari da zaɓuɓɓuka na ci gaba.', + esmUrl: 'URL na CDN ESM', + esmUrlDescription: 'Saita URL na CDN ESM don shigo da fakitin. Tsoho: https://esm.shakespeare.diy', + // Language Options english: 'Turanci', portuguese: 'Fotigis', @@ -1271,6 +1295,12 @@ const resources = { language: 'Ede', languageDescription: 'Yan ede ti o fẹran fun wiwo.', + // System Settings Section + systemSettings: 'Saitunan Eto', + systemSettingsDescription: 'Tunto awọn saitan eto ati awọn ayanfẹ ti o pọju.', + esmUrl: 'URL CDN ESM', + esmUrlDescription: 'Tunto URL CDN ESM fun gbigbe awọn package. Aiyipada: https://esm.shakespeare.diy', + // Language Options english: 'Gẹẹsi', portuguese: 'Potogisi', @@ -1577,6 +1607,12 @@ const resources = { language: 'Asụsụ', languageDescription: 'Họrọ asụsụ ị chọrọ maka interface.', + // System Settings Section + systemSettings: 'Ntọala Sistemu', + systemSettingsDescription: 'Hazie ntọala ọkwa sistemu na nhọrọ ndị ọganihu.', + esmUrl: 'URL CDN ESM', + esmUrlDescription: 'Hazie URL CDN ESM maka mbubata ngwugwu. Default: https://esm.shakespeare.diy', + // Language Options english: 'Bekee', portuguese: 'Portuguese', diff --git a/src/lib/tools/BuildProjectTool.ts b/src/lib/tools/BuildProjectTool.ts index 979a3526..2a4f1ad9 100644 --- a/src/lib/tools/BuildProjectTool.ts +++ b/src/lib/tools/BuildProjectTool.ts @@ -17,6 +17,7 @@ export class BuildProjectTool implements Tool { try { // Build and write the project files const result = await buildProject({ + esmUrl: 'https://esm.shakespeare.diy', fs: this.fs, projectPath: this.cwd, domParser: new DOMParser(), diff --git a/src/pages/Settings.tsx b/src/pages/Settings.tsx index cf978600..74cd1b7f 100644 --- a/src/pages/Settings.tsx +++ b/src/pages/Settings.tsx @@ -1,6 +1,6 @@ import { useNavigate } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; -import { Bot, GitBranch, Database, Wifi, Settings2, Info } from 'lucide-react'; +import { Bot, GitBranch, Database, Wifi, Settings2, Info, Cog } from 'lucide-react'; import { useIsMobile } from '@/hooks/useIsMobile'; interface SettingsItem { @@ -41,6 +41,13 @@ const getSettingsItems = (t: (key: string) => string): SettingsItem[] => [ icon: Wifi, href: '/settings/nostr', }, + { + id: 'system', + title: t('systemSettings'), + description: t('systemSettingsDescription'), + icon: Cog, + href: '/settings/system', + }, { id: 'data', title: t('dataSettings'), diff --git a/src/pages/SystemSettings.tsx b/src/pages/SystemSettings.tsx new file mode 100644 index 00000000..756ed080 --- /dev/null +++ b/src/pages/SystemSettings.tsx @@ -0,0 +1,80 @@ +import { Settings2, ArrowLeft, Globe } from "lucide-react"; +import { useTranslation } from 'react-i18next'; +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; + +import { Label } from "@/components/ui/label"; +import { useIsMobile } from "@/hooks/useIsMobile"; +import { useNavigate } from "react-router-dom"; +import { useAppContext } from "@/hooks/useAppContext"; + +export function SystemSettings() { + const { t } = useTranslation(); + const isMobile = useIsMobile(); + const navigate = useNavigate(); + const { config, updateConfig } = useAppContext(); + + return ( +
+ {isMobile && ( +
+ +
+

+ + {t('systemSettings')} +

+

+ {t('systemSettingsDescription')} +

+
+
+ )} + + {!isMobile && ( +
+

+ + {t('systemSettings')} +

+

+ {t('systemSettingsDescription')} +

+
+ )} + +
+
+ + updateConfig((current) => ({ + ...current, + esmUrl: e.target.value, + }))} + className="w-full max-w-xs" + placeholder="https://esm.shakespeare.diy" + /> +

+ {t('esmUrlDescription')} +

+
+
+
+ ); +} + +export default SystemSettings; \ No newline at end of file diff --git a/src/test/TestApp.tsx b/src/test/TestApp.tsx index e6909575..357ff959 100644 --- a/src/test/TestApp.tsx +++ b/src/test/TestApp.tsx @@ -31,6 +31,7 @@ export function TestApp({ children }: TestAppProps) { theme: 'light', relayUrl: 'wss://relay.nostr.band', deployServer: 'shakespeare.wtf', + esmUrl: 'https://esm.sh', language: 'en', };