-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloader.js
59 lines (49 loc) · 1.67 KB
/
loader.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
/* eslint @typescript-eslint/no-var-requires: "off" */
const minimatch = require("minimatch");
const { validateSchema } = require("webpack");
const OPTIONS_SCHEMA = {
type: "object",
required: ["layers"],
properties: {
layers: {
type: "array",
items: {
type: "object",
required: ["name"],
properties: {
path: {
description:
"All files that are matched with this value using minimatch package will be wrapped with this layer." +
"If undefined layer will only be included in layer order declaration (can be used for preexisting layers).",
type: "string",
},
name: {
description: "Name of layer",
type: "string",
},
},
},
},
},
};
module.exports = function (source) {
const options = this.getOptions() || {};
validateSchema(OPTIONS_SCHEMA, options, { name: "CSS Layering Loader" });
const layers = options.layers.filter((layer) => layer.path);
for (const layer of layers) {
const { path, name } = layer;
if (minimatch.minimatch(this.resourcePath, path)) {
return wrapSourceInLayer(source, name);
}
}
return source;
};
const wrapSourceInLayer = async (source, layerName) => {
const lines = source.split("\n");
const useLines = lines.filter((line) => line.trim().startsWith("@use"));
const otherLines = lines.filter((line) => !line.trim().startsWith("@use"));
const useLinesString = useLines.join("\n");
const otherLinesString = otherLines.join("\n");
return `${useLinesString}\n@layer ${layerName} {\n ${otherLinesString} \n}`;
};
module.exports.OPTIONS_SCHEMA = OPTIONS_SCHEMA;