Skip to content

Commit 916ad73

Browse files
Kocaljaviereguiluz
authored andcommitted
[Frontend] Add section for using JSX with Vue.js
1 parent dc35a22 commit 916ad73

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

frontend/encore/vuejs.rst

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

0 commit comments

Comments
 (0)