webpacker 5.0.1 → 6.0.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (169) hide show
  1. checksums.yaml +4 -4
  2. data/.eslintrc.js +8 -8
  3. data/.github/workflows/jest.yml +38 -0
  4. data/.github/workflows/js-lint.yml +39 -0
  5. data/.github/workflows/rubocop.yml +39 -0
  6. data/.github/workflows/ruby.yml +70 -0
  7. data/.gitignore +2 -0
  8. data/.node-version +1 -1
  9. data/.rubocop.yml +11 -16
  10. data/CHANGELOG.md +33 -1
  11. data/CONTRIBUTING.md +1 -1
  12. data/Gemfile.lock +91 -85
  13. data/README.md +27 -44
  14. data/docs/assets.md +21 -5
  15. data/docs/css.md +80 -30
  16. data/docs/deployment.md +18 -0
  17. data/docs/docker.md +2 -2
  18. data/docs/engines.md +1 -1
  19. data/docs/env.md +4 -1
  20. data/docs/es6.md +1 -1
  21. data/docs/integrations.md +2 -2
  22. data/docs/props.md +2 -38
  23. data/docs/react.md +183 -0
  24. data/docs/target.md +22 -0
  25. data/docs/testing.md +13 -3
  26. data/docs/troubleshooting.md +3 -1
  27. data/docs/typescript.md +92 -27
  28. data/docs/webpack-dev-server.md +17 -15
  29. data/docs/webpack.md +58 -107
  30. data/lib/install/config/webpack/base.js +3 -0
  31. data/lib/install/config/webpack/development.js +2 -2
  32. data/lib/install/config/webpack/production.js +2 -2
  33. data/lib/install/config/webpack/test.js +2 -2
  34. data/lib/install/config/webpacker.yml +9 -42
  35. data/lib/install/examples/{vue → vue3}/app.vue +10 -5
  36. data/lib/install/examples/vue3/hello_vue.js +15 -0
  37. data/lib/install/javascript/packs/application.css +9 -0
  38. data/lib/install/template.rb +33 -19
  39. data/lib/tasks/webpacker.rake +2 -11
  40. data/lib/tasks/webpacker/binstubs.rake +6 -4
  41. data/lib/tasks/webpacker/check_binstubs.rake +4 -4
  42. data/lib/tasks/webpacker/check_node.rake +1 -1
  43. data/lib/tasks/webpacker/check_yarn.rake +2 -3
  44. data/lib/tasks/webpacker/compile.rake +4 -2
  45. data/lib/tasks/webpacker/info.rake +12 -10
  46. data/lib/tasks/webpacker/install.rake +6 -4
  47. data/lib/tasks/webpacker/verify_install.rake +2 -1
  48. data/lib/tasks/webpacker/yarn_install.rake +9 -1
  49. data/lib/webpacker/commands.rb +1 -1
  50. data/lib/webpacker/compiler.rb +14 -9
  51. data/lib/webpacker/configuration.rb +7 -27
  52. data/lib/webpacker/dev_server_proxy.rb +3 -1
  53. data/lib/webpacker/dev_server_runner.rb +23 -4
  54. data/lib/webpacker/helper.rb +55 -46
  55. data/lib/webpacker/manifest.rb +1 -1
  56. data/lib/webpacker/railtie.rb +0 -43
  57. data/lib/webpacker/runner.rb +1 -0
  58. data/lib/webpacker/version.rb +1 -1
  59. data/lib/webpacker/webpack_runner.rb +7 -2
  60. data/package.json +27 -43
  61. data/package/__tests__/config.js +6 -27
  62. data/package/__tests__/dev_server.js +2 -0
  63. data/package/__tests__/development.js +18 -7
  64. data/package/__tests__/env.js +12 -4
  65. data/package/__tests__/production.js +6 -6
  66. data/package/__tests__/staging.js +7 -6
  67. data/package/__tests__/test.js +4 -5
  68. data/package/babel/preset-react.js +62 -0
  69. data/package/babel/preset.js +44 -0
  70. data/package/config.js +4 -9
  71. data/package/configPath.js +3 -0
  72. data/package/dev_server.js +1 -1
  73. data/package/env.js +9 -4
  74. data/package/environments/__tests__/base.js +21 -36
  75. data/package/environments/base.js +67 -128
  76. data/package/environments/development.js +46 -39
  77. data/package/environments/production.js +69 -67
  78. data/package/environments/test.js +2 -2
  79. data/package/index.js +6 -6
  80. data/package/rules/babel.js +20 -11
  81. data/package/rules/coffee.js +6 -0
  82. data/package/rules/erb.js +13 -0
  83. data/package/rules/file.js +19 -18
  84. data/package/rules/index.js +24 -18
  85. data/package/rules/less.js +18 -0
  86. data/package/rules/sass.js +6 -7
  87. data/package/rules/svg.js +23 -0
  88. data/package/utils/get_style_rule.js +22 -28
  89. data/package/utils/helpers.js +3 -35
  90. data/test/compiler_test.rb +4 -11
  91. data/test/configuration_test.rb +2 -32
  92. data/test/dev_server_runner_test.rb +25 -6
  93. data/test/engine_rake_tasks_test.rb +39 -0
  94. data/test/helper_test.rb +18 -9
  95. data/test/mounted_app/Rakefile +4 -0
  96. data/test/mounted_app/test/dummy/Rakefile +3 -0
  97. data/test/mounted_app/test/dummy/bin/rails +3 -0
  98. data/test/mounted_app/test/dummy/bin/rake +3 -0
  99. data/test/mounted_app/test/dummy/config.ru +5 -0
  100. data/test/mounted_app/test/dummy/config/application.rb +10 -0
  101. data/test/mounted_app/test/dummy/config/environment.rb +3 -0
  102. data/test/mounted_app/test/dummy/config/webpacker.yml +75 -0
  103. data/test/mounted_app/test/dummy/package.json +7 -0
  104. data/test/rake_tasks_test.rb +1 -10
  105. data/test/test_app/config/application.rb +0 -1
  106. data/test/test_app/config/webpacker.yml +3 -21
  107. data/test/test_app/config/webpacker_public_root.yml +0 -1
  108. data/test/test_app/public/packs/manifest.json +18 -13
  109. data/test/test_app/some.config.js +0 -0
  110. data/test/webpack_runner_test.rb +10 -4
  111. data/webpacker.gemspec +1 -1
  112. data/yarn.lock +2657 -6224
  113. metadata +53 -71
  114. data/.travis.yml +0 -43
  115. data/lib/install/angular.rb +0 -23
  116. data/lib/install/coffee.rb +0 -25
  117. data/lib/install/config/.browserslistrc +0 -1
  118. data/lib/install/config/babel.config.js +0 -72
  119. data/lib/install/config/postcss.config.js +0 -12
  120. data/lib/install/config/webpack/environment.js +0 -3
  121. data/lib/install/elm.rb +0 -39
  122. data/lib/install/erb.rb +0 -25
  123. data/lib/install/examples/angular/hello_angular.js +0 -7
  124. data/lib/install/examples/angular/hello_angular/app/app.component.ts +0 -9
  125. data/lib/install/examples/angular/hello_angular/app/app.module.ts +0 -16
  126. data/lib/install/examples/angular/hello_angular/index.ts +0 -8
  127. data/lib/install/examples/angular/hello_angular/polyfills.ts +0 -73
  128. data/lib/install/examples/coffee/hello_coffee.coffee +0 -4
  129. data/lib/install/examples/elm/Main.elm +0 -55
  130. data/lib/install/examples/elm/hello_elm.js +0 -16
  131. data/lib/install/examples/erb/hello_erb.js.erb +0 -6
  132. data/lib/install/examples/react/babel.config.js +0 -87
  133. data/lib/install/examples/react/hello_react.jsx +0 -26
  134. data/lib/install/examples/react/tsconfig.json +0 -20
  135. data/lib/install/examples/stimulus/application.js +0 -1
  136. data/lib/install/examples/stimulus/controllers/hello_controller.js +0 -18
  137. data/lib/install/examples/stimulus/controllers/index.js +0 -9
  138. data/lib/install/examples/svelte/app.svelte +0 -11
  139. data/lib/install/examples/svelte/hello_svelte.js +0 -20
  140. data/lib/install/examples/typescript/hello_typescript.ts +0 -4
  141. data/lib/install/examples/typescript/tsconfig.json +0 -23
  142. data/lib/install/examples/vue/hello_vue.js +0 -72
  143. data/lib/install/loaders/coffee.js +0 -6
  144. data/lib/install/loaders/elm.js +0 -25
  145. data/lib/install/loaders/erb.js +0 -11
  146. data/lib/install/loaders/svelte.js +0 -9
  147. data/lib/install/loaders/typescript.js +0 -11
  148. data/lib/install/loaders/vue.js +0 -6
  149. data/lib/install/react.rb +0 -18
  150. data/lib/install/stimulus.rb +0 -12
  151. data/lib/install/svelte.rb +0 -29
  152. data/lib/install/typescript.rb +0 -46
  153. data/lib/install/vue.rb +0 -49
  154. data/lib/tasks/installers.rake +0 -42
  155. data/package/config_types/__tests__/config_list.js +0 -118
  156. data/package/config_types/__tests__/config_object.js +0 -43
  157. data/package/config_types/config_list.js +0 -75
  158. data/package/config_types/config_object.js +0 -55
  159. data/package/config_types/index.js +0 -7
  160. data/package/rules/module.css.js +0 -3
  161. data/package/rules/module.sass.js +0 -8
  162. data/package/rules/node_modules.js +0 -24
  163. data/package/utils/__tests__/deep_assign.js +0 -32
  164. data/package/utils/__tests__/deep_merge.js +0 -10
  165. data/package/utils/__tests__/get_style_rule.js +0 -65
  166. data/package/utils/__tests__/objectify.js +0 -9
  167. data/package/utils/deep_assign.js +0 -22
  168. data/package/utils/deep_merge.js +0 -22
  169. data/package/utils/objectify.js +0 -3
