New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
vite-node fails to parse hashbang #1175
Comments
This bug is troubling for cli tool development. So, I'm considering two solutions. Solution 1: remove hashbang in vite-node via pluginI added the following plugin to import type { Plugin } from 'vite'
// remove shebeng from code (most often #!/usr/bin/env node)
// vite recognizes the shebang as a comment and needs to be removed because it causes a parse error.
export const RemoveShebangPlugin = (): Plugin => {
return {
name: 'vitest:remove-shebang-plugin',
enforce: 'pre',
transform(code) {
return code.replace(/^\#\!.*/, '')
},
}
} (This is no longer plugin:P just remove shebang) Bundling is not in the scope of vite-node, so I think there is no problem in deleting them when executing the script. Solution2: Suggest that the vite side be able to handle shebangHowever, I think that vite is mainly for development in the browser and is not really intended for creating cli tools... Anyway, I'm not sure this is good solution and if I should submit a PR. |
vite-node wraps code in a function, so hashbang appears not on the first line of the file, but inside a function. so, we should either remove it, or move on the first line. PR welcome |
Describe the bug
When a file has a hashbang/shebang (most often
#!/usr/bin/env node
), vite-node fails with this error:Reproduction
StackBlitz link: https://stackblitz.com/edit/node-fhzvli.
System Info
Used Package Manager
pnpm
Validations
The text was updated successfully, but these errors were encountered: