Skip to content

Commit ddc0481

Browse files
authored
fix(build): Fix regex in multiline-comment-removal Rollup plugin (#5783)
The existing regex, when presented with code like this: ```js /* * This is * a multiline * comment */ const thisIs = "real code" /* * This is * another multiline * comment */ const thisIsAlso = "real code" ``` matches everything between the initial `/*` on the first line and the final `*/` on the second-to-last line, and the plugin thus removes all but the last line of the file. This fixes the regex so that only the comments are removed.
1 parent 1540616 commit ddc0481

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

rollup/plugins/npmPlugins.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,11 @@ export function makeRemoveMultiLineCommentsPlugin() {
140140
return regexReplace({
141141
patterns: [
142142
{
143-
// The `s` flag makes the dot match newlines
144-
test: /^\/\*.*\*\//gs,
143+
// If we ever want to remove all comments instead of just /* ... */ ones, the regex is
144+
// /\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm. We also might consider a plugin like
145+
// https://github.com/aMarCruz/rollup-plugin-cleanup (though to remove only multi-line comments we'd end up with
146+
// a regex there, too).
147+
test: /\/\*[\s\S]*?\*\//gm,
145148
replace: '',
146149
},
147150
],

0 commit comments

Comments
 (0)