Skip to content

Commit 2f5d242

Browse files
authored
Breaking: Turn rule config into array (#2)
Fixes #1 This changes the config from a single object into an array of objects. This allows more flexibility if you have multiple different kinds of files that you want to wrap in layers (though if you only have a few, you should probably just add the layers to the files themselves for extra clarity when editing).
1 parent f16bc5b commit 2f5d242

File tree

3 files changed

+131
-23
lines changed

3 files changed

+131
-23
lines changed

README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,32 @@ If you do not use PostCSS, add it according to [official docs] and set this plug
7373
module.exports = {
7474
plugins: [
7575
require('autoprefixer'),
76-
+ require('postcss-assign-layer')({
77-
+ include: '**/*.module.css',
78-
+ layerName: 'components',
79-
+ }),
76+
+ require('postcss-assign-layer')(/* Optional configuration */),
8077
],
8178
}
8279
```
8380

8481
### **Step 4:** Configure the plugin
8582

86-
It's possible to configure the include glob, as well as the layer name.
83+
It's possible to configure the include glob and the layer name, and more than one set of rules can be provided.
84+
85+
```diff
86+
module.exports = {
87+
plugins: [
88+
require('autoprefixer'),
89+
+ require('postcss-assign-layer')([
90+
+ {
91+
+ include: '**/*.module.css',
92+
+ layerName: 'components',
93+
+ },
94+
+ {
95+
+ include: 'global/*.css',
96+
+ layerName: 'base',
97+
+ },
98+
+ ]),
99+
],
100+
}
101+
```
87102

88103
#### `include`
89104

index.js

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,47 @@
11
const { createFilter } = require("@rollup/pluginutils");
22

3+
const DEFAULT_INCLUDE = "**/*.module.css";
4+
const DEFAULT_LAYERNAME = "components";
5+
36
/**
47
* @type {import('postcss').PluginCreator}
58
*/
6-
module.exports = ({
7-
include = "**/*.module.css",
8-
layerName = "components",
9-
} = {}) => {
10-
const filter = createFilter(include);
9+
module.exports = (
10+
configItems = [
11+
{
12+
include: DEFAULT_INCLUDE,
13+
layerName: DEFAULT_LAYERNAME,
14+
},
15+
]
16+
) => {
17+
const filters = [];
18+
19+
for (const config of configItems) {
20+
const filter = createFilter(config.include ?? DEFAULT_INCLUDE);
21+
filters.push({ filter, layerName: config.layerName ?? DEFAULT_LAYERNAME });
22+
}
1123

1224
return {
1325
postcssPlugin: "postcss-assign-layers",
1426
async Once(root, { AtRule }) {
1527
const inputFile = root.source.input.file;
16-
if (!filter(inputFile)) return;
28+
const layerNames = [];
29+
30+
for (const { filter, layerName } of filters) {
31+
if (filter(inputFile)) {
32+
layerNames.push(layerName);
33+
}
34+
}
1735

18-
const layer = new AtRule({
19-
name: "layer",
20-
params: layerName,
21-
nodes: root.nodes,
22-
});
23-
root.removeAll();
24-
root.append(layer);
36+
for (const layerName of layerNames) {
37+
const layer = new AtRule({
38+
name: "layer",
39+
params: layerName,
40+
nodes: root.nodes,
41+
});
42+
root.removeAll();
43+
root.append(layer);
44+
}
2545
},
2646
};
2747
};

index.test.mjs

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe("postcss-assign-layer", () => {
5353
color: WhiteSmoke;
5454
}
5555
}`,
56-
{ layerName: "custom" },
56+
[{ layerName: "custom" }],
5757
{
5858
from: filePath,
5959
}
@@ -85,9 +85,11 @@ describe("postcss-assign-layer", () => {
8585
color: FireBrick;
8686
}
8787
}`,
88-
{
89-
include: "**/base.css",
90-
},
88+
[
89+
{
90+
include: "**/base.css",
91+
},
92+
],
9193
{
9294
from: filePath,
9395
}
@@ -106,11 +108,82 @@ describe("postcss-assign-layer", () => {
106108
i {
107109
color: WhiteSmoke;
108110
}`,
111+
[
112+
{
113+
include: "**/base.css",
114+
},
115+
],
116+
{
117+
from: filePath,
118+
}
119+
);
120+
});
121+
122+
it("allows specifying layer name and pattern", async () => {
123+
const filePath = path.resolve("test/fixtures/base.css");
124+
const file = readFileSync(filePath, "utf-8");
125+
await run(
126+
file,
127+
`@layer styles {
128+
a {
129+
color: FireBrick;
130+
}
131+
}`,
132+
[
133+
{
134+
include: "**/base.css",
135+
layerName: "styles",
136+
},
137+
],
138+
{
139+
from: filePath,
140+
}
141+
);
142+
});
143+
144+
it("allows multiple patterns and layers", async () => {
145+
const basePath = path.resolve("test/fixtures/base.css");
146+
const componentPath = path.resolve("test/fixtures/component.module.css");
147+
const baseFile = readFileSync(basePath, "utf-8");
148+
const componentFile = readFileSync(componentPath, "utf-8");
149+
const config = [
109150
{
110151
include: "**/base.css",
152+
layerName: "styles",
111153
},
112154
{
113-
from: filePath,
155+
// include: "**/*.module.css", <- the default is still used for unspecified properties
156+
layerName: "components",
157+
},
158+
];
159+
160+
await run(
161+
baseFile,
162+
`@layer styles {
163+
a {
164+
color: FireBrick;
165+
}
166+
}`,
167+
config,
168+
{
169+
from: basePath,
170+
}
171+
);
172+
173+
await run(
174+
componentFile,
175+
`@layer components {
176+
a {
177+
color: BurlyWood;
178+
}
179+
180+
i {
181+
color: WhiteSmoke;
182+
}
183+
}`,
184+
config,
185+
{
186+
from: componentPath,
114187
}
115188
);
116189
});

0 commit comments

Comments
 (0)