MCPcopy
hub / github.com/css/csso / compress

Function compress

lib/compress.js:123–196  ·  view source on GitHub ↗
(ast, options)

Source from the content-addressed store, hash-verified

121}
122
123export default function compress(ast, options) {
124 ast = ast || { type: 'StyleSheet', loc: null, children: new List() };
125 options = options || {};
126
127 const compressOptions = {
128 logger: typeof options.logger === 'function' ? options.logger : function() {},
129 restructuring: getRestructureOption(options),
130 forceMediaMerge: Boolean(options.forceMediaMerge),
131 usage: options.usage ? buildIndex(options.usage) : false
132 };
133 const output = new List();
134 let specialComments = getCommentsOption(options);
135 let firstAtrulesAllowed = true;
136 let input;
137 let chunk;
138 let chunkNum = 1;
139 let chunkChildren;
140
141 if (options.clone) {
142 ast = clone(ast);
143 }
144
145 if (ast.type === 'StyleSheet') {
146 input = ast.children;
147 ast.children = output;
148 } else {
149 input = wrapBlock(ast);
150 }
151
152 do {
153 chunk = readChunk(input, Boolean(specialComments));
154 compressChunk(chunk.stylesheet, firstAtrulesAllowed, chunkNum++, compressOptions);
155 chunkChildren = chunk.stylesheet.children;
156
157 if (chunk.comment) {
158 // add \n before comment if there is another content in output
159 if (!output.isEmpty) {
160 output.insert(List.createItem({
161 type: 'Raw',
162 value: '\n'
163 }));
164 }
165
166 output.insert(List.createItem(chunk.comment));
167
168 // add \n after comment if chunk is not empty
169 if (!chunkChildren.isEmpty) {
170 output.insert(List.createItem({
171 type: 'Raw',
172 value: '\n'
173 }));
174 }
175 }
176
177 if (firstAtrulesAllowed && !chunkChildren.isEmpty) {
178 const lastRule = chunkChildren.last;
179
180 if (lastRule.type !== 'Atrule' ||

Callers 4

minifyFunction · 0.85
testFnFunction · 0.85
compress.jsFile · 0.85
clone.jsFile · 0.85

Calls 6

buildIndexFunction · 0.90
getRestructureOptionFunction · 0.85
getCommentsOptionFunction · 0.85
wrapBlockFunction · 0.85
readChunkFunction · 0.85
compressChunkFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…