Skip to content

Commit 2a84954

Browse files
author
Tajudeen
committed
fix: correct React output import paths for minification
- Add post-build step to fix import paths in React output files - Replace './react/out/MODULE/index.js' with '../MODULE/index.js' - Replace '../react/out/MODULE/index.js' with '../../MODULE/index.js' - Fixes 'Could not resolve' errors during minify-vscode step - React outputs are in sibling directories, so paths need to be relative to each other - Keep React outputs in external array for tsup resolution, but fix paths after build
1 parent 8940ca2 commit 2a84954

2 files changed

Lines changed: 72 additions & 14 deletions

File tree

src/vs/workbench/contrib/cortexide/browser/react/build.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,52 @@ if (isWatch) {
190190
// DTS generation requires significantly more memory (12GB recommended)
191191
execSync(`node --max-old-space-size=12288 ${tsupPath}`, { stdio: 'inherit' });
192192

193+
// Post-build fix: Remove incorrect import statements from React output files
194+
// When React outputs are marked as external, tsup preserves import statements
195+
// But these imports use paths like './react/out/...' which don't resolve during minification
196+
// Since they're external, we need to remove these imports (they'll be loaded at runtime)
197+
// allow-any-unicode-next-line
198+
console.log('🔧 Fixing React output import paths...');
199+
const outDir = path.join(__dirname, 'out');
200+
const reactOutputModules = [
201+
'void-editor-widgets-tsx',
202+
'void-settings-tsx',
203+
'sidebar-tsx',
204+
'void-tooltip',
205+
'void-onboarding',
206+
'quick-edit-tsx',
207+
'diff'
208+
];
209+
210+
for (const module of reactOutputModules) {
211+
const indexPath = path.join(outDir, module, 'index.js');
212+
if (fs.existsSync(indexPath)) {
213+
let content = fs.readFileSync(indexPath, 'utf8');
214+
// Fix import paths: React outputs are in sibling directories under out/
215+
// Replace './react/out/MODULE/index.js' with '../MODULE/index.js'
216+
// Replace '../react/out/MODULE/index.js' with '../../MODULE/index.js'
217+
// This fixes the paths so they resolve correctly during minification
218+
content = content.replace(
219+
/from\s+['"]\.\/react\/out\/([^/'"]+)\/index\.js['"]/g,
220+
"from '../$1/index.js'"
221+
);
222+
content = content.replace(
223+
/from\s+['"]\.\.\/react\/out\/([^/'"]+)\/index\.js['"]/g,
224+
"from '../../$1/index.js'"
225+
);
226+
// Fix dynamic imports: import('./react/out/MODULE/index.js') -> import('../MODULE/index.js')
227+
content = content.replace(
228+
/import\s*\(\s*['"]\.\/react\/out\/([^/'"]+)\/index\.js['"]\s*\)/g,
229+
"import('../$1/index.js')"
230+
);
231+
content = content.replace(
232+
/import\s*\(\s*['"]\.\.\/react\/out\/([^/'"]+)\/index\.js['"]\s*\)/g,
233+
"import('../../$1/index.js')"
234+
);
235+
fs.writeFileSync(indexPath, content, 'utf8');
236+
}
237+
}
238+
193239
// allow-any-unicode-next-line
194240
console.log('✅ Build complete!');
195241
}

src/vs/workbench/contrib/cortexide/browser/react/tsup.config.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,32 @@ const externalVSCodePlugin = {
2727
}
2828
}
2929

30-
// Plugin to mark React output imports as external during DTS generation
31-
// This prevents tsup from trying to resolve imports to files that don't exist yet during the build
30+
// Plugin to mark React output imports as external ONLY during tsup's initial resolution
31+
// This prevents tsup from trying to resolve imports to files that don't exist yet
32+
// BUT we need to be careful: if these are marked external, they won't be bundled
33+
// The key is that this plugin only runs during esbuild's resolution, and we want
34+
// React outputs to be bundled together (not external) for minification
35+
//
36+
// Actually, the real issue is that files outside React dir import from ./react/out/...
37+
// When tsup builds, it shouldn't follow those imports. We need to prevent tsup
38+
// from following imports from files outside the entry points.
3239
const externalReactOutputsPlugin = {
3340
name: 'external-react-outputs',
3441
setup(build) {
42+
// Only mark as external if the import is coming from outside the React source directory
43+
// This prevents tsup from trying to resolve imports from files like cortexideSettingsPane.ts
44+
// But allows React components to import each other normally (which should be bundled)
3545
build.onResolve({ filter: /^\.\/react\/out\/|^\.\.\/react\/out\// }, (args) => {
36-
// Mark React output imports as external during build (they'll be available at runtime)
37-
// This prevents DTS generation from failing when these files don't exist yet
38-
return { path: args.path, external: true }
46+
// Check if the importer is outside the React source directory
47+
// If so, mark as external (these are runtime imports)
48+
// If not, let it be bundled (these are internal React component imports)
49+
if (args.importer && !args.importer.includes('/react/src') && !args.importer.includes('/react/src2')) {
50+
// This import is from outside React source, mark as external
51+
// This prevents tsup from trying to resolve it during build
52+
return { path: args.path, external: true }
53+
}
54+
// This is an internal import, let it be bundled
55+
return undefined
3956
})
4057
}
4158
}
@@ -64,18 +81,13 @@ export default defineConfig({
6481
outExtension: () => ({ js: '.js' }),
6582
treeshake: true,
6683
// Mark Node.js built-ins and React output imports as external
67-
// React outputs MUST be in the external array (not just plugin) because:
68-
// 1. tsup does its own import resolution BEFORE esbuild
69-
// 2. When tsup encounters imports from files outside React dir (e.g., cortexideSettingsPane.ts),
70-
// it tries to resolve them during tsup's resolution phase
71-
// 3. The external array is checked during tsup's phase, plugins only run during esbuild phase
72-
// 4. If not in external array, tsup tries to resolve files that don't exist yet → fails
84+
// CRITICAL: React outputs MUST be in external array for tsup's resolution phase
85+
// BUT: The plugin will handle them during esbuild phase to allow bundling
86+
// The external array prevents tsup from trying to resolve imports from files outside React dir
7387
external: [
7488
'url', 'module', 'fs', 'path', 'os', 'crypto', 'stream', 'util', 'events', 'buffer', 'process',
7589
// React output imports - MUST be external at tsup level to prevent resolution errors
76-
// tsup checks external array during its own resolution phase (before esbuild)
77-
// If not listed here, tsup tries to resolve files that don't exist yet → fails
78-
// These are the exact paths used in imports from files outside React directory
90+
// These prevent tsup from trying to resolve imports from files like cortexideSettingsPane.ts
7991
'./react/out/void-editor-widgets-tsx/index.js',
8092
'./react/out/void-settings-tsx/index.js',
8193
'./react/out/sidebar-tsx/index.js',

0 commit comments

Comments
 (0)