You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Run `webpack-dev-server` together with Rails server with development mode.
35
38
- Just single `RAILS_ENV`, no more `NODE_ENV`.
36
39
- Rails way configurations in `config/vue.yml`.
@@ -40,9 +43,13 @@ And then execute:
40
43
Out-of-box workflow:
41
44
42
45
1.`bundle exec rake vue:create` and follow the steps.
43
-
2. Put your entry point files under `app/assets/vue/views`.
44
-
3.`webpack-dev-server` auto starts alongside `rails server` in dev mode.
45
-
4. Invoke `bundle exec rake vue:compile` to compile assets (you still have to set `RAILS_ENV=production` manually).
46
+
47
+
> Don NOT select `In package.json` for "Where do you prefer placing config for Babel, PostCSS, ESLint, etc.?". Some functionalities like alias of jest may not work.
48
+
49
+
2. Put your JavaScript files under `app/assets/vue/entry_points`.
50
+
3. Insert your entry point by `vue_entry 'entry_point'` in views or `render_vue 'entry_point'` in controllers.
51
+
4.`webpack-dev-server` auto starts alongside `rails server` in dev mode.
52
+
5. Invoke `env RAILS_ENV=production bundle exec rake vue:compile` to compile assets (you still must manually set `RAILS_ENV` to `production`).
46
53
47
54
> More settings are available in `config/vue.yml`
48
55
@@ -52,17 +59,19 @@ Out-of-box workflow:
52
59
53
60
#### Concept: Entry Point and File structure
54
61
55
-
Webpack sees JavaScript files as the center of page rather than HTML. Thus all styles, images, fonts and other assets are related to a JS files. Any `.js` files under `app/assets/vue/views` will be treated as entry-point files.
62
+
The root path of your Vue assets is `app/assets/vue`. This gem will generate several folders. However, `app/assets/vue/entry_points` is the only one matters.
56
63
57
-
Please ONLY puts your entry-point files under `app/assets/vue/views` folder with `.js` extension name.
64
+
Webpack sees one JavaScript file as the center of a web page rather than HTML. Thus all styles, images, fonts and other assets are related to a JS files by `import 'css/png/svg/woff2/json'`. Any `.js` file under `app/assets/vue/entry_points` will be a entry-point.
65
+
66
+
Please ONLY puts your entry-point files under `app/assets/vue/entry_points` folder with `.js` extension name.
58
67
59
68
> Be aware, `.js.erb` and `.vue.erb` are NOT supported. I will explain the reason in [Q&A section](#difference-from-webpacker).
60
69
61
70
If you are new to modern front-end development, or more specifically with `webpack` in this case, please read [Q&A section](#qa) for more information.
62
71
63
72
#### Helper `vue_entry`
64
73
65
-
`vue_entry` is like `javascript_include_tag` and `stylesheet_link_tag` which generates relative assets links for your entry point. (It's like a combination of `stylesheet_pack_tag` and `javascript_packs_with_chunks_tag` in Webpacker 4. I will explain why it's different in [Q&A](#qa).)
74
+
`vue_entry` is like `javascript_include_tag` and `stylesheet_link_tag` which generates relative assets links for your entry point. (It's like `javascript_packs_with_chunks_tag` in Webpacker 4. I will explain why it's different in [Q&A](#qa).)
66
75
67
76
> You may have interest of path alias in `config/vue.yml`.
68
77
@@ -74,12 +83,11 @@ If you are new to modern front-end development, or more specifically with `webpa
@@ -168,6 +176,55 @@ If you are new to modern front-end development, or more specifically with `webpa
168
176
169
177
</details>
170
178
179
+
#### Use `render_vue` in controllers
180
+
181
+
Usually you only need `<div id="app"></div>` and `vue_entry 'entry/point'` to render a Vue page. You can use `render_vue 'entry/point'` inside your controller.
182
+
183
+
This method is simply a wrap of `render html: vue_entry('entry_point'), layout: true`. So you can pass any arguments supported by `render` including `layout`.
184
+
185
+
<details><summary>For example</summary>
186
+
187
+
```ruby
188
+
# app/controllers/my_vue_controller
189
+
classMyVueController < ApplicationController
190
+
layout 'vue_base'
191
+
192
+
deffoo
193
+
render_vue 'foo/bar'
194
+
end
195
+
end
196
+
```
197
+
198
+
```html
199
+
<!-- app/views/layouts/vue_base.erb -->
200
+
<!DOCTYPE html>
201
+
<html>
202
+
<head>
203
+
<title>My Vue</title>
204
+
</head>
205
+
<body>
206
+
<divid="app"></div>
207
+
<%= yield %>
208
+
</body>
209
+
</html>
210
+
```
211
+
212
+
```js
213
+
// app/assets/vue/entry_points/foo/bar.js
214
+
215
+
importVuefrom'vue';
216
+
217
+
importBarfrom'../views/Bar.vue';
218
+
219
+
Vue.config.productionTip=false;
220
+
221
+
newVue({
222
+
render:h=>h(Bar),
223
+
}).$mount('#app');
224
+
```
225
+
226
+
</details>
227
+
171
228
#### Public Output Path
172
229
173
230
If the default setting `vue_assets` does not bother you at all, you can ignore this section.
@@ -192,13 +249,19 @@ Actually `public_output_path` in `config/vue.yml` is very simple - just a sub pa
192
249
193
250
</details>
194
251
252
+
#### Summary
253
+
254
+
If you still feel confusing, please create a new project and select copy demo code.
255
+
256
+
I will explain what happens in [Explanation by Demo](#explanation-by-demo).
257
+
195
258
### Available Settings
196
259
197
260
#### General settings file is `config/vue.yml`
198
261
199
262
-`manifest_output`
200
263
201
-
Where to put `manifest.json` which required by Rails.
264
+
Where to put `manifest.json` which required by Rails production mode. You can set it in development mode for inspection.
202
265
203
266
All entry-points will be compiled into assets files. Rails needs `manifest.json` to know what are the files and will serve all its JS/CSS tags.
204
267
@@ -218,9 +281,13 @@ Actually `public_output_path` in `config/vue.yml` is very simple - just a sub pa
218
281
219
282
Please see [available options](#valid-vue-cli-config-options).
220
283
284
+
-`alias`
285
+
286
+
It's basically `resolve/alias` for Webpack. However, you don't have to config this settings in `.eslintrc.js` and `jest.config.js` again and again. `@vue/cli` will pass the settings to eslint via its plugin. The configuration for jest will be generated and passed to `jest.config.js` through `vue.rails.js`.
287
+
221
288
#### Customize your webpack configurations in `vue.config.js`
222
289
223
-
Feel free update `vue.config.js` by yourself. There are some lines of boiler-plate code to adapt `compression-webpack-plugin` and `webpack-bundle-analyzer`.
290
+
Feel free to update `vue.config.js` by yourself. There are some lines of boiler-plate code to adapt `compression-webpack-plugin` and `webpack-bundle-analyzer`.
224
291
225
292
### Rake Tasks
226
293
@@ -313,7 +380,7 @@ Feel free update `vue.config.js` by yourself. There are some lines of boiler-pla
313
380
}
314
381
```
315
382
316
-
> You may need to invoke with `bundle exec`. Rails 5 and above supports new `rails rake:task` flavor.
383
+
> You may need to invoke `rake`with `bundle exec`. Rails 5 and above supports new `rails rake:task` flavor.
317
384
318
385
## Valid Vue CLI config Options
319
386
@@ -505,3 +572,173 @@ Sorry, I don't think many gems work on Windows. Please install a virtual machine
505
572
Currently `vue.config.js` is reading configurations from `vue.rails.js` which depends on `js-yaml`. It will fallback to `bundle exec rake vue:json_config` without `js-yaml` installed. You may suffer performance issue if your rake tasks are slow.
506
573
507
574
</details>
575
+
576
+
## Explanation by Demo
577
+
578
+
<!-- <details><summary>Explanation by Demo</summary> -->
579
+
580
+
### Install
581
+
582
+
Run `bundle exec rake vue:create` or `rails vue:create` in Rails 5+, and follow the steps:
583
+
584
+
```
585
+
$ bundle exec rake vue:create
586
+
Which package manager to use? (Y=yarn, N=npm) [Yn]
587
+
...
588
+
? Generate project in current directory? Yes
589
+
...
590
+
? Check the features needed for your project: Babel, Linter, Unit
591
+
? Pick a linter / formatter config: Airbnb
592
+
? Pick additional lint features: (Press <space> to select, <a> to toggle all, <i> to invert selection)Lint on save
593
+
? Pick a unit testing solution: Jest
594
+
? Where do you prefer placing config for Babel, PostCSS, ESLint, etc.? In dedicated config files
595
+
...
596
+
Do you want to copy demo code? (Y=Yes, N=No) [yN]y
597
+
...
598
+
```
599
+
600
+
### First Taste
601
+
602
+
Now with `rails s`, open `http://localhost:3000/vue_demo/foo` in a browser you should be able to see a red big red "Foo" with blue "Hello Vue!".
603
+
604
+
Do not stop your rails server, open `app/assets/vue/views/Foo.vue` in your editor:
605
+
606
+
```diff
607
+
<template>
608
+
<div id="app">
609
+
<h1>Foo</h1>
610
+
- <HelloWorld msg="Vue!"></HelloWorld>
611
+
+ <HelloWorld msg="Rails!"></HelloWorld>
612
+
</div>
613
+
</template>
614
+
```
615
+
616
+
Change `msg="Vue!"` to `msg="Rails!"` and save. You will the text in your browser changed to "Hello Rails!". You can change styles or edit `app/assets/vue/components/HelloWorld.vue` and immediately see the result as well.
617
+
618
+
This functionality is called [HMR (Hot Module Replacement)](https://webpack.js.org/concepts/hot-module-replacement/) which is the killing feature provided by webpack-dev-server. You will soon fail in love with this feature and never want to go back to manually refresh your browser again and again.
0 commit comments