@@ -17,7 +17,7 @@ services:
17
17
  volumes:
18
18
  - .:/webpacker-example-app
19
19
  ports:
20
- - '3035:3035'
20
+ - '127.0.0.1:3035:3035'
21
21
  ```
22
22
 
23
23
  add nodejs and yarn as dependencies in Dockerfile,
@@ -56,7 +56,7 @@ then add the webpacker host name environment variable to the web/app service:
56
56
  volumes:
57
57
  - .:/usr/src/app
58
58
  ports:
59
- - "3000:3000"
59
+ - "127.0.0.1:3000:3000"
60
60
  environment:
61
61
  - WEBPACKER_DEV_SERVER_HOST=webpacker
62
62
  ```
@@ -99,7 +99,7 @@ Now you can use `stylesheet_pack_tag` and `javascript_pack_tag` from within your
99
99
 
100
100
  Add Rake task to compile assets in production (`rake my_engine:webpacker:compile`)
101
101
 
102
- - File `my_engine_rootlib/tasks/my_engine_tasks.rake`
102
+ - File `lib/tasks/my_engine_tasks.rake`
103
103
 
104
104
  ```ruby
105
105
  def ensure_log_goes_to_stdout
@@ -49,7 +49,10 @@ dotenvFiles.forEach((dotenvFile) => {
49
49
  dotenv.config({ path: dotenvFile, silent: true })
50
50
  })
51
51
 
52
- environment.plugins.prepend('Environment', new webpack.EnvironmentPlugin(JSON.parse(JSON.stringify(process.env))))
52
+ environment.plugins.insert(
53
+ "Environment",
54
+ new webpack.EnvironmentPlugin(process.env)
55
+ )
53
56
 
54
57
  module.exports = environment
55
58
  ```
