Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pirms 3 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. # babel-plugin-emotion
  2. > Babel plugin for the minification and optimization of emotion styles.
  3. `babel-plugin-emotion` is highly recommended, but not required in version 8 and
  4. above of `emotion`.
  5. ## Features
  6. <table>
  7. <thead>
  8. <tr>
  9. <th>Feature/Syntax</th>
  10. <th>Native</th>
  11. <th>Babel Plugin Required</th>
  12. <th>Notes</th>
  13. </tr>
  14. </thead>
  15. <tbody>
  16. <tr>
  17. <td><code>css``</code></td>
  18. <td align="center">✅</td>
  19. <td align="center"></td>
  20. <td></td>
  21. </tr>
  22. <tr>
  23. <td><code>css(...)</code></td>
  24. <td align="center">✅</td>
  25. <td align="center"></td>
  26. <td>Generally used for object styles.</td>
  27. </tr>
  28. <tr>
  29. <td>components as selectors</td>
  30. <td align="center"></td>
  31. <td align="center">✅</td>
  32. <td>Allows an emotion component to be <a href="https://emotion.sh/docs/styled#targeting-another-emotion-component">used as a CSS selector</a>.</td>
  33. </tr>
  34. <tr>
  35. <td>Minification</td>
  36. <td align="center"></td>
  37. <td align="center">✅</td>
  38. <td>Any leading/trailing space between properties in your <code>css</code> and <code>styled</code> blocks is removed. This can reduce the size of your final bundle.</td>
  39. </tr>
  40. <tr>
  41. <td>Dead Code Elimination</td>
  42. <td align="center"></td>
  43. <td align="center">✅</td>
  44. <td>Uglifyjs will use the injected <code>/*#__PURE__*/</code> flag comments to mark your <code>css</code> and <code>styled</code> blocks as candidates for dead code elimination.</td>
  45. </tr>
  46. <tr>
  47. <td>Source Maps</td>
  48. <td align="center"></td>
  49. <td align="center">✅</td>
  50. <td>When enabled, navigate directly to the style declaration in your javascript file.</td>
  51. </tr>
  52. <tr>
  53. <td>Contextual Class Names</td>
  54. <td align="center"></td>
  55. <td align="center">✅</td>
  56. <td>Generated class names include the name of the variable or component they were defined in.</td>
  57. </tr>
  58. </tbody>
  59. </table>
  60. ## Example
  61. **In**
  62. ```javascript
  63. const myStyles = css`
  64. font-size: 20px;
  65. @media (min-width: 420px) {
  66. color: blue;
  67. ${css`
  68. width: 96px;
  69. height: 96px;
  70. `};
  71. line-height: 26px;
  72. }
  73. background: green;
  74. ${{ backgroundColor: 'hotpink' }};
  75. `
  76. ```
  77. **Out**
  78. ```javascript
  79. const myStyles = /* #__PURE__ */ css(
  80. 'font-size:20px;@media(min-width:420px){color:blue;',
  81. /* #__PURE__ */ css('width:96px;height:96px;'),
  82. ';line-height:26px;}background:green;',
  83. { backgroundColor: 'hotpink' },
  84. ';'
  85. )
  86. ```
  87. ## Installation
  88. ```bash
  89. yarn add --dev babel-plugin-emotion
  90. ```
  91. or if you prefer npm
  92. ```bash
  93. npm install --save-dev babel-plugin-emotion
  94. ```
  95. ## Usage
  96. ### Via `.babelrc` (Recommended)
  97. **.babelrc**
  98. Without options:
  99. ```json
  100. {
  101. "plugins": ["emotion"]
  102. }
  103. ```
  104. With options:
  105. _Defaults Shown_
  106. ```js
  107. {
  108. "plugins": [
  109. [
  110. "emotion",
  111. {
  112. // sourceMap is on by default but source maps are dead code eliminated in production
  113. "sourceMap": true,
  114. "autoLabel": process.env.NODE_ENV !== 'production',
  115. "labelFormat": "[local]",
  116. "cssPropOptimization": true
  117. }
  118. ]
  119. ]
  120. }
  121. ```
  122. Recommended Setup
  123. **.babelrc**
  124. ```json
  125. {
  126. "plugins": ["emotion"]
  127. }
  128. ```
  129. ### Via CLI
  130. ```bash
  131. babel --plugins babel-plugin-emotion script.js
  132. ```
  133. ### Via Node API
  134. ```javascript
  135. require('@babel/core').transform('code', {
  136. plugins: ['babel-plugin-emotion']
  137. })
  138. ```
  139. ## Options
  140. ### `sourceMap`
  141. `boolean`, defaults to `true`.
  142. This option enables the following:
  143. - Injected source maps for use in browser dev tools
  144. [**Documentation**](https://emotion.sh/docs/source-maps)
  145. > Note:
  146. >
  147. > Source maps are on by default in babel-plugin-emotion but they will be removed in production builds
  148. ### `autoLabel`
  149. `boolean`, defaults to `process.env.NODE_ENV !== 'production'`.
  150. This option enables the following:
  151. - Automatically adds the `label` property to styles so that class names
  152. generated by `css` or `styled` include the name of the variable the result is
  153. assigned to.
  154. - Please note that non word characters in the variable will be removed
  155. (Eg. `iconStyles$1` will become `iconStyles1`) because `$` is not valid
  156. [CSS ClassName Selector](https://stackoverflow.com/questions/448981/which-characters-are-valid-in-css-class-names-selectors#449000)
  157. #### css
  158. **In**
  159. ```javascript
  160. const brownStyles = css({ color: 'brown' })
  161. ```
  162. **Out**
  163. ```javascript
  164. const brownStyles = /*#__PURE__*/ css({ color: 'brown' }, 'label:brownStyles;')
  165. ```
  166. `brownStyles`'s value would be `css-1q8eu9e-brownStyles`
  167. ### `labelFormat`
  168. `string`, defaults to `"[local]"`.
  169. This option only works when `autoLabel` is set to `true`. It allows you to
  170. define the format of the resulting `label`. The format is defined via string where
  171. variable parts are enclosed in square brackets `[]`.
  172. For example `labelFormat: "my-classname--[local]"`, where `[local]` will be replaced
  173. with the name of the variable the result is assigned to.
  174. Allowed values:
  175. - `[local]` - the name of the variable the result of the `css` or `styled` expression is assigned to.
  176. - `[filename]` - name of the file (without extension) where `css` or `styled` expression is located.
  177. - `[dirname]` - name of the directory containing the file where `css` or `styled` expression is located.
  178. This format only affects the label property of the expression, meaning that the `css` prefix and hash will
  179. be prepended automatically.
  180. #### css
  181. **In**
  182. ```javascript
  183. // BrownView.js
  184. // autoLabel: true
  185. // labelFormat: '[filename]--[local]'
  186. const brownStyles = css({ color: 'brown' })
  187. ```
  188. **Out**
  189. ```javascript
  190. const brownStyles = /*#__PURE__*/ css(
  191. { color: 'brown' },
  192. 'label:BrownView--brownStyles;'
  193. )
  194. ```
  195. `BrownView--brownStyles`'s value would be `css-hash-BrownView--brownStyles`
  196. #### styled
  197. **In**
  198. ```javascript
  199. const H1 = styled.h1({
  200. borderRadius: '50%',
  201. transition: 'transform 400ms ease-in-out',
  202. boxSizing: 'border-box',
  203. display: 'flex',
  204. ':hover': {
  205. transform: 'scale(1.2)'
  206. }
  207. })
  208. ```
  209. **Out**
  210. ```javascript
  211. const H1 = /*#__PURE__*/ styled('h1', {
  212. label: 'H1'
  213. })({
  214. borderRadius: '50%',
  215. transition: 'transform 400ms ease-in-out',
  216. boxSizing: 'border-box',
  217. display: 'flex',
  218. ':hover': {
  219. transform: 'scale(1.2)'
  220. }
  221. })
  222. ```
  223. `H1`'s class name attribute would be `css-hash-H1`
  224. ### `instances`
  225. `Array<string>`, defaults to
  226. ```jsx
  227. ;['emotion']
  228. ```
  229. This option allows `babel-plugin-emotion` to know which imports to treat as
  230. emotion imports and transform as such. This option is **only** required if you
  231. use a custom instance of emotion created with `create-emotion` or you're
  232. importing emotion from somewhere other than the paths above. Relative paths are
  233. resolved relative to `process.cwd()`(the current working directory).
  234. [**Documentation**](https://emotion.sh/docs/instances)
  235. ### `cssPropOptimization`
  236. `boolean`, defaults to `true`.
  237. This option assumes that you are using something to make `@emotion/core`'s `jsx` function work for all jsx. If you are not doing so and you do not want such optimizations to occur, disable this option.
  238. ## Babel Macros
  239. Instead of using `babel-plugin-emotion`, you can use emotion with [`babel-plugin-macros`](https://github.com/kentcdodds/babel-plugin-macros). Add `babel-plugin-macros` to your babel config (which is included in Create React App 2.0) and use the imports/packages shown below.
  240. ```jsx
  241. import styled from 'react-emotion/macro'
  242. import { css, keyframes, injectGlobal, flush, hydrate } from 'emotion/macro'
  243. import css from '@emotion/css/macro'
  244. import styled from '@emotion/styled/macro'
  245. ```