-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
postcss.js
64 lines (53 loc) · 1.49 KB
/
postcss.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
import {
autoprefixer,
postcss,
postcssImport,
postcssNesting,
} from "../deps/postcss.js";
import { merge } from "../utils.js";
// Default options
const defaults = {
extensions: [".css"],
sourceMap: false,
includes: false,
plugins: [
postcssNesting(),
autoprefixer(),
],
};
export default function (userOptions = {}) {
return (site) => {
const options = merge({
...defaults,
includes: site.src("_includes"),
}, userOptions);
const plugins = [...options.plugins];
if (options.includes) {
plugins.unshift(postcssImport({
path: options.includes,
}));
}
const runner = postcss(plugins);
site.loadAssets(options.extensions);
site.process(options.extensions, processor);
site.filter("postcss", filter, true);
async function processor(page) {
const from = site.src(page.src.path + page.src.ext);
const to = site.dest(page.dest.path + page.dest.ext);
const map = options.sourceMap ? { inline: false } : undefined;
// Fix the code with postcss
const result = await runner.process(page.content, { from, to, map });
page.content = result.css;
if (result.map) {
const mapFile = page.duplicate();
mapFile.content = result.map.toString();
mapFile.dest.ext = ".css.map";
site.pages.push(mapFile);
}
}
async function filter(code) {
const result = await runner.process(code, { from: undefined });
return result.css;
}
};
}