Skip to content

Commit a2a0d8d

Browse files
committed
initial commit
0 parents  commit a2a0d8d

File tree

12 files changed

+12684
-0
lines changed

12 files changed

+12684
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true

.github/workflows/test.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Test
2+
on:
3+
push:
4+
pull_request:
5+
env:
6+
FORCE_COLOR: 2
7+
jobs:
8+
full:
9+
name: Node.js 15 Full
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout the repository
13+
uses: actions/checkout@v2
14+
- name: Install Node.js
15+
uses: actions/setup-node@v2
16+
with:
17+
node-version: 15
18+
- name: Install dependencies
19+
uses: bahmutov/npm-install@v1
20+
- name: Run tests
21+
run: yarn test
22+
short:
23+
runs-on: ubuntu-latest
24+
strategy:
25+
matrix:
26+
node-version:
27+
- 14
28+
- 12
29+
- 10
30+
name: Node.js ${{ matrix.node-version }} Quick
31+
steps:
32+
- name: Checkout the repository
33+
uses: actions/checkout@v2
34+
- name: Install Node.js ${{ matrix.node-version }}
35+
uses: actions/setup-node@v2
36+
with:
37+
node-version: ${{ matrix.node-version }}
38+
- name: Install dependencies
39+
uses: bahmutov/npm-install@v1
40+
- name: Run unit tests
41+
run: npx jest

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules/
2+
npm-debug.log
3+
yarn-error.log
4+
5+
coverage/

.npmignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
yarn-error.log
2+
npm-debug.log
3+
package-lock.json
4+
yarn.lock
5+
6+
*.test.js
7+
.travis.yml
8+
.github
9+
.editorconfig
10+
coverage/

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Change Log
2+
3+
This project adheres to [Semantic Versioning](http://semver.org/).

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright 2022 Ian VanSchooten <ian.vanschooten@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# postcss-assign-layer <!-- omit in toc -->
2+
3+
[PostCSS] plugin to assign a css cascade layer to files based on a glob pattern
4+
5+
[postcss]: https://github.com/postcss/postcss
6+
7+
## Table of Contents <!-- omit in toc -->
8+
9+
- [Why?](#why)
10+
- [Usage](#usage)
11+
- [**Step 1:** Install plugin](#step-1-install-plugin)
12+
- [**Step 2:** Check your project for existing PostCSS config:](#step-2-check-your-project-for-existing-postcss-config)
13+
- [**Step 3:** Add the plugin to plugins list (at the end)](#step-3-add-the-plugin-to-plugins-list-at-the-end)
14+
- [**Step 4:** Configure the plugin](#step-4-configure-the-plugin)
15+
- [`include`](#include)
16+
- [`layerName`](#layername)
17+
18+
## Why?
19+
20+
[CSS Cascade Layers] are a powerful new tool to give us more control over the cascade. [A Complete Guide to CSS Cascade Layers] is an excellent introduction that goes in depth into the details of how it works and the reasons you might want to use it.
21+
22+
One useful approach is to create different layers for defaults, components, and utilities, with increasing order of priority. So, components will always override a selector with the same specificity in the defaults layer, but utilities will override components. Defaults and utilities can be easily assigned up-front, in your global stylesheets imported at the root of your applicaiton. But you might have separate css files spread over your app for your components, especially if you're using an approach like [css modules].
23+
24+
That's where this plugin comes in. By default, it will wrap the contents of each `*.module.css` file into a `components` layer (both the glob and the name of the layer can be changed). So, for example, given a css module file like:
25+
26+
```css
27+
/* component.module.css */
28+
29+
.foo {
30+
color: rebeccapurple;
31+
}
32+
33+
.bar {
34+
color: blanchedalmond;
35+
}
36+
```
37+
38+
The end result will be:
39+
40+
```css
41+
/* component.module.css */
42+
43+
@layer components {
44+
.foo {
45+
color: rebeccapurple;
46+
}
47+
48+
.bar {
49+
color: blanchedalmond;
50+
}
51+
}
52+
```
53+
54+
Now, you can be assured that your component css will be added to the correct layer!
55+
56+
## Usage
57+
58+
### **Step 1:** Install plugin
59+
60+
```sh
61+
npm install --save-dev postcss postcss-assign-layer
62+
```
63+
64+
### **Step 2:** Check your project for existing PostCSS config:
65+
66+
`postcss.config.js` in the project root, `"postcss"` section in `package.json` or `postcss` in bundle config.
67+
68+
If you do not use PostCSS, add it according to [official docs] and set this plugin in settings.
69+
70+
### **Step 3:** Add the plugin to plugins list (at the end)
71+
72+
```diff
73+
module.exports = {
74+
plugins: [
75+
require('autoprefixer'),
76+
+ require('postcss-assign-layer')({
77+
+ include: '**/*.module.css',
78+
+ layerName: 'components',
79+
+ }),
80+
],
81+
}
82+
```
83+
84+
### **Step 4:** Configure the plugin
85+
86+
It's possible to configure the include glob, as well as the layer name.
87+
88+
#### `include`
89+
90+
Default: `"**/*.module.css"`
91+
92+
A valid [picomatch] pattern, or array of patterns. Note that `picomatch` patterns are very similar to [minimatch] patterns, and in most use cases, they are interchangeable. If you have more specific pattern matching needs, you can view [this comparison table] to learn more about where the libraries differ.
93+
94+
#### `layerName`
95+
96+
Default: `'components'`
97+
98+
The [layer name] that will be used.
99+
100+
[css cascade layers]: https://www.w3.org/TR/css-cascade-5/#layering
101+
[a complete guide to css cascade layers]: https://css-tricks.com/css-cascade-layers/
102+
[css modules]: https://github.com/css-modules/css-modules
103+
[official docs]: https://github.com/postcss/postcss#usage
104+
[picomatch]: https://github.com/micromatch/picomatch#globbing-features
105+
[minimatch]: https://github.com/isaacs/minimatch#readme
106+
[this comparison table]: https://github.com/micromatch/picomatch#library-comparisons
107+
[layer name]: https://www.w3.org/TR/css-cascade-5/#typedef-layer-name

index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const { createFilter } = require("@rollup/pluginutils");
2+
3+
/**
4+
* @type {import('postcss').PluginCreator}
5+
*/
6+
module.exports = ({
7+
include = "**/*.module.css",
8+
layerName = "components",
9+
} = {}) => {
10+
const filter = createFilter(include);
11+
12+
return {
13+
postcssPlugin: "postcss-assign-layers",
14+
async Once(root, { AtRule }) {
15+
const inputFile = root.source.input.file;
16+
if (!filter(inputFile)) return;
17+
18+
const layer = new AtRule({
19+
name: "layer",
20+
params: layerName,
21+
nodes: root.nodes,
22+
});
23+
root.removeAll();
24+
root.append(layer);
25+
},
26+
};
27+
};
28+
29+
module.exports.postcss = true;

index.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const postcss = require('postcss')
2+
3+
const plugin = require('./')
4+
5+
async function run (input, output, opts = { }) {
6+
let result = await postcss([plugin(opts)]).process(input, { from: undefined })
7+
expect(result.css).toEqual(output)
8+
expect(result.warnings()).toHaveLength(0)
9+
}
10+
11+
/* Write tests here
12+
13+
it('does something', async () => {
14+
await run('a{ }', 'a{ }', { })
15+
})
16+
17+
*/

0 commit comments

Comments
 (0)