webpacker 5.4.3 → 6.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (161) hide show
  1. checksums.yaml +4 -4
  2. data/.eslintrc.js +1 -1
  3. data/.github/workflows/ruby.yml +12 -0
  4. data/.gitignore +2 -0
  5. data/.node-version +1 -1
  6. data/.rubocop.yml +3 -108
  7. data/6_0_upgrade.md +43 -0
  8. data/CHANGELOG.md +19 -18
  9. data/CONTRIBUTING.md +1 -1
  10. data/Gemfile.lock +91 -94
  11. data/README.md +220 -99
  12. data/gemfiles/Gemfile-rails-edge +1 -1
  13. data/lib/install/config/webpack/base.js +3 -0
  14. data/lib/install/config/webpack/development.js +2 -2
  15. data/lib/install/config/webpack/production.js +2 -2
  16. data/lib/install/config/webpack/test.js +2 -2
  17. data/lib/install/config/webpacker.yml +8 -37
  18. data/lib/install/javascript/packs/application.css +9 -0
  19. data/lib/install/javascript/packs/application.js +3 -1
  20. data/lib/install/template.rb +32 -26
  21. data/lib/tasks/webpacker/binstubs.rake +6 -4
  22. data/lib/tasks/webpacker/check_binstubs.rake +4 -4
  23. data/lib/tasks/webpacker/check_yarn.rake +1 -1
  24. data/lib/tasks/webpacker/compile.rake +4 -2
  25. data/lib/tasks/webpacker/info.rake +12 -10
  26. data/lib/tasks/webpacker/install.rake +6 -4
  27. data/lib/tasks/webpacker/verify_install.rake +2 -1
  28. data/lib/tasks/webpacker/yarn_install.rake +9 -7
  29. data/lib/tasks/webpacker.rake +2 -11
  30. data/lib/webpacker/compiler.rb +15 -8
  31. data/lib/webpacker/configuration.rb +10 -45
  32. data/lib/webpacker/dev_server_runner.rb +21 -2
  33. data/lib/webpacker/env.rb +1 -5
  34. data/lib/webpacker/helper.rb +22 -32
  35. data/lib/webpacker/manifest.rb +1 -1
  36. data/lib/webpacker/version.rb +1 -1
  37. data/lib/webpacker/webpack_runner.rb +5 -0
  38. data/package/__tests__/config.js +5 -37
  39. data/package/__tests__/development.js +9 -11
  40. data/package/__tests__/env.js +12 -4
  41. data/package/__tests__/production.js +6 -6
  42. data/package/__tests__/staging.js +7 -6
  43. data/package/__tests__/test.js +4 -5
  44. data/package/babel/preset.js +55 -0
  45. data/package/config.js +3 -11
  46. data/package/env.js +8 -2
  47. data/package/environments/__tests__/base.js +15 -47
  48. data/package/environments/base.js +62 -125
  49. data/package/environments/development.js +45 -44
  50. data/package/environments/production.js +63 -68
  51. data/package/environments/test.js +2 -2
  52. data/package/index.js +13 -8
  53. data/package/rules/babel.js +9 -7
  54. data/package/rules/coffee.js +6 -0
  55. data/package/rules/erb.js +15 -0
  56. data/package/rules/file.js +19 -19
  57. data/package/rules/index.js +15 -18
  58. data/package/rules/less.js +22 -0
  59. data/package/rules/sass.js +10 -10
  60. data/package/rules/svg.js +20 -0
  61. data/package/utils/get_style_rule.js +26 -36
  62. data/package/utils/helpers.js +26 -35
  63. data/package.json +29 -45
  64. data/test/compiler_test.rb +0 -12
  65. data/test/configuration_test.rb +1 -32
  66. data/test/dev_server_runner_test.rb +24 -5
  67. data/test/engine_rake_tasks_test.rb +39 -0
  68. data/test/helper_test.rb +24 -30
  69. data/test/mounted_app/Rakefile +4 -0
  70. data/test/mounted_app/test/dummy/Rakefile +3 -0
  71. data/test/mounted_app/test/dummy/bin/rails +3 -0
  72. data/test/mounted_app/test/dummy/bin/rake +3 -0
  73. data/test/mounted_app/test/dummy/config/application.rb +10 -0
  74. data/test/mounted_app/test/dummy/config/environment.rb +3 -0
  75. data/test/mounted_app/test/dummy/config/webpacker.yml +75 -0
  76. data/test/mounted_app/test/dummy/config.ru +5 -0
  77. data/test/mounted_app/test/dummy/package.json +7 -0
  78. data/test/rake_tasks_test.rb +1 -10
  79. data/test/test_app/config/webpacker.yml +1 -25
  80. data/test/test_app/config/webpacker_public_root.yml +0 -1
  81. data/test/test_app/public/packs/manifest.json +17 -13
  82. data/test/test_app/some.config.js +0 -0
  83. data/test/webpack_runner_test.rb +9 -3
  84. data/yarn.lock +2505 -4943
  85. metadata +41 -87
  86. data/.travis.yml +0 -43
  87. data/docs/assets.md +0 -119
  88. data/docs/cloud9.md +0 -310
  89. data/docs/css.md +0 -308
  90. data/docs/deployment.md +0 -130
  91. data/docs/docker.md +0 -68
  92. data/docs/engines.md +0 -213
  93. data/docs/env.md +0 -63
  94. data/docs/es6.md +0 -72
  95. data/docs/folder-structure.md +0 -66
  96. data/docs/integrations.md +0 -220
  97. data/docs/misc.md +0 -23
  98. data/docs/props.md +0 -223
  99. data/docs/target.md +0 -22
  100. data/docs/testing.md +0 -136
  101. data/docs/troubleshooting.md +0 -158
  102. data/docs/typescript.md +0 -190
  103. data/docs/v4-upgrade.md +0 -142
  104. data/docs/webpack-dev-server.md +0 -92
  105. data/docs/webpack.md +0 -364
  106. data/docs/yarn.md +0 -23
  107. data/lib/install/angular.rb +0 -23
  108. data/lib/install/coffee.rb +0 -25
  109. data/lib/install/config/.browserslistrc +0 -1
  110. data/lib/install/config/babel.config.js +0 -82
  111. data/lib/install/config/postcss.config.js +0 -12
  112. data/lib/install/config/webpack/environment.js +0 -3
  113. data/lib/install/elm.rb +0 -39
  114. data/lib/install/erb.rb +0 -25
  115. data/lib/install/examples/angular/hello_angular/app/app.component.ts +0 -9
  116. data/lib/install/examples/angular/hello_angular/app/app.module.ts +0 -16
  117. data/lib/install/examples/angular/hello_angular/index.ts +0 -8
  118. data/lib/install/examples/angular/hello_angular/polyfills.ts +0 -73
  119. data/lib/install/examples/angular/hello_angular.js +0 -7
  120. data/lib/install/examples/coffee/hello_coffee.coffee +0 -4
  121. data/lib/install/examples/elm/Main.elm +0 -55
  122. data/lib/install/examples/elm/hello_elm.js +0 -16
  123. data/lib/install/examples/erb/hello_erb.js.erb +0 -6
  124. data/lib/install/examples/react/babel.config.js +0 -99
  125. data/lib/install/examples/react/hello_react.jsx +0 -26
  126. data/lib/install/examples/react/tsconfig.json +0 -21
  127. data/lib/install/examples/stimulus/application.js +0 -1
  128. data/lib/install/examples/stimulus/controllers/hello_controller.js +0 -18
  129. data/lib/install/examples/stimulus/controllers/index.js +0 -9
  130. data/lib/install/examples/svelte/app.svelte +0 -11
  131. data/lib/install/examples/svelte/hello_svelte.js +0 -20
  132. data/lib/install/examples/typescript/hello_typescript.ts +0 -4
  133. data/lib/install/examples/typescript/tsconfig.json +0 -24
  134. data/lib/install/examples/vue/app.vue +0 -22
  135. data/lib/install/examples/vue/hello_vue.js +0 -72
  136. data/lib/install/loaders/coffee.js +0 -6
  137. data/lib/install/loaders/elm.js +0 -25
  138. data/lib/install/loaders/erb.js +0 -11
  139. data/lib/install/loaders/svelte.js +0 -9
  140. data/lib/install/loaders/vue.js +0 -6
  141. data/lib/install/react.rb +0 -18
  142. data/lib/install/stimulus.rb +0 -12
  143. data/lib/install/svelte.rb +0 -29
  144. data/lib/install/typescript.rb +0 -39
  145. data/lib/install/vue.rb +0 -49
  146. data/lib/tasks/installers.rake +0 -42
  147. data/package/config_types/__tests__/config_list.js +0 -118
  148. data/package/config_types/__tests__/config_object.js +0 -43
  149. data/package/config_types/config_list.js +0 -75
  150. data/package/config_types/config_object.js +0 -55
  151. data/package/config_types/index.js +0 -7
  152. data/package/rules/module.css.js +0 -3
  153. data/package/rules/module.sass.js +0 -8
  154. data/package/rules/node_modules.js +0 -22
  155. data/package/utils/__tests__/deep_assign.js +0 -32
  156. data/package/utils/__tests__/deep_merge.js +0 -10
  157. data/package/utils/__tests__/get_style_rule.js +0 -65
  158. data/package/utils/__tests__/objectify.js +0 -9
  159. data/package/utils/deep_assign.js +0 -22
  160. data/package/utils/deep_merge.js +0 -22
  161. data/package/utils/objectify.js +0 -3
