Файли
openidea/vite.config.ts
T
Deng Liuтаdengliu 85cbc6bacd feat(mdx): add side-by-side MDX preview with opt-in sandboxed JSX execution (#6881)
Adds Editor Only / Editor and Preview / Preview Only layouts for .mdx files.

- Safe Static Mode on open: frontmatter, GFM, math, Mermaid, MDX comments and documentation tags (Steps, CardGroup, Note, Tabs, Accordion) render through the sanitized Markdown pipeline with scroll sync and no script execution.
- Run JSX (or Trust Project in Restricted Mode) lazy-loads @mdx-js/mdx and sucrase and evaluates JSX, workspace/tsconfig/node_modules imports, .mdx transclusions, stylesheets, assets and custom layouts inside an opaque-origin sandboxed iframe with its own CSP, no network and no IPC.
- The host-guest bridge is a per-document MessagePort; image, media, module and link resolution stay inside the window's project roots, checked lexically and after canonicalization (new fs_canonicalize command, which rejects parent traversal).
- Execution requires a current, affirmative trust result for the file's owning project, including attached workspace members, whose trust changes now emit ProjectTrustChanged.
- External links open only from a real click on an anchor in the frame and always go through a confirmation dialog showing the parsed destination.
- Settings > Languages & Frameworks > Markdown adds MDX component execution (Ask before running / Run automatically in trusted projects / Disabled), a user-only setting that project settings cannot enable.

Closes #6876

Co-authored-by: dengliu <[email protected]>
2026-12-02 08:39:22 +08:00

133 рядки
4.0 KiB
TypeScript

/// <reference types="vitest/config" />
import { build as viteBuild, defineConfig, type Plugin } from "vite";
import react from "@vitejs/plugin-react";
import path from "node:path";
import { fileURLToPath } from "node:url";
import process from "node:process";
const host = process.env.TAURI_DEV_HOST;
const webServer = process.env.OPENIDEA_WEB_SERVER;
const VIRTUAL_MDX_GUEST = "virtual:mdx-guest-bundle";
const RESOLVED_MDX_GUEST = `\0${VIRTUAL_MDX_GUEST}`;
function mdxGuestBundlePlugin(): Plugin {
let cachedBundlePromise: Promise<string> | null = null;
const guestDir = path.resolve(import.meta.dirname, "src/features/viewers/markdown");
const guestEntry = path.join(guestDir, "mdxGuest.tsx");
const buildGuestIife = () => {
if (!cachedBundlePromise) {
cachedBundlePromise = viteBuild({
configFile: false,
mode: "production",
logLevel: "silent",
oxc: {
jsx: {
runtime: "automatic",
development: false,
},
},
define: {
"process.env.NODE_ENV": JSON.stringify("production"),
},
build: {
write: false,
target: "safari15.4",
minify: "oxc",
sourcemap: false,
lib: {
entry: guestEntry,
name: "__OpenIdeaMdxGuest",
formats: ["iife"],
fileName: () => "mdx-guest.js",
},
},
}).then((output) => {
const roll = Array.isArray(output) ? output[0] : output;
if (!roll || !("output" in roll)) {
throw new Error("Failed to bundle mdxGuest.tsx");
}
const chunk = roll.output.find((item) => item.type === "chunk");
if (!chunk) throw new Error("No JS chunk produced for mdxGuest.tsx");
return `window.__OPENIDEA_MDX_SANDBOX__ = true;\n${chunk.code}`;
});
}
return cachedBundlePromise;
};
return {
name: "openidea:mdx-guest-bundle",
resolveId(id) {
if (id === VIRTUAL_MDX_GUEST) return RESOLVED_MDX_GUEST;
return null;
},
handleHotUpdate(ctx) {
if (ctx.file.startsWith(`${guestDir}${path.sep}`) || ctx.file === guestEntry) {
cachedBundlePromise = null;
}
},
async load(id) {
if (id !== RESOLVED_MDX_GUEST) return null;
const code = await buildGuestIife();
return `export default ${JSON.stringify(code)};`;
},
};
}
export default defineConfig(() => ({
// Relative asset URLs keep both desktop bundles and reverse-proxied web deployments subpath-agnostic.
base: "./",
plugins: [react(), mdxGuestBundlePlugin()],
resolve: {
alias: [
{ find: "@", replacement: fileURLToPath(new URL("./src", import.meta.url)) },
// `invoke` with remote-development routing (src/platform/tauriCore.ts).
{
find: /^@tauri-apps\/api\/core$/,
replacement: fileURLToPath(new URL("./src/platform/tauriCore.ts", import.meta.url)),
},
],
},
// Tauri: keep Rust errors visible, fixed port, don't watch the backend.
clearScreen: false,
server: {
port: 1420,
strictPort: true,
host: host || false,
hmr: host ? { protocol: "ws", host } : undefined,
...(webServer
? {
proxy: {
"/api": {
target: webServer,
ws: true,
},
},
}
: {}),
watch: {
ignored: [
"**/src-tauri/**",
"**/crates/**",
"**/target/**",
// The packages' sources and builds (not `src/features/plugins`).
`${fileURLToPath(new URL("./plugins", import.meta.url))}/**`,
],
},
},
// The app's own entry only: plugins' web pages (`plugins/**/web-src`) are
// separate projects with their own dependencies, never part of this bundle.
optimizeDeps: { entries: ["index.html"] },
build: {
target: "safari15.4",
sourcemap: true,
},
test: {
environment: "jsdom",
globals: false,
setupFiles: ["./src/test/setup.ts"],
include: ["src/**/*.test.{ts,tsx}", "perf/**/*.test.ts", "evals/**/*.test.ts", "scripts/**/*.test.ts"],
},
}));