-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path.eleventy.js
119 lines (104 loc) · 2.84 KB
/
.eleventy.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
const fs = require("node:fs");
const path = require("node:path");
const browserslist = require("browserslist");
const {
bundle,
browserslistToTargets,
composeVisitors,
} = require("lightningcss");
// Set default transpiling targets
let browserslistTargets = "> 0.2% and not dead";
// Check for user's browserslist
try {
const package = path.resolve(__dirname, fs.realpathSync("package.json"));
const userPkgBrowserslist = require(package);
if (userPkgBrowserslist.browserslist) {
browserslistTargets = userPkgBrowserslist.browserslist;
} else {
try {
const browserslistrc = path.resolve(
__dirname,
fs.realpathSync(".browserslistrc")
);
fs.readFile(browserslistrc, "utf8", (_err, data) => {
if (data.length) {
browserslistTargets = [];
}
data.split(/\r?\n/).forEach((line) => {
if (line.length && !line.startsWith("#")) {
browserslistTargets.push(line);
}
});
});
} catch (err) {
// no .browserslistrc
}
}
} catch (err) {
// no package browserslist
}
module.exports = (eleventyConfig, options) => {
const defaults = {
importPrefix: "_",
nesting: true,
customMedia: true,
minify: true,
sourceMap: false,
visitors: [],
customAtRules: {},
};
const {
importPrefix,
nesting,
customMedia,
minify,
sourceMap,
visitors,
customAtRules,
} = {
...defaults,
...options,
};
// Recognize CSS as a "template language"
eleventyConfig.addTemplateFormats("css");
// Process CSS with LightningCSS
eleventyConfig.addExtension("css", {
outputFileExtension: "css",
compile: async function (_inputContent, inputPath) {
let parsed = path.parse(inputPath);
if (parsed.name.startsWith(importPrefix)) {
return;
}
// Support @import triggering regeneration for incremental builds
// h/t @julientaq for the fix
if (_inputContent.includes("@import")) {
// for each file create a list of files to look at
const fileList = [];
// get a list of import on the file your reading
const importRuleRegex =
/@import\s+(?:url\()?['"]?([^'"\);]+)['"]?\)?.*;/g;
let match;
while ((match = importRuleRegex.exec(_inputContent))) {
fileList.push(parsed.dir + "/" + match[1]);
}
this.addDependencies(inputPath, fileList);
}
let targets = browserslistToTargets(browserslist(browserslistTargets));
return async () => {
let { code } = await bundle({
filename: inputPath,
minify,
sourceMap,
targets,
drafts: {
nesting,
customMedia,
},
customAtRules,
visitor: composeVisitors(visitors),
});
return code;
};
},
});
};