Skip to content

Commit bfc468b

Browse files
committed
Support name option
1 parent b8655ff commit bfc468b

4 files changed

Lines changed: 141 additions & 27 deletions

File tree

.tape.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
module.exports = {
22
'postcss-extend-rule': {
33
'basic': {
4-
message: 'supports basic usage'
4+
message: 'supports @extend usage'
5+
},
6+
'basic:name': {
7+
message: 'ignores @extend usage when { name: "postcss-extend" }',
8+
options: {
9+
name: 'postcss-extend'
10+
},
11+
expect: 'basic.css'
12+
},
13+
'basic-postcss-name': {
14+
message: 'supports @postcss-extend when { name: "postcss-extend" }',
15+
options: {
16+
name: 'postcss-extend'
17+
},
18+
expect: 'basic.expect.css'
519
},
620
'advanced': {
7-
message: 'supports advanced usage (with postcss-nesting)',
21+
message: 'supports mixed usage (with postcss-nesting)',
822
plugin: () => require('postcss')(
923
require('postcss-nesting'),
1024
require('.')
@@ -14,14 +28,14 @@ module.exports = {
1428
'message': 'supports nested @media usage'
1529
},
1630
'nested-media:nesting-first': {
17-
'message': 'supports nested @media usage (with postcss-nesting running first)',
31+
'message': 'supports nested @media usage when postcss-nesting runs first',
1832
plugin: () => require('postcss')(
1933
require('postcss-nesting'),
2034
require('.')
2135
)
2236
},
2337
'nested-media:nesting-second': {
24-
'message': 'supports nested @media usage (with postcss-nesting running second)',
38+
'message': 'supports nested @medi usage when postcss-nesting runs second',
2539
plugin: () => require('postcss')(
2640
require('.'),
2741
require('postcss-nesting')

README.md

Lines changed: 104 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
[![NPM Version][npm-img]][npm-url]
44
[![Build Status][cli-img]][cli-url]
5-
[![Gitter Chat][git-img]][git-url]
5+
[![Windows Build Status][win-img]][win-url]
6+
[![Support Chat][git-img]][git-url]
67

78
[PostCSS Extend Rule] lets you use the `@extend` at-rule and
89
[Functional Selectors] in CSS, following the speculative
910
[CSS Extend Rules Specification].
1011

11-
```css
12+
```pcss
1213
%thick-border {
1314
border: thick dotted red;
1415
}
@@ -68,7 +69,9 @@ npm install postcss-extend-rule --save-dev
6869
Use [PostCSS Extend Rule] to process your CSS:
6970

7071
```js
71-
require('postcss-extend-rule').process(YOUR_CSS /*, PostCSS Options, Options */);
72+
import postcssExtend from 'postcss-extend-rule';
73+
74+
postcssExtend.process(YOUR_CSS, /* processOptions */ /*, pluginOptions */);
7275
```
7376

7477
#### PostCSS
@@ -82,11 +85,48 @@ npm install postcss --save-dev
8285
Use [PostCSS Extend Rule] as a plugin:
8386

8487
```js
88+
import postcss from 'gulp-postcss';
89+
import postcssExtend from 'postcss-extend-rule';
90+
8591
postcss([
86-
require('postcss-extend-rule')(/* Options */)
92+
postcssExtend(/* pluginOptions */)
8793
]).process(YOUR_CSS);
8894
```
8995

96+
#### Webpack
97+
98+
Add [PostCSS Loader] to your build tool:
99+
100+
```bash
101+
npm install postcss-loader --save-dev
102+
```
103+
104+
Use [PostCSS Extend Rule] in your Webpack configuration:
105+
106+
```js
107+
import postcssExtend from 'postcss-extend-rule';
108+
109+
module.exports = {
110+
module: {
111+
rules: [
112+
{
113+
test: /\.css$/,
114+
use: [
115+
'style-loader',
116+
{ loader: 'css-loader', options: { importLoaders: 1 } },
117+
{ loader: 'postcss-loader', options: {
118+
ident: 'postcss',
119+
plugins: () => [
120+
postcssExtend(/* pluginOptions */)
121+
]
122+
} }
123+
]
124+
}
125+
]
126+
}
127+
}
128+
```
129+
90130
#### Gulp
91131

92132
Add [Gulp PostCSS] to your build tool:
@@ -98,17 +138,16 @@ npm install gulp-postcss --save-dev
98138
Use [PostCSS Extend Rule] in your Gulpfile:
99139

100140
```js
101-
var postcss = require('gulp-postcss');
102-
103-
gulp.task('css', function () {
104-
return gulp.src('./src/*.css').pipe(
105-
postcss([
106-
require('postcss-extend-rule')(/* Options */)
107-
])
108-
).pipe(
109-
gulp.dest('.')
110-
);
111-
});
141+
import postcss from 'gulp-postcss';
142+
import postcssExtend from 'postcss-extend-rule';
143+
144+
gulp.task('css', () => gulp.src('./src/*.css').pipe(
145+
postcss([
146+
postcssExtend(/* pluginOptions */)
147+
])
148+
).pipe(
149+
gulp.dest('.')
150+
));
112151
```
113152

114153
#### Grunt
@@ -122,13 +161,15 @@ npm install grunt-postcss --save-dev
122161
Use [PostCSS Extend Rule] in your Gruntfile:
123162

124163
```js
164+
import postcssExtend from 'postcss-extend-rule';
165+
125166
grunt.loadNpmTasks('grunt-postcss');
126167

127168
grunt.initConfig({
128169
postcss: {
129170
options: {
130171
use: [
131-
require('postcss-extend-rule')(/* Options */)
172+
postcssExtend(/* pluginOptions */)
132173
]
133174
},
134175
dist: {
@@ -140,6 +181,18 @@ grunt.initConfig({
140181

141182
## Options
142183

184+
### name
185+
186+
The `name` option determines the at-rule name being used to extend selectors.
187+
By default, this name is `extend`, meaning `@extend` rules are parsed.
188+
189+
```js
190+
postcssExtend({ name: 'postcss-extend' })
191+
```
192+
193+
If the `name` option were changed to, say, `postcss-extend`, then only
194+
`@postcss-extend` at-rules would be parsed.
195+
143196
### onFunctionalSelector
144197

145198
The `onFunctionalSelector` option determines how functional selectors should be
@@ -150,6 +203,14 @@ handled. Its options are:
150203
- `warn` warns the user whenever it encounters a functional selector
151204
- `throw` throws an error if ever it encounters a functional selector
152205

206+
```js
207+
postcssExtend({ onFunctionalSelector: 'remove' /* default */ })
208+
```
209+
210+
```pcss
211+
%this-will-be-removed {}
212+
```
213+
153214
### onRecursiveExtend
154215

155216
The `onRecursiveExtend` option determines how recursive extend at-rules should
@@ -160,6 +221,16 @@ be handled. Its options are:
160221
- `warn` warns the user whenever it encounters a recursive extend at-rules
161222
- `throw` throws an error if ever it encounters a recursive extend at-rules
162223

224+
```js
225+
postcssExtend({ onRecursiveExtend: 'remove' /* default */ })
226+
```
227+
228+
```pcss
229+
.this-will-not-extend-itself {
230+
@extend .this-will-not-extend-itself;
231+
}
232+
```
233+
163234
### onUnusedExtend
164235

165236
The `onUnusedExtend` option determines how an unused extend at-rule should be
@@ -170,16 +241,29 @@ handled. Its options are:
170241
- `warn` warns the user whenever it encounters an unused extend at-rule
171242
- `throw` throws an error if ever it encounters an unused extend at-rule
172243

173-
[npm-url]: https://www.npmjs.com/package/postcss-extend-rule
174-
[npm-img]: https://img.shields.io/npm/v/postcss-extend-rule.svg
175-
[cli-url]: https://travis-ci.org/jonathantneal/postcss-extend-rule
244+
```js
245+
postcssExtend({ onUnusedExtend: 'remove' /* default */ })
246+
```
247+
248+
```pcss
249+
main {
250+
@extend .this-selector-does-not-exist-and-will-be-removed;
251+
}
252+
```
253+
176254
[cli-img]: https://img.shields.io/travis/jonathantneal/postcss-extend-rule.svg
255+
[cli-url]: https://travis-ci.org/jonathantneal/postcss-extend-rule
256+
[git-img]: https://img.shields.io/badge/support-chat-blue.svg
177257
[git-url]: https://gitter.im/postcss/postcss
178-
[git-img]: https://img.shields.io/badge/chat-gitter-blue.svg
258+
[npm-img]: https://img.shields.io/npm/v/postcss-extend-rule.svg
259+
[npm-url]: https://www.npmjs.com/package/postcss-extend-rule
260+
[win-img]: https://img.shields.io/appveyor/ci/jonathantneal/postcss-extend-rule.svg
261+
[win-url]: https://ci.appveyor.com/project/jonathantneal/postcss-extend-rule
179262

180263
[CSS Extend Rules Specification]: https://jonathantneal.github.io/specs/css-extend-rule/
181264
[Functional Selectors]: https://jonathantneal.github.io/specs/css-extend-rule/#functional-selector
182265
[Gulp PostCSS]: https://github.com/postcss/gulp-postcss
183266
[Grunt PostCSS]: https://github.com/nDmitry/grunt-postcss
184267
[PostCSS]: https://github.com/postcss/postcss
268+
[PostCSS Loader]: https://github.com/postcss/postcss-loader
185269
[PostCSS Extend Rule]: https://github.com/jonathantneal/postcss-extend-rule

index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import postcss from 'postcss';
22
import nesting from 'postcss-nesting';
33

4-
// extend at-rule match
5-
const extendMatch = /^(extend)$/i;
6-
74
// functional selector match
85
const functionalSelectorMatch = /(^|[^\w-])(%[_a-zA-Z]+[_a-zA-Z0-9-]*)([^\w-]|$)/i;
96

107
// plugin
118
export default postcss.plugin('postcss-extend-rule', rawopts => {
129
// options ( onFunctionalSelector, onRecursiveExtend, onUnusedExtend)
1310
const opts = Object(rawopts);
11+
const extendMatch = opts.name instanceof RegExp
12+
? opts.name
13+
: 'name' in opts
14+
? new RegExp(`^${opts.name}$`, 'i')
15+
: 'extend';
1416

1517
return (root, result) => {
1618
// for each extend at-rule

test/basic-postcss-name.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.modal {
2+
border: thick dotted red;
3+
color: red;
4+
}
5+
6+
.modal:hover {
7+
outline: none;
8+
}
9+
10+
.serious-modal {
11+
@postcss-extend .modal;
12+
13+
font-weight: bold;
14+
}

0 commit comments

Comments
 (0)