Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR implements post-build cleanup functionality for Monaco Editor language support files to reduce bundle size. The changes configure Monaco Editor with specific language workers and add an automated cleanup script that removes unused language files after the build process.
- Configures Monaco Editor plugin with specific language workers (JSON, TypeScript)
- Adds post-build cleanup script to remove unnecessary Monaco language files
- Updates build process to automatically run cleanup after Vite build
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| web/vite.config.js | Configures Monaco Editor plugin with specific language workers and global API |
| web/scripts/cleanup-monaco.js | Implements cleanup script to remove unused Monaco language files from build output |
| web/package.json | Updates build command to run cleanup script automatically after Vite build |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| import pkg from 'vite-plugin-monaco-editor'; | ||
| const monacoEditorPlugin = pkg.default || pkg; |
There was a problem hiding this comment.
[nitpick] The import pattern with fallback logic suggests the plugin may export differently across versions. Consider using a more explicit import pattern or adding a comment explaining why this approach is needed.
| import pkg from 'vite-plugin-monaco-editor'; | |
| const monacoEditorPlugin = pkg.default || pkg; |
| 'rust-', // Rust language support | ||
| 'javascript-', // JavaScript support | ||
| 'typescript-', // TypeScript support | ||
| 'css-', // CSS support | ||
| 'html-', // HTML support | ||
| 'markdown-', // Markdown support | ||
| 'scss-', // SCSS support | ||
| 'jsonMode-', // JSON mode (workers) | ||
| 'cssMode-', // CSS mode (workers) | ||
| 'htmlMode-', // HTML mode (workers) |
There was a problem hiding this comment.
The keep patterns include many languages (rust, javascript, css, html, etc.) but the Vite config only specifies 'json' and 'typescript' workers. This inconsistency could result in keeping files for languages that aren't properly configured.
| 'rust-', // Rust language support | |
| 'javascript-', // JavaScript support | |
| 'typescript-', // TypeScript support | |
| 'css-', // CSS support | |
| 'html-', // HTML support | |
| 'markdown-', // Markdown support | |
| 'scss-', // SCSS support | |
| 'jsonMode-', // JSON mode (workers) | |
| 'cssMode-', // CSS mode (workers) | |
| 'htmlMode-', // HTML mode (workers) | |
| 'jsonMode-', // JSON mode (workers) |
| files.forEach(file => { | ||
| const filePath = path.join(distAssetsDir, file); | ||
| const stats = fs.statSync(filePath); | ||
|
|
||
| if (!stats.isFile()) { | ||
| return; | ||
| } | ||
|
|
||
| // For JS files, check if we should keep them | ||
| if (file.endsWith('.js')) { | ||
| // Check if it matches any of our keep patterns | ||
| const shouldKeep = keepPatterns.some(pattern => file.startsWith(pattern)); | ||
|
|
||
| if (!shouldKeep) { | ||
| totalSizeSaved += stats.size; | ||
| fs.unlinkSync(filePath); | ||
| console.log(`Removed: ${file} (${(stats.size / 1024).toFixed(2)} KB)`); | ||
| deletedCount++; | ||
| } else { | ||
| console.log(`Kept: ${file}`); | ||
| keptCount++; | ||
| } | ||
| } | ||
| // Non-JS files (CSS, fonts, etc) are always kept | ||
| }); | ||
|
|
||
| console.log('\n=== Monaco Language Cleanup Complete ==='); | ||
| console.log(`Files removed: ${deletedCount}`); | ||
| console.log(`Files kept: ${keptCount}`); | ||
| console.log(`Total size saved: ${(totalSizeSaved / 1024 / 1024).toFixed(2)} MB`); No newline at end of file |
There was a problem hiding this comment.
Using synchronous file operations in a loop can be inefficient and block the event loop. Consider using fs.promises.unlink() with Promise.all() for better performance.
| files.forEach(file => { | |
| const filePath = path.join(distAssetsDir, file); | |
| const stats = fs.statSync(filePath); | |
| if (!stats.isFile()) { | |
| return; | |
| } | |
| // For JS files, check if we should keep them | |
| if (file.endsWith('.js')) { | |
| // Check if it matches any of our keep patterns | |
| const shouldKeep = keepPatterns.some(pattern => file.startsWith(pattern)); | |
| if (!shouldKeep) { | |
| totalSizeSaved += stats.size; | |
| fs.unlinkSync(filePath); | |
| console.log(`Removed: ${file} (${(stats.size / 1024).toFixed(2)} KB)`); | |
| deletedCount++; | |
| } else { | |
| console.log(`Kept: ${file}`); | |
| keptCount++; | |
| } | |
| } | |
| // Non-JS files (CSS, fonts, etc) are always kept | |
| }); | |
| console.log('\n=== Monaco Language Cleanup Complete ==='); | |
| console.log(`Files removed: ${deletedCount}`); | |
| console.log(`Files kept: ${keptCount}`); | |
| console.log(`Total size saved: ${(totalSizeSaved / 1024 / 1024).toFixed(2)} MB`); | |
| async function main() { | |
| const unlinkPromises = []; | |
| for (const file of files) { | |
| const filePath = path.join(distAssetsDir, file); | |
| const stats = fs.statSync(filePath); | |
| if (!stats.isFile()) { | |
| continue; | |
| } | |
| // For JS files, check if we should keep them | |
| if (file.endsWith('.js')) { | |
| // Check if it matches any of our keep patterns | |
| const shouldKeep = keepPatterns.some(pattern => file.startsWith(pattern)); | |
| if (!shouldKeep) { | |
| totalSizeSaved += stats.size; | |
| // Use async unlink | |
| unlinkPromises.push( | |
| fs.promises.unlink(filePath).then(() => { | |
| console.log(`Removed: ${file} (${(stats.size / 1024).toFixed(2)} KB)`); | |
| deletedCount++; | |
| }) | |
| ); | |
| } else { | |
| console.log(`Kept: ${file}`); | |
| keptCount++; | |
| } | |
| } | |
| // Non-JS files (CSS, fonts, etc) are always kept | |
| } | |
| await Promise.all(unlinkPromises); | |
| console.log('\n=== Monaco Language Cleanup Complete ==='); | |
| console.log(`Files removed: ${deletedCount}`); | |
| console.log(`Files kept: ${keptCount}`); | |
| console.log(`Total size saved: ${(totalSizeSaved / 1024 / 1024).toFixed(2)} MB`); | |
| } | |
| main().catch(err => { | |
| console.error('Error during Monaco cleanup:', err); | |
| process.exit(1); | |
| }); |
Pull Request
Description
Brief description of the changes in this PR.
Type of Change
Exercise Changes (if applicable)
Testing Checklist
cargo test)cargo clippy)Quality Checklist
Documentation Updates
Breaking Changes
Does this PR introduce any breaking changes?
If yes, describe the breaking changes and migration path:
Additional Notes
Any additional context, screenshots, or information relevant to this PR.