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
feat: support legalComments: 'linked'
#264
feat: support legalComments: 'linked'
#264
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if raw source manipulation is respectful of sourcemaps.
It might not be a big deal since it's at the bottom of the file, but using magic-string will preserve it.
|
||
private extractLegalComments(code: string, assetName: string): string[] | undefined { | ||
// Search the first position of legal comments | ||
const index = code.search(/^\/[/*]/m); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer against regex because it's slower than normal string searching. Possible to use .lastIndexOf()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Legal comments can be multiple and start with /*
or //
like this:
//! Legal comment A
/*! Legal comment B */
So, I think the /^\/[/*]/m
regex is necessary (slow though).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I don't think that regex will cover it:
A "legal comment" is considered to be any statement-level comment in JS or rule-level comment in CSS that contains
@license
or@preserve
or that starts with//!
or/*!
. These comments are preserved in output files by default since that follows the intent of the original authors of the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, you're right. It seems complicated to make all patterns to search legal comments... 😓
Co-authored-by: hiroki osame <hiroki.osame@gmail.com>
Indeed, the index-based string manipulation may be flaky. But, I'm unfamiliar with magic-string and concerned about adding an extra dependency. |
I made a request here evanw/esbuild#2390. But for reference, you can use |
It seems better to me to wait for the requested feature than to proceed with this pull request hackily. Supporting |
Sorry for leaving the PR for a while. I've lost interest in adding the feature, so I'm sorry but closing it. Feel free to reopen it or use a part of this PR code with another opportunity. Thanks. |
Fix #263
This pull request emulates the esbuild's behavior for
legalComments: 'linked'
since esbuild fails when passing the option as-is.When passing
'linked'
, the option is internally converted to'eof'
, and this change extracts legal comments from transformed code and then writes another file likeindex.js.LEGAL.txt
.If the code is wrong, feel free to let me know.