Skip to content

Commit 9cabf9b

Browse files
Kocalweaverryan
authored andcommitted
Add JSX integration for Vue.js
1 parent 6e1b371 commit 9cabf9b

File tree

17 files changed

+338
-3
lines changed

17 files changed

+338
-3
lines changed

fixtures/vuejs-jsx/App.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#app {
2+
font-family: 'Avenir', Helvetica, Arial, sans-serif;
3+
-webkit-font-smoothing: antialiased;
4+
-moz-osx-font-smoothing: grayscale;
5+
text-align: center;
6+
color: #2c3e50;
7+
margin-top: 60px;
8+
}

fixtures/vuejs-jsx/App.jsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import './App.css';
2+
import './App.scss';
3+
import './App.less';
4+
import Hello from './components/Hello';
5+
6+
class TestClassSyntax {
7+
8+
}
9+
10+
export default {
11+
name: 'app',
12+
render() {
13+
return (
14+
<div id="app">
15+
<img src={require('./assets/logo.png')}/>
16+
<Hello></Hello>
17+
</div>
18+
);
19+
},
20+
};

fixtures/vuejs-jsx/App.less

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#app {
2+
margin-top: 40px;
3+
}

fixtures/vuejs-jsx/App.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#app {
2+
display: flex;
3+
color: #2c3e90;
4+
}

fixtures/vuejs-jsx/assets/logo.png

6.69 KB
Loading
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.h1, .h2 {
2+
font-weight: normal;
3+
}
4+
5+
.ul {
6+
list-style-type: none;
7+
padding: 0;
8+
}
9+
10+
.li {
11+
display: inline-block;
12+
margin: 0 10px;
13+
}
14+
15+
.a {
16+
color: #42b983;
17+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import styles from './Hello.css?module';
2+
3+
export default {
4+
name: 'hello',
5+
data() {
6+
return {
7+
msg: 'Welcome to Your Vue.js App',
8+
};
9+
},
10+
render() {
11+
return (
12+
<div class="hello">
13+
<h1 class={styles.h1}>{this.msg}</h1>
14+
<h2 class={styles.h2}>Essential Links</h2>
15+
<ul class={styles.ul}>
16+
<li class={styles.li}><a class={styles.a} href="https://vuejs.org" target="_blank">Core Docs</a></li>
17+
<li class={styles.li}><a class={styles.a} href="https://forum.vuejs.org" target="_blank">Forum</a></li>
18+
<li class={styles.li}><a class={styles.a} href="https://gitter.im/vuejs/vue" target="_blank">Gitter Chat</a></li>
19+
<li class={styles.li}><a class={styles.a} href="https://twitter.com/vuejs" target="_blank">Twitter</a></li>
20+
<br/>
21+
<li class={styles.li}><a class={styles.a} href="http://vuejs-templates.github.io/webpack/" target="_blank">Docs for This Template</a></li>
22+
</ul>
23+
<h2 class={styles.h2}>Ecosystem</h2>
24+
<ul class={styles.ul}>
25+
<li class={styles.li}><a class={styles.a} href="http://router.vuejs.org/" target="_blank">vue-router</a></li>
26+
<li class={styles.li}><a class={styles.a} href="http://vuex.vuejs.org/" target="_blank">vuex</a></li>
27+
<li class={styles.li}><a class={styles.a} href="http://vue-loader.vuejs.org/" target="_blank">vue-loader</a></li>
28+
<li class={styles.li}><a class={styles.a} href="https://github.com/vuejs/awesome-vue" target="_blank">awesome-vue</a></li>
29+
</ul>
30+
</div>
31+
);
32+
},
33+
};

fixtures/vuejs-jsx/main.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Vue from 'vue'
2+
import App from './App'
3+
4+
new Vue({
5+
el: '#app',
6+
template: '<App/>',
7+
components: { App }
8+
})

index.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -936,11 +936,26 @@ class Encore {
936936
* options.preLoaders = { ... }
937937
* });
938938
*
939+
* // or configure Encore-specific options
940+
* Encore.enableVueLoader(() => {}, {
941+
* // set optional Encore-specific options, for instance:
942+
*
943+
* // enable JSX usage in Vue components
944+
* // https://vuejs.org/v2/guide/render-function.html#JSX
945+
* useJsx: true
946+
* })
947+
*
948+
* Supported options:
949+
* * {boolean} useJsx (default=false)
950+
* Configure Babel to use the preset "@vue/babel-preset-jsx",
951+
* in order to enable JSX usage in Vue components.
952+
*
939953
* @param {function} vueLoaderOptionsCallback
954+
* @param {object} encoreOptions
940955
* @returns {Encore}
941956
*/
942-
enableVueLoader(vueLoaderOptionsCallback = () => {}) {
943-
webpackConfig.enableVueLoader(vueLoaderOptionsCallback);
957+
enableVueLoader(vueLoaderOptionsCallback = () => {}, encoreOptions = {}) {
958+
webpackConfig.enableVueLoader(vueLoaderOptionsCallback, encoreOptions);
944959

945960
return this;
946961
}

lib/WebpackConfig.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ class WebpackConfig {
8888
useBuiltIns: false,
8989
corejs: null,
9090
};
91+
this.vueOptions = {
92+
useJsx: false,
93+
};
9194

9295
// Features/Loaders options callbacks
9396
this.postCssLoaderOptionsCallback = () => {};
@@ -602,14 +605,23 @@ class WebpackConfig {
602605
forkedTypeScriptTypesCheckOptionsCallback;
603606
}
604607

605-
enableVueLoader(vueLoaderOptionsCallback = () => {}) {
608+
enableVueLoader(vueLoaderOptionsCallback = () => {}, vueOptions = {}) {
606609
this.useVueLoader = true;
607610

608611
if (typeof vueLoaderOptionsCallback !== 'function') {
609612
throw new Error('Argument 1 to enableVueLoader() must be a callback function.');
610613
}
611614

612615
this.vueLoaderOptionsCallback = vueLoaderOptionsCallback;
616+
617+
// Check allowed keys
618+
for (const key of Object.keys(vueOptions)) {
619+
if (!(key in this.vueOptions)) {
620+
throw new Error(`"${key}" is not a valid key for enableVueLoader(). Valid keys: ${Object.keys(this.vueOptions).join(', ')}.`);
621+
}
622+
}
623+
624+
this.vueOptions = vueOptions;
613625
}
614626

615627
enableEslintLoader(eslintLoaderOptionsOrCallback = () => {}) {

0 commit comments

Comments
 (0)