1 | # @emotion/babel-preset-css-prop
|
2 |
|
3 | > A babel preset to automatically enable emotion's css prop
|
4 |
|
5 | - [Install](#install)
|
6 | - [Usage](#usage)
|
7 | - [Features](#features)
|
8 | - [Options](#options)
|
9 |
|
10 | ## Install
|
11 |
|
12 | ```bash
|
13 | yarn add @emotion/babel-preset-css-prop
|
14 | ```
|
15 |
|
16 | ## Usage
|
17 |
|
18 | **.babelrc**
|
19 |
|
20 | ```json
|
21 | {
|
22 | "presets": ["@emotion/babel-preset-css-prop"]
|
23 | }
|
24 | ```
|
25 |
|
26 | `@emotion/babel-preset-css-prop` includes the emotion plugin. The `babel-plugin-emotion` entry should be be removed from your config and any options moved to the preset.
|
27 |
|
28 | ```diff
|
29 | {
|
30 | + "presets": [
|
31 | + [
|
32 | + "@emotion/babel-preset-css-prop",
|
33 | + {
|
34 | + "autoLabel": true,
|
35 | + "labelFormat": "[local]"
|
36 | + }
|
37 | + ]
|
38 | + ],
|
39 | - "plugins": [
|
40 | - [
|
41 | - "emotion",
|
42 | - {
|
43 | - "autoLabel": true,
|
44 | - "labelFormat": "[local]"
|
45 | - }
|
46 | - ]
|
47 | - ]
|
48 | }
|
49 | ```
|
50 |
|
51 | See [the options documentation](#options) for more information.
|
52 |
|
53 | ### Via CLI
|
54 |
|
55 | ```bash
|
56 | babel --presets @emotion/babel-preset-css-prop script.js
|
57 | ```
|
58 |
|
59 | ### Via Node API
|
60 |
|
61 | ```javascript
|
62 | require('@babel/core').transform(code, {
|
63 | presets: ['@emotion/babel-preset-css-prop']
|
64 | })
|
65 | ```
|
66 |
|
67 | ## Features
|
68 |
|
69 | This preset enables the `css` prop for an entire project via a single entry to the babel configuration. After adding the preset, compiled jsx code will use emotion's `jsx` function instead of `React.createElement`.
|
70 |
|
71 | | | Input | Output |
|
72 | | ------ | -------------------------- | --------------------------------------------------- |
|
73 | | Before | `<img src="avatar.png" />` | `React.createElement('img', { src: 'avatar.png' })` |
|
74 | | After | `<img src="avatar.png" />` | `jsx('img', { src: 'avatar.png' })` |
|
75 |
|
76 | `import { jsx } from '@emotion/core'` is automatically added to the top of files where required.
|
77 |
|
78 | ## Example
|
79 |
|
80 | **In**
|
81 |
|
82 | ```javascript
|
83 | const Link = props => (
|
84 | <a
|
85 | css={{
|
86 | color: 'hotpink',
|
87 | '&:hover': {
|
88 | color: 'darkorchid'
|
89 | }
|
90 | }}
|
91 | {...props}
|
92 | />
|
93 | )
|
94 | ```
|
95 |
|
96 | **Out**
|
97 |
|
98 | ```javascript
|
99 | import { jsx as ___EmotionJSX } from '@emotion/core'
|
100 |
|
101 | function _extends() {
|
102 | /* babel Object.assign polyfill */
|
103 | }
|
104 |
|
105 | var _ref =
|
106 | process.env.NODE_ENV === 'production'
|
107 | ? {
|
108 | name: '1fpk7dx-Link',
|
109 | styles: 'color:hotpink;&:hover{color:darkorchid;}label:Link;'
|
110 | }
|
111 | : {
|
112 | name: '1fpk7dx-Link',
|
113 | styles: 'color:hotpink;&:hover{color:darkorchid;}label:Link;',
|
114 | map:
|
115 | '/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImF1dG9tYXRpYy1pbXBvcnQuanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBRUkiLCJmaWxlIjoiYXV0b21hdGljLWltcG9ydC5qcyIsInNvdXJjZXNDb250ZW50IjpbImNvbnN0IExpbmsgPSBwcm9wcyA9PiAoXG4gIDxhXG4gICAgY3NzPXt7XG4gICAgICBjb2xvcjogJ2hvdHBpbmsnLFxuICAgICAgJyY6aG92ZXInOiB7XG4gICAgICAgIGNvbG9yOiAnZGFya29yY2hpZCdcbiAgICAgIH1cbiAgICB9fVxuICAgIHsuLi5wcm9wc31cbiAgLz5cbilcbiJdfQ== */'
|
116 | }
|
117 |
|
118 | const Link = props =>
|
119 | ___EmotionJSX(
|
120 | 'a',
|
121 | _extends(
|
122 | {
|
123 | css: _ref
|
124 | },
|
125 | props
|
126 | )
|
127 | )
|
128 | ```
|
129 |
|
130 | _In addition to the custom pragma, this example includes `babel-plugin-emotion` transforms that are enabled by default._
|
131 |
|
132 | ## Options
|
133 |
|
134 | Options for both `babel-plugin-emotion` and `@babel/plugin-transform-react-jsx` are supported and will be forwarded to their respective plugin.
|
135 |
|
136 | > Refer to the plugin's documentation for full option documentation.
|
137 | >
|
138 | > - [`babel-plugin-emotion`](https://emotion.sh/docs/babel)
|
139 | >
|
140 | > - [`@babel/plugin-transform-react-jsx`](https://babeljs.io/docs/en/next/babel-plugin-transform-react-jsx)
|
141 |
|
142 | ### Examples
|
143 |
|
144 | ```json
|
145 | {
|
146 | "presets": [
|
147 | "@emotion/babel-preset-css-prop",
|
148 | {
|
149 | "autoLabel": true,
|
150 | "labelFormat": "[local]",
|
151 | "useBuiltIns": false,
|
152 | "throwIfNamespace": true
|
153 | }
|
154 | ]
|
155 | }
|
156 | ```
|
157 |
|
158 | _Options set to default values for demonstration purposes._
|