jetpacker 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.eslintignore +4 -0
- data/.eslintrc.js +14 -0
- data/.gitignore +11 -11
- data/.node-version +1 -0
- data/.rubocop.yml +125 -0
- data/.travis.yml +54 -5
- data/CHANGELOG.jetpacker.md +7 -0
- data/CHANGELOG.md +1000 -0
- data/CONTRIBUTING.md +33 -0
- data/Gemfile +9 -3
- data/Gemfile.lock +157 -21
- data/MIT-LICENSE +20 -0
- data/README.md +671 -16
- data/Rakefile +8 -3
- data/docs/assets.md +119 -0
- data/docs/cloud9.md +310 -0
- data/docs/css.md +253 -0
- data/docs/deployment.md +130 -0
- data/docs/docker.md +68 -0
- data/docs/engines.md +200 -0
- data/docs/env.md +65 -0
- data/docs/es6.md +72 -0
- data/docs/folder-structure.md +66 -0
- data/docs/misc.md +23 -0
- data/docs/props.md +223 -0
- data/docs/testing.md +137 -0
- data/docs/troubleshooting.md +156 -0
- data/docs/typescript.md +126 -0
- data/docs/v4-upgrade.md +142 -0
- data/docs/webpack-dev-server.md +92 -0
- data/docs/webpack.md +364 -0
- data/docs/yarn.md +23 -0
- data/gemfiles/Gemfile-rails-edge +12 -0
- data/gemfiles/Gemfile-rails.4.2.x +9 -0
- data/gemfiles/Gemfile-rails.5.0.x +9 -0
- data/gemfiles/Gemfile-rails.5.1.x +9 -0
- data/gemfiles/Gemfile-rails.5.2.x +9 -0
- data/gemfiles/Gemfile-rails.6.0.x +9 -0
- data/jetpacker.gemspec +28 -22
- data/lib/install/angular.rb +23 -0
- data/lib/install/bin/webpack +18 -0
- data/lib/install/bin/webpack-dev-server +18 -0
- data/lib/install/binstubs.rb +4 -0
- data/lib/install/coffee.rb +25 -0
- data/lib/install/config/.browserslistrc +1 -0
- data/lib/install/config/babel.config.js +72 -0
- data/lib/install/config/postcss.config.js +12 -0
- data/lib/install/config/webpack/development.js +5 -0
- data/lib/install/config/webpack/environment.js +3 -0
- data/lib/install/config/webpack/production.js +5 -0
- data/lib/install/config/webpack/test.js +5 -0
- data/lib/install/config/webpacker.yml +96 -0
- data/lib/install/elm.rb +39 -0
- data/lib/install/erb.rb +25 -0
- data/lib/install/examples/angular/hello_angular.js +7 -0
- data/lib/install/examples/angular/hello_angular/app/app.component.ts +9 -0
- data/lib/install/examples/angular/hello_angular/app/app.module.ts +16 -0
- data/lib/install/examples/angular/hello_angular/index.ts +8 -0
- data/lib/install/examples/angular/hello_angular/polyfills.ts +73 -0
- data/lib/install/examples/coffee/hello_coffee.coffee +4 -0
- data/lib/install/examples/elm/Main.elm +55 -0
- data/lib/install/examples/elm/hello_elm.js +16 -0
- data/lib/install/examples/erb/hello_erb.js.erb +6 -0
- data/lib/install/examples/react/babel.config.js +87 -0
- data/lib/install/examples/react/hello_react.jsx +26 -0
- data/lib/install/examples/react/tsconfig.json +20 -0
- data/lib/install/examples/stimulus/application.js +1 -0
- data/lib/install/examples/stimulus/controllers/hello_controller.js +18 -0
- data/lib/install/examples/stimulus/controllers/index.js +9 -0
- data/lib/install/examples/svelte/app.svelte +11 -0
- data/lib/install/examples/svelte/hello_svelte.js +20 -0
- data/lib/install/examples/typescript/hello_typescript.ts +4 -0
- data/lib/install/examples/typescript/tsconfig.json +23 -0
- data/lib/install/examples/vue/app.vue +22 -0
- data/lib/install/examples/vue/hello_vue.js +72 -0
- data/lib/install/javascript/packs/application.js +18 -0
- data/lib/install/loaders/coffee.js +6 -0
- data/lib/install/loaders/elm.js +25 -0
- data/lib/install/loaders/erb.js +11 -0
- data/lib/install/loaders/svelte.js +9 -0
- data/lib/install/loaders/typescript.js +11 -0
- data/lib/install/loaders/vue.js +6 -0
- data/lib/install/react.rb +18 -0
- data/lib/install/stimulus.rb +12 -0
- data/lib/install/svelte.rb +29 -0
- data/lib/install/template.rb +55 -0
- data/lib/install/typescript.rb +46 -0
- data/lib/install/vue.rb +49 -0
- data/lib/jetpacker/version.rb +2 -1
- data/lib/tasks/installers.rake +37 -0
- data/lib/tasks/webpacker.rake +28 -0
- data/lib/tasks/webpacker/binstubs.rake +11 -0
- data/lib/tasks/webpacker/check_binstubs.rake +12 -0
- data/lib/tasks/webpacker/check_node.rake +24 -0
- data/lib/tasks/webpacker/check_yarn.rake +24 -0
- data/lib/tasks/webpacker/clean.rake +21 -0
- data/lib/tasks/webpacker/clobber.rake +16 -0
- data/lib/tasks/webpacker/compile.rake +43 -0
- data/lib/tasks/webpacker/info.rake +20 -0
- data/lib/tasks/webpacker/install.rake +13 -0
- data/lib/tasks/webpacker/verify_install.rake +13 -0
- data/lib/tasks/webpacker/yarn_install.rake +21 -0
- data/lib/webpacker.rb +46 -0
- data/lib/webpacker/commands.rb +50 -0
- data/lib/webpacker/compiler.rb +107 -0
- data/lib/webpacker/configuration.rb +113 -0
- data/lib/webpacker/dev_server.rb +66 -0
- data/lib/webpacker/dev_server_proxy.rb +31 -0
- data/lib/webpacker/dev_server_runner.rb +72 -0
- data/lib/webpacker/env.rb +39 -0
- data/lib/webpacker/helper.rb +176 -0
- data/lib/webpacker/instance.rb +37 -0
- data/lib/webpacker/manifest.rb +118 -0
- data/lib/webpacker/railtie.rb +98 -0
- data/lib/webpacker/rake_tasks.rb +6 -0
- data/lib/webpacker/runner.rb +22 -0
- data/lib/webpacker/version.rb +4 -0
- data/lib/webpacker/webpack_runner.rb +32 -0
- data/package.json +82 -0
- data/package/__tests__/config.js +55 -0
- data/package/__tests__/dev_server.js +43 -0
- data/package/__tests__/development.js +30 -0
- data/package/__tests__/env.js +46 -0
- data/package/__tests__/production.js +29 -0
- data/package/__tests__/staging.js +29 -0
- data/package/__tests__/test.js +26 -0
- data/package/config.js +37 -0
- data/package/config_types/__tests__/config_list.js +118 -0
- data/package/config_types/__tests__/config_object.js +43 -0
- data/package/config_types/config_list.js +75 -0
- data/package/config_types/config_object.js +55 -0
- data/package/config_types/index.js +7 -0
- data/package/dev_server.js +20 -0
- data/package/env.js +19 -0
- data/package/environments/__tests__/base.js +74 -0
- data/package/environments/base.js +166 -0
- data/package/environments/development.js +51 -0
- data/package/environments/production.js +79 -0
- data/package/environments/test.js +3 -0
- data/package/index.js +24 -0
- data/package/rules/babel.js +21 -0
- data/package/rules/css.js +3 -0
- data/package/rules/file.js +20 -0
- data/package/rules/index.js +20 -0
- data/package/rules/module.css.js +3 -0
- data/package/rules/module.sass.js +8 -0
- data/package/rules/node_modules.js +24 -0
- data/package/rules/sass.js +8 -0
- data/package/utils/__tests__/deep_assign.js +32 -0
- data/package/utils/__tests__/deep_merge.js +10 -0
- data/package/utils/__tests__/get_style_rule.js +65 -0
- data/package/utils/__tests__/objectify.js +9 -0
- data/package/utils/deep_assign.js +22 -0
- data/package/utils/deep_merge.js +22 -0
- data/package/utils/get_style_rule.js +45 -0
- data/package/utils/helpers.js +58 -0
- data/package/utils/objectify.js +3 -0
- data/test/command_test.rb +33 -0
- data/test/compiler_test.rb +75 -0
- data/test/configuration_test.rb +108 -0
- data/test/dev_server_runner_test.rb +51 -0
- data/test/dev_server_test.rb +47 -0
- data/test/env_test.rb +23 -0
- data/test/helper_test.rb +142 -0
- data/test/manifest_test.rb +42 -0
- data/test/rake_tasks_test.rb +69 -0
- data/test/test_app/Rakefile +3 -0
- data/test/test_app/app/javascript/packs/application.js +10 -0
- data/test/test_app/bin/webpack +14 -0
- data/test/test_app/bin/webpack-dev-server +14 -0
- data/test/test_app/config.ru +5 -0
- data/test/test_app/config/application.rb +12 -0
- data/test/test_app/config/environment.rb +4 -0
- data/test/test_app/config/webpack/development.js +0 -0
- data/test/test_app/config/webpacker.yml +97 -0
- data/test/test_app/config/webpacker_public_root.yml +19 -0
- data/test/test_app/package.json +13 -0
- data/test/test_app/public/packs/manifest.json +31 -0
- data/test/test_app/yarn.lock +11 -0
- data/test/test_helper.rb +33 -0
- data/test/webpack_runner_test.rb +51 -0
- data/test/webpacker_test.rb +13 -0
- data/yarn.lock +8321 -0
- metadata +267 -29
- data/.rspec +0 -3
- data/LICENSE.txt +0 -21
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/lib/jetpacker.rb +0 -6
data/docs/es6.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# ES6
|
2
|
+
|
3
|
+
## Babel
|
4
|
+
|
5
|
+
Webpacker ships with [babel](https://babeljs.io/) - a JavaScript compiler so
|
6
|
+
you can use next generation JavaScript, today. The Webpacker installer sets up a
|
7
|
+
standard `babel.config.js` file in your app root, which will work great in most cases
|
8
|
+
because of [@babel/preset-env](https://github.com/babel/babel/tree/master/packages/babel-preset-env).
|
9
|
+
|
10
|
+
Following ES6/7 features are supported out of the box:
|
11
|
+
|
12
|
+
* Async/await.
|
13
|
+
* Object Rest/Spread Properties.
|
14
|
+
* Exponentiation Operator.
|
15
|
+
* Dynamic import() - useful for route level code-splitting
|
16
|
+
* Class Fields and Static Properties.
|
17
|
+
|
18
|
+
We have also included [core-js](https://github.com/zloirock/core-js) to polyfill features in the
|
19
|
+
older browsers.
|
20
|
+
|
21
|
+
Don't forget to add these lines into your main entry point:
|
22
|
+
|
23
|
+
```js
|
24
|
+
import "core-js/stable";
|
25
|
+
import "regenerator-runtime/runtime";
|
26
|
+
```
|
27
|
+
|
28
|
+
## Dynamic/Lazy Chunk Loading
|
29
|
+
|
30
|
+
For this section, you need Webpack and Webpacker 4. Then enable `SplitChunks` as it is explained in [docs/webpack](webpack.md).
|
31
|
+
|
32
|
+
[Dynamic code splitting](https://webpack.js.org/guides/code-splitting#dynamic-imports) enables you to conditionally request/run only the JS that you need. For example, if your site has a `searchBarComponent` on every page, you can reduce the page overhead by deferring the request for the `searchBarComponent` code until after the page has loaded, until the user has scrolled it into view, or until the user has clicked on an element.
|
33
|
+
|
34
|
+
```js
|
35
|
+
function loadSearchBarComponent() {
|
36
|
+
return import(/* webpackChunkName: "searchBarComponent" */ './pathTo/searchBarComponent')
|
37
|
+
}
|
38
|
+
```
|
39
|
+
|
40
|
+
The comment you see above (`/* webpackChunkName */`) is not arbitrary, it is one of webpacks [magic comments](https://webpack.js.org/api/module-methods/#magic-comments). They can be used to fine-tune `import()` with settings such as `defer` or `prefetch`.
|
41
|
+
|
42
|
+
**Warning**: You should not attempt to dynamically load anything from your `packs/` folder. Instead, try to make your `pack` scripts a hub from which you dynamically load `non-pack` scripts.
|
43
|
+
|
44
|
+
- [Docs for using magic comments](https://webpack.js.org/api/module-methods/#magic-comments)
|
45
|
+
- [Docs for configuring `splitChunks` in webpacker](https://github.com/rails/webpacker/blob/master/docs/webpack.md#add-splitchunks-webpack-v4).
|
46
|
+
- [Docs for using dynamic `import()`](https://webpack.js.org/guides/code-splitting#dynamic-imports).
|
47
|
+
|
48
|
+
## Module import vs require()
|
49
|
+
|
50
|
+
While you are free to use `require()` and `module.exports`, we encourage you
|
51
|
+
to use `import` and `export` instead since it reads and looks much better.
|
52
|
+
|
53
|
+
```js
|
54
|
+
import Button from 'react-bootstrap/lib/Button'
|
55
|
+
|
56
|
+
// or
|
57
|
+
import { Button } from 'react-bootstrap'
|
58
|
+
|
59
|
+
class Foo {
|
60
|
+
// code...
|
61
|
+
}
|
62
|
+
|
63
|
+
export default Foo
|
64
|
+
import Foo from './foo'
|
65
|
+
```
|
66
|
+
|
67
|
+
You can also use named export and import
|
68
|
+
|
69
|
+
```js
|
70
|
+
export const foo = () => console.log('hello world')
|
71
|
+
import { foo } from './foo'
|
72
|
+
```
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# Folder Structure
|
2
|
+
|
3
|
+
|
4
|
+
## Packs a.k.a webpack entries
|
5
|
+
|
6
|
+
"Packs" is a special directory made only for webpack entry files so don't put anything
|
7
|
+
here that you don't want to link in your views.
|
8
|
+
|
9
|
+
|
10
|
+
## Source
|
11
|
+
|
12
|
+
You can put your app source under `app/javascript` folder or whatever you have configured
|
13
|
+
in `config/webpacker.yml`.
|
14
|
+
|
15
|
+
|
16
|
+
## Example
|
17
|
+
|
18
|
+
Let's say you're building a calendar app. Your JS app structure could look like this:
|
19
|
+
|
20
|
+
```js
|
21
|
+
// app/javascript/packs/calendar.js
|
22
|
+
|
23
|
+
import 'calendar'
|
24
|
+
```
|
25
|
+
|
26
|
+
```
|
27
|
+
app/javascript/calendar/index.js // gets loaded by import 'calendar'
|
28
|
+
app/javascript/calendar/components/grid.jsx
|
29
|
+
app/javascript/calendar/styles/grid.sass
|
30
|
+
app/javascript/calendar/models/month.js
|
31
|
+
```
|
32
|
+
|
33
|
+
```erb
|
34
|
+
<%# app/views/layouts/application.html.erb %>
|
35
|
+
|
36
|
+
<%= javascript_pack_tag 'calendar' %>
|
37
|
+
<%= stylesheet_pack_tag 'calendar' %>
|
38
|
+
```
|
39
|
+
|
40
|
+
But it could also look a million other ways.
|
41
|
+
|
42
|
+
|
43
|
+
## Namespacing
|
44
|
+
|
45
|
+
You can also namespace your packs using directories similar to a Rails app.
|
46
|
+
|
47
|
+
```
|
48
|
+
app/javascript/packs/admin/orders.js
|
49
|
+
app/javascript/packs/shop/orders.js
|
50
|
+
```
|
51
|
+
|
52
|
+
and reference them in your views like this:
|
53
|
+
|
54
|
+
```erb
|
55
|
+
<%# app/views/admin/orders/index.html.erb %>
|
56
|
+
|
57
|
+
<%= javascript_pack_tag 'admin/orders' %>
|
58
|
+
```
|
59
|
+
|
60
|
+
and
|
61
|
+
|
62
|
+
```erb
|
63
|
+
<%# app/views/shop/orders/index.html.erb %>
|
64
|
+
|
65
|
+
<%= javascript_pack_tag 'shop/orders' %>
|
66
|
+
```
|
data/docs/misc.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# How-Tos
|
2
|
+
|
3
|
+
|
4
|
+
## Ignoring swap files
|
5
|
+
|
6
|
+
If you are using vim or emacs and want to ignore certain files you can add `ignore-loader`:
|
7
|
+
|
8
|
+
```
|
9
|
+
yarn add ignore-loader
|
10
|
+
```
|
11
|
+
|
12
|
+
and add `ignore-loader` to `config/webpack/environment.js`
|
13
|
+
|
14
|
+
```js
|
15
|
+
// ignores vue~ swap files
|
16
|
+
const { environment } = require('@rails/webpacker')
|
17
|
+
environment.loaders.append('ignore', {
|
18
|
+
test: /.vue~$/,
|
19
|
+
loader: 'ignore-loader'
|
20
|
+
})
|
21
|
+
```
|
22
|
+
|
23
|
+
And now all files with `.vue~` will be ignored by the webpack compiler.
|
data/docs/props.md
ADDED
@@ -0,0 +1,223 @@
|
|
1
|
+
# Props
|
2
|
+
|
3
|
+
|
4
|
+
## React
|
5
|
+
|
6
|
+
If you need more advanced React-integration, like server rendering, redux, or react-router, see [shakacode/react_on_rails](https://github.com/shakacode/react_on_rails), [react-rails](https://github.com/reactjs/react-rails), and [webpacker-react](https://github.com/renchap/webpacker-react).
|
7
|
+
|
8
|
+
If you're not concerned with view helpers to pass props or server rendering, can do it yourself:
|
9
|
+
|
10
|
+
```erb
|
11
|
+
<%# views/layouts/application.html.erb %>
|
12
|
+
|
13
|
+
<%= content_tag :div,
|
14
|
+
id: "hello-react",
|
15
|
+
data: {
|
16
|
+
message: 'Hello!',
|
17
|
+
name: 'David'
|
18
|
+
}.to_json do %>
|
19
|
+
<% end %>
|
20
|
+
```
|
21
|
+
|
22
|
+
```js
|
23
|
+
// app/javascript/packs/hello_react.js
|
24
|
+
|
25
|
+
const Hello = props => (
|
26
|
+
<div className='react-app-wrapper'>
|
27
|
+
<img src={clockIcon} alt="clock" />
|
28
|
+
<h5 className='hello-react'>
|
29
|
+
{props.message} {props.name}!
|
30
|
+
</h5>
|
31
|
+
</div>
|
32
|
+
)
|
33
|
+
|
34
|
+
// Render component with data
|
35
|
+
document.addEventListener('DOMContentLoaded', () => {
|
36
|
+
const node = document.getElementById('hello-react')
|
37
|
+
const data = JSON.parse(node.getAttribute('data'))
|
38
|
+
|
39
|
+
ReactDOM.render(<Hello {...data} />, node)
|
40
|
+
})
|
41
|
+
```
|
42
|
+
|
43
|
+
|
44
|
+
## Vue
|
45
|
+
|
46
|
+
Add the data as attributes in the element you are going to use (or any other element for that matter).
|
47
|
+
|
48
|
+
```erb
|
49
|
+
<%= content_tag :div,
|
50
|
+
id: "hello-vue",
|
51
|
+
data: {
|
52
|
+
message: "Hello!",
|
53
|
+
name: "David"
|
54
|
+
}.to_json do %>
|
55
|
+
<% end %>
|
56
|
+
```
|
57
|
+
|
58
|
+
This should produce the following HTML:
|
59
|
+
|
60
|
+
```html
|
61
|
+
<div id="hello-vue" data="{"message":"Hello!","name":"David"}"></div>
|
62
|
+
```
|
63
|
+
|
64
|
+
Now, modify your Vue app to expect the properties.
|
65
|
+
|
66
|
+
```html
|
67
|
+
<template>
|
68
|
+
<div id="app">
|
69
|
+
<p>{{test}}{{message}}{{name}}</p>
|
70
|
+
</div>
|
71
|
+
</template>
|
72
|
+
|
73
|
+
<script>
|
74
|
+
export default {
|
75
|
+
// A child component needs to explicitly declare
|
76
|
+
// the props it expects to receive using the props option
|
77
|
+
// See https://vuejs.org/v2/guide/components.html#Props
|
78
|
+
props: ["message","name"],
|
79
|
+
data: function () {
|
80
|
+
return {
|
81
|
+
test: 'This will display: '
|
82
|
+
}
|
83
|
+
}
|
84
|
+
}
|
85
|
+
</script>
|
86
|
+
|
87
|
+
<style>
|
88
|
+
</style>
|
89
|
+
|
90
|
+
```
|
91
|
+
|
92
|
+
```js
|
93
|
+
document.addEventListener('DOMContentLoaded', () => {
|
94
|
+
// Get the properties BEFORE the app is instantiated
|
95
|
+
const node = document.getElementById('hello-vue')
|
96
|
+
const props = JSON.parse(node.getAttribute('data'))
|
97
|
+
|
98
|
+
// Render component with props
|
99
|
+
new Vue({
|
100
|
+
render: h => h(App, { props })
|
101
|
+
}).$mount('#hello-vue');
|
102
|
+
})
|
103
|
+
```
|
104
|
+
|
105
|
+
You can follow same steps for Angular too.
|
106
|
+
|
107
|
+
|
108
|
+
## Elm
|
109
|
+
|
110
|
+
Just like with other implementations, we'll render our data inside a `data`
|
111
|
+
attribute:
|
112
|
+
|
113
|
+
```erb
|
114
|
+
<%= content_tag :div,
|
115
|
+
id: "hello-elm",
|
116
|
+
data: {
|
117
|
+
message: "Hello",
|
118
|
+
name: "David"
|
119
|
+
}.to_json do %>
|
120
|
+
<% end %>
|
121
|
+
```
|
122
|
+
|
123
|
+
We parse the JSON data and pass it to Elm as flags:
|
124
|
+
|
125
|
+
```js
|
126
|
+
import Elm from '../Main'
|
127
|
+
|
128
|
+
document.addEventListener('DOMContentLoaded', () => {
|
129
|
+
const node = document.getElementById('hello-elm')
|
130
|
+
const data = JSON.parse(node.getAttribute('data'))
|
131
|
+
Elm.Main.embed(node, data)
|
132
|
+
})
|
133
|
+
```
|
134
|
+
|
135
|
+
Defining `Flags` as a `type alias`, we instruct Elm to demand flags `message`
|
136
|
+
and `name` of type `String` on initialization.
|
137
|
+
|
138
|
+
Using `programWithFlags` we bring all the pieces together:
|
139
|
+
|
140
|
+
|
141
|
+
```elm
|
142
|
+
module Main exposing (..)
|
143
|
+
|
144
|
+
import Html exposing (Html, programWithFlags, h1, text)
|
145
|
+
import Html.Attributes exposing (style)
|
146
|
+
|
147
|
+
|
148
|
+
-- MODEL
|
149
|
+
|
150
|
+
|
151
|
+
type alias Flags =
|
152
|
+
{ message : String
|
153
|
+
, name : String
|
154
|
+
}
|
155
|
+
|
156
|
+
|
157
|
+
type alias Model =
|
158
|
+
{ message : String
|
159
|
+
, name : String
|
160
|
+
}
|
161
|
+
|
162
|
+
|
163
|
+
type Msg
|
164
|
+
= NoOp
|
165
|
+
|
166
|
+
|
167
|
+
|
168
|
+
-- INIT
|
169
|
+
|
170
|
+
|
171
|
+
init : Flags -> ( Model, Cmd Msg )
|
172
|
+
init flags =
|
173
|
+
let
|
174
|
+
{ message, name } =
|
175
|
+
flags
|
176
|
+
in
|
177
|
+
( Model message name, Cmd.none )
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
-- UPDATE
|
182
|
+
|
183
|
+
|
184
|
+
update : Msg -> Model -> ( Model, Cmd Msg )
|
185
|
+
update msg model =
|
186
|
+
case msg of
|
187
|
+
NoOp ->
|
188
|
+
( model, Cmd.none )
|
189
|
+
|
190
|
+
|
191
|
+
|
192
|
+
-- SUBSCRIPTIONS
|
193
|
+
|
194
|
+
|
195
|
+
subscriptions : Model -> Sub Msg
|
196
|
+
subscriptions model =
|
197
|
+
Sub.none
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
-- VIEW
|
202
|
+
|
203
|
+
|
204
|
+
view : Model -> Html Msg
|
205
|
+
view model =
|
206
|
+
h1 [ style [ ( "display", "flex" ), ( "justify-content", "center" ) ] ]
|
207
|
+
[ text (model.message ++ ", " ++ model.name ++ "!") ]
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
-- MAIN
|
212
|
+
|
213
|
+
|
214
|
+
main : Program Flags Model Msg
|
215
|
+
main =
|
216
|
+
programWithFlags
|
217
|
+
{ view = view
|
218
|
+
, init = init
|
219
|
+
, update = update
|
220
|
+
, subscriptions = subscriptions
|
221
|
+
}
|
222
|
+
|
223
|
+
```
|
data/docs/testing.md
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
# Testing
|
2
|
+
|
3
|
+
## Karma setup for Typescript
|
4
|
+
|
5
|
+
Webpacker does not setup `Karma` by default, so you've to manually install it along with its dependencies as per your need. Following things marked as optional can be used to fancify the test results (Recommended).
|
6
|
+
|
7
|
+
```js
|
8
|
+
// package.json
|
9
|
+
"scripts": {
|
10
|
+
"test": "NODE_ENV=test karma start"
|
11
|
+
},
|
12
|
+
"dependencies": {
|
13
|
+
"typescript": "^2.5.2",
|
14
|
+
"ts-loader": "^2.3.7"
|
15
|
+
},
|
16
|
+
"devDependencies": {
|
17
|
+
"karma": "^1.7.1",
|
18
|
+
"karma-webpack": "^2.0.4",
|
19
|
+
"karma-chrome-launcher": "^2.2.0",
|
20
|
+
"karma-jquery": "^0.2.2",
|
21
|
+
"karma-jasmine": "^1.1.0",
|
22
|
+
"karma-jasmine-jquery": "^0.1.1",
|
23
|
+
"jasmine-core": "^2.8.0",
|
24
|
+
[optional] "karma-coverage": "^1.1.1",
|
25
|
+
[optional] "karma-coverage-istanbul-reporter": "^1.3.0",
|
26
|
+
[optional] "karma-spec-reporter": "0.0.31",
|
27
|
+
[optional] "istanbul-instrumenter-loader": "^3.0.0",
|
28
|
+
}
|
29
|
+
```
|
30
|
+
|
31
|
+
It is beneficial to use the same webpack configuration file (generated by webpacker) in Karma configuration to avoid redundancy. Following line tells Karma not to write transpiled source files onto filesystem while testing to avoid `Error: EACCES: permission denied, mkdir '/_karma_webpack_' ` error. Then inject a new rule a.k.a. loader in the existing ones (needed only if you have installed `istanbul-instrumenter-loader`) to generate a coverage report under `/coverage` directory.
|
32
|
+
|
33
|
+
```js
|
34
|
+
// config/webpack/test.js
|
35
|
+
const environment = require('./environment')
|
36
|
+
environment.plugins.get('Manifest').opts.writeToFileEmit = process.env.NODE_ENV !== 'test'
|
37
|
+
environment.loaders.append('istanbul-instrumenter', {
|
38
|
+
test: /\.ts$/,
|
39
|
+
enforce: "post",
|
40
|
+
loader: "istanbul-instrumenter-loader",
|
41
|
+
query: {
|
42
|
+
esModules: true
|
43
|
+
},
|
44
|
+
exclude: ["node_modules", /\.test\.ts$/]
|
45
|
+
}) /* optional */
|
46
|
+
module.exports = environment.toWebpackConfig()
|
47
|
+
```
|
48
|
+
|
49
|
+
Finally, update `karma.conf.js` to read the same `test.js` file mentioned above. Rest of the things are mandatory (few marked as optional wherever appropriate).
|
50
|
+
|
51
|
+
```js
|
52
|
+
// karma.conf.js
|
53
|
+
const webpackConfig = require('./config/webpack/test.js')
|
54
|
+
module.exports = function(config) {
|
55
|
+
config.set({
|
56
|
+
basePath: "",
|
57
|
+
frameworks: ["jquery-3.2.1", "jasmine-jquery", "jasmine"],
|
58
|
+
plugins: [
|
59
|
+
"karma-jquery",
|
60
|
+
"karma-jasmine-jquery",
|
61
|
+
"karma-jasmine",
|
62
|
+
"karma-webpack",
|
63
|
+
"karma-chrome-launcher",
|
64
|
+
"karma-coverage-istanbul-reporter" /* optional */,
|
65
|
+
"karma-spec-reporter" /* optional */
|
66
|
+
],
|
67
|
+
files: [ "/* add spec files */" ],
|
68
|
+
exclude: [],
|
69
|
+
webpack: webpackConfig,
|
70
|
+
preprocessors: {"/* add spec files */" : ["webpack"]},
|
71
|
+
mime: { "text/x-typescript": ["ts"] },
|
72
|
+
reporters: ["progress", "coverage-istanbul" /* optional */],
|
73
|
+
coverageIstanbulReporter: {
|
74
|
+
reports: [ 'html', 'lcovonly', 'text-summary' ],
|
75
|
+
fixWebpackSourcePaths: true
|
76
|
+
} /* optional */,
|
77
|
+
port: 9876,
|
78
|
+
colors: true,
|
79
|
+
logLevel: config.LOG_INFO,
|
80
|
+
autoWatch: true,
|
81
|
+
browsers: ["Chrome"],
|
82
|
+
singleRun: true
|
83
|
+
});
|
84
|
+
};
|
85
|
+
```
|
86
|
+
|
87
|
+
## Lazy compilation
|
88
|
+
|
89
|
+
Webpacker lazily compiles assets in test env so you can write your tests without any extra
|
90
|
+
setup and everything will just work out of the box.
|
91
|
+
|
92
|
+
Here is a sample system test case with hello_react example component:
|
93
|
+
|
94
|
+
```js
|
95
|
+
// Example React component
|
96
|
+
|
97
|
+
import React from 'react'
|
98
|
+
import ReactDOM from 'react-dom'
|
99
|
+
import PropTypes from 'prop-types'
|
100
|
+
|
101
|
+
const Hello = props => (
|
102
|
+
<div>Hello David</div>
|
103
|
+
)
|
104
|
+
|
105
|
+
document.addEventListener('DOMContentLoaded', () => {
|
106
|
+
ReactDOM.render(
|
107
|
+
<Hello />,
|
108
|
+
document.body.appendChild(document.createElement('div')),
|
109
|
+
)
|
110
|
+
})
|
111
|
+
```
|
112
|
+
|
113
|
+
```erb
|
114
|
+
<%# views/pages/home.html.erb %>
|
115
|
+
|
116
|
+
<%= javascript_pack_tag "hello_react" %>
|
117
|
+
```
|
118
|
+
|
119
|
+
```rb
|
120
|
+
# Tests example React component
|
121
|
+
require "application_system_test_case"
|
122
|
+
class HomesTest < ApplicationSystemTestCase
|
123
|
+
test "can see the hello message" do
|
124
|
+
visit root_url
|
125
|
+
assert_selector "h5", text: "Hello! David"
|
126
|
+
end
|
127
|
+
end
|
128
|
+
```
|
129
|
+
|
130
|
+
## Capybara setup for Rails
|
131
|
+
Make sure you configure Rails to serve static files from the public directory in the test environment.
|
132
|
+
|
133
|
+
```rb
|
134
|
+
# config/environments/test.rb
|
135
|
+
# Configure public file server for tests with Cache-Control for performance.
|
136
|
+
config.public_file_server.enabled = true
|
137
|
+
```
|