-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Description
Description
As a developer using Vite I want to be able to debug my code in vite.config.ts via VSCode so I can troubleshoot any problems in plugins or other code snippets added in the file.
I start the debugging by adding debugger; statements and then launch vite via:
- using vite commands in the JavaScript Debug Terminal
- Opening the package.json and hit debug on the inline hint
Suggested solution
Vite currently deletes the temporary node_modules/.vite-temp/vite.config.ts.timestamp-.... file after it was loaded:
vite/packages/vite/src/node/config.ts
Lines 1999 to 2012 in c9e086d
| const tempFileName = nodeModulesDir | |
| ? path.resolve( | |
| nodeModulesDir, | |
| `.vite-temp/${path.basename(fileName)}.${hash}.mjs`, | |
| ) | |
| : `${fileName}.${hash}.mjs` | |
| await fsp.writeFile(tempFileName, bundledCode) | |
| try { | |
| return (await import(pathToFileURL(tempFileName).href)).default | |
| } finally { | |
| fs.unlink(tempFileName, () => {}) // Ignore errors | |
| } | |
| } | |
| // for cjs, we can register a custom loader via `_require.extensions` |
It would be great if Vite would detect that a debugger is currently attached and keep the file in this case.
According to this StackOverflow Post this could be achieved by checking whether there is a inspector url defined (works fine for me in VSCode). But also to check for some special node argvs is a potential candidate.
My proposal would be to use something like:
import inspector from 'inspector';
function isInDebugMode() {
const argv = process.execArgv.join();
return inspector.url() !== undefined || argv.includes('inspect') || argv.includes('debug') || process.env.VITE_KEEP_CONFIG;
}Alternative
- Only add an environment variable. (a bit less convienient as you have to remember to set it every time).
- Add a config key which can be filled by the user to keep the transpiled config.
- Ensure (somehow?) that sourcemaps are working and breakpoints can be set in the original file directly. Currently source maps are generated but they do not work for VS Code Debugging.
Additional context
vite/packages/vite/src/node/config.ts
Lines 1999 to 2012 in c9e086d
| const tempFileName = nodeModulesDir | |
| ? path.resolve( | |
| nodeModulesDir, | |
| `.vite-temp/${path.basename(fileName)}.${hash}.mjs`, | |
| ) | |
| : `${fileName}.${hash}.mjs` | |
| await fsp.writeFile(tempFileName, bundledCode) | |
| try { | |
| return (await import(pathToFileURL(tempFileName).href)).default | |
| } finally { | |
| fs.unlink(tempFileName, () => {}) // Ignore errors | |
| } | |
| } | |
| // for cjs, we can register a custom loader via `_require.extensions` |
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that request the same feature to avoid creating a duplicate.