webpacker 3.0.2 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.eslintignore +1 -0
- data/.travis.yml +10 -0
- data/CHANGELOG.md +110 -0
- data/Gemfile.lock +69 -67
- data/MIT-LICENSE +1 -1
- data/README.md +86 -30
- data/docs/assets.md +4 -4
- data/docs/cloud9.md +310 -0
- data/docs/css.md +32 -3
- data/docs/deployment.md +42 -4
- data/docs/docker.md +49 -0
- data/docs/env.md +5 -5
- data/docs/folder-structure.md +2 -2
- data/docs/testing.md +14 -34
- data/docs/troubleshooting.md +40 -4
- data/docs/typescript.md +2 -2
- data/docs/webpack-dev-server.md +21 -4
- data/docs/webpack.md +103 -25
- data/gemfiles/Gemfile-rails-edge +13 -0
- data/gemfiles/Gemfile-rails.4.2.x +10 -0
- data/gemfiles/Gemfile-rails.5.0.x +10 -0
- data/gemfiles/Gemfile-rails.5.1.x +10 -0
- data/lib/install/angular.rb +5 -5
- data/lib/install/config/webpacker.yml +7 -0
- data/lib/install/elm.rb +7 -7
- data/lib/install/examples/vue/hello_vue.js +30 -4
- data/lib/install/react.rb +5 -5
- data/lib/install/template.rb +19 -9
- data/lib/install/vue.rb +4 -4
- data/lib/tasks/installers.rake +2 -2
- data/lib/tasks/webpacker.rake +7 -6
- data/lib/tasks/webpacker/check_binstubs.rake +3 -3
- data/lib/tasks/webpacker/compile.rake +15 -8
- data/lib/tasks/webpacker/install.rake +4 -4
- data/lib/tasks/webpacker/verify_install.rake +1 -1
- data/lib/webpacker/compiler.rb +6 -6
- data/lib/webpacker/dev_server.rb +2 -2
- data/lib/webpacker/dev_server_proxy.rb +2 -1
- data/lib/webpacker/dev_server_runner.rb +4 -4
- data/lib/webpacker/helper.rb +3 -3
- data/lib/webpacker/manifest.rb +2 -2
- data/lib/webpacker/railtie.rb +41 -2
- data/lib/webpacker/runner.rb +1 -1
- data/lib/webpacker/version.rb +1 -1
- data/package.json +29 -21
- data/package/asset_host.js +4 -5
- data/package/config.js +7 -1
- data/package/config_types/__tests__/config_list.js +123 -0
- data/package/config_types/__tests__/config_object.js +43 -0
- data/package/config_types/config_list.js +83 -0
- data/package/config_types/config_object.js +55 -0
- data/package/config_types/index.js +7 -0
- data/package/environment.js +64 -40
- data/package/environments/development.js +31 -34
- data/package/environments/production.js +14 -11
- data/package/index.js +7 -2
- data/package/{loaders → rules}/babel.js +6 -4
- data/package/{loaders → rules}/coffee.js +3 -1
- data/package/rules/css.js +39 -0
- data/package/rules/elm.js +23 -0
- data/package/rules/erb.js +11 -0
- data/package/{loaders → rules}/file.js +1 -1
- data/package/rules/index.js +23 -0
- data/package/rules/sass.js +15 -0
- data/package/{loaders → rules}/typescript.js +3 -1
- data/package/rules/url.js +13 -0
- data/package/rules/vue.js +13 -0
- data/package/utils/__tests__/deep_assign.js +11 -0
- data/package/utils/__tests__/deep_merge.js +10 -0
- data/package/utils/__tests__/objectify.js +9 -0
- data/package/utils/deep_assign.js +22 -0
- data/package/utils/deep_merge.js +23 -0
- data/package/utils/helpers.js +32 -0
- data/package/utils/objectify.js +4 -0
- data/test/command_test.rb +1 -1
- data/test/compiler_test.rb +5 -1
- data/test/configuration_test.rb +1 -1
- data/test/dev_server_test.rb +1 -1
- data/test/helper_test.rb +15 -10
- data/test/manifest_test.rb +1 -1
- data/test/rake_tasks_test.rb +29 -0
- data/test/test_app/Rakefile +3 -0
- data/test/test_app/config/application.rb +11 -0
- data/test/test_app/config/environment.rb +4 -0
- data/test/{webpacker_test_helper.rb → test_helper.rb} +3 -14
- data/webpacker.gemspec +1 -1
- data/yarn.lock +1552 -829
- metadata +43 -16
- data/package/loaders/elm.js +0 -19
- data/package/loaders/erb.js +0 -9
- data/package/loaders/style.js +0 -31
- data/package/loaders/vue.js +0 -12
- data/test/test_app/config/secrets.yml +0 -5
data/docs/docker.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Docker
|
2
|
+
|
3
|
+
To setup webpacker with a dockerized Rails application is trivial.
|
4
|
+
|
5
|
+
First, add a new service for webpacker in docker-compose.yml:
|
6
|
+
|
7
|
+
```Dockerfile
|
8
|
+
version: '3'
|
9
|
+
services:
|
10
|
+
webpacker:
|
11
|
+
build: .
|
12
|
+
env_file:
|
13
|
+
- '.env.docker'
|
14
|
+
command: bundle exec webpack-dev-server
|
15
|
+
volumes:
|
16
|
+
- .:/webpacker-example-app
|
17
|
+
ports:
|
18
|
+
- '3035:3035'
|
19
|
+
```
|
20
|
+
|
21
|
+
add nodejs and yarn as dependencies in Dockerfile,
|
22
|
+
|
23
|
+
```dockerfile
|
24
|
+
FROM ruby:2.4.1
|
25
|
+
RUN apt-get update -qq
|
26
|
+
|
27
|
+
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
|
28
|
+
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash
|
29
|
+
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
|
30
|
+
|
31
|
+
RUN apt-get install -y nodejs
|
32
|
+
RUN apt-get update && apt-get install yarn
|
33
|
+
|
34
|
+
# Rest of the commands....
|
35
|
+
```
|
36
|
+
|
37
|
+
and create an env file to load environment variables from:
|
38
|
+
|
39
|
+
```env
|
40
|
+
NODE_ENV=development
|
41
|
+
RAILS_ENV=development
|
42
|
+
WEBPACKER_DEV_SERVER_HOST: 0.0.0.0
|
43
|
+
```
|
44
|
+
|
45
|
+
Lastly, rebuild your container:
|
46
|
+
|
47
|
+
```bash
|
48
|
+
docker-compose up --build
|
49
|
+
```
|
data/docs/env.md
CHANGED
@@ -2,12 +2,12 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
Environment variables are supported out of the box in Webpacker. For example if
|
5
|
-
you run the
|
5
|
+
you run the webpack dev server like so:
|
6
6
|
```
|
7
7
|
FOO=hello BAR=world ./bin/webpack-dev-server
|
8
8
|
```
|
9
9
|
|
10
|
-
You can then reference these variables in your
|
10
|
+
You can then reference these variables in your JavaScript app code with
|
11
11
|
`process.env`:
|
12
12
|
|
13
13
|
```js
|
@@ -18,11 +18,11 @@ You may want to store configuration in environment variables via `.env` files,
|
|
18
18
|
similar to the [dotenv Ruby gem](https://github.com/bkeepers/dotenv).
|
19
19
|
|
20
20
|
In development, if you use [Foreman](http://ddollar.github.io/foreman) or [Invoker](http://invoker.codemancers.com)
|
21
|
-
to launch the
|
21
|
+
to launch the webpack server, both of these tools have basic support for a
|
22
22
|
`.env` file (Invoker also supports `.env.local`), so no further configuration
|
23
23
|
is needed.
|
24
24
|
|
25
|
-
However, if you run the
|
25
|
+
However, if you run the webpack server without Foreman/Invoker, or if you
|
26
26
|
want more control over what `.env` files to load, you can use the
|
27
27
|
[dotenv npm package](https://github.com/motdotla/dotenv). Here is what you could
|
28
28
|
do to support a "Ruby-like" dotenv:
|
@@ -58,7 +58,7 @@ module.exports = environment
|
|
58
58
|
confusing behavior, in that Foreman/Invoker variables take precedence over
|
59
59
|
npm dotenv variables.
|
60
60
|
|
61
|
-
If you'd like to pass custom variables to the on demand compiler, use `
|
61
|
+
If you'd like to pass custom variables to the on demand compiler, use `Webpacker::Compiler.env` attribute.
|
62
62
|
|
63
63
|
```rb
|
64
64
|
Webpacker::Compiler.env['FRONTEND_API_KEY'] = 'your_secret_key'
|
data/docs/folder-structure.md
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# Folder Structure
|
2
2
|
|
3
3
|
|
4
|
-
## Packs a.k.a
|
4
|
+
## Packs a.k.a webpack entries
|
5
5
|
|
6
|
-
"Packs" is a special directory made only for
|
6
|
+
"Packs" is a special directory made only for webpack entry files so don't put anything
|
7
7
|
here that you don't want to link in your views.
|
8
8
|
|
9
9
|
|
data/docs/testing.md
CHANGED
@@ -28,21 +28,13 @@ Webpacker does not setup `Karma` by default, so you've to manually install it al
|
|
28
28
|
}
|
29
29
|
```
|
30
30
|
|
31
|
-
It is beneficial to use the same webpack configuration file (generated by webpacker) in Karma configuration to avoid redundancy. Following line tells Karma not to write transpiled source files onto filesystem while testing to avoid `Error: EACCES: permission denied, mkdir '/_karma_webpack_' ` error.
|
31
|
+
It is beneficial to use the same webpack configuration file (generated by webpacker) in Karma configuration to avoid redundancy. Following line tells Karma not to write transpiled source files onto filesystem while testing to avoid `Error: EACCES: permission denied, mkdir '/_karma_webpack_' ` error. Then inject a new rule a.k.a. loader in the existing ones (needed only if you have installed `istanbul-instrumenter-loader`) to generate a coverage report under `/coverage` directory.
|
32
32
|
|
33
33
|
```js
|
34
34
|
// config/webpack/test.js
|
35
|
-
const
|
35
|
+
const environment = require('./environment')
|
36
36
|
environment.plugins.get('Manifest').opts.writeToFileEmit = process.env.NODE_ENV !== 'test'
|
37
|
-
|
38
|
-
```
|
39
|
-
|
40
|
-
Finally, update `karma.conf.js` to read the same `test.js` file. 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. Rest of the things are mandatory (few marked as optional wherever appropriate).
|
41
|
-
|
42
|
-
```js
|
43
|
-
// karma.conf.js
|
44
|
-
const webpackConfig = require('./config/webpack/test.js')
|
45
|
-
webpackConfig.module.rules.push({
|
37
|
+
environment.loaders.set('istanbul-instrumenter', {
|
46
38
|
test: /\.ts$/,
|
47
39
|
enforce: "post",
|
48
40
|
loader: "istanbul-instrumenter-loader",
|
@@ -51,7 +43,14 @@ webpackConfig.module.rules.push({
|
|
51
43
|
},
|
52
44
|
exclude: ["node_modules", /\.test\.ts$/]
|
53
45
|
}) /* optional */
|
46
|
+
module.exports = environment.toWebpackConfig()
|
47
|
+
```
|
48
|
+
|
49
|
+
Finally, update `karma.conf.js` to read the same `test.js` file mentioned above. Rest of the things are mandatory (few marked as optional wherever appropriate).
|
54
50
|
|
51
|
+
```js
|
52
|
+
// karma.conf.js
|
53
|
+
const webpackConfig = require('./config/webpack/test.js')
|
55
54
|
module.exports = function(config) {
|
56
55
|
config.set({
|
57
56
|
basePath: "",
|
@@ -65,29 +64,10 @@ module.exports = function(config) {
|
|
65
64
|
"karma-coverage-istanbul-reporter" /* optional */,
|
66
65
|
"karma-spec-reporter" /* optional */
|
67
66
|
],
|
68
|
-
files:
|
69
|
-
let files = [ "/* add spec files */" ]
|
70
|
-
for (let entry in webpackConfig.entry) {
|
71
|
-
files.push({
|
72
|
-
pattern: webpackConfig.entry[entry],
|
73
|
-
watched: true,
|
74
|
-
included: false,
|
75
|
-
served: true
|
76
|
-
})
|
77
|
-
}
|
78
|
-
return files;
|
79
|
-
})(),
|
67
|
+
files: [ "/* add spec files */" ],
|
80
68
|
exclude: [],
|
81
69
|
webpack: webpackConfig,
|
82
|
-
preprocessors:
|
83
|
-
let preprocessors = {
|
84
|
-
"/* add spec files */" : ["webpack"]
|
85
|
-
}
|
86
|
-
for (let entry in webpackConfig.entry) {
|
87
|
-
preprocessors[webpackConfig.entry[entry]] = ["webpack"]
|
88
|
-
}
|
89
|
-
return preprocessors;
|
90
|
-
})(),
|
70
|
+
preprocessors: {"/* add spec files */" : ["webpack"]},
|
91
71
|
mime: { "text/x-typescript": ["ts"] },
|
92
72
|
reporters: ["progress", "coverage-istanbul" /* optional */],
|
93
73
|
coverageIstanbulReporter: {
|
@@ -112,7 +92,7 @@ setup and everything will just work out of the box.
|
|
112
92
|
Here is a sample system test case with hello_react example component:
|
113
93
|
|
114
94
|
```js
|
115
|
-
// Example
|
95
|
+
// Example React component
|
116
96
|
|
117
97
|
import React from 'react'
|
118
98
|
import ReactDOM from 'react-dom'
|
@@ -137,7 +117,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
137
117
|
```
|
138
118
|
|
139
119
|
```rb
|
140
|
-
# Tests example
|
120
|
+
# Tests example React component
|
141
121
|
require "application_system_test_case"
|
142
122
|
class HomesTest < ApplicationSystemTestCase
|
143
123
|
test "can see the hello message" do
|
data/docs/troubleshooting.md
CHANGED
@@ -15,7 +15,7 @@ you install any new modules.
|
|
15
15
|
## Can't find hello_react.js in manifest.json
|
16
16
|
|
17
17
|
* If you get this error `Can't find hello_react.js in manifest.json`
|
18
|
-
when loading a view in the browser it's because
|
18
|
+
when loading a view in the browser it's because webpack is still compiling packs.
|
19
19
|
Webpacker uses a `manifest.json` file to keep track of packs in all environments,
|
20
20
|
however since this file is generated after packs are compiled by webpack. So,
|
21
21
|
if you load a view in browser whilst webpack is compiling you will get this error.
|
@@ -48,9 +48,9 @@ bundle config --delete bin
|
|
48
48
|
```
|
49
49
|
|
50
50
|
|
51
|
-
## Running
|
51
|
+
## Running webpack on Windows
|
52
52
|
|
53
|
-
If you are running
|
53
|
+
If you are running webpack on Windows, your command shell may not be able to interpret the preferred interpreter
|
54
54
|
for the scripts generated in `bin/webpack` and `bin/webpack-dev-server`. Instead you'll want to run the scripts
|
55
55
|
manually with Ruby:
|
56
56
|
|
@@ -60,6 +60,42 @@ C:\path>ruby bin\webpack-dev-server
|
|
60
60
|
```
|
61
61
|
|
62
62
|
|
63
|
-
## Invalid configuration object.
|
63
|
+
## Invalid configuration object. webpack has been initialised using a configuration object that does not match the API schema.
|
64
64
|
|
65
65
|
If you receive this error when running `$ ./bin/webpack-dev-server` ensure your configuration is correct; most likely the path to your "packs" folder is incorrect if you modified from the original "source_path" defined in `config/webpacker.yml`.
|
66
|
+
|
67
|
+
## Running Elm on Continuous Integration (CI) services such as CircleCI, CodeShip, Travis CI
|
68
|
+
|
69
|
+
If your tests are timing out or erroring on CI it is likely that you are experiencing the slow Elm compilation issue described here: [elm-compiler issue #1473](https://github.com/elm-lang/elm-compiler/issues/1473)
|
70
|
+
|
71
|
+
The issue is related to CPU count exposed by the underlying service. The basic solution involves using [libsysconfcpus](https://github.com/obmarg/libsysconfcpus) to change the reported CPU count.
|
72
|
+
|
73
|
+
Basic fix involves:
|
74
|
+
|
75
|
+
```bash
|
76
|
+
# install sysconfcpus on CI
|
77
|
+
git clone https://github.com/obmarg/libsysconfcpus.git $HOME/dependencies/libsysconfcpus
|
78
|
+
cd libsysconfcpus
|
79
|
+
.configure --prefix=$HOME/dependencies/sysconfcpus
|
80
|
+
make && make install
|
81
|
+
|
82
|
+
# use sysconfcpus with elm-make
|
83
|
+
mv $HOME/your_rails_app/node_modules/.bin/elm-make $HOME/your_rails_app/node_modules/.bin/elm-make-old
|
84
|
+
printf "#\041/bin/bash\n\necho \"Running elm-make with sysconfcpus -n 2\"\n\n$HOME/dependencies/sysconfcpus/bin/sysconfcpus -n 2 $HOME/your_rails_app/node_modules/.bin/elm-make-old \"\$@\"" > $HOME/your_rails_app/node_modules/.bin/elm-make
|
85
|
+
chmod +x $HOME/your_rails_app/node_modules/.bin/elm-make
|
86
|
+
```
|
87
|
+
|
88
|
+
## Rake assets:precompile fails. ExecJS::RuntimeError
|
89
|
+
This error occurs because you are trying to uglify a pack that's already been minified by Webpacker. To avoid this conflict and prevent appearing of ExecJS::RuntimeError error, you will need to disable uglifier from Rails config:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
// production.rb
|
93
|
+
# From
|
94
|
+
|
95
|
+
Rails.application.config.assets.js_compressor = :uglifier
|
96
|
+
|
97
|
+
# To
|
98
|
+
|
99
|
+
#Rails.application.config.assets.js_compressor = :uglifier
|
100
|
+
|
101
|
+
```
|
data/docs/typescript.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
|
4
4
|
## Typescript with React
|
5
5
|
|
6
|
-
1. Setup react using
|
6
|
+
1. Setup react using Webpacker [react installer](#react). Then add required depedencies
|
7
7
|
for using typescript with React:
|
8
8
|
|
9
9
|
```bash
|
@@ -44,7 +44,7 @@ typescript, JSX with React.
|
|
44
44
|
|
45
45
|
## HTML templates with Typescript and Angular
|
46
46
|
|
47
|
-
After you have installed
|
47
|
+
After you have installed Angular using `bundle exec rails webpacker:install:angular`
|
48
48
|
you would need to follow these steps to add HTML templates support:
|
49
49
|
|
50
50
|
1. Use `yarn` to add html-loader
|
data/docs/webpack-dev-server.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# webpack-dev-server
|
2
2
|
|
3
3
|
|
4
4
|
## HTTPS
|
@@ -11,7 +11,7 @@ Please note that the `webpack-dev-server` will use a self-signed certificate,
|
|
11
11
|
so your web browser will display a warning/exception upon accessing the page. If you get
|
12
12
|
`https://localhost:3035/sockjs-node/info?t=1503127986584 net::ERR_INSECURE_RESPONSE`
|
13
13
|
in your console, simply open the link in your browser and accept the SSL exception.
|
14
|
-
Now if you refresh your
|
14
|
+
Now if you refresh your Rails view everything should work as expected.
|
15
15
|
|
16
16
|
|
17
17
|
## HOT module replacement
|
@@ -36,10 +36,10 @@ otherwise you will get not found error for stylesheets.
|
|
36
36
|
|
37
37
|
If you use Nginx in development to proxy requests to your Rails server from
|
38
38
|
another domain, like `myapp.dev`, the Webpacker middleware will be able to
|
39
|
-
forward requests for "packs" to the
|
39
|
+
forward requests for "packs" to the webpack dev server.
|
40
40
|
|
41
41
|
If you're using `inline` mode behing Nginx, you may also need to provide the
|
42
|
-
hostname to
|
42
|
+
hostname to webpack dev server so it can initiate the websocket connection for
|
43
43
|
live reloading ([Webpack
|
44
44
|
docs](https://webpack.js.org/configuration/dev-server/#devserver-public)).
|
45
45
|
|
@@ -73,3 +73,20 @@ server {
|
|
73
73
|
# ...
|
74
74
|
}
|
75
75
|
```
|
76
|
+
|
77
|
+
## Customizing Logging
|
78
|
+
|
79
|
+
By default, the dev server will display a colored progress notification while
|
80
|
+
your code is being compiled. (Under the hood, we are using `webpack-dev-server
|
81
|
+
--progress --color`). However, this might cause issues if you don't use
|
82
|
+
`foreman` and/or try to log webpack-dev-server's output to a file. You can
|
83
|
+
disable this stylized output by adding `pretty: false` to your `dev_server`
|
84
|
+
config:
|
85
|
+
|
86
|
+
```yaml
|
87
|
+
development:
|
88
|
+
# ...
|
89
|
+
dev_server:
|
90
|
+
# ...
|
91
|
+
pretty: false
|
92
|
+
```
|
data/docs/webpack.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# webpack
|
2
2
|
|
3
3
|
|
4
4
|
## Configuration
|
@@ -28,29 +28,36 @@ module.exports = {
|
|
28
28
|
}
|
29
29
|
}
|
30
30
|
|
31
|
-
// config/webpack/
|
32
|
-
const merge = require('webpack-merge')
|
31
|
+
// config/webpack/environment.js
|
33
32
|
const environment = require('./environment')
|
34
33
|
const customConfig = require('./custom')
|
35
34
|
|
36
|
-
|
37
|
-
|
35
|
+
// Set nested object prop using path notation
|
36
|
+
environment.config.set('resolve.extensions', ['.foo', '.bar'])
|
37
|
+
environment.config.set('output.filename', '[name].js')
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
39
|
+
// Merge custom config
|
40
|
+
environment.config.merge(customConfig)
|
41
|
+
|
42
|
+
// Delete a property
|
43
|
+
environment.config.delete('output.chunkFilename')
|
43
44
|
|
44
|
-
|
45
|
+
module.exports = environment
|
45
46
|
```
|
46
47
|
|
47
|
-
|
48
|
-
|
48
|
+
If you need access to configs within Webpacker's configuration,
|
49
|
+
you can import them like so:
|
49
50
|
|
51
|
+
```js
|
52
|
+
const { config, asset_host } = require('@rails/webpacker')
|
53
|
+
|
54
|
+
console.log(asset_host.publicPathWithHost)
|
55
|
+
console.log(config.source_path)
|
56
|
+
```
|
50
57
|
|
51
58
|
## Loaders
|
52
59
|
|
53
|
-
You can add additional loaders beyond the base set that
|
60
|
+
You can add additional loaders beyond the base set that Webpacker provides by
|
54
61
|
adding it to your environment. We'll use `json-loader` as an example:
|
55
62
|
|
56
63
|
```
|
@@ -61,34 +68,86 @@ yarn add json-loader
|
|
61
68
|
// config/webpack/environment.js
|
62
69
|
const { environment } = require('@rails/webpacker')
|
63
70
|
|
64
|
-
environment.loaders.
|
71
|
+
environment.loaders.append('json', {
|
65
72
|
test: /\.json$/,
|
66
73
|
use: 'json-loader'
|
67
74
|
})
|
68
75
|
|
76
|
+
// Insert json loader at the top of list
|
77
|
+
environment.loaders.prepend('json', jsonLoader)
|
78
|
+
|
79
|
+
// Insert json loader after/before a given loader
|
80
|
+
environment.loaders.insert('json', jsonLoader, { after: 'style'} )
|
81
|
+
environment.loaders.insert('json', jsonLoader, { before: 'babel'} )
|
82
|
+
|
69
83
|
module.exports = environment
|
70
84
|
```
|
71
85
|
|
72
|
-
Finally add `.json` to the list of extensions in `config/webpacker.yml`. Now if you `import()` any `.json` files inside your
|
86
|
+
Finally add `.json` to the list of extensions in `config/webpacker.yml`. Now if you `import()` any `.json` files inside your JavaScript
|
73
87
|
they will be processed using `json-loader`. Voila!
|
74
88
|
|
75
|
-
You can also modify the loaders that
|
89
|
+
You can also modify the loaders that Webpacker pre-configures for you. We'll update
|
76
90
|
the `babel` loader as an example:
|
77
91
|
|
78
92
|
```js
|
79
93
|
// config/webpack/environment.js
|
80
94
|
const { environment } = require('@rails/webpacker')
|
81
95
|
|
82
|
-
// Update an option directly
|
83
96
|
const babelLoader = environment.loaders.get('babel')
|
84
97
|
babelLoader.options.cacheDirectory = false
|
85
98
|
|
86
99
|
module.exports = environment
|
87
100
|
```
|
88
101
|
|
89
|
-
###
|
102
|
+
### Coffeescript 2
|
103
|
+
|
104
|
+
Out of the box webpacker supports coffeescript 1,
|
105
|
+
but here is how you can use Coffeescript 2:
|
106
|
+
|
107
|
+
```
|
108
|
+
yarn add coffeescript@2.0.1
|
109
|
+
```
|
110
|
+
|
111
|
+
```js
|
112
|
+
// config/webpack/environment.js
|
113
|
+
const { environment } = require('@rails/webpacker')
|
114
|
+
|
115
|
+
const babelLoader = environment.loaders.get('babel')
|
116
|
+
|
117
|
+
// Replace existing coffee loader with CS2 version
|
118
|
+
environment.loaders.insert('coffee', {
|
119
|
+
test: /\.coffee(\.erb)?$/,
|
120
|
+
use: babelLoader.use.concat(['coffee-loader'])
|
121
|
+
})
|
122
|
+
|
123
|
+
module.exports = environment
|
124
|
+
```
|
125
|
+
|
126
|
+
### React SVG loader
|
127
|
+
|
128
|
+
To use react svg loader, you should append svg loader before file loader:
|
129
|
+
|
130
|
+
```js
|
131
|
+
const { environment } = require('@rails/webpacker')
|
132
|
+
|
133
|
+
const babelLoader = environment.loaders.get('babel')
|
90
134
|
|
91
|
-
|
135
|
+
environment.loaders.insert('svg', {
|
136
|
+
test: /\.svg$/,
|
137
|
+
use: babelLoader.use.concat([
|
138
|
+
{
|
139
|
+
loader: 'react-svg-loader',
|
140
|
+
options: {
|
141
|
+
jsx: true // true outputs JSX tags
|
142
|
+
}
|
143
|
+
}
|
144
|
+
])
|
145
|
+
}, { before: 'file' })
|
146
|
+
```
|
147
|
+
|
148
|
+
### Overriding Loader Options in webpack 3+ (for CSS Modules etc.)
|
149
|
+
|
150
|
+
In webpack 3+, if you'd like to specify additional or different options for a loader, edit `config/webpack/environment.js` and provide an options object to override. This is similar to the technique shown above, but the following example shows specifically how to apply CSS Modules, which is what you may be looking for:
|
92
151
|
|
93
152
|
```javascript
|
94
153
|
const { environment } = require('@rails/webpacker')
|
@@ -126,10 +185,11 @@ const { environment } = require('@rails/webpacker')
|
|
126
185
|
const webpack = require('webpack')
|
127
186
|
|
128
187
|
// Get a pre-configured plugin
|
129
|
-
environment.plugins.get('
|
188
|
+
const manifestPlugin = environment.plugins.get('Manifest')
|
189
|
+
manifestPlugin.opts.writeToFileEmit = false
|
130
190
|
|
131
191
|
// Add an additional plugin of your choosing : ProvidePlugin
|
132
|
-
environment.plugins.
|
192
|
+
environment.plugins.prepend(
|
133
193
|
'Provide',
|
134
194
|
new webpack.ProvidePlugin({
|
135
195
|
$: 'jquery',
|
@@ -143,9 +203,27 @@ environment.plugins.set(
|
|
143
203
|
})
|
144
204
|
)
|
145
205
|
|
206
|
+
// Insert before a given plugin
|
207
|
+
environment.plugins.insert('CommonChunkVendor',
|
208
|
+
new webpack.optimize.CommonsChunkPlugin({
|
209
|
+
name: 'vendor', // Vendor code
|
210
|
+
minChunks: (module) => module.context && module.context.indexOf('node_modules') !== -1
|
211
|
+
})
|
212
|
+
, { before: 'manifest' })
|
213
|
+
|
146
214
|
module.exports = environment
|
147
215
|
```
|
148
216
|
|
217
|
+
## Resolved modules
|
218
|
+
|
219
|
+
To add new paths to `resolve.modules`, the API is same as loaders and plugins:
|
220
|
+
|
221
|
+
```js
|
222
|
+
const { environment } = require('@rails/webpacker')
|
223
|
+
|
224
|
+
// Resolved modules list API - prepend, append, insert
|
225
|
+
environment.resolvedModules.append('vendor', 'vendor')
|
226
|
+
```
|
149
227
|
|
150
228
|
### Add common chunks
|
151
229
|
|
@@ -156,18 +234,18 @@ Add the plugins in `config/webpack/environment.js`:
|
|
156
234
|
```js
|
157
235
|
const webpack = require('webpack')
|
158
236
|
|
159
|
-
environment.plugins.
|
237
|
+
environment.plugins.append(
|
160
238
|
'CommonsChunkVendor',
|
161
239
|
new webpack.optimize.CommonsChunkPlugin({
|
162
240
|
name: 'vendor',
|
163
241
|
minChunks: (module) => {
|
164
242
|
// this assumes your vendor imports exist in the node_modules directory
|
165
|
-
return module.context && module.context.indexOf('node_modules') !== -1
|
243
|
+
return module.context && module.context.indexOf('node_modules') !== -1
|
166
244
|
}
|
167
245
|
})
|
168
246
|
)
|
169
247
|
|
170
|
-
environment.plugins.
|
248
|
+
environment.plugins.append(
|
171
249
|
'CommonsChunkManifest',
|
172
250
|
new webpack.optimize.CommonsChunkPlugin({
|
173
251
|
name: 'manifest',
|
@@ -189,4 +267,4 @@ Now, add these files to your `layouts/application.html.erb`:
|
|
189
267
|
<%= stylesheet_pack_tag "vendor" %>
|
190
268
|
```
|
191
269
|
|
192
|
-
More detailed guides available here: [
|
270
|
+
More detailed guides available here: [webpack guides](https://webpack.js.org/guides/)
|