shakapacker 6.0.0.rc.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.eslintignore +4 -0
- data/.eslintrc.js +14 -0
- data/.github/ISSUE_TEMPLATE/bug_report.md +20 -0
- data/.github/ISSUE_TEMPLATE/feature-request.md +18 -0
- data/.github/workflows/jest.yml +30 -0
- data/.github/workflows/js-lint.yml +31 -0
- data/.github/workflows/rubocop.yml +39 -0
- data/.github/workflows/ruby.yml +48 -0
- data/.gitignore +13 -0
- data/.node-version +1 -0
- data/.rubocop.yml +229 -0
- data/CHANGELOG.md +32 -0
- data/CONTRIBUTING.md +62 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +183 -0
- data/MIT-LICENSE +20 -0
- data/README.md +666 -0
- data/Rakefile +11 -0
- data/config/README.md +3 -0
- data/config/webpacker.yml +1 -0
- data/docs/customizing_babel_config.md +59 -0
- data/docs/deployment.md +116 -0
- data/docs/developing_webpacker.md +29 -0
- data/docs/troubleshooting.md +212 -0
- data/docs/v6_upgrade.md +158 -0
- data/gemfiles/Gemfile-rails-edge +12 -0
- data/gemfiles/Gemfile-rails.5.2.x +9 -0
- data/gemfiles/Gemfile-rails.6.0.x +9 -0
- data/gemfiles/Gemfile-rails.6.1.x +12 -0
- data/lib/install/application.js +15 -0
- data/lib/install/bin/webpacker +15 -0
- data/lib/install/bin/webpacker-dev-server +18 -0
- data/lib/install/bin/yarn +18 -0
- data/lib/install/binstubs.rb +4 -0
- data/lib/install/config/webpack/webpack.config.js +5 -0
- data/lib/install/config/webpacker.yml +64 -0
- data/lib/install/package.json +15 -0
- data/lib/install/template.rb +100 -0
- data/lib/shakapacker/utils/git_utils.rb +23 -0
- data/lib/shakapacker/utils/version_syntax_converter.rb +24 -0
- data/lib/tasks/webpacker/binstubs.rake +15 -0
- data/lib/tasks/webpacker/check_binstubs.rake +12 -0
- data/lib/tasks/webpacker/check_node.rake +31 -0
- data/lib/tasks/webpacker/check_yarn.rake +33 -0
- data/lib/tasks/webpacker/clean.rake +25 -0
- data/lib/tasks/webpacker/clobber.rake +20 -0
- data/lib/tasks/webpacker/compile.rake +45 -0
- data/lib/tasks/webpacker/info.rake +21 -0
- data/lib/tasks/webpacker/install.rake +17 -0
- data/lib/tasks/webpacker/verify_config.rake +14 -0
- data/lib/tasks/webpacker/verify_install.rake +4 -0
- data/lib/tasks/webpacker/yarn_install.rake +18 -0
- data/lib/tasks/webpacker.rake +19 -0
- data/lib/tasks/yarn.rake +38 -0
- data/lib/webpacker/commands.rb +79 -0
- data/lib/webpacker/compiler.rb +130 -0
- data/lib/webpacker/configuration.rb +111 -0
- data/lib/webpacker/dev_server.rb +72 -0
- data/lib/webpacker/dev_server_proxy.rb +33 -0
- data/lib/webpacker/dev_server_runner.rb +96 -0
- data/lib/webpacker/env.rb +43 -0
- data/lib/webpacker/helper.rb +161 -0
- data/lib/webpacker/instance.rb +41 -0
- data/lib/webpacker/manifest.rb +120 -0
- data/lib/webpacker/railtie.rb +63 -0
- data/lib/webpacker/runner.rb +23 -0
- data/lib/webpacker/version.rb +4 -0
- data/lib/webpacker/webpack_runner.rb +58 -0
- data/lib/webpacker.rb +46 -0
- data/package/__tests__/config.js +34 -0
- data/package/__tests__/dev_server.js +45 -0
- data/package/__tests__/development.js +35 -0
- data/package/__tests__/env.js +58 -0
- data/package/__tests__/index.js +9 -0
- data/package/__tests__/production.js +29 -0
- data/package/__tests__/staging.js +30 -0
- data/package/__tests__/test.js +25 -0
- data/package/babel/preset.js +41 -0
- data/package/config.js +32 -0
- data/package/configPath.js +3 -0
- data/package/dev_server.js +20 -0
- data/package/env.js +27 -0
- data/package/environments/__tests__/base.js +69 -0
- data/package/environments/base.js +116 -0
- data/package/environments/development.js +55 -0
- data/package/environments/production.js +79 -0
- data/package/environments/test.js +3 -0
- data/package/index.js +33 -0
- data/package/inliningCss.js +7 -0
- data/package/rules/babel.js +30 -0
- data/package/rules/coffee.js +6 -0
- data/package/rules/css.js +3 -0
- data/package/rules/erb.js +15 -0
- data/package/rules/file.js +23 -0
- data/package/rules/index.js +18 -0
- data/package/rules/less.js +22 -0
- data/package/rules/raw.js +5 -0
- data/package/rules/sass.js +16 -0
- data/package/rules/stylus.js +26 -0
- data/package/utils/get_style_rule.js +37 -0
- data/package/utils/helpers.js +51 -0
- data/package.json +71 -0
- data/rakelib/release.rake +57 -0
- data/test/command_test.rb +109 -0
- data/test/compiler_test.rb +68 -0
- data/test/configuration_test.rb +78 -0
- data/test/dev_server_runner_test.rb +81 -0
- data/test/dev_server_test.rb +47 -0
- data/test/engine_rake_tasks_test.rb +39 -0
- data/test/env_test.rb +23 -0
- data/test/helper_test.rb +159 -0
- data/test/manifest_test.rb +89 -0
- data/test/mounted_app/Rakefile +4 -0
- data/test/mounted_app/test/dummy/Rakefile +3 -0
- data/test/mounted_app/test/dummy/bin/rails +3 -0
- data/test/mounted_app/test/dummy/bin/rake +3 -0
- data/test/mounted_app/test/dummy/config/application.rb +10 -0
- data/test/mounted_app/test/dummy/config/environment.rb +3 -0
- data/test/mounted_app/test/dummy/config/webpacker.yml +75 -0
- data/test/mounted_app/test/dummy/config.ru +5 -0
- data/test/mounted_app/test/dummy/package.json +7 -0
- data/test/rake_tasks_test.rb +71 -0
- data/test/test_app/Rakefile +3 -0
- data/test/test_app/app/packs/entrypoints/application.js +10 -0
- data/test/test_app/app/packs/entrypoints/multi_entry.css +4 -0
- data/test/test_app/app/packs/entrypoints/multi_entry.js +4 -0
- data/test/test_app/bin/webpacker +14 -0
- data/test/test_app/bin/webpacker-dev-server +14 -0
- data/test/test_app/config/application.rb +11 -0
- data/test/test_app/config/environment.rb +4 -0
- data/test/test_app/config/initializers/inspect_autoload_paths.rb +1 -0
- data/test/test_app/config/webpack/webpack.config.js +0 -0
- data/test/test_app/config/webpacker.yml +77 -0
- data/test/test_app/config/webpacker_other_location.yml +79 -0
- data/test/test_app/config/webpacker_public_root.yml +18 -0
- data/test/test_app/config.ru +5 -0
- data/test/test_app/package.json +13 -0
- data/test/test_app/public/packs/manifest.json +50 -0
- data/test/test_app/some.config.js +0 -0
- data/test/test_app/yarn.lock +11 -0
- data/test/test_helper.rb +33 -0
- data/test/webpack_runner_test.rb +57 -0
- data/test/webpacker_test.rb +34 -0
- data/webpacker.gemspec +31 -0
- data/yarn.lock +4029 -0
- metadata +331 -0
data/README.md
ADDED
@@ -0,0 +1,666 @@
|
|
1
|
+
# Shakapacker
|
2
|
+
|
3
|
+
|
4
|
+
This is a fork of [rails/webpacker](https://github.com/rails/webpacker). For pre v6, see [rails/webpacker 5-x-stable](https://github.com/rails/webpacker/tree/5-x-stable).
|
5
|
+
|
6
|
+
[![Ruby specs](https://github.com/shakacode/shakapacker/workflows/Ruby%20specs/badge.svg)](https://github.com/shakacode/shakapacker/actions)
|
7
|
+
[![Jest specs](https://github.com/shakacode/shakapacker/workflows/Jest%20specs/badge.svg)](https://github.com/shakacode/shakapacker/actions)
|
8
|
+
[![Rubocop](https://github.com/shakacode/shakapacker/workflows/Rubocop/badge.svg)](https://github.com/shakacode/shakapacker/actions)
|
9
|
+
[![JS lint](https://github.com/shakacode/shakapacker/workflows/JS%20lint/badge.svg)](https://github.com/shakacode/shakapacker/actions)
|
10
|
+
|
11
|
+
[![node.js](https://img.shields.io/badge/node-%3E%3D%2012.0.0-brightgreen.svg)](https://www.npmjs.com/package/@shakacode/shakapacker)
|
12
|
+
[![Gem](https://img.shields.io/gem/v/shakapacker.svg)](https://rubygems.org/gems/shakapacker)
|
13
|
+
|
14
|
+
Webpacker makes it easy to use the JavaScript pre-processor and bundler
|
15
|
+
[Webpack v5](https://webpack.js.org/)
|
16
|
+
to manage application-like JavaScript in Rails. It can coexist with the asset pipeline,
|
17
|
+
leaving Webpack responsible solely for app-like JavaScript, or it can be used exclusively, making it also responsible for images, fonts, and CSS.
|
18
|
+
|
19
|
+
|
20
|
+
Visit [Shipping Webpacker v6 – Status](https://discuss.rubyonrails.org/t/shipping-webpacker-v6-status/79683) for the latest news on a v6 release.
|
21
|
+
|
22
|
+
Discussion forums to discuss debugging and troubleshooting tips. Please open issues for bugs and feature requests:
|
23
|
+
1. [rails/webpacker discussion forum](https://discuss.rubyonrails.org/c/webpacker/10)
|
24
|
+
2. [Discussions tab](https://github.com/shakacode/shakapacker/discussions)
|
25
|
+
|
26
|
+
---
|
27
|
+
|
28
|
+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
29
|
+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
30
|
+
|
31
|
+
- [Prerequisites](#prerequisites)
|
32
|
+
- [Features](#features)
|
33
|
+
- [Optional support](#optional-support)
|
34
|
+
- [Installation](#installation)
|
35
|
+
- [Rails v6](#rails-v6)
|
36
|
+
- [Rails v7](#rails-v7)
|
37
|
+
- [Manual Installation Steps](#manual-installation-steps)
|
38
|
+
- [Usage](#usage)
|
39
|
+
- [Defer for `javascript_pack_tag`](#defer-for-javascript_pack_tag)
|
40
|
+
- [Server-Side Rendering (SSR)](#server-side-rendering-ssr)
|
41
|
+
- [Development](#development)
|
42
|
+
- [Webpack Configuration](#webpack-configuration)
|
43
|
+
- [Babel configuration](#babel-configuration)
|
44
|
+
- [Integrations](#integrations)
|
45
|
+
- [React](#react)
|
46
|
+
- [Typescript](#typescript)
|
47
|
+
- [CoffeeScript](#coffeescript)
|
48
|
+
- [TypeScript](#typescript)
|
49
|
+
- [CSS](#css)
|
50
|
+
- [Postcss](#postcss)
|
51
|
+
- [Sass](#sass)
|
52
|
+
- [Less](#less)
|
53
|
+
- [Stylus](#stylus)
|
54
|
+
- [Other frameworks](#other-frameworks)
|
55
|
+
- [Custom Rails environments](#custom-rails-environments)
|
56
|
+
- [Upgrading](#upgrading)
|
57
|
+
- [Paths](#paths)
|
58
|
+
- [Additional paths](#additional-paths)
|
59
|
+
- [Deployment](#deployment)
|
60
|
+
- [Troubleshooting](#troubleshooting)
|
61
|
+
- [Contributing](#contributing)
|
62
|
+
- [License](#license)
|
63
|
+
|
64
|
+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
65
|
+
|
66
|
+
## Prerequisites
|
67
|
+
|
68
|
+
- Ruby 2.7+
|
69
|
+
- Rails 5.2+
|
70
|
+
- Node.js 12.13.0+ || 14+
|
71
|
+
- Yarn
|
72
|
+
|
73
|
+
## Features
|
74
|
+
|
75
|
+
- [Webpack v5](https://webpack.js.org/)
|
76
|
+
- ES6 with [babel](https://babeljs.io/)
|
77
|
+
- Automatic code splitting using multiple entry points
|
78
|
+
- Asset compression, source-maps, and minification
|
79
|
+
- CDN support
|
80
|
+
- Rails view helpers
|
81
|
+
- Extensible and configurable
|
82
|
+
|
83
|
+
### Optional support
|
84
|
+
|
85
|
+
_requires extra packages to be installed_
|
86
|
+
|
87
|
+
- Stylesheets - Sass, Less, Stylus and Css, PostCSS
|
88
|
+
- CoffeeScript
|
89
|
+
- TypeScript
|
90
|
+
- React
|
91
|
+
|
92
|
+
## Installation
|
93
|
+
|
94
|
+
### Rails v6
|
95
|
+
With Rails v6, webpacker is installed by default:
|
96
|
+
```bash
|
97
|
+
rails new myapp
|
98
|
+
```
|
99
|
+
|
100
|
+
### Rails v7
|
101
|
+
|
102
|
+
With Rails v7, skip JavaScript for a new app and follow below Manual Installation Steps to manually add the `webpacker` gem to your Gemfile.
|
103
|
+
```bash
|
104
|
+
rails new myapp --skip-javascript
|
105
|
+
```
|
106
|
+
|
107
|
+
### Manual Installation Steps
|
108
|
+
Update your `Gemfile`:
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
# Gemfile
|
112
|
+
gem 'webpacker', '~> 6.0'
|
113
|
+
|
114
|
+
# OR if you prefer to use master
|
115
|
+
gem 'webpacker', git: 'https://github.com/shakacode/shakapacker.git'
|
116
|
+
yarn add https://github.com/shakacode/shakapacker.git
|
117
|
+
```
|
118
|
+
|
119
|
+
Then running the following to install Webpacker:
|
120
|
+
|
121
|
+
```bash
|
122
|
+
./bin/bundle install
|
123
|
+
./bin/rails webpacker:install
|
124
|
+
```
|
125
|
+
|
126
|
+
When `package.json` and/or `yarn.lock` changes, such as when pulling down changes to your local environment in a team settings, be sure to keep your NPM packages up-to-date:
|
127
|
+
|
128
|
+
```bash
|
129
|
+
yarn
|
130
|
+
```
|
131
|
+
|
132
|
+
Note, in v6, most JS packages are peer dependencies. Thus, the installer will add the packages:
|
133
|
+
|
134
|
+
```bash
|
135
|
+
yarn add @babel/core @babel/plugin-transform-runtime @babel/preset-env @babel/runtime babel-loader \
|
136
|
+
compression-webpack-plugin pnp-webpack-plugin terser-webpack-plugin \
|
137
|
+
webpack webpack-assets-manifest webpack-cli webpack-merge webpack-sources webpack-dev-server
|
138
|
+
```
|
139
|
+
|
140
|
+
Previously, these "webpack" and "babel" packages were direct dependencies for `@shakacode/shakapacker`. By
|
141
|
+
making these peer dependencies, you have control over the versions used in your webpack and babel configs.
|
142
|
+
|
143
|
+
## Usage
|
144
|
+
|
145
|
+
Once installed, you can start writing modern ES6-flavored JavaScript apps right away:
|
146
|
+
|
147
|
+
```yml
|
148
|
+
app/javascript:
|
149
|
+
# Only Webpack entry files here
|
150
|
+
└── application.js
|
151
|
+
└── application.css
|
152
|
+
└── src:
|
153
|
+
│ └── my_component.js
|
154
|
+
└── stylesheets:
|
155
|
+
│ └── my_styles.css
|
156
|
+
└── images:
|
157
|
+
└── logo.svg
|
158
|
+
```
|
159
|
+
|
160
|
+
You can then link the JavaScript pack in Rails views using the `javascript_pack_tag` helper. If you have styles imported in your pack file, you can link them by using `stylesheet_pack_tag`:
|
161
|
+
|
162
|
+
```erb
|
163
|
+
<%= javascript_pack_tag 'application' %>
|
164
|
+
<%= stylesheet_pack_tag 'application' %>
|
165
|
+
```
|
166
|
+
|
167
|
+
The `javascript_pack_tag` and `stylesheet_pack_tag` helpers will include all the transpiled
|
168
|
+
packs with the chunks in your view, which creates html tags for all the chunks.
|
169
|
+
|
170
|
+
The result looks like this:
|
171
|
+
|
172
|
+
```erb
|
173
|
+
<%= javascript_pack_tag 'calendar', 'map', 'data-turbolinks-track': 'reload' %>
|
174
|
+
|
175
|
+
<script src="/packs/vendor-16838bab065ae1e314.js" data-turbolinks-track="reload" defer></script>
|
176
|
+
<script src="/packs/calendar~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload" defer></script>
|
177
|
+
<script src="/packs/calendar-1016838bab065ae1e314.js" data-turbolinks-track="reload" defer"></script>
|
178
|
+
<script src="/packs/map~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload" defer></script>
|
179
|
+
<script src="/packs/map-16838bab065ae1e314.js" data-turbolinks-track="reload" defer></script>
|
180
|
+
```
|
181
|
+
|
182
|
+
**Important:** Pass all your pack names as multiple arguments, not multiple calls, when using `javascript_pack_tag` and the **`stylesheet_pack_tag`. Otherwise, you will get duplicated chunks on the page. Be especially careful if you might be calling these view helpers from your view, partials, and the layout for a page. You will need some logic to ensure you call the helpers only once with multiple arguments.
|
183
|
+
|
184
|
+
```erb
|
185
|
+
<%# DO %>
|
186
|
+
<%= javascript_pack_tag 'calendar', 'map' %>
|
187
|
+
<%= stylesheet_pack_tag 'calendar', 'map' %>
|
188
|
+
|
189
|
+
<%# DON'T %>
|
190
|
+
<%= javascript_pack_tag 'calendar' %>
|
191
|
+
<%= javascript_pack_tag 'map' %>
|
192
|
+
<%= stylesheet_pack_tag 'calendar' %>
|
193
|
+
<%= stylesheet_pack_tag 'map' %>
|
194
|
+
```
|
195
|
+
|
196
|
+
If you want to link a static asset for `<img />` tag, you can use the `asset_pack_path` helper:
|
197
|
+
```erb
|
198
|
+
<img src="<%= asset_pack_path 'images/logo.svg' %>" />
|
199
|
+
```
|
200
|
+
|
201
|
+
Or use the dedicated helper:
|
202
|
+
```erb
|
203
|
+
<%= image_pack_tag 'application.png', size: '16x10', alt: 'Edit Entry' %>
|
204
|
+
<%= image_pack_tag 'picture.png', srcset: { 'picture-2x.png' => '2x' } %>
|
205
|
+
```
|
206
|
+
|
207
|
+
If you want to create a favicon:
|
208
|
+
```erb
|
209
|
+
<%= favicon_pack_tag 'mb-icon.png', rel: 'apple-touch-icon', type: 'image/png' %>
|
210
|
+
```
|
211
|
+
|
212
|
+
If you want to preload a static asset in your `<head>`, you can use the `preload_pack_asset` helper:
|
213
|
+
```erb
|
214
|
+
<%= preload_pack_asset 'fonts/fa-regular-400.woff2' %>
|
215
|
+
```
|
216
|
+
|
217
|
+
If you want to use images in your stylesheets:
|
218
|
+
|
219
|
+
```css
|
220
|
+
.foo {
|
221
|
+
background-image: url('../images/logo.svg')
|
222
|
+
}
|
223
|
+
```
|
224
|
+
##### Defer for `javascript_pack_tag`
|
225
|
+
Note, the default of "defer" for the `javascript_pack_tag`. You can override that to `false`. If you expose jquery globally with `expose-loader,` by using `import $ from "expose-loader?exposes=$,jQuery!jquery"` in your `app/packs/entrypoints/application.js`, pass the option `defer: false` to your `javascript_pack_tag`.
|
226
|
+
|
227
|
+
#### Server-Side Rendering (SSR)
|
228
|
+
|
229
|
+
Note, if you are using server-side rendering of JavaScript with dynamic code-splitting, as is often done with extensions to Webpacker, like [React on Rails](https://github.com/shakacode/react_on_rails), your JavaScript should create the link prefetch HTML tags that you will use, so you won't need to use to `asset_pack_path` in those circumstances.
|
230
|
+
|
231
|
+
**Note:** In order for your styles or static assets files to be available in your view, you would need to link them in your "pack" or entry file. Otherwise, Webpack won't know to package up those files.
|
232
|
+
|
233
|
+
### Development
|
234
|
+
|
235
|
+
Webpacker ships with two binstubs: `./bin/webpacker` and `./bin/webpacker-dev-server`. Both are thin wrappers around the standard `webpack.js` and `webpack-dev-server.js` executables to ensure that the right configuration files and environmental variables are loaded based on your environment.
|
236
|
+
|
237
|
+
In development, Webpacker compiles on demand rather than upfront by default. This happens when you refer to any of the pack assets using the Webpacker helper methods. This means that you don't have to run any separate processes. Compilation errors are logged to the standard Rails log. However, this auto-compilation happens when a web request is made that requires an updated webpack build, not when files change. Thus, that can be painfully slow for front-end development in this default way. Instead, you should either run the `bin/webpacker --watch` or run `./bin/webpacker-dev-server`
|
238
|
+
|
239
|
+
If you want to use live code reloading, or you have enough JavaScript that on-demand compilation is too slow, you'll need to run `./bin/webpacker-dev-server` or `ruby ./bin/webpacker-dev-server`. Windows users will need to run these commands in a terminal separate from `bundle exec rails s`. This process will watch for changes in the relevant files, defined by `webpacker.yml` configuration settings for `source_path`, `source_entry_path`, and `additional_paths`, and it will then automatically reload the browser to match. This feature is also known as [Hot Module Replacement](https://webpack.js.org/concepts/hot-module-replacement/).
|
240
|
+
|
241
|
+
```bash
|
242
|
+
# webpack dev server
|
243
|
+
./bin/webpacker-dev-server
|
244
|
+
|
245
|
+
# watcher
|
246
|
+
./bin/webpacker --watch --progress
|
247
|
+
|
248
|
+
# standalone build
|
249
|
+
./bin/webpacker --progress
|
250
|
+
|
251
|
+
# Help
|
252
|
+
./bin/webpacker help
|
253
|
+
|
254
|
+
# Version
|
255
|
+
./bin/webpacker version
|
256
|
+
|
257
|
+
# Info
|
258
|
+
./bin/webpacker info
|
259
|
+
```
|
260
|
+
|
261
|
+
Once you start this webpack development server, Webpacker will automatically start proxying all webpack asset requests to this server. When you stop this server, Rails will detect that it's not running and Rails will revert back to on-demand compilation _if_ you have the `compile` option set to true in your `config/webpacker.yml`
|
262
|
+
|
263
|
+
You can use environment variables as options supported by [webpack-dev-server](https://webpack.js.org/configuration/dev-server/) in the form `WEBPACKER_DEV_SERVER_<OPTION>`. Please note that these environmental variables will always take precedence over the ones already set in the configuration file, and that the _same_ environmental variables must be available to the `rails server` process.
|
264
|
+
|
265
|
+
```bash
|
266
|
+
WEBPACKER_DEV_SERVER_HOST=example.com WEBPACKER_DEV_SERVER_INLINE=true WEBPACKER_DEV_SERVER_HOT=false ./bin/webpacker-dev-server
|
267
|
+
```
|
268
|
+
|
269
|
+
By default, the webpack dev server listens on `localhost` in development for security purposes. However, if you want your app to be available over local LAN IP or a VM instance like vagrant, you can set the `host` when running `./bin/webpacker-dev-server` binstub:
|
270
|
+
|
271
|
+
```bash
|
272
|
+
WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpacker-dev-server
|
273
|
+
```
|
274
|
+
|
275
|
+
**Note:** You need to allow webpack-dev-server host as an allowed origin for `connect-src` if you are running your application in a restrict CSP environment (like Rails 5.2+). This can be done in Rails 5.2+ in the CSP initializer `config/initializers/content_security_policy.rb` with a snippet like this:
|
276
|
+
|
277
|
+
```ruby
|
278
|
+
Rails.application.config.content_security_policy do |policy|
|
279
|
+
policy.connect_src :self, :https, 'http://localhost:3035', 'ws://localhost:3035' if Rails.env.development?
|
280
|
+
end
|
281
|
+
```
|
282
|
+
|
283
|
+
**Note:** Don't forget to prefix `ruby` when running these binstubs on Windows
|
284
|
+
|
285
|
+
|
286
|
+
### Webpack Configuration
|
287
|
+
|
288
|
+
Webpacker gives you a default configuration file for your test, development, and production environments in `config/webpack/*.js`.
|
289
|
+
|
290
|
+
By default, you don't need to make any changes to `config/webpack/webpack.config.js` files since it's all standard production-ready configuration. However, if you do need to customize or add a new loader, this is where you would go.
|
291
|
+
|
292
|
+
Here is how you can modify webpack configuration:
|
293
|
+
|
294
|
+
You might add separate files to keep your code more organized.
|
295
|
+
|
296
|
+
```js
|
297
|
+
// config/webpack/custom.js
|
298
|
+
module.exports = {
|
299
|
+
resolve: {
|
300
|
+
alias: {
|
301
|
+
jquery: 'jquery/src/jquery',
|
302
|
+
vue: 'vue/dist/vue.js',
|
303
|
+
React: 'react',
|
304
|
+
ReactDOM: 'react-dom',
|
305
|
+
vue_resource: 'vue-resource/dist/vue-resource'
|
306
|
+
}
|
307
|
+
}
|
308
|
+
}
|
309
|
+
```
|
310
|
+
|
311
|
+
Then `require` this file in your `config/webpack/webpack.config.js`:
|
312
|
+
|
313
|
+
```js
|
314
|
+
// config/webpack/webpack.config.js
|
315
|
+
const { webpackConfig, merge } = require('@shakacode/shakapacker')
|
316
|
+
const customConfig = require('./custom')
|
317
|
+
|
318
|
+
module.exports = merge(webpackConfig, customConfig)
|
319
|
+
```
|
320
|
+
|
321
|
+
If you need access to configs within Webpacker's configuration, you can import them like so:
|
322
|
+
|
323
|
+
```js
|
324
|
+
// config/webpack/webpack.config.js
|
325
|
+
const { webpackConfig } = require('@shakacode/shakapacker')
|
326
|
+
|
327
|
+
console.log(webpackConfig.output_path)
|
328
|
+
console.log(webpackConfig.source_path)
|
329
|
+
|
330
|
+
// Or to print out your whole webpack configuration
|
331
|
+
console.log(JSON.stringify(webpackConfig, undefined, 2))
|
332
|
+
```
|
333
|
+
|
334
|
+
### Babel configuration
|
335
|
+
|
336
|
+
By default, you will find the Webpacker preset in your `package.json`.
|
337
|
+
|
338
|
+
```json
|
339
|
+
"babel": {
|
340
|
+
"presets": [
|
341
|
+
"./node_modules/@shakacode/shakapacker/package/babel/preset.js"
|
342
|
+
]
|
343
|
+
},
|
344
|
+
```
|
345
|
+
|
346
|
+
Optionally, you can change your Babel configuration by removing these lines in your `package.json` and add [a Babel configuration file](https://babeljs.io/docs/en/config-files) in your project. For an example customization based on the original, see [Customizing Babel Config](./docs/customizing_babel_config.md).
|
347
|
+
|
348
|
+
### Integrations
|
349
|
+
|
350
|
+
Webpacker out of the box supports JS and static assets (fonts, images etc.) compilation. To enable support for CoffeeScript or TypeScript install relevant packages:
|
351
|
+
|
352
|
+
#### React
|
353
|
+
|
354
|
+
See customization example the [Customizing Babel Config](./docs/customizing_babel_config.md) for React configuration.
|
355
|
+
|
356
|
+
#### Typescript
|
357
|
+
...if you are using typescript, update your `tsconfig.json`
|
358
|
+
|
359
|
+
```json
|
360
|
+
{
|
361
|
+
"compilerOptions": {
|
362
|
+
"declaration": false,
|
363
|
+
"emitDecoratorMetadata": true,
|
364
|
+
"experimentalDecorators": true,
|
365
|
+
"lib": ["es6", "dom"],
|
366
|
+
"module": "es6",
|
367
|
+
"moduleResolution": "node",
|
368
|
+
"sourceMap": true,
|
369
|
+
"target": "es5",
|
370
|
+
"jsx": "react",
|
371
|
+
"noEmit": true
|
372
|
+
},
|
373
|
+
"exclude": ["**/*.spec.ts", "node_modules", "vendor", "public"],
|
374
|
+
"compileOnSave": false
|
375
|
+
}
|
376
|
+
```
|
377
|
+
|
378
|
+
#### CoffeeScript
|
379
|
+
|
380
|
+
```bash
|
381
|
+
yarn add coffeescript coffee-loader
|
382
|
+
```
|
383
|
+
|
384
|
+
#### TypeScript
|
385
|
+
|
386
|
+
```bash
|
387
|
+
yarn add typescript @babel/preset-typescript
|
388
|
+
```
|
389
|
+
|
390
|
+
Babel won’t perform any type-checking on TypeScript code. To optionally use type-checking run:
|
391
|
+
|
392
|
+
```bash
|
393
|
+
yarn add fork-ts-checker-webpack-plugin
|
394
|
+
```
|
395
|
+
|
396
|
+
Add tsconfig.json
|
397
|
+
|
398
|
+
```json
|
399
|
+
{
|
400
|
+
"compilerOptions": {
|
401
|
+
"declaration": false,
|
402
|
+
"emitDecoratorMetadata": true,
|
403
|
+
"experimentalDecorators": true,
|
404
|
+
"lib": ["es6", "dom"],
|
405
|
+
"module": "es6",
|
406
|
+
"moduleResolution": "node",
|
407
|
+
"baseUrl": ".",
|
408
|
+
"paths": {
|
409
|
+
"*": ["node_modules/*", "app/packs/*"]
|
410
|
+
},
|
411
|
+
"sourceMap": true,
|
412
|
+
"target": "es5",
|
413
|
+
"noEmit": true
|
414
|
+
},
|
415
|
+
"exclude": ["**/*.spec.ts", "node_modules", "vendor", "public"],
|
416
|
+
"compileOnSave": false
|
417
|
+
}
|
418
|
+
```
|
419
|
+
|
420
|
+
Then modify the webpack config to use it as a plugin:
|
421
|
+
|
422
|
+
```js
|
423
|
+
// config/webpack/webpack.config.js
|
424
|
+
const { webpackConfig, merge } = require("@shakacode/shakapacker");
|
425
|
+
const ForkTSCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
|
426
|
+
|
427
|
+
module.exports = merge(webpackConfig, {
|
428
|
+
plugins: [new ForkTSCheckerWebpackPlugin()],
|
429
|
+
});
|
430
|
+
```
|
431
|
+
|
432
|
+
#### CSS
|
433
|
+
|
434
|
+
To enable CSS support in your application, add following packages:
|
435
|
+
|
436
|
+
```bash
|
437
|
+
yarn add css-loader style-loader mini-css-extract-plugin css-minimizer-webpack-plugin
|
438
|
+
```
|
439
|
+
|
440
|
+
Optionally, add the `CSS` extension to webpack config for easy resolution.
|
441
|
+
|
442
|
+
```js
|
443
|
+
// config/webpack/webpack.config.js
|
444
|
+
const { webpackConfig, merge } = require('@shakacode/shakapacker')
|
445
|
+
const customConfig = {
|
446
|
+
resolve: {
|
447
|
+
extensions: ['.css']
|
448
|
+
}
|
449
|
+
}
|
450
|
+
|
451
|
+
module.exports = merge(webpackConfig, customConfig)
|
452
|
+
```
|
453
|
+
|
454
|
+
To enable `PostCSS`, `Sass` or `Less` support, add `CSS` support first and
|
455
|
+
then add the relevant pre-processors:
|
456
|
+
|
457
|
+
#### Postcss
|
458
|
+
|
459
|
+
```bash
|
460
|
+
yarn add postcss postcss-loader
|
461
|
+
```
|
462
|
+
|
463
|
+
Optionally add these two plugins if they are required in your `postcss.config.js`:
|
464
|
+
```bash
|
465
|
+
yarn add postcss-preset-env postcss-flexbugs-fixes
|
466
|
+
```
|
467
|
+
|
468
|
+
#### Sass
|
469
|
+
|
470
|
+
```bash
|
471
|
+
yarn add sass sass-loader
|
472
|
+
```
|
473
|
+
|
474
|
+
#### Less
|
475
|
+
|
476
|
+
```bash
|
477
|
+
yarn add less less-loader
|
478
|
+
```
|
479
|
+
|
480
|
+
#### Stylus
|
481
|
+
|
482
|
+
```bash
|
483
|
+
yarn add stylus stylus-loader
|
484
|
+
```
|
485
|
+
|
486
|
+
#### Other frameworks
|
487
|
+
|
488
|
+
Please follow webpack integration guide for relevant framework or library,
|
489
|
+
|
490
|
+
1. [Svelte](https://github.com/sveltejs/svelte-loader#install)
|
491
|
+
2. [Angular](https://v2.angular.io/docs/ts/latest/guide/webpack.html#!#configure-webpack)
|
492
|
+
3. [Vue](https://vue-loader.vuejs.org/guide/)
|
493
|
+
|
494
|
+
For example to add Vue support:
|
495
|
+
```js
|
496
|
+
// config/webpack/rules/vue.js
|
497
|
+
const { VueLoaderPlugin } = require('vue-loader')
|
498
|
+
|
499
|
+
module.exports = {
|
500
|
+
module: {
|
501
|
+
rules: [
|
502
|
+
{
|
503
|
+
test: /\.vue$/,
|
504
|
+
loader: 'vue-loader'
|
505
|
+
}
|
506
|
+
]
|
507
|
+
},
|
508
|
+
plugins: [new VueLoaderPlugin()],
|
509
|
+
resolve: {
|
510
|
+
extensions: ['.vue']
|
511
|
+
}
|
512
|
+
}
|
513
|
+
```
|
514
|
+
|
515
|
+
```js
|
516
|
+
// config/webpack/webpack.config.js
|
517
|
+
const { webpackConfig, merge } = require('@shakacode/shakapacker')
|
518
|
+
const vueConfig = require('./rules/vue')
|
519
|
+
|
520
|
+
module.exports = merge(vueConfig, webpackConfig)
|
521
|
+
```
|
522
|
+
|
523
|
+
|
524
|
+
### Custom Rails environments
|
525
|
+
|
526
|
+
Out of the box Webpacker ships with - development, test and production environments in `config/webpacker.yml` however, in most production apps extra environments are needed as part of deployment workflow. Webpacker supports this out of the box from version 3.4.0+ onwards.
|
527
|
+
|
528
|
+
You can choose to define additional environment configurations in webpacker.yml,
|
529
|
+
|
530
|
+
```yml
|
531
|
+
staging:
|
532
|
+
<<: *default
|
533
|
+
|
534
|
+
# Production depends on precompilation of packs prior to booting for performance.
|
535
|
+
compile: false
|
536
|
+
|
537
|
+
# Cache manifest.json for performance
|
538
|
+
cache_manifest: true
|
539
|
+
|
540
|
+
# Compile staging packs to a separate directory
|
541
|
+
public_output_path: packs-staging
|
542
|
+
```
|
543
|
+
|
544
|
+
Otherwise Webpacker will use production environment as a fallback environment for loading configurations. Please note, `NODE_ENV` can either be set to `production`, `development` or `test`. This means you don't need to create additional environment files inside `config/webpacker/*` and instead use webpacker.yml to load different configurations using `RAILS_ENV`.
|
545
|
+
|
546
|
+
For example, the below command will compile assets in production mode but will use staging configurations from `config/webpacker.yml` if available or use fallback production environment configuration:
|
547
|
+
|
548
|
+
```bash
|
549
|
+
RAILS_ENV=staging bundle exec rails assets:precompile
|
550
|
+
```
|
551
|
+
|
552
|
+
And, this will compile in development mode and load configuration for cucumber environment if defined in webpacker.yml or fallback to production configuration
|
553
|
+
|
554
|
+
```bash
|
555
|
+
RAILS_ENV=cucumber NODE_ENV=development bundle exec rails assets:precompile
|
556
|
+
```
|
557
|
+
|
558
|
+
Please note, binstubs compiles in development mode however rake tasks compiles in production mode.
|
559
|
+
|
560
|
+
```bash
|
561
|
+
# Compiles in development mode unless NODE_ENV is specified, per the binstub source
|
562
|
+
./bin/webpacker
|
563
|
+
./bin/webpacker-dev-server
|
564
|
+
|
565
|
+
# Compiles in production mode by default unless NODE_ENV is specified, per `lib/tasks/webpacker/compile.rake`
|
566
|
+
bundle exec rails assets:precompile
|
567
|
+
bundle exec rails webpacker:compile
|
568
|
+
```
|
569
|
+
|
570
|
+
### Upgrading
|
571
|
+
|
572
|
+
You can run following commands to upgrade Webpacker to the latest stable version. This process involves upgrading the gem and related JavaScript packages:
|
573
|
+
|
574
|
+
```bash
|
575
|
+
# check your Gemfile for version restrictions
|
576
|
+
bundle update webpacker
|
577
|
+
|
578
|
+
# overwrite your changes to the default install files and revert any unwanted changes from the install
|
579
|
+
rails webpacker:install
|
580
|
+
|
581
|
+
# yarn 1 instructions
|
582
|
+
yarn upgrade @shakacode/shakapacker --latest
|
583
|
+
yarn upgrade webpack-dev-server --latest
|
584
|
+
|
585
|
+
# yarn 2 instructions
|
586
|
+
yarn up @shakacode/shakapacker@latest
|
587
|
+
yarn up webpack-dev-server@latest
|
588
|
+
|
589
|
+
# Or to install the latest release (including pre-releases)
|
590
|
+
yarn add @shakacode/shakapacker@next
|
591
|
+
```
|
592
|
+
|
593
|
+
Also, consult the [CHANGELOG](./CHANGELOG.md) for additional upgrade links.
|
594
|
+
|
595
|
+
## Paths
|
596
|
+
|
597
|
+
By default, Webpacker ships with simple conventions for where the JavaScript app files and compiled webpack bundles will go in your Rails app. All these options are configurable from `config/webpacker.yml` file.
|
598
|
+
|
599
|
+
The configuration for what webpack is supposed to compile by default rests on the convention that every file in `app/packs/entrypoints/*`**(default)** or whatever path you set for `source_entry_path` in the `webpacker.yml` configuration is turned into their own output files (or entry points, as webpack calls it). Therefore you don't want to put anything inside `packs` directory that you do not want to be an entry file. As a rule of thumb, put all files you want to link in your views inside "packs" directory and keep everything else under `app/packs`.
|
600
|
+
|
601
|
+
Suppose you want to change the source directory from `app/packs` to `frontend` and output to `assets/packs`. This is how you would do it:
|
602
|
+
|
603
|
+
```yml
|
604
|
+
# config/webpacker.yml
|
605
|
+
source_path: frontend # packs are in frontend/packs
|
606
|
+
public_output_path: assets/packs # outputs to => public/assets/packs
|
607
|
+
```
|
608
|
+
|
609
|
+
Similarly you can also control and configure `webpack-dev-server` settings from `config/webpacker.yml` file:
|
610
|
+
|
611
|
+
```yml
|
612
|
+
# config/webpacker.yml
|
613
|
+
development:
|
614
|
+
dev_server:
|
615
|
+
host: localhost
|
616
|
+
port: 3035
|
617
|
+
```
|
618
|
+
|
619
|
+
If you have `hmr` turned to true, then the `stylesheet_pack_tag` generates no output, as you will want to configure your styles to be inlined in your JavaScript for hot reloading. During production and testing, the `stylesheet_pack_tag` will create the appropriate HTML tags.
|
620
|
+
|
621
|
+
### Additional paths
|
622
|
+
|
623
|
+
If you are adding Webpacker to an existing app that has most of the assets inside `app/assets` or inside an engine, and you want to share that with webpack modules, you can use the `additional_paths` option available in `config/webpacker.yml`. This lets you
|
624
|
+
add additional paths that webpack should look up when resolving modules:
|
625
|
+
|
626
|
+
```yml
|
627
|
+
additional_paths: ['app/assets', 'vendor/assets']
|
628
|
+
```
|
629
|
+
|
630
|
+
You can then import these items inside your modules like so:
|
631
|
+
|
632
|
+
```js
|
633
|
+
// Note it's relative to parent directory i.e. app/assets
|
634
|
+
import 'stylesheets/main'
|
635
|
+
import 'images/rails.png'
|
636
|
+
```
|
637
|
+
|
638
|
+
**Note:** Please be careful when adding paths here otherwise it will make the compilation slow, consider adding specific paths instead of whole parent directory if you just need to reference one or two modules
|
639
|
+
|
640
|
+
**Also note:** While importing assets living outside your `source_path` defined in webpacker.yml (like, for instance, assets under `app/assets`) from within your packs using _relative_ paths like `import '../../assets/javascripts/file.js'` will work in development, Webpacker won't recompile the bundle in production unless a file that lives in one of it's watched paths has changed (check out `Webpacker::Compiler#watched_files_digest`). That's why you'd need to add `app/assets` to the additional_paths as stated above and use `import 'javascripts/file.js'` instead.
|
641
|
+
|
642
|
+
|
643
|
+
## Deployment
|
644
|
+
|
645
|
+
Webpacker hooks up a new `webpacker:compile` task to `assets:precompile`, which gets run whenever you run `assets:precompile`. If you are not using Sprockets, `webpacker:compile` is automatically aliased to `assets:precompile`. Similar to sprockets both rake tasks will compile packs in production mode but will use `RAILS_ENV` to load configuration from `config/webpacker.yml` (if available).
|
646
|
+
|
647
|
+
When compiling assets for production on a remote server, such as a continuous integration environment, it's recommended to use `yarn install --frozen-lockfile` to install NPM packages on the remote host to ensure that the installed packages match the `yarn.lock` file.
|
648
|
+
|
649
|
+
If you are using a CDN setup, webpacker will use the configured [asset host](https://guides.rubyonrails.org/configuring.html#rails-general-configuration) value to prefix URLs for images or font icons which are included inside JS code or CSS. It is possible to override this value during asset compilation by setting the `WEBPACKER_ASSET_HOST` environment variable.
|
650
|
+
|
651
|
+
|
652
|
+
## Troubleshooting
|
653
|
+
|
654
|
+
See the doc page for [Troubleshooting](./docs/troubleshooting.md).
|
655
|
+
|
656
|
+
|
657
|
+
## Contributing
|
658
|
+
|
659
|
+
[![Code Helpers](https://www.codetriage.com/shakacode/shakapacker/badges/users.svg)](https://www.codetriage.com/shakacode/shakapacker)
|
660
|
+
|
661
|
+
We encourage you to contribute to Webpacker! See [CONTRIBUTING](CONTRIBUTING.md) for guidelines about how to proceed.
|
662
|
+
|
663
|
+
|
664
|
+
## License
|
665
|
+
|
666
|
+
Webpacker is released under the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/config/README.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
../lib/install/config/webpacker.yml
|