webpacker 6.0.0.beta.7 → 6.0.0.pre.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/jest.yml +1 -1
- data/.github/workflows/js-lint.yml +1 -1
- data/.github/workflows/ruby.yml +6 -9
- data/.rubocop.yml +0 -105
- data/CHANGELOG.md +6 -22
- data/CONTRIBUTING.md +1 -1
- data/Gemfile.lock +90 -93
- data/README.md +110 -308
- data/docs/assets.md +135 -0
- data/docs/cloud9.md +310 -0
- data/docs/css.md +303 -0
- data/docs/deployment.md +29 -9
- data/docs/docker.md +68 -0
- data/docs/engines.md +213 -0
- data/docs/env.md +68 -0
- data/docs/es6.md +72 -0
- data/docs/folder-structure.md +66 -0
- data/docs/integrations.md +220 -0
- data/docs/misc.md +23 -0
- data/docs/props.md +187 -0
- data/docs/react.md +183 -0
- data/docs/target.md +22 -0
- data/docs/testing.md +147 -0
- data/docs/troubleshooting.md +3 -5
- data/docs/typescript.md +190 -0
- data/docs/v4-upgrade.md +142 -0
- data/docs/webpack-dev-server.md +94 -0
- data/docs/webpack.md +315 -0
- data/docs/yarn.md +23 -0
- data/lib/install/config/webpacker.yml +3 -5
- data/lib/install/examples/vue3/app.vue +27 -0
- data/lib/install/examples/vue3/hello_vue.js +15 -0
- data/lib/install/javascript/packs/application.css +9 -0
- data/lib/install/{packs/entrypoints → javascript/packs}/application.js +2 -4
- data/lib/install/template.rb +9 -16
- data/lib/tasks/webpacker/binstubs.rake +2 -2
- data/lib/tasks/webpacker/check_node.rake +0 -1
- data/lib/tasks/webpacker/check_yarn.rake +0 -1
- data/lib/tasks/webpacker/install.rake +2 -2
- data/lib/webpacker/commands.rb +1 -2
- data/lib/webpacker/compiler.rb +3 -9
- data/lib/webpacker/configuration.rb +4 -4
- data/lib/webpacker/dev_server_runner.rb +0 -2
- data/lib/webpacker/helper.rb +43 -13
- data/lib/webpacker/manifest.rb +1 -1
- data/lib/webpacker/version.rb +1 -1
- data/lib/webpacker/webpack_runner.rb +0 -1
- data/package.json +1 -1
- data/package/__tests__/development.js +1 -2
- data/package/babel/preset-react.js +62 -0
- data/package/babel/preset.js +13 -24
- data/package/environments/__tests__/base.js +5 -5
- data/package/environments/base.js +20 -15
- data/package/environments/development.js +0 -1
- data/package/environments/production.js +30 -28
- data/package/index.js +2 -7
- data/package/rules/babel.js +1 -1
- data/package/rules/coffee.js +5 -5
- data/package/rules/erb.js +3 -5
- data/package/rules/file.js +3 -5
- data/package/rules/index.js +17 -9
- data/package/rules/less.js +10 -14
- data/package/rules/sass.js +9 -13
- data/package/rules/svg.js +23 -0
- data/package/utils/get_style_rule.js +31 -27
- data/package/utils/helpers.js +0 -25
- data/test/configuration_test.rb +2 -2
- data/test/dev_server_runner_test.rb +2 -10
- data/test/helper_test.rb +39 -33
- data/test/manifest_test.rb +0 -8
- data/test/mounted_app/test/dummy/config/webpacker.yml +3 -3
- data/test/test_app/app/{packs/entrypoints → javascript/packs}/application.js +1 -1
- data/test/test_app/app/{packs/entrypoints → javascript/packs}/multi_entry.css +0 -0
- data/test/test_app/app/{packs/entrypoints → javascript/packs}/multi_entry.js +0 -0
- data/test/test_app/config/webpacker.yml +3 -3
- data/test/test_app/public/packs/manifest.json +0 -7
- metadata +36 -18
- data/config/README.md +0 -3
- data/config/webpacker.yml +0 -1
- data/docs/v6_upgrade.md +0 -86
- data/package/__tests__/index.js +0 -9
- data/package/rules/raw.js +0 -5
- data/package/rules/stylus.js +0 -26
data/docs/react.md
ADDED
@@ -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
|
+
|
data/docs/target.md
ADDED
@@ -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)
|
data/docs/testing.md
ADDED
@@ -0,0 +1,147 @@
|
|
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
|
+
```
|
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)
|
data/docs/troubleshooting.md
CHANGED
@@ -6,11 +6,9 @@
|
|
6
6
|
that is not matching what Webpack expects.
|
7
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
|
-
config. For example, install the Chrome extension
|
10
|
-
|
11
|
-
|
12
|
-
For more details on debugging, see the official
|
13
|
-
[Webpack docs on debugging](https://webpack.js.org/contribute/debugging/#devtools)
|
9
|
+
config. For example, install the Chrome extension [NiM](https://chrome.google.com/webstore/detail/nodejs-v8-inspector-manag/gnhhdgbaldcilmgcpfddgdbkhjohddkj) and
|
10
|
+
set the option for the dev tools to open automatically. For more details on debugging,
|
11
|
+
see the official [Webpack docs on debugging](https://webpack.js.org/contribute/debugging/#devtools)
|
14
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.
|
15
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`
|
16
14
|
|
data/docs/typescript.md
ADDED
@@ -0,0 +1,190 @@
|
|
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 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
|
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!
|