-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
istanbul comments stripped when compiling in jest-transform #516
Comments
You are correct, esbuild does not preserve comments. It's not built to preserve comments. Preserving comments means it would operate more like a code formatter than a compiler. For example, you would probably want a token spanning tree instead of an AST. Doing this is currently a non-goal of esbuild. You should probably be using another tool for this. |
This is unfortunate... Guess esbuild is a none starter for JS/TS tooling then 😔 will check out swc and see how it fairs. Super job overall, worked really well until this wall. |
Wow, that's a bummer. Doesn't AST support something like |
I'm seing that there is a support of legalComments: inline https://esbuild.github.io/api/#legal-comments I just switched from babel or ts in my jest testing using esbuild and this is blazing fast that is pretty awesome to do TDD in watch mode. But then if we cannot instrument our code to tell jest via comments to ignore some parts we cannot match our requirements and that is very frustrating. Esbuild start to have a great visibility because it is blazing fast, there lot of projects on top of it like Vite for the FE but then there is a few limitations that you cannot use it as a drop replacement of the well known tools and I think that's a shame. Maybe I have to look for swc too... |
Can't you use |
Does not seems to work neither |
yeah most tooling ignores that format of comments so its pretty useless tbh unfortunately |
In case of Greasemonkey, the user script tool, file must start with // ==UserScript==
// @name Foobar
// @version 1
// ==/UserScript==
window.alert('1') which is esbuild just removes it, and makes the user script does not work. |
You're looking for this feature: https://esbuild.github.io/api/#banner |
How? const { code } = require('esbuild').transformSync(
[
'// ==UserScript==',
'// @name Foobar',
'// @version 1',
'// ==/UserScript==',
'',
'window.alert("1")',
].join('\n'),
{
banner: '// ==UserScript==', // <<---- how?
},
)
if (!code.includes('@version')) throw new Error('Metadata not preserved') |
@xnuk const banner = `// ==UserScript==
// @name Foobar
// @version 1
// ==/UserScript==
`
const source = `
window.alert("1")
`
const { code } = require('esbuild').transformSync(source, { banner })
if (!code.includes('@version')) throw new Error('Metadata not preserved') But to be honest, in your example the easiest workaround would be directly this: const banner = `// ==UserScript==
// @name Foobar
// @version 1
// ==/UserScript==
`
const source = `
window.alert("1")
`
const { code } = require('esbuild').transformSync(source)
fs.writeFileSync('index.js', banner + code) My original topic about Nevermind it seems that everyone fallback to use |
SWC provides an option to preserve comments in its compiler options. This is perfect because there are instances when you want to strip comments and when you want to preserve them. A transform for jest is perfect example of when you want to preserve comments for tooling like istanbul, stripping the comments in a jest transform instantly makes it inviable unfortunately. We moved to SWC and have found it much easier to work with and allows much better compatibility with tooling. Still hoping esbuild becomes more tooling friendly in future. |
As work-around you can use |
@evanw Sorry for bumping this, what would would be the idiomatic way for you to use |
I have a jest-transform I'm using on a project (v2 beta of https://github.com/ts-engine/ts-engine) that compiles with esbuild and it works great, istanbul coverage works fine too. However because comments are stripped from the compiled output you can't ignore statements/files etc on the occasions you need to. An option to maintain comments would be great if possible.
Here is the jest transform for reference:
Adding an option like
preserveComments: true
to esbuild would fix this issue.The text was updated successfully, but these errors were encountered: