@@ -2,6 +2,7 @@ import { makePlaceholder, splitByPlaceholders } from '../css/placeholderUtils'
22
33const makeMultilineCommentRegex = newlinePattern => new RegExp ( '\\/\\*(.|' + newlinePattern + ')*?\\*\\/' , 'g' )
44const lineCommentStart = / \/ \/ / g
5+ const symbolRegex = / ( \s * [ ; : { } , ] \s * ) / g
56
67// Counts occurences of substr inside str
78const countOccurences = ( str , substr ) => str . split ( substr ) . length - 1
@@ -37,6 +38,25 @@ export const stripLineComment = line => (
3738 ) )
3839)
3940
41+ export const compressSymbols = code => code
42+ . split ( symbolRegex )
43+ . reduce ( ( str , fragment , index ) => {
44+ // Even-indices are non-symbol fragments
45+ if ( index % 2 === 0 ) {
46+ return str + fragment
47+ }
48+
49+ // Only manipulate symbols outside of strings
50+ if (
51+ countOccurences ( str , '\'' ) % 2 === 0 &&
52+ countOccurences ( str , '\"' ) % 2 === 0
53+ ) {
54+ return str + fragment . trim ( )
55+ }
56+
57+ return str + fragment
58+ } , '' )
59+
4060// Detects lines that are exclusively line comments
4161const isLineComment = line => line . trim ( ) . startsWith ( '//' )
4262
@@ -46,14 +66,14 @@ const minify = linebreakPattern => {
4666 const multilineCommentRegex = makeMultilineCommentRegex ( linebreakPattern )
4767
4868 return code => {
49- const lines = code
69+ const newCode = code
5070 . replace ( multilineCommentRegex , '\n' ) // Remove multiline comments
5171 . split ( linebreakRegex ) // Split at newlines
72+ . filter ( line => line . length > 0 && ! isLineComment ( line ) ) // Removes lines containing only line comments
5273 . map ( stripLineComment ) // Remove line comments inside text
74+ . join ( ' ' ) // Rejoin all lines
5375
54- return lines
55- . filter ( line => ! isLineComment ( line ) ) // Removes lines containing only line comments
56- . join ( '' )
76+ return compressSymbols ( newCode )
5777 }
5878}
5979
0 commit comments