postcss 7.0.35 → 7.0.39

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.

@@ -1,156 +0,0 @@
1
- ## PostCSS Architecture
2
-
3
- General overview of the PostCSS architecture.
4
- It can be useful for everyone who wishes to contribute to the core or develop a better understanding of the tool.
5
-
6
- **Table of Contents**
7
-
8
- - [Overview](#overview)
9
- - [Workflow](#workflow)
10
- - [Core Structures](#core-structures)
11
- * [Tokenizer](#tokenizer--libtokenizees6-)
12
- * [Parser](#parser--libparsees6-libparseres6-)
13
- * [Processor](#processor--libprocessores6-)
14
- * [Stringifier](#stringifier--libstringifyes6-libstringifieres6-)
15
- - [API](#api-reference)
16
-
17
- ### Overview
18
-
19
- > This section describes ideas lying behind PostCSS
20
-
21
- Before diving deeper into the development of PostCSS let's briefly describe what is PostCSS and what is not.
22
-
23
- **PostCSS**
24
-
25
- - *is **NOT** a style preprocessor like `Sass` or `Less`.*
26
-
27
- It does not define a custom syntax and semantics, it's not actually a language.
28
- PostCSS works with CSS and can be easily integrated with the tools described above. That being said any valid CSS can be processed by PostCSS.
29
-
30
- - *is a tool for CSS syntax transformations*
31
-
32
- It allows you to define custom CSS like syntax that could be understandable and transformed by plugins. That being said PostCSS is not strictly about CSS spec but about syntax definition manner of CSS. In such a way you can define custom syntax constructs like at-rule, that could be very helpful for tools build around PostCSS. PostCSS plays the role of a framework for building outstanding tools for CSS manipulations.
33
-
34
- - *is a big player in CSS ecosystem*
35
-
36
- A Large amount of lovely tools like `Autoprefixer`, `Stylelint`, `CSSnano` were built on PostCSS ecosystem. There is a big chance that you already use it implicitly, just check your `node_modules` :smiley:
37
-
38
- ### Workflow
39
-
40
- This is a high-level overview of the whole PostCSS workflow
41
-
42
- <img width="300" src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/aa/PostCSS_scheme.svg/512px-PostCSS_scheme.svg.png" alt="workflow">
43
-
44
- As you can see from the diagram above, PostCSS architecture is pretty straightforward but some parts of it could be misunderstood.
45
-
46
- You can see a part called *Parser*, this construct will be described in details later on, just for now think about it as a structure that can understand your CSS like syntax and create an object representation of it.
47
-
48
- That being said, there are few ways to write a parser.
49
-
50
- - *Write a single file with string to AST transformation*
51
-
52
- This method is quite popular, for example, the [Rework analyzer](https://github.com/reworkcss/css/blob/master/lib/parse/index.js) was written in this style. But with a large code base, the code becomes hard to read and pretty slow.
53
-
54
- - *Split it into lexical analysis/parsing steps (source string → tokens → AST)*
55
-
56
- This is the way of how we do it in PostCSS and also the most popular one.
57
- A lot of parsers like [`@babel/parser` (parser behind Babel)](https://github.com/babel/babel/tree/master/packages/babel-parser), [`CSSTree`](https://github.com/csstree/csstree) were written in such way.
58
- The main reasons to separate tokenization from parsing steps are performance and abstracting complexity.
59
-
60
- Let think about why the second way is better for our needs.
61
-
62
- First of all, because string to tokens step takes more time than parsing step. We operate on large source string and process it char by char, this is why it is very inefficient operation in terms of performance and we should perform it only once.
63
-
64
- But from other side tokens to AST transformation is logically more complex so with such separation we could write very fast tokenizer (but from this comes sometimes hard to read code) and easy to read (but slow) parser.
65
-
66
- Summing it up splitting into two steps improve performance and code readability.
67
-
68
- So now let's look more closely on structures that play the main role in PostCSS workflow.
69
-
70
- ### Core Structures
71
-
72
- - #### Tokenizer ( [lib/tokenize.es6](https://github.com/postcss/postcss/blob/master/lib/tokenize.es6) )
73
-
74
- Tokenizer (aka Lexer) plays important role in syntax analysis.
75
-
76
- It accepts CSS string and returns a list of tokens.
77
-
78
- Token is a simple structure that describes some part of syntax like `at-rule`, `comment` or `word`. It can also contain positional information for more descriptive errors.
79
-
80
- For example, if we consider following CSS
81
-
82
- ```css
83
- .className { color: #FFF; }
84
- ```
85
-
86
- corresponding tokens from PostCSS will be
87
- ```js
88
- [
89
- ["word", ".className", 1, 1, 1, 10]
90
- ["space", " "]
91
- ["{", "{", 1, 12]
92
- ["space", " "]
93
- ["word", "color", 1, 14, 1, 18]
94
- [":", ":", 1, 19]
95
- ["space", " "]
96
- ["word", "#FFF" , 1, 21, 1, 23]
97
- [";", ";", 1, 24]
98
- ["space", " "]
99
- ["}", "}", 1, 26]
100
- ]
101
- ```
102
-
103
- As you can see from the example above a single token represented as a list and also `space` token doesn't have positional information.
104
-
105
- Let's look more closely on single token like `word`. As it was said each token represented as a list and follow such pattern.
106
-
107
- ```js
108
- const token = [
109
- // represents token type
110
- 'word',
111
-
112
- // represents matched word
113
- '.className',
114
-
115
- // This two numbers represent start position of token.
116
- // It is optional value as we saw in the example above,
117
- // tokens like `space` don't have such information.
118
-
119
- // Here the first number is line number and the second one is corresponding column.
120
- 1, 1,
121
-
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
- 1, 10
124
- ]
125
- ```
126
- There are many patterns how tokenization could be done, PostCSS motto is performance and simplicity. Tokenization is a complex computing operation and takes a 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
-
128
- PostCSS' Tokenizer uses some sort of streaming/chaining API where you expose [`nextToken()`](https://github.com/postcss/postcss/blob/master/lib/tokenize.es6#L48-L308) method to Parser. In this manner, we provide a clean interface for Parser and reduce memory usage by storing only a few tokens and not the whole list of tokens.
129
-
130
- - #### Parser ( [lib/parse.es6](https://github.com/postcss/postcss/blob/master/lib/parse.es6), [lib/parser.es6](https://github.com/postcss/postcss/blob/master/lib/parser.es6) )
131
-
132
- Parser is the main structure responsible for [syntax analysis](https://en.wikipedia.org/wiki/Parsing) of incoming CSS. Parser produces a structure called [Abstract Syntax Tree (AST)](https://en.wikipedia.org/wiki/Abstract_syntax_tree) that could then be transformed by plugins later on.
133
-
134
- Parser works in common with Tokenizer and operates over tokens, not source string, as it would be a very inefficient operation.
135
-
136
- It uses mostly `nextToken` and `back` methods provided by Tokenizer for obtaining single or multiple tokens and then construct part of AST called `Node`.
137
-
138
- There are multiple Node types that PostCSS could produce but all of them inherit from base Node [class](https://github.com/postcss/postcss/blob/master/lib/node.es6#L34).
139
-
140
- - #### Processor ( [lib/processor.es6](https://github.com/postcss/postcss/blob/master/lib/processor.es6) )
141
-
142
- Processor is a very plain structure that initializes plugins and runs syntax transformations. Plugin is just a function registered with [postcss.plugin](https://github.com/postcss/postcss/blob/master/lib/postcss.es6#L109) call.
143
-
144
- It exposes only a few public API methods. Description of them could be found on [api.postcss.org/Processor](http://api.postcss.org/Processor.html)
145
-
146
- - #### Stringifier ( [lib/stringify.es6](https://github.com/postcss/postcss/blob/master/lib/stringify.es6), [lib/stringifier.es6](https://github.com/postcss/postcss/blob/master/lib/stringifier.es6) )
147
-
148
- Stringifier is a base class that translates modified AST to pure CSS string. Stringifier traverses AST starting from provided Node and generates a raw string representation of it calling corresponding methods.
149
-
150
- The most essential method is [`Stringifier.stringify`](https://github.com/postcss/postcss/blob/master/lib/stringifier.es6#L25-L27)
151
- that accepts initial Node and semicolon indicator.
152
- You can learn more by checking [stringifier.es6](https://github.com/postcss/postcss/blob/master/lib/stringifier.es6)
153
-
154
- ### API Reference
155
-
156
- More descriptive API documentation could be found [here](http://api.postcss.org/)
@@ -1,195 +0,0 @@
1
- # PostCSS Plugin Guidelines
2
-
3
- A PostCSS plugin is a function that receives and, usually,
4
- transforms a CSS AST from the PostCSS parser.
5
-
6
- The rules below are *mandatory* for all PostCSS plugins.
7
-
8
- See also [ClojureWerkz’s recommendations] for open source projects.
9
-
10
- [ClojureWerkz’s recommendations]: http://blog.clojurewerkz.org/blog/2013/04/20/how-to-make-your-open-source-project-really-awesome/
11
-
12
- ## 1. API
13
-
14
- ### 1.1 Clear name with `postcss-` prefix
15
-
16
- The plugin’s purpose should be clear just by reading its name.
17
- If you wrote a transpiler for CSS 4 Custom Media, `postcss-custom-media`
18
- would be a good name. If you wrote a plugin to support mixins,
19
- `postcss-mixins` would be a good name.
20
-
21
- The prefix `postcss-` shows that the plugin is part of the PostCSS ecosystem.
22
-
23
- This rule is not mandatory for plugins that can run as independent tools,
24
- without the user necessarily knowing that it is powered by
25
- PostCSS — for example, [RTLCSS] and [Autoprefixer].
26
-
27
- [Autoprefixer]: https://github.com/postcss/autoprefixer
28
- [RTLCSS]: https://rtlcss.com/
29
-
30
- ### 1.2. Do one thing, and do it well
31
-
32
- Do not create multitool plugins. Several small, one-purpose plugins bundled into
33
- a plugin pack is usually a better solution.
34
-
35
- For example, [`postcss-preset-env`] contains many small plugins,
36
- one for each W3C specification. And [`cssnano`] contains a separate plugin
37
- for each of its optimization.
38
-
39
- [`postcss-preset-env`]: https://preset-env.cssdb.org/
40
- [`cssnano`]: https://github.com/ben-eb/cssnano
41
-
42
- ### 1.3. Do not use mixins
43
-
44
- Preprocessors libraries like Compass provide an API with mixins.
45
-
46
- PostCSS plugins are different.
47
- A plugin cannot be just a set of mixins for [`postcss-mixins`].
48
-
49
- To achieve your goal, consider transforming valid CSS
50
- or using custom at-rules and custom properties.
51
-
52
- [`postcss-mixins`]: https://github.com/postcss/postcss-mixins
53
-
54
- ### 1.4. Create plugin by `postcss.plugin`
55
-
56
- By wrapping your function in this method,
57
- you are hooking into a common plugin API:
58
-
59
- ```js
60
- module.exports = postcss.plugin('plugin-name', opts => {
61
- return (root, result) => {
62
- // Plugin code
63
- }
64
- })
65
- ```
66
-
67
- ## 2. Processing
68
-
69
- ### 2.1. Plugin must be tested
70
-
71
- A CI service like [Travis] is also recommended for testing code in
72
- different environments. You should test in (at least) Node.js [active LTS](https://github.com/nodejs/LTS) and current stable version.
73
-
74
- [Travis]: https://travis-ci.org/
75
-
76
- ### 2.2. Use asynchronous methods whenever possible
77
-
78
- For example, use `fs.writeFile` instead of `fs.writeFileSync`:
79
-
80
- ```js
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
- ```
95
-
96
- ### 2.3. Set `node.source` for new nodes
97
-
98
- Every node must have a relevant `source` so PostCSS can generate
99
- an accurate source map.
100
-
101
- So if you add a new declaration based on some existing declaration, you should
102
- clone the existing declaration in order to save that original `source`.
103
-
104
- ```js
105
- if (needPrefix(decl.prop)) {
106
- decl.cloneBefore({ prop: '-webkit-' + decl.prop })
107
- }
108
- ```
109
-
110
- You can also set `source` directly, copying from some existing node:
111
-
112
- ```js
113
- if (decl.prop === 'animation') {
114
- const keyframe = createAnimationByName(decl.value)
115
- keyframes.source = decl.source
116
- decl.root().append(keyframes)
117
- }
118
- ```
119
-
120
- ### 2.4. Use only the public PostCSS API
121
-
122
- PostCSS plugins must not rely on undocumented properties or methods,
123
- which may be subject to change in any minor release. The public API
124
- is described in [API docs].
125
-
126
- [API docs]: http://api.postcss.org/
127
-
128
- ## 3. Errors
129
-
130
- ### 3.1. Use `node.error` on CSS relevant errors
131
-
132
- If you have an error because of input CSS (like an unknown name
133
- in a mixin plugin) you should use `node.error` to create an error
134
- that includes source position:
135
-
136
- ```js
137
- if (typeof mixins[name] === 'undefined') {
138
- throw decl.error('Unknown mixin ' + name, { plugin: 'postcss-mixins' })
139
- }
140
- ```
141
-
142
- ### 3.2. Use `result.warn` for warnings
143
-
144
- Do not print warnings with `console.log` or `console.warn`,
145
- because some PostCSS runner may not allow console output.
146
-
147
- ```js
148
- if (outdated(decl.prop)) {
149
- result.warn(decl.prop + ' is outdated', { node: decl })
150
- }
151
- ```
152
-
153
- If CSS input is a source of the warning, the plugin must set the `node` option.
154
-
155
- ## 4. Documentation
156
-
157
- ### 4.1. Document your plugin in English
158
-
159
- PostCSS plugins must have their `README.md` wrote in English. Do not be afraid
160
- of your English skills, as the open source community will fix your errors.
161
-
162
- Of course, you are welcome to write documentation in other languages;
163
- just name them appropriately (e.g. `README.ja.md`).
164
-
165
- ### 4.2. Include input and output examples
166
-
167
- The plugin's `README.md` must contain example input and output CSS.
168
- A clear example is the best way to describe how your plugin works.
169
-
170
- The first section of the `README.md` is a good place to put examples.
171
- See [postcss-opacity](https://github.com/iamvdo/postcss-opacity) for an example.
172
-
173
- Of course, this guideline does not apply if your plugin does not
174
- transform the CSS.
175
-
176
- ### 4.3. Maintain a changelog
177
-
178
- PostCSS plugins must describe the changes of all their releases
179
- in a separate file, such as `CHANGELOG.md`, `History.md`, or [GitHub Releases].
180
- Visit [Keep A Changelog] for more information about how to write one of these.
181
-
182
- Of course, you should be using [SemVer].
183
-
184
- [Keep A Changelog]: http://keepachangelog.com/
185
- [GitHub Releases]: https://help.github.com/articles/creating-releases/
186
- [SemVer]: http://semver.org/
187
-
188
- ### 4.4. Include `postcss-plugin` keyword in `package.json`
189
-
190
- PostCSS plugins written for npm must have the `postcss-plugin` keyword
191
- in their `package.json`. This special keyword will be useful for feedback about
192
- the PostCSS ecosystem.
193
-
194
- For packages not published to npm, this is not mandatory, but is recommended
195
- if the package format can contain keywords.
@@ -1,143 +0,0 @@
1
- # PostCSS Runner Guidelines
2
-
3
- A PostCSS runner is a tool that processes CSS through a user-defined list
4
- of plugins; for example, [`postcss-cli`] or [`gulp‑postcss`].
5
- These rules are mandatory for any such runners.
6
-
7
- For single-plugin tools, like [`gulp-autoprefixer`],
8
- these rules are not mandatory but are highly recommended.
9
-
10
- See also [ClojureWerkz’s recommendations] for open source projects.
11
-
12
- [ClojureWerkz’s recommendations]: http://blog.clojurewerkz.org/blog/2013/04/20/how-to-make-your-open-source-project-really-awesome/
13
- [`gulp-autoprefixer`]: https://github.com/sindresorhus/gulp-autoprefixer
14
- [`gulp‑postcss`]: https://github.com/w0rm/gulp-postcss
15
- [`postcss-cli`]: https://github.com/postcss/postcss-cli
16
-
17
- ## 1. API
18
-
19
- ### 1.1. Accept functions in plugin parameters
20
-
21
- If your runner uses a config file, it must be written in JavaScript, so that
22
- it can support plugins which accept a function, such as [`postcss-assets`]:
23
-
24
- ```js
25
- module.exports = [
26
- require('postcss-assets')({
27
- cachebuster: function (file) {
28
- return fs.statSync(file).mtime.getTime().toString(16)
29
- }
30
- })
31
- ]
32
- ```
33
-
34
- [`postcss-assets`]: https://github.com/borodean/postcss-assets
35
-
36
- ## 2. Processing
37
-
38
- ### 2.1. Set `from` and `to` processing options
39
-
40
- To ensure that PostCSS generates source maps and displays better syntax errors,
41
- runners must specify the `from` and `to` options. If your runner does not handle
42
- writing to disk (for example, a gulp transform), you should set both options
43
- to point to the same file:
44
-
45
- ```js
46
- processor.process({ from: file.path, to: file.path })
47
- ```
48
-
49
- ### 2.2. Use only the asynchronous API
50
-
51
- PostCSS runners must use only the asynchronous API.
52
- The synchronous API is provided only for debugging, is slower,
53
- and can’t work with asynchronous plugins.
54
-
55
- ```js
56
- processor.process(opts).then(result => {
57
- // processing is finished
58
- });
59
- ```
60
-
61
- ### 2.3. Use only the public PostCSS API
62
-
63
- PostCSS runners must not rely on undocumented properties or methods,
64
- which may be subject to change in any minor release. The public API
65
- is described in [API docs].
66
-
67
- [API docs]: http://api.postcss.org/
68
-
69
- ## 3. Output
70
-
71
- ### 3.1. Don’t show JS stack for `CssSyntaxError`
72
-
73
- PostCSS runners must not show a stack trace for CSS syntax errors,
74
- as the runner can be used by developers who are not familiar with JavaScript.
75
- Instead, handle such errors gracefully:
76
-
77
- ```js
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
- ```
86
-
87
- ### 3.2. Display `result.warnings()`
88
-
89
- PostCSS runners must output warnings from `result.warnings()`:
90
-
91
- ```js
92
- result.warnings().forEach(warn => {
93
- process.stderr.write(warn.toString())
94
- })
95
- ```
96
-
97
- See also [postcss-log-warnings] and [postcss-messages] plugins.
98
-
99
- [postcss-log-warnings]: https://github.com/davidtheclark/postcss-log-warnings
100
- [postcss-messages]: https://github.com/postcss/postcss-messages
101
-
102
- ### 3.3. Allow the user to write source maps to different files
103
-
104
- PostCSS by default will inline source maps in the generated file; however,
105
- PostCSS runners must provide an option to save the source map in a different
106
- file:
107
-
108
- ```js
109
- if (result.map) {
110
- fs.writeFile(opts.to + '.map', result.map.toString())
111
- }
112
- ```
113
-
114
- ## 4. Documentation
115
-
116
- ### 4.1. Document your runner in English
117
-
118
- PostCSS runners must have their `README.md` wrote in English. Do not be afraid
119
- of your English skills, as the open source community will fix your errors.
120
-
121
- Of course, you are welcome to write documentation in other languages;
122
- just name them appropriately (e.g. `README.ja.md`).
123
-
124
- ### 4.2. Maintain a changelog
125
-
126
- PostCSS runners must describe changes of all releases in a separate file,
127
- such as `ChangeLog.md`, `History.md`, or with [GitHub Releases].
128
- Visit [Keep A Changelog] for more information on how to write one of these.
129
-
130
- Of course, you should use [SemVer].
131
-
132
- [Keep A Changelog]: http://keepachangelog.com/
133
- [GitHub Releases]: https://help.github.com/articles/creating-releases/
134
- [SemVer]: http://semver.org/
135
-
136
- ### 4.3. `postcss-runner` keyword in `package.json`
137
-
138
- PostCSS runners written for npm must have the `postcss-runner` keyword
139
- in their `package.json`. This special keyword will be useful for feedback about
140
- the PostCSS ecosystem.
141
-
142
- For packages not published to npm, this is not mandatory, but recommended
143
- if the package format is allowed to contain keywords.
@@ -1,74 +0,0 @@
1
- # PostCSS and Source Maps
2
-
3
- PostCSS has great [source maps] support. It can read and interpret maps
4
- from previous transformation steps, autodetect the format that you expect,
5
- and output both external and inline maps.
6
-
7
- To ensure that you generate an accurate source map, you must indicate the input
8
- and output CSS file paths — using the options `from` and `to`, respectively.
9
-
10
- To generate a new source map with the default options, simply set `map: true`.
11
- This will generate an inline source map that contains the source content.
12
- If you don’t want the map inlined, you can set `map.inline: false`.
13
-
14
- ```js
15
- processor
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
- ```
28
-
29
- If PostCSS finds source maps from a previous transformation,
30
- it will automatically update that source map with the same options.
31
-
32
- ## Options
33
-
34
- If you want more control over source map generation, you can define the `map`
35
- option as an object with the following parameters:
36
-
37
- * `inline` boolean: indicates that the source map should be embedded
38
- in the output CSS as a Base64-encoded comment. By default, it is `true`.
39
- But if all previous maps are external, not inline, PostCSS will not embed
40
- the map even if you do not set this option.
41
-
42
- If you have an inline source map, the `result.map` property will be empty,
43
- as the source map will be contained within the text of `result.css`.
44
-
45
- * `prev` string, object, boolean or function: source map content from
46
- a previous processing step (for example, Sass compilation).
47
- PostCSS will try to read the previous source map automatically
48
- (based on comments within the source CSS), but you can use this option
49
- to identify it manually. If desired, you can omit the previous map
50
- with `prev: false`.
51
-
52
- * `sourcesContent` boolean: indicates that PostCSS should set the origin
53
- content (for example, Sass source) of the source map. By default,
54
- it is `true`. But if all previous maps do not contain sources content,
55
- PostCSS will also leave it out even if you do not set this option.
56
-
57
- * `annotation` boolean or string: indicates that PostCSS should add annotation
58
- comments to the CSS. By default, PostCSS will always add a comment with a path
59
- to the source map. PostCSS will not add annotations to CSS files that
60
- do not contain any comments.
61
-
62
- By default, PostCSS presumes that you want to save the source map as
63
- `opts.to + '.map'` and will use this path in the annotation comment.
64
- A different path can be set by providing a string value for `annotation`.
65
-
66
- If you have set `inline: true`, annotation cannot be disabled.
67
-
68
- * `from` string: by default, PostCSS will set the `sources` property of the map
69
- to the value of the `from` option. If you want to override this behaviour, you
70
- can use `map.from` to explicitly set the source map's `sources` property.
71
- Path should be absolute or relative from generated file
72
- (`to` option in `process()` method).
73
-
74
- [source maps]: http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/