Skip to content

Commit 70a4b8b

Browse files
author
Lars Munkholm
committed
first commit
0 parents  commit 70a4b8b

11 files changed

+4502
-0
lines changed

.editorconfig

+9
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

+41
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

+5
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

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

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Change Log
2+
3+
## Release 1.0.0 (2021-08-20)
4+
5+
- Initial release

LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright 2021 Lars Munkholm <[email protected]>
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

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# PostCSS Prefix Hover
2+
3+
[PostCSS] plugin for prefixing a selection containing :hover.
4+
5+
[PostCSS]: https://github.com/postcss/postcss
6+
7+
```css
8+
/* Input example */
9+
.foo a:hover {
10+
color: black;
11+
}
12+
```
13+
14+
```css
15+
/* Output example */
16+
.using-mouse .foo a:hover {
17+
color: black;
18+
}
19+
```
20+
21+
## Usage
22+
23+
**Step 1:** Install plugin:
24+
25+
```sh
26+
npm install --save-dev postcss postcss-prefix-hover
27+
```
28+
29+
**Step 2:** Check you project for existed PostCSS config: `postcss.config.js`
30+
in the project root, `"postcss"` section in `package.json`
31+
or `postcss` in bundle config.
32+
33+
If you do not use PostCSS, add it according to [official docs]
34+
and set this plugin in settings.
35+
36+
**Step 3:** Add the plugin to plugins list:
37+
38+
```diff
39+
module.exports = {
40+
plugins: [
41+
+ require('postcss-prefix-hover'),
42+
require('autoprefixer')
43+
]
44+
}
45+
```
46+
47+
[official docs]: https://github.com/postcss/postcss#usage

index.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module.exports = (opts) => {
2+
const useCssModulesGlobal = opts && typeof opts.useCssModulesGlobal !== "undefined" ? opts.useCssModulesGlobal : false;
3+
const prefix = opts && typeof opts.prefix !== "undefined" ? opts.prefix : ".using-mouse";
4+
5+
function setPrefix (selector) {
6+
const thePefix = (useCssModulesGlobal ? ":global(" : "") + prefix + (useCssModulesGlobal ? ")" : "");
7+
return thePefix + " " + selector.replace(/,\s+/g, ", " + thePefix + " ");
8+
}
9+
10+
return {
11+
postcssPlugin: 'postcss-prefix-hover',
12+
Root (root) {
13+
root.walkRules((rule) => {
14+
if (rule.selector.indexOf(":hover") > -1) {
15+
rule.selector = setPrefix(rule.selector);
16+
}
17+
});
18+
}
19+
}
20+
}
21+
22+
module.exports.postcss = true;

index.test.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
it("with default settings", async () => {
12+
await run(
13+
'main .container a:hover { color: black; }',
14+
'.using-mouse main .container a:hover { color: black; }',
15+
{}
16+
)
17+
});
18+
19+
it("with prefix option", async () => {
20+
await run(
21+
'a:hover { color: black; }',
22+
'.prefix a:hover { color: black; }',
23+
{ prefix: ".prefix" }
24+
)
25+
});
26+
27+
it("useCssModulesGlobal", async () => {
28+
await run(
29+
'a:hover { color: black; }',
30+
':global(.mouse) a:hover { color: black; }',
31+
{ prefix: ".mouse", useCssModulesGlobal: true }
32+
)
33+
});
34+
35+
it("nested and has children", async () => {
36+
await run(
37+
'.tis, .prut { &:hover { &::before { color: black; } } }',
38+
'.tis, .prut { .using-mouse &:hover { &::before { color: black; } } }',
39+
{}
40+
)
41+
});

package.json

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"name": "postcss-prefix-hover",
3+
"version": "1.0.0",
4+
"description": "PostCSS plugin for prefixing a selection containing :hover",
5+
"keywords": [
6+
"postcss",
7+
"css",
8+
"postcss-plugin",
9+
"hover"
10+
],
11+
"scripts": {
12+
"test": "jest --coverage && eslint ."
13+
},
14+
"author": "Lars Munkholm <[email protected]>",
15+
"license": "MIT",
16+
"repository": "larsmunkholm/postcss-prefix-hover",
17+
"engines": {
18+
"node": ">=10.0.0"
19+
},
20+
"peerDependencies": {
21+
"postcss": "^8.2.8"
22+
},
23+
"devDependencies": {
24+
"clean-publish": "^2.1.0",
25+
"eslint": "^7.21.0",
26+
"eslint-plugin-jest": "^24.2.1",
27+
"jest": "^26.6.3",
28+
"lint-staged": "^10.5.4",
29+
"postcss": "^8.2.8",
30+
"simple-git-hooks": "^2.0.2"
31+
},
32+
"simple-git-hooks": {
33+
"pre-commit": "npx lint-staged"
34+
},
35+
"lint-staged": {
36+
"*.js": "eslint --fix"
37+
},
38+
"eslintConfig": {
39+
"parserOptions": {
40+
"ecmaVersion": 2017
41+
},
42+
"env": {
43+
"node": true,
44+
"es6": true
45+
},
46+
"extends": [
47+
"eslint:recommended",
48+
"plugin:jest/recommended"
49+
],
50+
"rules": {
51+
"jest/expect-expect": "off"
52+
}
53+
},
54+
"jest": {
55+
"testEnvironment": "node",
56+
"coverageThreshold": {
57+
"global": {
58+
"statements": 100
59+
}
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)