postcss 6.0.22 → 7.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of postcss might be problematic. Click here for more details.
- package/CHANGELOG.md +18 -0
- package/README-cn.md +54 -51
- package/README.md +60 -52
- package/docs/architecture.md +2 -2
- package/docs/guidelines/plugin.md +28 -28
- package/docs/guidelines/runner.md +21 -21
- package/docs/source-maps.md +11 -11
- package/docs/syntax.md +23 -23
- package/gulpfile.js +78 -74
- package/lib/at-rule.js +14 -14
- package/lib/comment.js +4 -4
- package/lib/container.js +773 -801
- package/lib/css-syntax-error.js +197 -180
- package/lib/declaration.js +15 -14
- package/lib/input.js +149 -143
- package/lib/lazy-result.js +364 -337
- package/lib/list.js +69 -70
- package/lib/map-generator.js +270 -275
- package/lib/node.js +558 -570
- package/lib/parse.js +20 -21
- package/lib/parser.js +476 -479
- package/lib/postcss.d.ts +1246 -1246
- package/lib/postcss.js +61 -57
- package/lib/previous-map.js +117 -122
- package/lib/processor.js +96 -75
- package/lib/result.js +70 -64
- package/lib/root.js +91 -90
- package/lib/rule.js +12 -12
- package/lib/stringifier.js +295 -295
- package/lib/stringify.js +5 -4
- package/lib/terminal-highlight.js +50 -50
- package/lib/tokenize.js +259 -259
- package/lib/vendor.js +27 -27
- package/lib/warn-once.js +6 -4
- package/lib/warning.js +30 -26
- package/package.json +4 -3
- package/CONTRIBUTING.md +0 -78
package/CHANGELOG.md
CHANGED
@@ -1,6 +1,24 @@
|
|
1
1
|
# Change Log
|
2
2
|
This project adheres to [Semantic Versioning](http://semver.org/).
|
3
3
|
|
4
|
+
## 7.0.2
|
5
|
+
* Fix warning text (by Rui Pedro M Lima).
|
6
|
+
|
7
|
+
## 7.0.1
|
8
|
+
* Fix JSDoc (by Steven Lambert).
|
9
|
+
|
10
|
+
## 7.0 “President Amy”
|
11
|
+
* Remove Node.js 9 and Node.js 4 support.
|
12
|
+
* Remove IE and “dead” browsers from Babel.
|
13
|
+
* Add CSS position on error happened inside `walk()` (by Nikhil Gaba).
|
14
|
+
* Add `LazyResult#finally` (by Igor Kamyshev).
|
15
|
+
* Add warning on calling PostCSS without plugins and syntax options.
|
16
|
+
* Reduce client-side size.
|
17
|
+
|
18
|
+
## 6.0.23
|
19
|
+
* Fix parsing nested at-rules without semicolon, params, and spaces.
|
20
|
+
* Fix docs (by Kevin Schiffer and Pat Cavit).
|
21
|
+
|
4
22
|
## 6.0.22
|
5
23
|
* Fix `Node#prev` and `Node#next` on missed parent.
|
6
24
|
|
package/README-cn.md
CHANGED
@@ -14,6 +14,8 @@ PostCSS 是一个允许使用 JS 插件转换样式的工具。
|
|
14
14
|
PostCSS 在工业界被广泛地应用,其中不乏很多有名的行业领导者,如:维基百科,Twitter,阿里巴巴,
|
15
15
|
JetBrains。PostCSS 的 [Autoprefixer] 插件是最流行的 CSS 处理工具之一。
|
16
16
|
|
17
|
+
PostCSS 接收一个 CSS 文件并提供了一个 API 来分析、修改它的规则(通过把 CSS 规则转换成一个[抽象语法树]的方式)。在这之后,这个 API 便可被许多[插件]利用来做有用的事情,比如寻错或自动添加 CSS vendor 前缀。
|
18
|
+
|
17
19
|
**Twitter 账号:** [@postcss](https://twitter.com/postcss)<br>
|
18
20
|
**支持 / 讨论:** [Gitter](https://gitter.im/postcss/postcss)<br>
|
19
21
|
|
@@ -21,7 +23,9 @@ JetBrains。PostCSS 的 [Autoprefixer] 插件是最流行的 CSS 处理工具之
|
|
21
23
|
PostCSS 插件),请联系 [Evil Martians](https://evilmartians.com/?utm_source=postcss)
|
22
24
|
邮箱 <surrender@evilmartians.com>。
|
23
25
|
|
26
|
+
[抽象语法树]: https://zh.wikipedia.org/wiki/%E6%8A%BD%E8%B1%A1%E8%AA%9E%E6%B3%95%E6%A8%B9
|
24
27
|
[Autoprefixer]: https://github.com/postcss/autoprefixer
|
28
|
+
[插件]: https://github.com/postcss/postcss/blob/master/README-cn.md#%E6%8F%92%E4%BB%B6
|
25
29
|
|
26
30
|
<a href="https://evilmartians.com/?utm_source=postcss">
|
27
31
|
<img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg"
|
@@ -50,7 +54,6 @@ PostCSS 插件),请联系 [Evil Martians](https://evilmartians.com/?utm_sou
|
|
50
54
|
|
51
55
|
* [`autoprefixer`] 添加了 vendor 浏览器前缀,它使用 Can I Use 上面的数据。
|
52
56
|
* [`postcss-preset-env`] 允许你使用未来的 CSS 特性。
|
53
|
-
* [`postcss-image-set-polyfill`] 为所有浏览器模拟了 [`image-set`] 函数逻辑。
|
54
57
|
|
55
58
|
### 更佳的 CSS 可读性
|
56
59
|
|
@@ -81,7 +84,6 @@ PostCSS 插件),请联系 [Evil Martians](https://evilmartians.com/?utm_sou
|
|
81
84
|
* [`lost`] 是一个功能强大的 `calc()` 栅格系统。
|
82
85
|
* [`rtlcss`] 镜像翻转 CSS 样式,适用于 right-to-left 的应用场景。
|
83
86
|
|
84
|
-
[`postcss-image-set-polyfill`]: https://github.com/SuperOl3g/postcss-image-set-polyfill
|
85
87
|
[`postcss-inline-svg`]: https://github.com/TrySound/postcss-inline-svg
|
86
88
|
[`postcss-preset-env`]: https://github.com/jonathantneal/postcss-preset-env
|
87
89
|
[`react-css-modules`]: https://github.com/gajus/react-css-modules
|
@@ -102,7 +104,6 @@ PostCSS 插件),请联系 [Evil Martians](https://evilmartians.com/?utm_sou
|
|
102
104
|
[`css-modules`]: https://github.com/css-modules/css-modules
|
103
105
|
[`colorguard`]: https://github.com/SlexAxton/css-colorguard
|
104
106
|
[`stylelint`]: https://github.com/stylelint/stylelint
|
105
|
-
[`image-set`]: https://drafts.csswg.org/css-images-4/#image-set-notation
|
106
107
|
[`stylefmt`]: https://github.com/morishitter/stylefmt
|
107
108
|
[`cssnano`]: http://cssnano.co
|
108
109
|
[`precss`]: https://github.com/jonathantneal/precss
|
@@ -120,6 +121,7 @@ PostCSS 可以转化样式到任意语法,不仅仅是 CSS。
|
|
120
121
|
* [`postcss-syntax`] 通过文件扩展名自动切换语法。
|
121
122
|
* [`postcss-html`] 解析类 HTML 文件里`<style>`标签中的样式。
|
122
123
|
* [`postcss-markdown`] 解析 Markdown 文件里代码块中的样式。
|
124
|
+
* [`postcss-jsx`] 解析源文件里模板或对象字面量中的CSS。
|
123
125
|
* [`postcss-styled`] 解析源文件里模板字面量中的CSS。
|
124
126
|
* [`postcss-scss`] 允许你使用 SCSS *(但并没有将 SCSS 编译到 CSS)*。
|
125
127
|
* [`postcss-sass`] 允许你使用 Sass *(但并没有将 Sass 编译到 CSS)*。
|
@@ -134,6 +136,7 @@ PostCSS 可以转化样式到任意语法,不仅仅是 CSS。
|
|
134
136
|
[`postcss-syntax`]: https://github.com/gucong3000/postcss-syntax
|
135
137
|
[`postcss-html`]: https://github.com/gucong3000/postcss-html
|
136
138
|
[`postcss-markdown`]: https://github.com/gucong3000/postcss-markdown
|
139
|
+
[`postcss-jsx`]: https://github.com/gucong3000/postcss-jsx
|
137
140
|
[`postcss-styled`]: https://github.com/gucong3000/postcss-styled
|
138
141
|
[`postcss-scss`]: https://github.com/postcss/postcss-scss
|
139
142
|
[`postcss-sass`]: https://github.com/AleshaOleg/postcss-sass
|
@@ -169,28 +172,28 @@ PostCSS 可以转化样式到任意语法,不仅仅是 CSS。
|
|
169
172
|
|
170
173
|
```js
|
171
174
|
module.exports = {
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
}
|
186
|
-
},
|
187
|
-
{
|
188
|
-
loader: 'postcss-loader'
|
189
|
-
}
|
190
|
-
]
|
175
|
+
module: {
|
176
|
+
rules: [
|
177
|
+
{
|
178
|
+
test: /\.css$/,
|
179
|
+
exclude: /node_modules/,
|
180
|
+
use: [
|
181
|
+
{
|
182
|
+
loader: 'style-loader',
|
183
|
+
},
|
184
|
+
{
|
185
|
+
loader: 'css-loader',
|
186
|
+
options: {
|
187
|
+
importLoaders: 1,
|
191
188
|
}
|
189
|
+
},
|
190
|
+
{
|
191
|
+
loader: 'postcss-loader'
|
192
|
+
}
|
192
193
|
]
|
193
|
-
|
194
|
+
}
|
195
|
+
]
|
196
|
+
}
|
194
197
|
}
|
195
198
|
```
|
196
199
|
|
@@ -198,10 +201,10 @@ module.exports = {
|
|
198
201
|
|
199
202
|
```js
|
200
203
|
module.exports = {
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
204
|
+
plugins: [
|
205
|
+
require('precss'),
|
206
|
+
require('autoprefixer')
|
207
|
+
]
|
205
208
|
}
|
206
209
|
```
|
207
210
|
|
@@ -212,16 +215,16 @@ module.exports = {
|
|
212
215
|
使用 [`gulp-postcss`] 和 [`gulp-sourcemaps`].
|
213
216
|
|
214
217
|
```js
|
215
|
-
gulp.task('css',
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
})
|
218
|
+
gulp.task('css', () => {
|
219
|
+
const postcss = require('gulp-postcss')
|
220
|
+
const sourcemaps = require('gulp-sourcemaps')
|
221
|
+
|
222
|
+
return gulp.src('src/**/*.css')
|
223
|
+
.pipe( sourcemaps.init() )
|
224
|
+
.pipe( postcss([ require('precss'), require('autoprefixer') ]) )
|
225
|
+
.pipe( sourcemaps.write('.') )
|
226
|
+
.pipe( gulp.dest('build/') )
|
227
|
+
})
|
225
228
|
```
|
226
229
|
|
227
230
|
[`gulp-sourcemaps`]: https://github.com/floridoo/gulp-sourcemaps
|
@@ -246,10 +249,10 @@ postcss --use autoprefixer -c options.json -o main.css css/*.css
|
|
246
249
|
你可以用 [`postcss-js`] 然后转换样式对象。
|
247
250
|
|
248
251
|
```js
|
249
|
-
var postcss = require('postcss-js')
|
250
|
-
var prefixer = postcss.sync([ require('autoprefixer') ])
|
252
|
+
var postcss = require('postcss-js')
|
253
|
+
var prefixer = postcss.sync([ require('autoprefixer') ])
|
251
254
|
|
252
|
-
prefixer({ display: 'flex' })
|
255
|
+
prefixer({ display: 'flex' }) //=> { display: ['-webkit-box', '-webkit-flex', '-ms-flexbox', 'flex'] }
|
253
256
|
```
|
254
257
|
|
255
258
|
[`postcss-js`]: https://github.com/postcss/postcss-js
|
@@ -276,19 +279,19 @@ prefixer({ display: 'flex' }); //=> { display: ['-webkit-box', '-webkit-flex', '
|
|
276
279
|
对于其它的应用环境,你可以使用 JS API:
|
277
280
|
|
278
281
|
```js
|
279
|
-
const
|
280
|
-
const postcss = require('postcss')
|
281
|
-
const precss = require('precss')
|
282
|
-
const
|
282
|
+
const autoprefixer = require('autoprefixer')
|
283
|
+
const postcss = require('postcss')
|
284
|
+
const precss = require('precss')
|
285
|
+
const fs = require('fs')
|
283
286
|
|
284
287
|
fs.readFile('src/app.css', (err, css) => {
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
})
|
288
|
+
postcss([precss, autoprefixer])
|
289
|
+
.process(css, { from: 'src/app.css', to: 'dest/app.css' })
|
290
|
+
.then(result => {
|
291
|
+
fs.writeFile('dest/app.css', result.css)
|
292
|
+
if ( result.map ) fs.writeFile('dest/app.css.map', result.map)
|
293
|
+
})
|
294
|
+
})
|
292
295
|
```
|
293
296
|
|
294
297
|
阅读 [PostCSS API 文档] 获取更多有关 JS API 的信息.
|
package/README.md
CHANGED
@@ -15,6 +15,11 @@ PostCSS is used by industry leaders including Wikipedia, Twitter, Alibaba,
|
|
15
15
|
and JetBrains. The [Autoprefixer] PostCSS plugin is one of the most popular
|
16
16
|
CSS processors.
|
17
17
|
|
18
|
+
PostCSS takes a CSS file and provides an API to analyze and modify its rules
|
19
|
+
(by transforming them into an [Abstract Syntax Tree]).
|
20
|
+
This API can then be used by [plugins] to do a lot of useful things,
|
21
|
+
e.g. to find errors automatically insert vendor prefixes.
|
22
|
+
|
18
23
|
**Support / Discussion:** [Gitter](https://gitter.im/postcss/postcss)<br>
|
19
24
|
**Twitter account:** [@postcss](https://twitter.com/postcss)<br>
|
20
25
|
**VK.com page:** [postcss](https://vk.com/postcss)<br>
|
@@ -24,7 +29,9 @@ For PostCSS commercial support (consulting, improving the front-end culture
|
|
24
29
|
of your company, PostCSS plugins), contact [Evil Martians](https://evilmartians.com/?utm_source=postcss)
|
25
30
|
at <surrender@evilmartians.com>.
|
26
31
|
|
27
|
-
[
|
32
|
+
[Abstract Syntax Tree]: https://en.wikipedia.org/wiki/Abstract_syntax_tree
|
33
|
+
[Autoprefixer]: https://github.com/postcss/autoprefixer
|
34
|
+
[plugins]: https://github.com/postcss/postcss#plugins
|
28
35
|
|
29
36
|
<a href="https://evilmartians.com/?utm_source=postcss">
|
30
37
|
<img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg"
|
@@ -60,7 +67,6 @@ If you have any new ideas, [PostCSS plugin development] is really easy.
|
|
60
67
|
|
61
68
|
* [`autoprefixer`] adds vendor prefixes, using data from Can I Use.
|
62
69
|
* [`postcss-preset-env`] allows you to use future CSS features today.
|
63
|
-
* [`postcss-image-set-polyfill`] emulates [`image-set`] function logic for all browsers
|
64
70
|
|
65
71
|
### Better CSS Readability
|
66
72
|
|
@@ -93,7 +99,6 @@ If you have any new ideas, [PostCSS plugin development] is really easy.
|
|
93
99
|
* [`lost`] is a feature-rich `calc()` grid system.
|
94
100
|
* [`rtlcss`] mirrors styles for right-to-left locales.
|
95
101
|
|
96
|
-
[`postcss-image-set-polyfill`]: https://github.com/SuperOl3g/postcss-image-set-polyfill
|
97
102
|
[PostCSS plugin development]: https://github.com/postcss/postcss/blob/master/docs/writing-a-plugin.md
|
98
103
|
[`postcss-inline-svg`]: https://github.com/TrySound/postcss-inline-svg
|
99
104
|
[`postcss-preset-env`]: https://github.com/jonathantneal/postcss-preset-env
|
@@ -114,7 +119,6 @@ If you have any new ideas, [PostCSS plugin development] is really easy.
|
|
114
119
|
[`css-modules`]: https://github.com/css-modules/css-modules
|
115
120
|
[`colorguard`]: https://github.com/SlexAxton/css-colorguard
|
116
121
|
[`stylelint`]: https://github.com/stylelint/stylelint
|
117
|
-
[`image-set`]: https://drafts.csswg.org/css-images-4/#image-set-notation
|
118
122
|
[`stylefmt`]: https://github.com/morishitter/stylefmt
|
119
123
|
[`cssnano`]: http://cssnano.co
|
120
124
|
[`precss`]: https://github.com/jonathantneal/precss
|
@@ -133,6 +137,7 @@ you can write a parser and/or stringifier to extend PostCSS.
|
|
133
137
|
* [`postcss-syntax`] switch syntax automatically by file extensions.
|
134
138
|
* [`postcss-html`] parsing styles in `<style>` tags of HTML-like files.
|
135
139
|
* [`postcss-markdown`] parsing styles in code blocks of Markdown files.
|
140
|
+
* [`postcss-jsx`] parsing CSS in template / object literals of source files.
|
136
141
|
* [`postcss-styled`] parsing CSS in template literals of source files.
|
137
142
|
* [`postcss-scss`] allows you to work with SCSS
|
138
143
|
*(but does not compile SCSS to CSS)*.
|
@@ -152,6 +157,7 @@ you can write a parser and/or stringifier to extend PostCSS.
|
|
152
157
|
[`postcss-syntax`]: https://github.com/gucong3000/postcss-syntax
|
153
158
|
[`postcss-html`]: https://github.com/gucong3000/postcss-html
|
154
159
|
[`postcss-markdown`]: https://github.com/gucong3000/postcss-markdown
|
160
|
+
[`postcss-jsx`]: https://github.com/gucong3000/postcss-jsx
|
155
161
|
[`postcss-styled`]: https://github.com/gucong3000/postcss-styled
|
156
162
|
[`postcss-scss`]: https://github.com/postcss/postcss-scss
|
157
163
|
[`postcss-sass`]: https://github.com/AleshaOleg/postcss-sass
|
@@ -187,28 +193,28 @@ Use [`postcss-loader`] in `webpack.config.js`:
|
|
187
193
|
|
188
194
|
```js
|
189
195
|
module.exports = {
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
}
|
204
|
-
},
|
205
|
-
{
|
206
|
-
loader: 'postcss-loader'
|
207
|
-
}
|
208
|
-
]
|
196
|
+
module: {
|
197
|
+
rules: [
|
198
|
+
{
|
199
|
+
test: /\.css$/,
|
200
|
+
exclude: /node_modules/,
|
201
|
+
use: [
|
202
|
+
{
|
203
|
+
loader: 'style-loader',
|
204
|
+
},
|
205
|
+
{
|
206
|
+
loader: 'css-loader',
|
207
|
+
options: {
|
208
|
+
importLoaders: 1,
|
209
209
|
}
|
210
|
+
},
|
211
|
+
{
|
212
|
+
loader: 'postcss-loader'
|
213
|
+
}
|
210
214
|
]
|
211
|
-
|
215
|
+
}
|
216
|
+
]
|
217
|
+
}
|
212
218
|
}
|
213
219
|
```
|
214
220
|
|
@@ -216,10 +222,10 @@ Then create `postcss.config.js`:
|
|
216
222
|
|
217
223
|
```js
|
218
224
|
module.exports = {
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
225
|
+
plugins: [
|
226
|
+
require('precss'),
|
227
|
+
require('autoprefixer')
|
228
|
+
]
|
223
229
|
}
|
224
230
|
```
|
225
231
|
|
@@ -230,16 +236,16 @@ module.exports = {
|
|
230
236
|
Use [`gulp-postcss`] and [`gulp-sourcemaps`].
|
231
237
|
|
232
238
|
```js
|
233
|
-
gulp.task('css',
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
})
|
239
|
+
gulp.task('css', () => {
|
240
|
+
const postcss = require('gulp-postcss')
|
241
|
+
const sourcemaps = require('gulp-sourcemaps')
|
242
|
+
|
243
|
+
return gulp.src('src/**/*.css')
|
244
|
+
.pipe( sourcemaps.init() )
|
245
|
+
.pipe( postcss([ require('precss'), require('autoprefixer') ]) )
|
246
|
+
.pipe( sourcemaps.write('.') )
|
247
|
+
.pipe( gulp.dest('build/') )
|
248
|
+
})
|
243
249
|
```
|
244
250
|
|
245
251
|
[`gulp-sourcemaps`]: https://github.com/floridoo/gulp-sourcemaps
|
@@ -266,10 +272,10 @@ To apply PostCSS plugins to React Inline Styles, JSS, Radium
|
|
266
272
|
and other [CSS-in-JS], you can use [`postcss-js`] and transforms style objects.
|
267
273
|
|
268
274
|
```js
|
269
|
-
var postcss = require('postcss-js')
|
270
|
-
var prefixer = postcss.sync([ require('autoprefixer') ])
|
275
|
+
var postcss = require('postcss-js')
|
276
|
+
var prefixer = postcss.sync([ require('autoprefixer') ])
|
271
277
|
|
272
|
-
prefixer({ display: 'flex' })
|
278
|
+
prefixer({ display: 'flex' }) //=> { display: ['-webkit-box', '-webkit-flex', '-ms-flexbox', 'flex'] }
|
273
279
|
```
|
274
280
|
|
275
281
|
[`postcss-js`]: https://github.com/postcss/postcss-js
|
@@ -296,19 +302,21 @@ prefixer({ display: 'flex' }); //=> { display: ['-webkit-box', '-webkit-flex', '
|
|
296
302
|
For other environments, you can use the JS API:
|
297
303
|
|
298
304
|
```js
|
299
|
-
const
|
300
|
-
const postcss = require('postcss')
|
301
|
-
const precss = require('precss')
|
302
|
-
const
|
305
|
+
const autoprefixer = require('autoprefixer')
|
306
|
+
const postcss = require('postcss')
|
307
|
+
const precss = require('precss')
|
308
|
+
const fs = require('fs')
|
303
309
|
|
304
310
|
fs.readFile('src/app.css', (err, css) => {
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
}
|
311
|
+
postcss([precss, autoprefixer])
|
312
|
+
.process(css, { from: 'src/app.css', to: 'dest/app.css' })
|
313
|
+
.then(result => {
|
314
|
+
fs.writeFile('dest/app.css', result.css, () => true)
|
315
|
+
if ( result.map ) {
|
316
|
+
fs.writeFile('dest/app.css.map', result.map, () => true)
|
317
|
+
}
|
318
|
+
})
|
319
|
+
})
|
312
320
|
```
|
313
321
|
|
314
322
|
Read the [PostCSS API documentation] for more details about the JS API.
|
package/docs/architecture.md
CHANGED
@@ -104,7 +104,7 @@ So now lets look more closely on structures that play main role in PostCSS workf
|
|
104
104
|
|
105
105
|
Lets look more closely on single token like `word`. As it was said each token represented as a list and follow such pattern.
|
106
106
|
|
107
|
-
```
|
107
|
+
```js
|
108
108
|
const token = [
|
109
109
|
// represents token type
|
110
110
|
'word',
|
@@ -121,7 +121,7 @@ So now lets look more closely on structures that play main role in PostCSS workf
|
|
121
121
|
|
122
122
|
// Next two numbers also optional and represent end position for multichar tokens like this one. Numbers follow same rule as was described above
|
123
123
|
1, 10
|
124
|
-
]
|
124
|
+
]
|
125
125
|
```
|
126
126
|
There are many patterns how tokenization could be done, PostCSS motto is performance and simplicity. Tokenization is complex computing operation and take large amount of syntax analysis time ( ~90% ), that why PostCSS' Tokenizer looks dirty but it was optimized for speed. Any high-level constructs like classes could dramatically slow down tokenizer.
|
127
127
|
|
@@ -57,11 +57,11 @@ By wrapping your function in this method,
|
|
57
57
|
you are hooking into a common plugin API:
|
58
58
|
|
59
59
|
```js
|
60
|
-
module.exports = postcss.plugin('plugin-name',
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
})
|
60
|
+
module.exports = postcss.plugin('plugin-name', opts => {
|
61
|
+
return (root, result) => {
|
62
|
+
// Plugin code
|
63
|
+
}
|
64
|
+
})
|
65
65
|
```
|
66
66
|
|
67
67
|
## 2. Processing
|
@@ -78,19 +78,19 @@ different environments. You should test in (at least) Node.js [active LTS](https
|
|
78
78
|
For example, use `fs.writeFile` instead of `fs.writeFileSync`:
|
79
79
|
|
80
80
|
```js
|
81
|
-
postcss.plugin('plugin-sprite',
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
})
|
81
|
+
postcss.plugin('plugin-sprite', opts => {
|
82
|
+
return (root, result) => {
|
83
|
+
|
84
|
+
return new Promise((resolve, reject) => {
|
85
|
+
const sprite = makeSprite()
|
86
|
+
fs.writeFile(opts.file, sprite, err => {
|
87
|
+
if (err) return reject(err)
|
88
|
+
resolve()
|
89
|
+
})
|
90
|
+
})
|
91
|
+
|
92
|
+
}
|
93
|
+
})
|
94
94
|
```
|
95
95
|
|
96
96
|
### 2.3. Set `node.source` for new nodes
|
@@ -102,18 +102,18 @@ So if you add new declaration based on some existing declaration, you should
|
|
102
102
|
clone the existing declaration in order to save that original `source`.
|
103
103
|
|
104
104
|
```js
|
105
|
-
if (
|
106
|
-
|
105
|
+
if (needPrefix(decl.prop)) {
|
106
|
+
decl.cloneBefore({ prop: '-webkit-' + decl.prop })
|
107
107
|
}
|
108
108
|
```
|
109
109
|
|
110
110
|
You can also set `source` directly, copying from some existing node:
|
111
111
|
|
112
112
|
```js
|
113
|
-
if (
|
114
|
-
|
115
|
-
|
116
|
-
|
113
|
+
if (decl.prop === 'animation') {
|
114
|
+
const keyframe = createAnimationByName(decl.value)
|
115
|
+
keyframes.source = decl.source
|
116
|
+
decl.root().append(keyframes)
|
117
117
|
}
|
118
118
|
```
|
119
119
|
|
@@ -134,8 +134,8 @@ in a mixin plugin) you should use `node.error` to create an error
|
|
134
134
|
that includes source position:
|
135
135
|
|
136
136
|
```js
|
137
|
-
if (
|
138
|
-
|
137
|
+
if (typeof mixins[name] === 'undefined') {
|
138
|
+
throw decl.error('Unknown mixin ' + name, { plugin: 'postcss-mixins' })
|
139
139
|
}
|
140
140
|
```
|
141
141
|
|
@@ -145,8 +145,8 @@ Do not print warnings with `console.log` or `console.warn`,
|
|
145
145
|
because some PostCSS runner may not allow console output.
|
146
146
|
|
147
147
|
```js
|
148
|
-
if (
|
149
|
-
|
148
|
+
if (outdated(decl.prop)) {
|
149
|
+
result.warn(decl.prop + ' is outdated', { node: decl })
|
150
150
|
}
|
151
151
|
```
|
152
152
|
|
@@ -23,12 +23,12 @@ it can support plugins which accept a function, such as [`postcss-assets`]:
|
|
23
23
|
|
24
24
|
```js
|
25
25
|
module.exports = [
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
]
|
26
|
+
require('postcss-assets')({
|
27
|
+
cachebuster: function (file) {
|
28
|
+
return fs.statSync(file).mtime.getTime().toString(16)
|
29
|
+
}
|
30
|
+
})
|
31
|
+
]
|
32
32
|
```
|
33
33
|
|
34
34
|
[`postcss-assets`]: https://github.com/borodean/postcss-assets
|
@@ -43,7 +43,7 @@ writing to disk (for example, a gulp transform), you should set both options
|
|
43
43
|
to point to the same file:
|
44
44
|
|
45
45
|
```js
|
46
|
-
processor.process({ from: file.path, to: file.path })
|
46
|
+
processor.process({ from: file.path, to: file.path })
|
47
47
|
```
|
48
48
|
|
49
49
|
### 2.2. Use only the asynchronous API
|
@@ -53,8 +53,8 @@ The synchronous API is provided only for debugging, is slower,
|
|
53
53
|
and can’t work with asynchronous plugins.
|
54
54
|
|
55
55
|
```js
|
56
|
-
processor.process(opts).then(
|
57
|
-
|
56
|
+
processor.process(opts).then(result => {
|
57
|
+
// processing is finished
|
58
58
|
});
|
59
59
|
```
|
60
60
|
|
@@ -75,13 +75,13 @@ as the runner can be used by developers who are not familiar with JavaScript.
|
|
75
75
|
Instead, handle such errors gracefully:
|
76
76
|
|
77
77
|
```js
|
78
|
-
processor.process(opts).catch(
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
})
|
78
|
+
processor.process(opts).catch(error => {
|
79
|
+
if (error.name === 'CssSyntaxError') {
|
80
|
+
process.stderr.write(error.message + error.showSourceCode())
|
81
|
+
} else {
|
82
|
+
throw error
|
83
|
+
}
|
84
|
+
})
|
85
85
|
```
|
86
86
|
|
87
87
|
### 3.2. Display `result.warnings()`
|
@@ -89,9 +89,9 @@ processor.process(opts).catch(function (error) {
|
|
89
89
|
PostCSS runners must output warnings from `result.warnings()`:
|
90
90
|
|
91
91
|
```js
|
92
|
-
result.warnings().forEach(
|
93
|
-
|
94
|
-
})
|
92
|
+
result.warnings().forEach(warn => {
|
93
|
+
process.stderr.write(warn.toString())
|
94
|
+
})
|
95
95
|
```
|
96
96
|
|
97
97
|
See also [postcss-log-warnings] and [postcss-messages] plugins.
|
@@ -106,8 +106,8 @@ PostCSS runners must provide an option to save the source map in a different
|
|
106
106
|
file:
|
107
107
|
|
108
108
|
```js
|
109
|
-
if (
|
110
|
-
|
109
|
+
if (result.map) {
|
110
|
+
fs.writeFile(opts.to + '.map', result.map.toString())
|
111
111
|
}
|
112
112
|
```
|
113
113
|
|
package/docs/source-maps.md
CHANGED
@@ -13,17 +13,17 @@ If you don’t want the map inlined, you can set `map.inline: false`.
|
|
13
13
|
|
14
14
|
```js
|
15
15
|
processor
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
16
|
+
.process(css, {
|
17
|
+
from: 'app.sass.css',
|
18
|
+
to: 'app.css',
|
19
|
+
map: { inline: false }
|
20
|
+
})
|
21
|
+
.then(result => {
|
22
|
+
result.map //=> '{ "version":3,
|
23
|
+
// "file":"app.css",
|
24
|
+
// "sources":["app.sass"],
|
25
|
+
// "mappings":"AAAA,KAAI" }'
|
26
|
+
})
|
27
27
|
```
|
28
28
|
|
29
29
|
If PostCSS finds source maps from a previous transformation,
|