Skip to content

Commit 698daf5

Browse files
committed
Merge branch '3.4' into 4.2
* 3.4: [Frontend] Add section for using JSX with Vue.js
2 parents f3f14a9 + 3926224 commit 698daf5

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

frontend/encore/vuejs.rst

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,120 @@ updated styles still requires a page refresh.
4040

4141
See :doc:`/frontend/encore/dev-server` for more details.
4242

43+
JSX Support
44+
-----------
45+
46+
You can enable `JSX with Vue.js`_ by configuring the second parameter of the
47+
``.enableVueLoader()`` method:
48+
49+
.. code-block:: diff
50+
51+
// webpack.config.js
52+
// ...
53+
54+
Encore
55+
// ...
56+
.addEntry('main', './assets/main.js')
57+
58+
- .enableVueLoader()
59+
+ .enableVueLoader(() => {}, {
60+
+ useJsx: true
61+
+ })
62+
;
63+
64+
Next, run or restart Encore. When you do, you will see an error message helping
65+
you install any missing dependencies. After running that command and restarting
66+
Encore, you're done!
67+
68+
Your ``.jsx`` files will now be transformed through ``@vue/babel-preset-jsx``.
69+
70+
Using styles
71+
~~~~~~~~~~~~
72+
73+
You can't use ``<style>`` in ``.jsx`` files. As a workaround, you can import
74+
``.css``, ``.scss``, etc. files manually:
75+
76+
.. code-block:: javascript
77+
78+
// App.jsx
79+
80+
import './App.css'
81+
82+
export default {
83+
name: 'App',
84+
render() {
85+
return (
86+
<div>
87+
...
88+
</div>
89+
)
90+
}
91+
}
92+
93+
.. note::
94+
95+
Importing styles this way makes them global. See the next section for
96+
scoping them to your component.
97+
98+
Using Scoped Styles
99+
~~~~~~~~~~~~~~~~~~~
100+
101+
You can't use `Scoped Styles`_ (``<style scoped>``) either in ``.jsx`` files. As
102+
a workaround, you can use `CSS Modules`_ by suffixing import paths with
103+
``?module``:
104+
105+
.. code-block:: javascript
106+
107+
// Component.jsx
108+
109+
import styles from './Component.css?module' // suffix with "?module"
110+
111+
export default {
112+
name: 'Component',
113+
render() {
114+
return (
115+
<div>
116+
<h1 class={styles.title}>
117+
Hello World
118+
</h1>
119+
</div>
120+
)
121+
}
122+
}
123+
124+
.. code-block:: css
125+
126+
/* Component.css */
127+
128+
.title {
129+
color: red
130+
}
131+
132+
The output will be something like ``<h1 class="h1_a3dKp">Hello World</h1>``.
133+
134+
Using images
135+
~~~~~~~~~~~~
136+
137+
You can't use ``<img src="./image.png">`` in ``.jsx`` files. As a workaround,
138+
you can import them with ``require()`` function:
139+
140+
.. code-block:: javascript
141+
142+
export default {
143+
name: 'Component',
144+
render() {
145+
return (
146+
<div>
147+
<img src={require("./image.png")} />
148+
</div>
149+
)
150+
}
151+
}
152+
43153
.. _`babel-preset-react`: https://babeljs.io/docs/plugins/preset-react/
44154
.. _`Vue.js`: https://vuejs.org/
45155
.. _`vue-loader options`: https://vue-loader.vuejs.org/options.html
46156
.. _`Encore's index.js file`: https://github.com/symfony/webpack-encore/blob/master/index.js
157+
.. _`JSX with Vue.js`: https://github.com/vuejs/jsx
158+
.. _`Scoped Styles`: https://vue-loader.vuejs.org/guide/scoped-css.html
159+
.. _`CSS Modules`: https://github.com/css-modules/css-modules

0 commit comments

Comments
 (0)