@@ -99,6 +99,38 @@ import copy from 'rollup-plugin-copy';
99
99
import serve from ' rollup-plugin-serve' ;
100
100
import { terser } from ' rollup-plugin-terser' ;
101
101
102
+ /**
103
+ * Reduces extra spaces in HTML tagged templates.
104
+ *
105
+ * @param {string} data - the fragment value
106
+ * @returns string
107
+ */
108
+ export function transformHTMLFragment (data ) {
109
+ data = data .replace (/ \s * ([<>] )\s * / g , ' $1' ); // remove spaces before and after angle brackets
110
+ return data .replace (/ \s {2,} / g , ' ' ); // Collapse all sequences to 1 space
111
+ }
112
+
113
+ /**
114
+ * Reduces extra spaces in CSS tagged templates.
115
+ *
116
+ * Breakdown of this regex:
117
+ * (?:\s*\/\*(?:.|\s)+?\*\/\s*) Remove comments (non-capturing)
118
+ * (?:;)\s+(?=\}) Remove semicolons and spaces followed by property list end (non-capturing)
119
+ * \s+(?=\{) Remove spaces before property list start (non-capturing)
120
+ * (?<=:)\s+ Remove spaces after property declarations (non-capturing)
121
+ * \s*([{};,])\s* Remove extra spaces before and after braces, semicolons, and commas (captures)
122
+ *
123
+ * @param {string} data - the fragment value
124
+ * @returns string
125
+ */
126
+ export function transformCSSFragment (data ) {
127
+ return data .replace (/ (?:\s * \/\* (?:. | \s )+? \*\/ \s * )| (?:;)\s + (?=\} )| \s + (?=\{ )| (?<=:)\s + | \s * ([{};,] )\s * / g , ' $1' );
128
+ }
129
+
130
+ const parserOptions = {
131
+ sourceType: ' module' ,
132
+ };
133
+
102
134
export default {
103
135
input: ' src/main.ts' ,
104
136
output: {
@@ -109,23 +141,14 @@ export default {
109
141
},
110
142
plugins: [
111
143
transformTaggedTemplate ({
112
- tagsToProcess: [' html' ,' css' ],
113
- parserOptions: {
114
- sourceType: " module" ,
115
- plugins: [
116
- " typescript" ,
117
- [
118
- " decorators" ,
119
- { decoratorsBeforeExport: true }
120
- ]
121
- ]
122
- },
123
- transformer (data ) {
124
- data = data .replace (/ \s ([{}()>~+=^$:!;] )\s / gm , ' $1' );
125
- data = data .replace (/ ([",[] ])\s + / gm , ' $1' );
126
- data = data .replace (/ \s {2,} / gm , ' ' );
127
- return data .trim ();
128
- }
144
+ tagsToProcess: [' css' ],
145
+ transformer: transformCSSFragment,
146
+ parserOptions,
147
+ }),
148
+ transformTaggedTemplate ({
149
+ tagsToProcess: [' html' ],
150
+ transformer: transformHTMLFragment,
151
+ parserOptions,
129
152
}),
130
153
typescript (),
131
154
resolve (),
0 commit comments