-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathminify-css.js
269 lines (226 loc) · 7.98 KB
/
minify-css.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import sourcemap from "source-map";
import { createHash } from "crypto";
import LRU from "lru-cache";
//START AUTOPREFIX
import autoprefixer from 'autoprefixer';
import postcss from 'postcss';
import fs from 'fs';
const path = Plugin.path;
const configpath = path.join(process.cwd(),'postcss.json');
let config;
if (!fs.existsSync(configpath)) {
config = { autoprefixer: {} };
} else {
try {
config = JSON.parse(fs.readFileSync(configpath, 'utf-8'));
} catch (e) {
throw 'Post css configuration file error: ' + e;
}
}
//END AUTOPREFIX
Plugin.registerMinifier({
extensions: ["css"],
archMatching: "web"
}, function () {
const minifier = new CssToolsMinifier();
return minifier;
});
class CssToolsMinifier {
async processFilesForBundle (files, options) {
const mode = options.minifyMode;
if (! files.length) return;
const merged = await mergeCss(files);
//START AUTOPREFIX
let result;
try {
result = await postcss([ autoprefixer(config.autoprefixer) ])
.process(merged.code, {
from: 'merged-stylesheets.css',
to: 'merged-stylesheets-prefixed.css',
map: { inline: false, prev: merged.sourceMap }
});
result.warnings().forEach(function (warn) {
console.warn(warn.toString());
});
} catch (error) {
files[0].error({
message: error,
});
throw error;
}
//END AUTOPREFIX
if (mode === 'development') {
files[0].addStylesheet({
data: result.css,
sourceMap: result.map.toString(),
path: 'merged-stylesheets-prefixed.css'
});
return;
}
const minifiedFiles = CssTools.minifyCss(result.css);
if (files.length) {
minifiedFiles.forEach(function (minified) {
files[0].addStylesheet({
data: minified
});
});
}
}
}
const mergeCache = new LRU({
max: 100
});
const hashFiles = Profile("hashFiles", function (files) {
const hash = createHash("sha1");
files.forEach(f => {
hash.update(f.getSourceHash()).update("\0");
});
return hash.digest("hex");
});
function disableSourceMappingURLs(css) {
return css.replace(/# sourceMappingURL=/g,
"# sourceMappingURL_DISABLED=");
}
// Lints CSS files and merges them into one file, fixing up source maps and
// pulling any @import directives up to the top since the CSS spec does not
// allow them to appear in the middle of a file.
const mergeCss = Profile("mergeCss", async function (css) {
const hashOfFiles = hashFiles(css);
let merged = mergeCache.get(hashOfFiles);
if (merged) {
return merged;
}
// Filenames passed to AST manipulator mapped to their original files
const originals = {};
const cssAsts = css.map(function (file) {
const filename = file.getPathInBundle();
originals[filename] = file;
let ast;
try {
const parseOptions = { source: filename, position: true };
const css = disableSourceMappingURLs(file.getContentsAsString());
ast = CssTools.parseCss(css, parseOptions);
ast.filename = filename;
} catch (e) {
if (e.reason) {
file.error({
message: e.reason,
line: e.line,
column: e.column
});
} else {
// Just in case it's not the normal error the library makes.
file.error({message: e.message});
}
return { type: "stylesheet", stylesheet: { rules: [] }, filename };
}
return ast;
});
const warnCb = (filename, msg) => {
// XXX make this a buildmessage.warning call rather than a random log.
// this API would be like buildmessage.error, but wouldn't cause
// the build to fail.
console.log(`${filename}: warn: ${msg}`);
};
const mergedCssAst = CssTools.mergeCssAsts(cssAsts, warnCb);
// Overwrite the CSS files list with the new concatenated file
const stringifiedCss = CssTools.stringifyCss(mergedCssAst, {
sourcemap: true,
// don't try to read the referenced sourcemaps from the input
inputSourcemaps: false
});
if (! stringifiedCss.code) {
mergeCache.set(hashOfFiles, merged = { code: '' });
return merged;
}
// Add the contents of the input files to the source map of the new file
stringifiedCss.map.sourcesContent =
stringifiedCss.map.sources.map(function (filename) {
const file = originals[filename] || null;
return file && file.getContentsAsString();
});
// Compose the concatenated file's source map with source maps from the
// previous build step if necessary.
const newMap = await Profile.time("composing source maps", async function () {
const newMap = new sourcemap.SourceMapGenerator();
const concatConsumer = await new sourcemap.SourceMapConsumer(stringifiedCss.map);
// Create a dictionary of source map consumers for fast access
const consumers = Object.create(null);
await Promise.all(Object.entries(originals).map(async ([name, file]) => {
const sourceMap = file.getSourceMap();
if (sourceMap) {
try {
consumers[name] = await new sourcemap.SourceMapConsumer(sourceMap);
} catch (err) {
// If we can't apply the source map, silently drop it.
//
// XXX This is here because there are some less files that
// produce source maps that throw when consumed. We should
// figure out exactly why and fix it, but this will do for now.
}
}
}));
// Maps each original source file name to the SourceMapConsumer that
// can provide its content.
const sourceToConsumerMap = Object.create(null);
// Find mappings from the concatenated file back to the original files
concatConsumer.eachMapping((mapping) => {
let { source } = mapping;
const consumer = consumers[source];
let original = {
line: mapping.originalLine,
column: mapping.originalColumn
};
// If there is a source map for the original file, e.g., if it has been
// compiled from Less to CSS, find the source location in the original's
// original file. Otherwise, use the mapping of the concatenated file's
// source map.
if (consumer) {
const newOriginal = consumer.originalPositionFor(original);
// Finding the original position should always be possible (otherwise,
// one of the source maps would have incorrect mappings). However, in
// case there is something wrong, use the intermediate mapping.
if (newOriginal.source !== null) {
original = newOriginal;
source = original.source;
if (source) {
// Since the new consumer provided a different
// original.source, we should ask it for the original source
// content instead of asking the concatConsumer.
sourceToConsumerMap[source] = consumer;
}
}
}
if (source && ! sourceToConsumerMap[source]) {
// If we didn't set sourceToConsumerMap[source] = consumer above,
// use the concatConsumer to determine the original content.
sourceToConsumerMap[source] = concatConsumer;
}
// Add a new mapping to the final source map
newMap.addMapping({
generated: {
line: mapping.generatedLine,
column: mapping.generatedColumn
},
original,
source,
});
});
// The consumer.sourceContentFor and newMap.setSourceContent methods
// are relatively fast, but not entirely trivial, so it's better to
// call them only once per source, rather than calling them every time
// we call newMap.addMapping in the loop above.
Object.entries(sourceToConsumerMap).forEach(([source, consumer]) => {
const content = consumer.sourceContentFor(source);
newMap.setSourceContent(source, content);
});
concatConsumer.destroy();
Object.values(consumers).forEach(consumer => consumer.destroy());
return newMap;
});
mergeCache.set(hashOfFiles, merged = {
code: stringifiedCss.code,
sourceMap: newMap.toString()
});
return merged;
});