data/docs/integrations.md DELETED
@@ -1,220 +0,0 @@
1
- # Integrations
2
-
3
- Webpacker ships with basic out-of-the-box integration for React, Angular, Vue and Elm.
4
- You can see a list of available commands/tasks by running `bundle exec rails webpacker`:
5
-
6
- ## React
7
-
8
- To use Webpacker with [React](https://facebook.github.io/react/), create a
9
- new Rails 5.1+ app using `--webpack=react` option:
10
-
11
- ```bash
12
- # Rails 5.1+
13
- rails new myapp --webpack=react
14
- ```
15
-
16
- (or run `bundle exec rails webpacker:install:react` in an existing Rails app already
17
- setup with Webpacker).
18
-
19
- The installer will add all relevant dependencies using Yarn, changes
20
- to the configuration files, and an example React component to your
21
- project in `app/javascript/packs` so that you can experiment with React right away.
22
-
23
-
24
- ## Angular with TypeScript
25
-
26
- To use Webpacker with [Angular](https://angular.io/), create a
27
- new Rails 5.1+ app using `--webpack=angular` option:
28
-
29
- ```bash
30
- # Rails 5.1+
31
- rails new myapp --webpack=angular
32
- ```
33
-
34
- (or run `bundle exec rails webpacker:install:angular` on a Rails app already
35
- setup with Webpacker).
36
-
37
- The installer will add the TypeScript and Angular core libraries using Yarn alongside
38
- a few changes to the configuration files. An example component written in
39
- TypeScript will also be added to your project in `app/javascript` so that
40
- you can experiment with Angular right away.
41
-
42
- By default, Angular uses a JIT compiler for development environment. This
43
- compiler is not compatible with restrictive CSP (Content Security
44
- Policy) environments like Rails 5.2+. You can use Angular AOT compiler
45
- in development with the [@ngtools/webpack](https://www.npmjs.com/package/@ngtools/webpack#usage) plugin.
46
-
47
- Alternatively if you're using Rails 5.2+ you can enable `unsafe-eval` rule for your
48
- development environment. This can be done in the `config/initializers/content_security_policy.rb`
49
- with the following code:
50
-
51
- ```ruby
52
- Rails.application.config.content_security_policy do |policy|
53
- if Rails.env.development?
54
- policy.script_src :self, :https, :unsafe_eval
55
- else
56
- policy.script_src :self, :https
57
- end
58
- end
59
- ```
60
-
61
-
62
- ## Vue
63
-
64
- To use Webpacker with [Vue](https://vuejs.org/), create a
65
- new Rails 5.1+ app using `--webpack=vue` option:
66
-
67
- ```bash
68
- # Rails 5.1+
69
- rails new myapp --webpack=vue
70
- ```
71
- (or run `bundle exec rails webpacker:install:vue` on a Rails app already setup with Webpacker).
72
-
73
- The installer will add Vue and its required libraries using Yarn alongside
74
- automatically applying changes needed to the configuration files. An example component will
75
- be added to your project in `app/javascript` so that you can experiment with Vue right away.
76
-
77
- If you're using Rails 5.2+ you'll need to enable `unsafe-eval` rule for your development environment.
78
- This can be done in the `config/initializers/content_security_policy.rb` with the following
79
- configuration:
80
-
81
- ```ruby
82
- Rails.application.config.content_security_policy do |policy|
83
- if Rails.env.development?
84
- policy.script_src :self, :https, :unsafe_eval
85
- else
86
- policy.script_src :self, :https
87
- end
88
- end
89
- ```
90
- You can read more about this in the [Vue docs](https://vuejs.org/v2/guide/installation.html#CSP-environments).
91
-
92
- ### Lazy loading integration
93
-
94
- See [docs/es6](es6.md) to know more about Webpack and Webpacker configuration.
95
-
96
- For instance, you can lazy load Vue JS components:
97
-
98
- Before:
99
- ```js
100
- import Vue from 'vue'
101
- import { VCard } from 'vuetify/lib'
102
-
103
- Vue.component('VCard', VCard)
104
- ```
105
-
106
- After:
107
- ```js
108
- import Vue from 'vue'
109
-
110
- // With destructuring assignment
111
- Vue.component('VCard', import('vuetify/lib').then(({ VCard }) => VCard)
112
-
113
- // Or without destructuring assignment
114
- Vue.component('OtherComponent', () => import('./OtherComponent'))
115
- ```
116
-
117
- You can use it in a Single File Component as well:
118
-
119
- ```html
120
- <template>
121
- ...
122
- </template>
123
-
124
- <script>
125
- export default {
126
- components: {
127
- OtherComponent: () => import('./OtherComponent')
128
- }
129
- }
130
- </script>
131
- ```
132
-
133
- By wrapping the import function into an arrow function, Vue will execute it only when it gets requested, loading the module in that moment.
134
-
135
- ##### Automatic registration
136
-
137
- ```js
138
- /**
139
- * The following block of code may be used to automatically register your
140
- * Vue components. It will recursively scan this directory for the Vue
141
- * components and automatically register them with their "basename".
142
- *
143
- * Eg. ./components/OtherComponent.vue -> <other-component></other-component>
144
- * Eg. ./UI/ButtonComponent.vue -> <button-component></button-component>
145
- */
146
-
147
- const files = require.context('./', true, /\.vue$/i)
148
- files.keys().map(key => {
149
- const component = key.split('/').pop().split('.')[0]
150
-
151
- // With Lazy Loading
152
- Vue.component(component, () => import(`${key}`))
153
-
154
- // Or without Lazy Loading
155
- Vue.component(component, files(key).default)
156
- })
157
- ```
158
-
159
- ## Elm
160
-
161
- To use Webpacker with [Elm](http://elm-lang.org), create a
162
- new Rails 5.1+ app using `--webpack=elm` option:
163
-
164
- ```
165
- # Rails 5.1+
166
- rails new myapp --webpack=elm
167
- ```
168
-
169
- (or run `bundle exec rails webpacker:install:elm` on a Rails app already setup with Webpacker).
170
-
171
- The Elm library and its core packages will be added via Yarn and Elm.
172
- An example `Main.elm` app will also be added to your project in `app/javascript`
173
- so that you can experiment with Elm right away.
174
-
175
- ## Svelte
176
-
177
- To use Webpacker with [Svelte](https://svelte.dev), create a
178
- new Rails 5.1+ app using `--webpack=svelte` option:
179
-
180
- ```
181
- # Rails 5.1+
182
- rails new myapp --webpack=svelte
183
- ```
184
-
185
- (or run `bundle exec rails webpacker:install:svelte` on a Rails app already setup with Webpacker).
186
-
187
- Please play with the [Svelte Tutorial](https://svelte.dev/tutorial/basics) or learn more about its API at https://svelte.dev/docs
188
-
189
- ## Stimulus
190
-
191
- To use Webpacker with [Stimulus](http://stimulusjs.org), create a
192
- new Rails 5.1+ app using `--webpack=stimulus` option:
193
-
194
- ```
195
- # Rails 5.1+
196
- rails new myapp --webpack=stimulus
197
- ```
198
-
199
- (or run `bundle exec rails webpacker:install:stimulus` on a Rails app already setup with Webpacker).
200
-
201
- Please read [The Stimulus Handbook](https://stimulusjs.org/handbook/introduction) or learn more about its source code at https://github.com/stimulusjs/stimulus
202
-
203
- ## CoffeeScript
204
-
205
- To add [CoffeeScript](http://coffeescript.org/) support,
206
- run `bundle exec rails webpacker:install:coffee` on a Rails app already
207
- setup with Webpacker.
208
-
209
- An example `hello_coffee.coffee` file will also be added to your project
210
- in `app/javascript/packs` so that you can experiment with CoffeeScript right away.
211
-
212
- ## Erb
213
-
214
- To add [Erb](https://apidock.com/ruby/ERB) support in your JS templates,
215
- run `bundle exec rails webpacker:install:erb` on a Rails app already
216
- setup with Webpacker.
217
-
218
- An example `hello_erb.js.erb` file will also be added to your project
219
- in `app/javascript/packs` so that you can experiment with Erb-flavoured
220
- javascript right away.
data/docs/misc.md DELETED
@@ -1,23 +0,0 @@
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 DELETED
@@ -1,223 +0,0 @@
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="{&quot;message&quot;:&quot;Hello!&quot;,&quot;name&quot;:&quot;David&quot;}"></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/target.md DELETED
@@ -1,22 +0,0 @@
1
- # Target browsers
2
-
3
- By default webpacker provides these front-end tools:
4
- - [@babel/preset-env](https://github.com/babel/babel/tree/master/packages/babel-preset-env)
5
- - [Autoprefixer](https://github.com/postcss/autoprefixer)
6
- - [postcss-preset-env](https://github.com/csstools/postcss-preset-env)
7
-
8
- All these tools use [Browserslist](https://github.com/browserslist/browserslist) to detect which environment your users have
9
-
10
- Webpacker browserslist default target:
11
- ```
12
- defaults
13
- ```
14
-
15
- `defaults`: `(> 0.5%, last 2 versions, Firefox ESR, not dead)`, [browserl.ist](https://browserl.ist/) is an online tool to check what browsers will be selected by some query.
16
-
17
- To keep browsers data up to date, you need to run:
18
- ```bash
19
- yarn upgrade caniuse-lite
20
- ```
21
-
22
- at least once every few months, to prevent such [problems](https://github.com/browserslist/browserslist/issues/492)
data/docs/testing.md DELETED
@@ -1,136 +0,0 @@
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
- },
15
- "devDependencies": {
16
- "karma": "^1.7.1",
17
- "karma-webpack": "^2.0.4",
18
- "karma-chrome-launcher": "^2.2.0",
19
- "karma-jquery": "^0.2.2",
20
- "karma-jasmine": "^1.1.0",
21
- "karma-jasmine-jquery": "^0.1.1",
22
- "jasmine-core": "^2.8.0",
23
- [optional] "karma-coverage": "^1.1.1",
24
- [optional] "karma-coverage-istanbul-reporter": "^1.3.0",
25
- [optional] "karma-spec-reporter": "0.0.31",
26
- [optional] "istanbul-instrumenter-loader": "^3.0.0",
27
- }
28
- ```
29
-
30
- 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.
31
-
32
- ```js
33
- // config/webpack/test.js
34
- const environment = require('./environment')
35
- environment.plugins.get('Manifest').options.writeToFileEmit = process.env.NODE_ENV !== 'test'
36
- environment.loaders.append('istanbul-instrumenter', {
37
- test: /\.ts$/,
38
- enforce: "post",
39
- loader: "istanbul-instrumenter-loader",
40
- query: {
41
- esModules: true
42
- },
43
- exclude: ["node_modules", /\.test\.ts$/]
44
- }) /* optional */
45
- module.exports = environment.toWebpackConfig()
46
- ```
47
-
48
- 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).
49
-
50
- ```js
51
- // karma.conf.js
52
- const webpackConfig = require('./config/webpack/test.js')
53
- module.exports = function(config) {
54
- config.set({
55
- basePath: "",
56
- frameworks: ["jquery-3.2.1", "jasmine-jquery", "jasmine"],
57
- plugins: [
58
- "karma-jquery",
59
- "karma-jasmine-jquery",
60
- "karma-jasmine",
61
- "karma-webpack",
62
- "karma-chrome-launcher",
63
- "karma-coverage-istanbul-reporter" /* optional */,
64
- "karma-spec-reporter" /* optional */
65
- ],
66
- files: [ "/* add spec files */" ],
67
- exclude: [],
68
- webpack: webpackConfig,
69
- preprocessors: {"/* add spec files */" : ["webpack"]},
70
- mime: { "text/x-typescript": ["ts"] },
71
- reporters: ["progress", "coverage-istanbul" /* optional */],
72
- coverageIstanbulReporter: {
73
- reports: [ 'html', 'lcovonly', 'text-summary' ],
74
- fixWebpackSourcePaths: true
75
- } /* optional */,
76
- port: 9876,
77
- colors: true,
78
- logLevel: config.LOG_INFO,
79
- autoWatch: true,
80
- browsers: ["Chrome"],
81
- singleRun: true
82
- });
83
- };
84
- ```
85
-
86
- ## Lazy compilation
87
-
88
- Webpacker lazily compiles assets in test env so you can write your tests without any extra
89
- setup and everything will just work out of the box.
90
-
91
- Here is a sample system test case with hello_react example component:
92
-
93
- ```js
94
- // Example React component
95
-
96
- import React from 'react'
97
- import ReactDOM from 'react-dom'
98
- import PropTypes from 'prop-types'
99
-
100
- const Hello = props => (
101
- <div>Hello David</div>
102
- )
103
-
104
- document.addEventListener('DOMContentLoaded', () => {
105
- ReactDOM.render(
106
- <Hello />,
107
- document.body.appendChild(document.createElement('div')),
108
- )
109
- })
110
- ```
111
-
112
- ```erb
113
- <%# views/pages/home.html.erb %>
114
-
115
- <%= javascript_pack_tag "hello_react" %>
116
- ```
117
-
118
- ```rb
119
- # Tests example React component
120
- require "application_system_test_case"
121
- class HomesTest < ApplicationSystemTestCase
122
- test "can see the hello message" do
123
- visit root_url
124
- assert_selector "h5", text: "Hello! David"
125
- end
126
- end
127
- ```
128
-
129
- ## Capybara setup for Rails
130
- Make sure you configure Rails to serve static files from the public directory in the test environment.
131
-
132
- ```rb
133
- # config/environments/test.rb
134
- # Configure public file server for tests with Cache-Control for performance.
135
- config.public_file_server.enabled = true
136
- ```