webpacker 5.4.0 → 6.0.0.rc.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (191) hide show
  1. checksums.yaml +4 -4
  2. data/.eslintrc.js +1 -1
  3. data/.github/workflows/jest.yml +7 -15
  4. data/.github/workflows/js-lint.yml +7 -15
  5. data/.github/workflows/rubocop.yml +1 -1
  6. data/.github/workflows/ruby.yml +18 -36
  7. data/.gitignore +2 -0
  8. data/.node-version +1 -1
  9. data/.rubocop.yml +5 -4
  10. data/CHANGELOG.md +45 -9
  11. data/CONTRIBUTING.md +20 -1
  12. data/Gemfile.lock +83 -83
  13. data/README.md +394 -180
  14. data/config/README.md +3 -0
  15. data/config/webpacker.yml +1 -0
  16. data/docs/deployment.md +9 -11
  17. data/docs/developing_webpacker.md +29 -0
  18. data/docs/troubleshooting.md +61 -25
  19. data/docs/v6_upgrade.md +113 -0
  20. data/gemfiles/Gemfile-rails-edge +1 -1
  21. data/gemfiles/Gemfile-rails.6.1.x +12 -0
  22. data/lib/install/{javascript/packs/application.js → application.js} +2 -5
  23. data/lib/install/bin/webpack +4 -7
  24. data/lib/install/bin/yarn +18 -0
  25. data/lib/install/config/webpack/base.js +3 -0
  26. data/lib/install/config/webpack/development.js +2 -2
  27. data/lib/install/config/webpack/production.js +2 -2
  28. data/lib/install/config/webpack/test.js +2 -2
  29. data/lib/install/config/webpacker.yml +21 -49
  30. data/lib/install/package.json +15 -0
  31. data/lib/install/template.rb +63 -28
  32. data/lib/tasks/webpacker/binstubs.rake +6 -4
  33. data/lib/tasks/webpacker/check_binstubs.rake +4 -4
  34. data/lib/tasks/webpacker/check_node.rake +3 -0
  35. data/lib/tasks/webpacker/check_yarn.rake +4 -1
  36. data/lib/tasks/webpacker/clobber.rake +1 -1
  37. data/lib/tasks/webpacker/compile.rake +4 -2
  38. data/lib/tasks/webpacker/info.rake +12 -10
  39. data/lib/tasks/webpacker/install.rake +6 -4
  40. data/lib/tasks/webpacker/verify_config.rake +14 -0
  41. data/lib/tasks/webpacker/verify_install.rake +1 -10
  42. data/lib/tasks/webpacker/yarn_install.rake +9 -7
  43. data/lib/tasks/webpacker.rake +2 -11
  44. data/lib/tasks/yarn.rake +38 -0
  45. data/lib/webpacker/commands.rb +21 -16
  46. data/lib/webpacker/compiler.rb +16 -9
  47. data/lib/webpacker/configuration.rb +8 -32
  48. data/lib/webpacker/dev_server.rb +6 -0
  49. data/lib/webpacker/dev_server_runner.rb +28 -4
  50. data/lib/webpacker/helper.rb +47 -81
  51. data/lib/webpacker/instance.rb +4 -0
  52. data/lib/webpacker/manifest.rb +2 -3
  53. data/lib/webpacker/railtie.rb +8 -2
  54. data/lib/webpacker/runner.rb +1 -1
  55. data/lib/webpacker/version.rb +1 -1
  56. data/lib/webpacker/webpack_runner.rb +29 -3
  57. data/lib/webpacker.rb +1 -1
  58. data/package/__tests__/config.js +5 -37
  59. data/package/__tests__/development.js +13 -21
  60. data/package/__tests__/env.js +16 -4
  61. data/package/__tests__/index.js +9 -0
  62. data/package/__tests__/production.js +6 -6
  63. data/package/__tests__/staging.js +7 -6
  64. data/package/__tests__/test.js +4 -5
  65. data/package/babel/preset.js +54 -0
  66. data/package/config.js +6 -14
  67. data/package/env.js +13 -4
  68. data/package/environments/__tests__/base.js +20 -52
  69. data/package/environments/base.js +68 -128
  70. data/package/environments/development.js +49 -47
  71. data/package/environments/production.js +66 -64
  72. data/package/environments/test.js +2 -2
  73. data/package/index.js +15 -8
  74. data/package/inliningCss.js +7 -0
  75. data/package/rules/babel.js +10 -8
  76. data/package/rules/coffee.js +6 -0
  77. data/package/rules/erb.js +15 -0
  78. data/package/rules/file.js +21 -19
  79. data/package/rules/index.js +16 -18
  80. data/package/rules/less.js +22 -0
  81. data/package/rules/raw.js +5 -0
  82. data/package/rules/sass.js +9 -10
  83. data/package/rules/stylus.js +26 -0
  84. data/package/utils/get_style_rule.js +28 -36
  85. data/package/utils/helpers.js +28 -35
  86. data/package.json +23 -43
  87. data/test/command_test.rb +76 -0
  88. data/test/compiler_test.rb +0 -12
  89. data/test/configuration_test.rb +4 -35
  90. data/test/dev_server_runner_test.rb +36 -6
  91. data/test/engine_rake_tasks_test.rb +39 -0
  92. data/test/helper_test.rb +76 -62
  93. data/test/manifest_test.rb +16 -0
  94. data/test/mounted_app/Rakefile +4 -0
  95. data/test/mounted_app/test/dummy/Rakefile +3 -0
  96. data/test/mounted_app/test/dummy/bin/rails +3 -0
  97. data/test/mounted_app/test/dummy/bin/rake +3 -0
  98. data/test/mounted_app/test/dummy/config/application.rb +10 -0
  99. data/test/mounted_app/test/dummy/config/environment.rb +3 -0
  100. data/test/mounted_app/test/dummy/config/webpacker.yml +75 -0
  101. data/test/mounted_app/test/dummy/config.ru +5 -0
  102. data/test/mounted_app/test/dummy/package.json +7 -0
  103. data/test/rake_tasks_test.rb +1 -10
  104. data/test/test_app/app/{javascript/packs → packs/entrypoints}/application.js +1 -1
  105. data/test/test_app/config/initializers/inspect_autoload_paths.rb +1 -0
  106. data/test/test_app/config/webpacker.yml +5 -31
  107. data/test/test_app/config/webpacker_other_location.yml +79 -0
  108. data/test/test_app/config/webpacker_public_root.yml +0 -1
  109. data/test/test_app/public/packs/manifest.json +36 -18
  110. data/test/test_app/some.config.js +0 -0
  111. data/test/webpack_runner_test.rb +9 -3
  112. data/test/webpacker_test.rb +21 -0
  113. data/webpacker.gemspec +2 -2
  114. data/yarn.lock +2183 -6524
  115. metadata +63 -93
  116. data/.travis.yml +0 -43
  117. data/docs/assets.md +0 -119
  118. data/docs/cloud9.md +0 -310
  119. data/docs/css.md +0 -308
  120. data/docs/docker.md +0 -68
  121. data/docs/engines.md +0 -213
  122. data/docs/env.md +0 -63
  123. data/docs/es6.md +0 -72
  124. data/docs/folder-structure.md +0 -66
  125. data/docs/integrations.md +0 -220
  126. data/docs/misc.md +0 -23
  127. data/docs/props.md +0 -223
  128. data/docs/target.md +0 -22
  129. data/docs/testing.md +0 -136
  130. data/docs/typescript.md +0 -190
  131. data/docs/v4-upgrade.md +0 -142
  132. data/docs/webpack-dev-server.md +0 -92
  133. data/docs/webpack.md +0 -364
  134. data/docs/yarn.md +0 -23
  135. data/lib/install/angular.rb +0 -23
  136. data/lib/install/coffee.rb +0 -25
  137. data/lib/install/config/.browserslistrc +0 -1
  138. data/lib/install/config/babel.config.js +0 -70
  139. data/lib/install/config/postcss.config.js +0 -12
  140. data/lib/install/config/webpack/environment.js +0 -3
  141. data/lib/install/elm.rb +0 -39
  142. data/lib/install/erb.rb +0 -25
  143. data/lib/install/examples/angular/hello_angular/app/app.component.ts +0 -9
  144. data/lib/install/examples/angular/hello_angular/app/app.module.ts +0 -16
  145. data/lib/install/examples/angular/hello_angular/index.ts +0 -8
  146. data/lib/install/examples/angular/hello_angular/polyfills.ts +0 -73
  147. data/lib/install/examples/angular/hello_angular.js +0 -7
  148. data/lib/install/examples/coffee/hello_coffee.coffee +0 -4
  149. data/lib/install/examples/elm/Main.elm +0 -55
  150. data/lib/install/examples/elm/hello_elm.js +0 -16
  151. data/lib/install/examples/erb/hello_erb.js.erb +0 -6
  152. data/lib/install/examples/react/babel.config.js +0 -87
  153. data/lib/install/examples/react/hello_react.jsx +0 -26
  154. data/lib/install/examples/react/tsconfig.json +0 -21
  155. data/lib/install/examples/stimulus/application.js +0 -1
  156. data/lib/install/examples/stimulus/controllers/hello_controller.js +0 -18
  157. data/lib/install/examples/stimulus/controllers/index.js +0 -9
  158. data/lib/install/examples/svelte/app.svelte +0 -11
  159. data/lib/install/examples/svelte/hello_svelte.js +0 -20
  160. data/lib/install/examples/typescript/hello_typescript.ts +0 -4
  161. data/lib/install/examples/typescript/tsconfig.json +0 -24
  162. data/lib/install/examples/vue/app.vue +0 -22
  163. data/lib/install/examples/vue/hello_vue.js +0 -72
  164. data/lib/install/loaders/coffee.js +0 -6
  165. data/lib/install/loaders/elm.js +0 -25
  166. data/lib/install/loaders/erb.js +0 -11
  167. data/lib/install/loaders/svelte.js +0 -9
  168. data/lib/install/loaders/vue.js +0 -6
  169. data/lib/install/react.rb +0 -18
  170. data/lib/install/stimulus.rb +0 -12
  171. data/lib/install/svelte.rb +0 -29
  172. data/lib/install/typescript.rb +0 -39
  173. data/lib/install/vue.rb +0 -49
  174. data/lib/tasks/installers.rake +0 -42
  175. data/package/config_types/__tests__/config_list.js +0 -118
  176. data/package/config_types/__tests__/config_object.js +0 -43
  177. data/package/config_types/config_list.js +0 -75
  178. data/package/config_types/config_object.js +0 -55
  179. data/package/config_types/index.js +0 -7
  180. data/package/rules/module.css.js +0 -3
  181. data/package/rules/module.sass.js +0 -8
  182. data/package/rules/node_modules.js +0 -22
  183. data/package/utils/__tests__/deep_assign.js +0 -32
  184. data/package/utils/__tests__/deep_merge.js +0 -10
  185. data/package/utils/__tests__/get_style_rule.js +0 -65
  186. data/package/utils/__tests__/objectify.js +0 -9
  187. data/package/utils/deep_assign.js +0 -22
  188. data/package/utils/deep_merge.js +0 -22
  189. data/package/utils/objectify.js +0 -3
  190. /data/test/test_app/app/{javascript/packs → packs/entrypoints}/multi_entry.css +0 -0
  191. /data/test/test_app/app/{javascript/packs → packs/entrypoints}/multi_entry.js +0 -0
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
- ```
data/docs/typescript.md DELETED
@@ -1,190 +0,0 @@
1
- # TypeScript
2
-
3
- ## Installation
4
-
5
- 1. Run the TypeScript installer
6
-
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 `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](../README.md#react). Then run the TypeScript installer
87
-
88
- ```bash
89
- bundle exec rails webpacker:install:typescript
90
- ```
91
-
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.
94
-
95
- ## TypeScript with Vue components
96
-
97
- 1. Setup Vue using the Webpacker [Vue installer](../README.md#vue). Then run the TypeScript installer
98
-
99
- ```bash
100
- bundle exec rails webpacker:install:typescript
101
- ```
102
-
103
- 2. Rename generated `hello_vue.js` to `hello_vue.ts`.
104
- 3. Install the right Babel preset: `yarn add babel-preset-typescript-vue`
105
- 4. Change the generated `babel.config.js` from
106
-
107
- ```js
108
- ["@babel/preset-typescript", { "allExtensions": true, "isTSX": true }]
109
- ```
110
-
111
- to
112
-
113
- ```js
114
- ["babel-preset-typescript-vue", { "allExtensions": true, "isTSX": true }]
115
- ```
116
-
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.
118
-
119
- ## HTML templates with TypeScript and Angular
120
-
121
- After you have installed Angular using `bundle exec rails webpacker:install:angular`
122
- you would need to follow these steps to add HTML templates support:
123
-
124
- 1. Use `yarn` to add html-loader
125
-
126
- ```bash
127
- yarn add html-loader
128
- ```
129
-
130
- 2. Add html-loader to `config/webpack/environment.js`
131
-
132
- ```js
133
- environment.loaders.append('html', {
134
- test: /\.html$/,
135
- use: [{
136
- loader: 'html-loader',
137
- options: {
138
- minimize: true,
139
- removeAttributeQuotes: false,
140
- caseSensitive: true,
141
- customAttrSurround: [ [/#/, /(?:)/], [/\*/, /(?:)/], [/\[?\(?/, /(?:)/] ],
142
- customAttrAssign: [ /\)?\]?=/ ]
143
- }
144
- }]
145
- })
146
- ```
147
-
148
- 3. Add `.html` to `config/webpacker.yml`
149
-
150
- ```yml
151
- extensions:
152
- - .elm
153
- - .coffee
154
- - .html
155
- ```
156
-
157
- 4. Setup a custom `d.ts` definition
158
-
159
- ```ts
160
- // app/javascript/hello_angular/html.d.ts
161
-
162
- declare module "*.html" {
163
- const content: string
164
- export default content
165
- }
166
- ```
167
-
168
- 5. Add a template.html file relative to `app.component.ts`
169
-
170
- ```html
171
- <h1>Hello {{name}}</h1>
172
- ```
173
-
174
- 6. Import template into `app.component.ts`
175
-
176
- ```ts
177
- import { Component } from '@angular/core'
178
- import templateString from './template.html'
179
-
180
- @Component({
181
- selector: 'hello-angular',
182
- template: templateString
183
- })
184
-
185
- export class AppComponent {
186
- name = 'Angular!'
187
- }
188
- ```
189
-
190
- That's all. Voila!