Updates Monaco editor path handling for production builds

Configures the editor to load Monaco assets from a local directory in production, improving reliability and performance by avoiding CDN usage.
Adds prebuild script to copy Monaco files, and updates ignore rules to exclude copied assets from version control.
This commit is contained in:
LAPTOP-O016UC3M\Qi Chen
2026-01-08 10:34:49 +08:00
parent 131553128a
commit 044165319e
4 changed files with 22 additions and 1 deletions

1
.gitignore vendored
View File

@@ -22,6 +22,7 @@ coverage
/release
/out
*.asar
/public/monaco
# Editor directories and files
.vscode/*

View File

@@ -12,7 +12,10 @@ import type * as Monaco from 'monaco-editor';
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
// Configure Monaco to use local files instead of CDN
loader.config({ paths: { vs: './node_modules/monaco-editor/min/vs' } });
const monacoBasePath = import.meta.env.DEV
? './node_modules/monaco-editor/min/vs'
: `${import.meta.env.BASE_URL}monaco/vs`;
loader.config({ paths: { vs: monacoBasePath } });
import { useI18n } from '../application/i18n/I18nProvider';
import { getLanguageId, getLanguageName, getSupportedLanguages } from '../lib/sftpFileUtils';

View File

@@ -10,6 +10,7 @@
"scripts": {
"dev": "npm run lint && concurrently -k \"vite\" \"npm:dev:electron\"",
"dev:electron": "wait-on http-get://localhost:5173 && cross-env VITE_DEV_SERVER_URL=http://localhost:5173 node electron/launch.cjs",
"prebuild": "node scripts/copy-monaco.cjs",
"build": "vite build",
"preview": "vite preview",
"start": "node electron/launch.cjs",

16
scripts/copy-monaco.cjs Normal file
View File

@@ -0,0 +1,16 @@
const fs = require('fs');
const path = require('path');
const repoRoot = path.resolve(__dirname, '..');
const source = path.join(repoRoot, 'node_modules', 'monaco-editor', 'min', 'vs');
const target = path.join(repoRoot, 'public', 'monaco', 'vs');
if (!fs.existsSync(source)) {
console.error('[copy-monaco] Source not found:', source);
process.exit(1);
}
fs.rmSync(target, { recursive: true, force: true });
fs.mkdirSync(path.dirname(target), { recursive: true });
fs.cpSync(source, target, { recursive: true });
console.log('[copy-monaco] Copied Monaco VS assets to', target);