@@ -42,7 +42,7 @@ The comment you see above (`/* webpackChunkName */`) is not arbitrary, it is one
42
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
43
 
44
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).
45
+ - [Docs for configuring `splitChunks` in webpacker](/docs/webpack.md#add-splitchunks-webpack-v4).
46
46
  - [Docs for using dynamic `import()`](https://webpack.js.org/guides/code-splitting#dynamic-imports).
47
47
 
48
48
  ## Module import vs require()
@@ -91,7 +91,7 @@ You can read more about this in the [Vue docs](https://vuejs.org/v2/guide/instal
91
91
 
92
92
  ### Lazy loading integration
93
93
 
94
- See [docs/es6](docs/es6.md) to know more about Webpack and Webpacker configuration.
94
+ See [docs/es6](es6.md) to know more about Webpack and Webpacker configuration.
95
95
 
96
96
  For instance, you can lazy load Vue JS components:
97
97
 
@@ -213,7 +213,7 @@ in `app/javascript/packs` so that you can experiment with CoffeeScript right awa
213
213
 
214
214
  To add [Erb](https://apidock.com/ruby/ERB) support in your JS templates,
215
215
  run `bundle exec rails webpacker:install:erb` on a Rails app already
216
- setup with Webpacker.
216
+ setup with Webpacker and add extension 'erb' on file `config/webpacker.yml`.
217
217
 
218
218
  An example `hello_erb.js.erb` file will also be added to your project
219
219
  in `app/javascript/packs` so that you can experiment with Erb-flavoured
@@ -1,45 +1,9 @@
1
1
  # Props
2
2
 
3
+ How do you pass props from your view to your JavaScript component? Here you go.
3
4
 
4
5
  ## 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
-
6
+ See [docs/react.md](./react.md#props-hydration-and-server-side-rendering-ssr).
43
7
 
44
8
  ## Vue
45
9
 
@@ -0,0 +1,183 @@
1
+ # React
2
+
3
+ ## Props Hydration and Server-Side Rendering (SSR)
4
+ You only _need_ props hydration if you need SSR. However, there's no good reason to
5
+ have your app make a second round trip to the Rails server to get initialization props.
6
+
7
+ Server-Side Rendering (SSR) results in Rails rendering HTML for your React components.
8
+ The main reasons to use SSR are better SEO and pages display more quickly.
9
+
10
+ ### Rails and React Integration Gems
11
+ If you desire more advanced React-integration, like server-side rendering, SSR with react-router, SSR with code splitting, then you should consider these gems:
12
+
13
+ | Gem | Props Hydration | Server-Side-Rendering (SSR) | SSR with HMR | SSR with React-Router | SSR with Code Splitting | Node SSR |
14
+ | --- | --------------- | --- | --------------------- | ----------------------| ------------------------|----|
15
+ | [shakacode/react_on_rails](https://github.com/shakacode/react_on_rails) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
16
+ | [react-rails](https://github.com/reactjs/react-rails) | ✅ | ✅ | | | | | |
17
+ | [webpacker-react](https://github.com/renchap/webpacker-react) | ✅ | | | | | | |
18
+
19
+ Note, Node SSR for React on Rails requires [React on Rails Pro](https://www.shakacode.com/react-on-rails-pro).
20
+
21
+ ### Hydration of Props the Manual Way
22
+
23
+ If you're not concerned with view helpers to pass props or server-side rendering, you can do it like this:
24
+
25
+ ```erb
26
+ <%# views/layouts/application.html.erb %>
27
+
28
+ <%= content_tag :div,
29
+ id: "hello-react",
30
+ data: {
31
+ message: 'Hello!',
32
+ name: 'David'
33
+ }.to_json do %>
34
+ <% end %>
35
+ ```
36
+
37
+ ```js
38
+ // app/javascript/packs/hello_react.js
39
+
40
+ const Hello = props => (
41
+ <div className='react-app-wrapper'>
42
+ <img src={clockIcon} alt="clock" />
43
+ <h5 className='hello-react'>
44
+ {props.message} {props.name}!
45
+ </h5>
46
+ </div>
47
+ )
48
+
49
+ // Render component with data
50
+ document.addEventListener('DOMContentLoaded', () => {
51
+ const node = document.getElementById('hello-react')
52
+ const data = JSON.parse(node.getAttribute('data'))
53
+
54
+ ReactDOM.render(<Hello {...data} />, node)
55
+ })
56
+ ```
57
+
58
+ ----
59
+
60
+ ## HMR and React Hot Reloading
61
+
62
+ Before turning HMR on, consider upgrading to the latest stable gems and packages:
63
+ https://github.com/rails/webpacker#upgrading
64
+
65
+ Configure `config/webpacker.yml` file:
66
+
67
+ ```yaml
68
+ development:
69
+ extract_css: false
70
+ dev_server:
71
+ hmr: true
72
+ inline: true
73
+ ```
74
+
75
+ This basic configuration alone will have HMR working with the default webpacker setup. However, an code saves will trigger a full page refresh each time you save a file.
76
+
77
+ Webpack's HMR allows the replacement of modules for React in-place without reloading the browser. To do this, you have two options:
78
+
79
+ 1. Steps below for the [github.com/pmmmwh/react-refresh-webpack-plugin](https://github.com/pmmmwh/react-refresh-webpack-plugin).
80
+ 1. Deprecated steps below for using the [github.com/gaearon/react-hot-loader](https://github.com/gaearon/react-hot-loader).
81
+
82
+ ### React Refresh Webpack Plugin
83
+ [github.com/pmmmwh/react-refresh-webpack-plugin](https://github.com/pmmmwh/react-refresh-webpack-plugin)
84
+
85
+ You can see an example commit of adding this [here](https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh/commit/7e53803fce7034f5ecff335db1f400a5743a87e7).
86
+
87
+ 1. Add react refresh packages:
88
+ `yarn add @pmmmwh/react-refresh-webpack-plugin react-refresh -D`
89
+ 2. Update `babel.config.js` adding
90
+ ```js
91
+ plugins: [
92
+ process.env.WEBPACK_DEV_SERVER && 'react-refresh/babel',
93
+ // other plugins
94
+ ```
95
+ 3. Update `config/webpack/development.js`, only including the plugin if running the WEBPACK_DEV_SERVER
96
+ ```js
97
+ const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
98
+ const environment = require('./environment')
99
+
100
+ const isWebpackDevServer = process.env.WEBPACK_DEV_SERVER;
101
+
102
+ //plugins
103
+ if (isWebpackDevServer) {
104
+ environment.plugins.append(
105
+ 'ReactRefreshWebpackPlugin',
106
+ new ReactRefreshWebpackPlugin({
107
+ overlay: {
108
+ sockPort: 3035
109
+ }
110
+ })
111
+ );
112
+ }
113
+ ```
114
+
115
+ ### React Hot Loader (Deprecated)
116
+
117
+ 1. Add the `react-hot-loader` and ` @hot-loader/react-dom` npm packages.
118
+ ```sh
119
+ yarn add --dev react-hot-loader @hot-loader/react-dom
120
+ ```
121
+
122
+ 2. Update your babel config, `babel.config.js`. Add the plugin `react-hot-loader/babel`
123
+ with option `"safetyNet": false`:
124
+
125
+ ```
126
+ {
127
+ "plugins": [
128
+ [
129
+ "react-hot-loader/babel",
130
+ {
131
+ "safetyNet": false
132
+ }
133
+ ]
134
+ ]
135
+ }
136
+ ```
137
+
138
+ 3. Add changes like this to your entry points:
139
+
140
+ ```diff
141
+ // app/javascript/app.jsx
142
+
143
+ import React from 'react';
144
+ + import { hot } from 'react-hot-loader/root';
145
+
146
+ const App = () => <SomeComponent(s) />
147
+
148
+ - export default App;
149
+ + export default hot(App);
150
+ ```
151
+
152
+ 4. Adjust your webpack configuration for development so that `sourceMapContents` option for the sass
153
+ loader is `false`:
154
+
155
+ ```diff
156
+ // config/webpack/development.js
157
+
158
+ process.env.NODE_ENV = process.env.NODE_ENV || 'development'
159
+
160
+ const environment = require('./environment')
161
+
162
+ // allows for editing sass/scss files directly in browser
163
+ + if (!module.hot) {
164
+ + environment.loaders.get('sass').use.find(item => item.loader === 'sass-loader').options.sourceMapContents = false
165
+ + }
166
+ +
167
+ module.exports = environment.toWebpackConfig()
168
+ ```
169
+
170
+ 5. Adjust your `config/webpack/environment.js` for a
171
+
172
+ ```diff
173
+ // config/webpack/environment.js
174
+
175
+ // ...
176
+
177
+ // Fixes: React-Hot-Loader: react-🔥-dom patch is not detected. React 16.6+ features may not work.
178
+ // https://github.com/gaearon/react-hot-loader/issues/1227#issuecomment-482139583
179
+ + environment.config.merge({ resolve: { alias: { 'react-dom': '@hot-loader/react-dom' } } });
180
+
181
+ module.exports = environment;
182
+ ```
183
+
@@ -0,0 +1,22 @@
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)
@@ -10,8 +10,7 @@ Webpacker does not setup `Karma` by default, so you've to manually install it al
10
10
  "test": "NODE_ENV=test karma start"
11
11
  },
12
12
  "dependencies": {
13
- "typescript": "^2.5.2",
14
- "ts-loader": "^2.3.7"
13
+ "typescript": "^2.5.2"
15
14
  },
16
15
  "devDependencies": {
17
16
  "karma": "^1.7.1",
@@ -33,7 +32,7 @@ It is beneficial to use the same webpack configuration file (generated by webpac
33
32
  ```js
34
33
  // config/webpack/test.js
35
34
  const environment = require('./environment')
36
- environment.plugins.get('Manifest').opts.writeToFileEmit = process.env.NODE_ENV !== 'test'
35
+ environment.plugins.get('Manifest').options.writeToFileEmit = process.env.NODE_ENV !== 'test'
37
36
  environment.loaders.append('istanbul-instrumenter', {
38
37
  test: /\.ts$/,
39
38
  enforce: "post",
@@ -135,3 +134,14 @@ Make sure you configure Rails to serve static files from the public directory in
135
134
  # Configure public file server for tests with Cache-Control for performance.
136
135
  config.public_file_server.enabled = true
137
136
  ```
137
+
138
+ ## Webpacker Configuration and Rails Tests
139
+
140
+ Webpacker ships with three javascript configuration files: `test.js`,
141
+ `development.js`, and `production.js`. The `NODE_ENV` environment
142
+ variable determines which config will be used. `NODE_ENV` is
143
+ independent of `RAILS_ENV` and is set to `development` by [default](/lib/install/bin/webpack#L4).
144
+ This means that `rails test` or `rspec` will use `development.js`
145
+ by default, _not_ `test.js`.
146
+
147
+ For more information see [Why doesn't Webpacker use my test config when I run Rails tests?](https://rossta.net/blog/why-doesnt-webpacker-use-my-test-config-when-i-run-rails-tests.html)
@@ -4,11 +4,13 @@
4
4
 
5
5
  1. Read the error message carefully. The error message will tell you the precise key value
6
6
  that is not matching what Webpack expects.
7
- 2. Put a `debugger` statement in your Webpack configuration and run `bin/webpack --debug`.
7
+ 2. Put a `debugger` statement in your Webpack configuration and run `bin/webpack --debug-webpacker`.
8
8
  If you have a node debugger installed, you'll see the Chrome debugger for your webpack
9
9
  config. For example, install the Chrome extension [NiM](https://chrome.google.com/webstore/detail/nodejs-v8-inspector-manag/gnhhdgbaldcilmgcpfddgdbkhjohddkj) and
10
10
  set the option for the dev tools to open automatically. For more details on debugging,
11
11
  see the official [Webpack docs on debugging](https://webpack.js.org/contribute/debugging/#devtools)
12
+ 3. Any arguments that you add to bin/webpack get sent to webpack. For example, you can pass `--debug` to switch loaders to debug mode. See [webpack CLI debug options](https://webpack.js.org/api/cli/#debug-options) for more information on the available options.
13
+ 4. You can also pass additional options to the command to run the webpack-dev-server and start the webpack-dev-server with the option `--debug-webpacker`
12
14
 
13
15
  ## ENOENT: no such file or directory - node-sass
14
16
 
@@ -1,18 +1,98 @@
1
- # Typescript
1
+ # TypeScript
2
2
 
3
+ ## Installation
3
4
 
4
- ## Typescript with React
5
+ 1. Run the TypeScript installer
5
6
 
6
- 1. Setup react using Webpacker [react installer](../README.md#react). Then run the typescript installer
7
+ ```bash
8
+ bundle exec rails webpacker:install:typescript
9
+ ```
10
+
11
+ After that, a new file called `hello_typescript.ts` will be present in your `packs` directory (or rather the `source_entry_path` of your `webpacker.yml` configuration). You're now ready to write TypeScript.
12
+
13
+ ## (Optional) Adding Compile-Time Type Checking
14
+
15
+ The default installation only transpiles your TypeScript code using Babel. If you would like to enable type checking as part of the Webpack compilation process (i.e. fail the build if there are TS errors), you can do the following:
16
+
17
+ 1. Install the Fork TS Checker Webpack Plugin
18
+
19
+ ```sh
20
+ yarn add --dev fork-ts-checker-webpack-plugin
21
+ ```
22
+
23
+ 2. Then add it to your development environment config in `config/webpack/development.js`
24
+
25
+ ```js
26
+ const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
27
+ const path = require("path");
28
+
29
+ environment.plugins.append(
30
+ "ForkTsCheckerWebpackPlugin",
31
+ new ForkTsCheckerWebpackPlugin({
32
+ typescript: {
33
+ configFile: path.resolve(__dirname, "../../tsconfig.json"),
34
+ },
35
+ async: false,
36
+ })
37
+ );
38
+ ```
39
+
40
+ If you are using `fork-ts-checker-webpack-plugin` older than 5.0, the `tsconfig` option also needs to be specified:
41
+
42
+ ```js
43
+ const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
44
+ const path = require("path");
45
+
46
+ environment.plugins.append(
47
+ "ForkTsCheckerWebpackPlugin",
48
+ new ForkTsCheckerWebpackPlugin({
49
+ // this is a relative path to your project's TypeScript config
50
+ tsconfig: path.resolve(__dirname, "../../tsconfig.json"),
51
+ // non-async so type checking will block compilation
52
+ async: false,
53
+ })
54
+ );
55
+ ```
56
+
57
+ ## Upgrading to 5.1
58
+
59
+ If you update your App to `webpacker >= 5.1` and had TypeScript installed before, you need to add some new/remove some old configurations:
60
+
61
+ 1. Remove old packages:
62
+ - `yarn remove ts-loader`
63
+
64
+ 2. Add new packages:
65
+ - `yarn add @babel/preset-typescript`
66
+
67
+ 3. Remove old configuration files:
68
+ - Delete this file: `config/webpack/loaders/typescript.js`
69
+
70
+ 4. Remove the following lines from `config/webpack/environment.js`:
71
+ - `const typescript = require('./loaders/typescript')`
72
+ - `environment.loaders.prepend('typescript', typescript)`
73
+
74
+ 5. Add the TypeScript preset to your `babel.config.js`:
75
+ - This line `['@babel/preset-typescript', { 'allExtensions': true, 'isTSX': true }]` has to be added as the last item to the `presets` array in your `babel.config.js`
76
+
77
+ ### Upgrading to 5.1 for Vue users
78
+
79
+ 1. Remove old packages:
80
+ - `yarn remove ts-loader pnp-webpack-plugin`
81
+
82
+ 2. Follow point 3 and 4 from the `TypeScript with Vue components` section
83
+
84
+ ## TypeScript with React
85
+
86
+ 1. Setup react using Webpacker [react installer](integrations.md#react). Then run the TypeScript installer
7
87
 
8
88
  ```bash
9
89
  bundle exec rails webpacker:install:typescript
10
90
  ```
11
91
 
12
- 2. Rename the generated `hello_react.js` to `hello_react.tsx`. Make the file valid typescript and
13
- now you can use typescript, JSX with React.
92
+ 2. Rename the generated `hello_react.js` to `hello_react.tsx`. Make the file valid TypeScript and
93
+ now you can use TypeScript, JSX with React.
14
94
 
15
- ## Typescript with Vue components
95
+ ## TypeScript with Vue components
16
96
 
17
97
  1. Setup Vue using the Webpacker [Vue installer](../README.md#vue). Then run the TypeScript installer
18
98
 
@@ -21,37 +101,22 @@ bundle exec rails webpacker:install:typescript
21
101
  ```
22
102
 
23
103
  2. Rename generated `hello_vue.js` to `hello_vue.ts`.
24
- 3. Add the webpack plug-n-play plugin to your yarn packages with `yarn add pnp-webpack-plugin`.
25
- 4. Change the generated `config/webpack/loaders/typescript.js` from
104
+ 3. Install the right Babel preset: `yarn add babel-preset-typescript-vue`
105
+ 4. Change the generated `babel.config.js` from
26
106
 
27
107
  ```js
28
- module.exports = {
29
- test: /\.tsx?(\.erb)?$/,
30
- use: [{
31
- loader: 'ts-loader'
32
- }]
33
- }
108
+ ["@babel/preset-typescript", { "allExtensions": true, "isTSX": true }]
34
109
  ```
35
110
 
36
111
  to
37
112
 
38
113
  ```js
39
- const PnpWebpackPlugin = require('pnp-webpack-plugin');
40
-
41
- module.exports = {
42
- test: /\.tsx?(\.erb)?$/,
43
- use: [{
44
- loader: 'ts-loader',
45
- options: PnpWebpackPlugin.tsLoaderOptions({
46
- appendTsSuffixTo: [/\.vue$/]
47
- })
48
- }]
49
- }
114
+ ["babel-preset-typescript-vue", { "allExtensions": true, "isTSX": true }]
50
115
  ```
51
116
 
52
- and now you can use `<script lang="ts">` in your `.vue` component files. See [the pnp-webpack-plugin docs for the `ts-loader` integration](https://github.com/arcanis/pnp-webpack-plugin#ts-loader-integration) for more info.
117
+ and now you can use `<script lang="ts">` in your `.vue` component files. See [the babel-preset-typescript-vue docs](https://www.npmjs.com/package/babel-preset-typescript-vue) for more info.
53
118
 
54
- ## HTML templates with Typescript and Angular
119
+ ## HTML templates with TypeScript and Angular
55
120
 
56
121
  After you have installed Angular using `bundle exec rails webpacker:install:angular`
57
122
  you would need to follow these steps to add HTML templates support: