Skip to content

Commit d6c9c6c

Browse files
committed
perf: Only put legal notice where legal comments existed
1 parent a4e7135 commit d6c9c6c

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

vite.config.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,29 @@ import devtoolsJson from 'vite-plugin-devtools-json';
1313
import mkcert from 'vite-plugin-mkcert';
1414

1515
function jsBannerPlugin(banner: string): Plugin {
16+
// Matches preserved/legal comment markers that OXC's minifier strips when
17+
// `output.comments.legal === false`: `/*!` starts, plus `@license`,
18+
// `@preserve`, and `@cc_on` JSDoc annotations.
19+
const LEGAL_COMMENT_RE = /\/\*!|@(?:license|preserve|cc_on)\b/;
20+
const modulesWithLegal = new Set<string>();
21+
1622
return {
1723
name: 'js-banner',
1824
enforce: 'post',
25+
buildStart() {
26+
modulesWithLegal.clear();
27+
},
28+
transform(code, id) {
29+
if (LEGAL_COMMENT_RE.test(code)) {
30+
modulesWithLegal.add(id);
31+
}
32+
return null;
33+
},
1934
generateBundle(_, bundle) {
2035
for (const chunk of Object.values(bundle)) {
21-
if (chunk.type === 'chunk') {
36+
if (chunk.type !== 'chunk') continue;
37+
const hasLegal = Object.keys(chunk.modules).some((id) => modulesWithLegal.has(id));
38+
if (hasLegal) {
2239
chunk.code = banner + '\n' + chunk.code;
2340
}
2441
}

0 commit comments

Comments
 (0)