| 1 | // Vite settings for the level 2 vendor bundle. build.mjs calls configFor() |
| 2 | // once per entry; there is no `vite build` CLI step. Each entry becomes ONE |
| 3 | // ES module file (the sandbox loads them from blob: URLs, which cannot |
| 4 | // resolve relative imports), with every asset inlined and no source map. |
| 5 | import path from 'node:path'; |
| 6 | import { fileURLToPath } from 'node:url'; |
| 7 | |
| 8 | const HERE = path.dirname(fileURLToPath(import.meta.url)); |
| 9 | |
| 10 | export const ENTRIES = ['sandbox-main', 'exthost-worker', 'editor-worker']; |
| 11 | |
| 12 | // A URL built from import.meta.url would point at wherever the blob came |
| 13 | // from. Any that survive bundling point at a name that never resolves and |
| 14 | // that the sandbox's CSP refuses anyway: such a URL fails closed. |
| 15 | export const DEAD_BASE = 'https://x2.invalid/'; |
| 16 | |
| 17 | const STUBS = { |
| 18 | jschardet: path.join(HERE, 'src/stubs/jschardet.js'), |
| 19 | '@vscode/iconv-lite-umd': path.join(HERE, 'src/stubs/iconv-lite-umd.js'), |
| 20 | '@vscode/diff': path.join(HERE, 'src/stubs/vscode-diff.js'), |
| 21 | }; |
| 22 | const KEYBOARD_LAYOUTS = /[\\/]keyboardLayouts[\\/]layout\.contribution\.(darwin|linux|win)\.js$/; |
| 23 | |
| 24 | function stubs() { |
| 25 | return { |
| 26 | name: 'elffuss-x2-stubs', |
| 27 | enforce: 'pre', |
| 28 | async resolveId(source, importer, options) { |
| 29 | if (Object.hasOwn(STUBS, source)) return STUBS[source]; |
| 30 | if (/layout\.contribution\.(darwin|linux|win)\.js$/.test(source) && importer) { |
| 31 | const resolved = await this.resolve(source, importer, { ...options, skipSelf: true }); |
| 32 | if (resolved && KEYBOARD_LAYOUTS.test(resolved.id)) return path.join(HERE, 'src/stubs/keyboard-layouts.js'); |
| 33 | } |
| 34 | return null; |
| 35 | }, |
| 36 | }; |
| 37 | } |
| 38 | |
| 39 | // VS Code's CSS is imported from JS (`import * as x from './x.css'`) and |
| 40 | // registered as text by the library; load it as a string export. |
| 41 | function cssAsString() { |
| 42 | return { |
| 43 | name: 'elffuss-x2-css-as-string', |
| 44 | enforce: 'pre', |
| 45 | async resolveId(source, importer, options) { |
| 46 | if (!source.endsWith('.css') || source.includes('?')) return null; |
| 47 | const resolved = await this.resolve(source, importer, { ...options, skipSelf: true }); |
| 48 | if (resolved && /[\\/]node_modules[\\/](@codingame[\\/]monaco-vscode|monaco-editor)/.test(resolved.id) && resolved.id.endsWith('.css')) { |
| 49 | return { ...resolved, id: resolved.id + '?inline' }; |
| 50 | } |
| 51 | return null; |
| 52 | }, |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | // The library points at its own copy of the extension-host iframe document |
| 57 | // with `new URL('….html', import.meta.url)`. The sandbox registers its own |
| 58 | // document instead (registerAssets), so that copy must not be emitted: the |
| 59 | // reference is pinned to the dead base before Vite turns it into an asset. |
| 60 | function noHtmlAssets() { |
| 61 | const PATTERN = /new URL\(\s*(['"`])([^'"`]+\.html)\1\s*,\s*import\.meta\.url\s*\)/g; |
| 62 | return { |
| 63 | name: 'elffuss-x2-no-html-assets', |
| 64 | enforce: 'pre', |
| 65 | transform(code, id) { |
| 66 | if (!/[\\/]node_modules[\\/]/.test(id) || !code.includes('.html')) return null; |
| 67 | const out = code.replace(PATTERN, (_, q, file) => `new URL(${JSON.stringify(file)}, ${JSON.stringify(DEAD_BASE)})`); |
| 68 | return out === code ? null : { code: out, map: null }; |
| 69 | }, |
| 70 | }; |
| 71 | } |
| 72 | |
| 73 | function deadImportMetaUrl() { |
| 74 | return { |
| 75 | name: 'elffuss-x2-dead-import-meta-url', |
| 76 | renderChunk(code) { |
| 77 | if (!code.includes('import.meta.url')) return null; |
| 78 | return { code: code.split('import.meta.url').join(JSON.stringify(DEAD_BASE)), map: null }; |
| 79 | }, |
| 80 | }; |
| 81 | } |
| 82 | |
| 83 | export function configFor(entry, { outDir }) { |
| 84 | if (!ENTRIES.includes(entry)) throw new Error('unknown entry ' + entry); |
| 85 | return { |
| 86 | configFile: false, |
| 87 | root: HERE, |
| 88 | logLevel: 'warn', |
| 89 | clearScreen: false, |
| 90 | publicDir: false, |
| 91 | envDir: false, |
| 92 | define: { 'process.env.NODE_ENV': '"production"' }, |
| 93 | plugins: [stubs(), cssAsString(), noHtmlAssets(), deadImportMetaUrl()], |
| 94 | resolve: { dedupe: ['vscode', 'monaco-editor'] }, |
| 95 | build: { |
| 96 | outDir, |
| 97 | emptyOutDir: true, |
| 98 | target: 'es2022', |
| 99 | minify: true, |
| 100 | sourcemap: false, |
| 101 | assetsInlineLimit: 4 * 1024 * 1024, |
| 102 | cssCodeSplit: false, |
| 103 | copyPublicDir: false, |
| 104 | reportCompressedSize: false, |
| 105 | modulePreload: false, |
| 106 | write: true, |
| 107 | license: { fileName: entry + '.licenses.json' }, |
| 108 | // not library mode: Vite keeps the whitespace of ES libraries, and this |
| 109 | // bundle is never re-bundled by anyone |
| 110 | rolldownOptions: { |
| 111 | input: { [entry]: path.join(HERE, 'src', entry + '.js') }, |
| 112 | preserveEntrySignatures: 'strict', |
| 113 | output: { format: 'es', codeSplitting: false, entryFileNames: '[name].js' }, |
| 114 | }, |
| 115 | }, |
| 116 | }; |
| 117 | } |