-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpostcss.mjs
217 lines (196 loc) · 7.9 KB
/
postcss.mjs
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
import path from "path";
import { fs, chokidar, readCache, bold, dim, cyan, magenta } from "carbon-pipeline";
import postcss from "postcss";
import {
config,
styleFiles as files,
watch,
error,
dynamicImport,
compression,
print,
sass,
sassFileCheck,
humanFileSize,
humanDuration,
equalArrays,
} from "./Lib/helper.mjs";
import { getAncestorDirs, dependencyGraph, dependencies, rc } from "./Lib/postcssHelper.mjs";
let compressFunction = compression ? await dynamicImport("./compress.mjs", null) : {};
let sassFunction = sass ? await dynamicImport("./sass.mjs", null) : {};
let { writeBr, writeGz } = compressFunction;
let outputLength = 0;
function renderFiles(keys) {
if (typeof keys === "string") {
keys = [keys];
}
if (!keys) {
keys = Object.keys(files);
}
return Promise.all(
keys.map((key) => {
return readCache(key).then((content) => {
const time = process.hrtime();
const file = files[key];
outputLength = Math.max(outputLength, file.length);
if (file.sass) {
const result = sassFunction.render(key);
files[key].importedFiles = result.importedFiles;
content = result.css;
}
return css(content, file, time);
});
})
);
}
function getImportedSaasFiles(inputs, entries) {
if (!entries) {
entries = [];
}
let hasNew = true;
const currentFiles = [...entries];
inputs.forEach((input) => {
const entry = files[input];
if (entry.sass) {
entries = entries.concat(entry.importedFiles);
}
});
entries = [...new Set(entries.filter((file) => !!file))];
if (equalArrays(currentFiles, entries)) {
hasNew = false;
}
return { entries, hasNew };
}
function build() {
Promise.resolve()
.then(() => {
return renderFiles();
})
.then((results) => {
if (watch) {
const inputs = results.map((result) => path.resolve(result.opts.from));
const watcher = chokidar.watch(inputs.concat(dependencies(results)), {
awaitWriteFinish: {
stabilityThreshold: 50,
pollInterval: 10,
},
});
// Add files from sass
let importedSaasFiles = [];
if (sass) {
const { entries } = getImportedSaasFiles(inputs);
if (entries.length) {
importedSaasFiles = entries;
watcher.add(entries);
}
}
print(dim("\nWaiting for file changes..."));
watcher.on("change", (file) => {
const isSassFile = sassFileCheck(file);
let recompile = [];
if (inputs.includes(file)) {
recompile.push(file);
}
let dependants = [];
if (isSassFile) {
const { entries, hasNew } = getImportedSaasFiles(inputs, importedSaasFiles);
if (entries.length) {
if (hasNew) {
// Add new files from sass
importedSaasFiles = entries;
watcher.add(entries);
}
for (const input in files) {
const entry = files[input];
if (entry.sass && entry.importedFiles.includes(file)) {
recompile.push(input);
}
}
}
} else {
dependants = dependencyGraph
.dependantsOf(file)
.concat(getAncestorDirs(file).flatMap(dependencyGraph.dependantsOf));
recompile = recompile.concat(dependants.filter((file) => inputs.includes(file)));
}
if (!recompile.length) {
recompile = inputs;
}
return renderFiles([...new Set(recompile)])
.then((results) => watcher.add(dependencies(results)))
.catch(error);
});
}
});
}
function css(css, file, time) {
const { postcssOptions: { additionalPackagePathPrefixes } = [] } = config;
const combinedPaths = Array.isArray(additionalPackagePathPrefixes) ? additionalPackagePathPrefixes.map(e => {
return `${e}/`
}).join("|") : "";
const regexPattern = new RegExp(`(/_Resources/Static/Packages/)(?:${combinedPaths})?([\\w]+.[\\w]+/)Resources/Public/`, "g");
return rc()
.then((ctx) => {
return postcss(ctx.plugins)
.process(css, {
from: file.from,
to: file.to[0],
map: file.sourcemap
? {
absolute: false,
inline: false,
sourcesContent: true,
}
: null,
...(ctx.options || {}),
})
.then((result) => {
const tasks = [];
// This fixes url done with resolve()
result.css = result.css.replace(regexPattern,"$1$2");
const cssFilesize = humanFileSize(result.css.length);
let mapFilesize = 0;
file.to.forEach((to, index) => {
tasks.push(fs.outputFile(to, result.css));
if (compression && !file.inline) {
tasks.push(writeGz(to, result.css));
tasks.push(writeBr(to, result.css));
}
if (result.map) {
const file = `${to}.map`;
const map = result.map.toString();
tasks.push(
fs.outputFile(file, map, () => {
if (!watch && index === 0) {
const size = fs.statSync(file)?.size;
mapFilesize = humanFileSize(size);
}
})
);
if (compression) {
tasks.push(writeGz(file, map));
tasks.push(writeBr(file, map));
}
}
});
return Promise.all(tasks).then(() => {
const fileOuput = file.to[0];
const outputFilename = path.join(path.dirname(fileOuput), bold(path.basename(fileOuput)));
const spaces = " ".repeat(Math.max(outputLength - fileOuput.length, 0));
const duration = watch ? " " + magenta(humanDuration(process.hrtime(time))) : "";
print(` ${outputFilename} ${spaces} ${cyan(cssFilesize) + duration}`);
if (mapFilesize) {
print(` ${outputFilename}${bold(".map")} ${spaces} ${cyan(mapFilesize)}`);
}
result.warnings().forEach((warn) => {
print(warn.toString());
});
return result;
});
});
})
.catch((err) => {
throw err;
});
}
build();