webpacker 6.0.0.pre.2 → 6.0.0.rc.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 +5 -2
- data/.github/workflows/js-lint.yml +5 -2
- data/.github/workflows/rubocop.yml +1 -1
- data/.github/workflows/ruby.yml +17 -14
- data/.node-version +1 -1
- data/.rubocop.yml +106 -0
- data/CHANGELOG.md +36 -9
- data/CONTRIBUTING.md +1 -1
- data/Gemfile.lock +93 -90
- data/README.md +363 -107
- data/config/README.md +3 -0
- data/config/webpacker.yml +1 -0
- data/docs/deployment.md +9 -29
- data/docs/developing_webpacker.md +29 -0
- data/docs/troubleshooting.md +57 -25
- data/docs/v6_upgrade.md +75 -0
- data/gemfiles/Gemfile-rails-edge +1 -1
- data/gemfiles/Gemfile-rails.6.1.x +12 -0
- data/lib/install/config/webpacker.yml +5 -7
- data/lib/install/{javascript/packs → packs/entrypoints}/application.js +4 -3
- data/lib/install/template.rb +17 -10
- data/lib/tasks/webpacker/binstubs.rake +2 -2
- data/lib/tasks/webpacker/check_node.rake +3 -0
- data/lib/tasks/webpacker/check_yarn.rake +4 -1
- data/lib/tasks/webpacker/clobber.rake +1 -1
- data/lib/tasks/webpacker/install.rake +2 -2
- data/lib/tasks/webpacker/verify_config.rake +14 -0
- data/lib/tasks/webpacker/verify_install.rake +1 -11
- data/lib/webpacker.rb +1 -1
- data/lib/webpacker/commands.rb +2 -1
- data/lib/webpacker/compiler.rb +9 -3
- data/lib/webpacker/configuration.rb +19 -8
- data/lib/webpacker/dev_server.rb +6 -0
- data/lib/webpacker/dev_server_runner.rb +7 -2
- data/lib/webpacker/env.rb +5 -1
- data/lib/webpacker/helper.rb +26 -50
- data/lib/webpacker/instance.rb +4 -0
- data/lib/webpacker/manifest.rb +1 -2
- data/lib/webpacker/railtie.rb +1 -2
- data/lib/webpacker/runner.rb +1 -1
- data/lib/webpacker/version.rb +1 -1
- data/lib/webpacker/webpack_runner.rb +1 -0
- data/package.json +25 -29
- data/package/__tests__/development.js +3 -2
- data/package/__tests__/env.js +8 -4
- data/package/__tests__/index.js +9 -0
- data/package/babel/preset.js +24 -14
- data/package/env.js +7 -1
- data/package/environments/__tests__/base.js +7 -7
- data/package/environments/base.js +25 -25
- data/package/environments/development.js +7 -8
- data/package/environments/production.js +28 -30
- data/package/index.js +9 -2
- data/package/inliningCss.js +7 -0
- data/package/rules/babel.js +1 -1
- data/package/rules/coffee.js +5 -5
- data/package/rules/erb.js +5 -3
- data/package/rules/file.js +5 -3
- data/package/rules/index.js +9 -17
- data/package/rules/less.js +14 -10
- data/package/rules/raw.js +5 -0
- data/package/rules/sass.js +12 -9
- data/package/rules/stylus.js +26 -0
- data/package/utils/get_style_rule.js +28 -30
- data/package/utils/helpers.js +25 -0
- data/test/configuration_test.rb +3 -3
- data/test/dev_server_runner_test.rb +13 -2
- data/test/helper_test.rb +59 -60
- data/test/manifest_test.rb +16 -0
- data/test/mounted_app/test/dummy/config/webpacker.yml +4 -4
- data/test/test_app/app/{javascript/packs → packs/entrypoints}/application.js +1 -1
- data/test/test_app/app/{javascript/packs → packs/entrypoints}/multi_entry.css +0 -0
- data/test/test_app/app/{javascript/packs → packs/entrypoints}/multi_entry.js +0 -0
- data/test/test_app/config/webpacker.yml +4 -6
- data/test/test_app/config/webpacker_other_location.yml +79 -0
- data/test/test_app/public/packs/manifest.json +19 -5
- data/test/webpacker_test.rb +17 -0
- data/yarn.lock +1567 -1039
- metadata +24 -36
- data/docs/assets.md +0 -135
- data/docs/cloud9.md +0 -310
- data/docs/css.md +0 -303
- data/docs/docker.md +0 -68
- data/docs/engines.md +0 -213
- data/docs/env.md +0 -68
- data/docs/es6.md +0 -72
- data/docs/folder-structure.md +0 -66
- data/docs/integrations.md +0 -220
- data/docs/misc.md +0 -23
- data/docs/props.md +0 -187
- data/docs/react.md +0 -183
- data/docs/target.md +0 -22
- data/docs/testing.md +0 -147
- data/docs/typescript.md +0 -190
- data/docs/v4-upgrade.md +0 -142
- data/docs/webpack-dev-server.md +0 -94
- data/docs/webpack.md +0 -315
- data/docs/yarn.md +0 -23
- data/lib/install/examples/vue3/app.vue +0 -27
- data/lib/install/examples/vue3/hello_vue.js +0 -15
- data/lib/install/javascript/packs/application.css +0 -9
- data/package/babel/preset-react.js +0 -62
- data/package/rules/svg.js +0 -23
data/docs/css.md
DELETED
@@ -1,303 +0,0 @@
|
|
1
|
-
# CSS, Sass and SCSS
|
2
|
-
|
3
|
-
Webpacker supports importing CSS, Sass and SCSS files directly into your JavaScript files.
|
4
|
-
|
5
|
-
Importing and loading styles is a two step process:
|
6
|
-
|
7
|
-
1. You need to tell webpack which file(s) it has to compile and know how to load
|
8
|
-
|
9
|
-
When you do `import '../scss/application.scss'`, you're telling webpack to include `application.scss` in the build. This does not mean it's going to be compiled into your javascript, only that webpack now compiles and knows how to load this file. (How that file compilation is handled is depending on how your loaders (`css-loader`, `sass-loader`, `file-loader`, etc.) are configured.)
|
10
|
-
|
11
|
-
2. You need to load those files in your views
|
12
|
-
|
13
|
-
In order to have styles load in production, you need to include `stylesheet_pack_tag` with the same name as the javascript file that imports the styles.
|
14
|
-
|
15
|
-
When you do `<%= stylesheet_pack_tag 'application' %>`, that's a run-time inclusion from Rails, where Rails gets the correct "asset path" to that file from webpack.
|
16
|
-
|
17
|
-
## Import global styles into your JS app
|
18
|
-
|
19
|
-
### Importing CSS as a multi-file pack (Webpacker v5)
|
20
|
-
|
21
|
-
When you add a CSS/SCSS/SASS file to `app/javascript/packs/` directory, make sure to use the same pack name as its complementary JavaScript pack, e.g. `application.js` and `application.css`. By Webpacker convention (as of Webpacker v5), this will bundle `application.js` and `application.scss` as part of the same entry point (also described as [a multi-file entry point in the webpack docs](https://webpack.js.org/concepts/entry-points/#single-entry-shorthand-syntax)). With this approach, you can avoid importing CSS from JS, if desired.
|
22
|
-
|
23
|
-
```
|
24
|
-
app/
|
25
|
-
javascript/
|
26
|
-
packs/
|
27
|
-
application.js
|
28
|
-
application.scss
|
29
|
-
```
|
30
|
-
|
31
|
-
### Importing CSS from CSS
|
32
|
-
|
33
|
-
You can import additional CSS/SCSS/SASS files from within a CSS file:
|
34
|
-
|
35
|
-
```
|
36
|
-
app/
|
37
|
-
javascript/
|
38
|
-
stylesheets/
|
39
|
-
application.scss
|
40
|
-
posts.scss
|
41
|
-
comments.scss
|
42
|
-
```
|
43
|
-
|
44
|
-
```css
|
45
|
-
/* app/javascript/stylesheets/application.scss */
|
46
|
-
|
47
|
-
@import './posts';
|
48
|
-
@import './comments';
|
49
|
-
```
|
50
|
-
|
51
|
-
### Importing CSS provided by an NPM package from SCSS/CSS
|
52
|
-
|
53
|
-
Given your application installs an NPM package that provides CSS, such as `flatpickr`, you can import the CSS file(s) by path from the package directory within `node_modules/`:
|
54
|
-
|
55
|
-
```js
|
56
|
-
/* app/javascript/stylesheets/application.scss */
|
57
|
-
|
58
|
-
@import "flatpickr/dist/flatpickr.css"
|
59
|
-
```
|
60
|
-
|
61
|
-
### Importing CSS from JS
|
62
|
-
|
63
|
-
```sass
|
64
|
-
// app/javascript/hello_react/styles/hello-react.sass
|
65
|
-
|
66
|
-
.hello-react
|
67
|
-
padding: 20px
|
68
|
-
font-size: 12px
|
69
|
-
```
|
70
|
-
|
71
|
-
```js
|
72
|
-
// React component example
|
73
|
-
// app/javascript/packs/hello_react.jsx
|
74
|
-
|
75
|
-
import React from 'react'
|
76
|
-
import helloIcon from '../hello_react/images/icon.png'
|
77
|
-
import '../hello_react/styles/hello-react'
|
78
|
-
|
79
|
-
const Hello = (props) => (
|
80
|
-
<div className='hello-react'>
|
81
|
-
<img src={helloIcon} alt='hello-icon' />
|
82
|
-
<p>Hello {props.name}!</p>
|
83
|
-
</div>
|
84
|
-
)
|
85
|
-
```
|
86
|
-
|
87
|
-
### Importing CSS provided by an NPM package from JS
|
88
|
-
|
89
|
-
Given your application installs an NPM package that provides CSS, such as `flatpickr`, you can import the CSS file(s) by path from the package directory within `node_modules/`. This is an alternative to importing from within a CSS file, as above:
|
90
|
-
|
91
|
-
```js
|
92
|
-
// app/javascript/packs/application.js
|
93
|
-
|
94
|
-
import 'flatpickr/dist/flatpickr.css'
|
95
|
-
```
|
96
|
-
|
97
|
-
## Import scoped styles into your JS app
|
98
|
-
|
99
|
-
Stylesheets that end with `.module.*` are treated as [CSS Modules](https://github.com/css-modules/css-modules).
|
100
|
-
|
101
|
-
```sass
|
102
|
-
// app/javascript/hello_react/styles/hello-react.module.sass
|
103
|
-
|
104
|
-
.helloReact
|
105
|
-
padding: 20px
|
106
|
-
font-size: 12px
|
107
|
-
```
|
108
|
-
|
109
|
-
```js
|
110
|
-
// React component example
|
111
|
-
// app/javascript/packs/hello_react.jsx
|
112
|
-
|
113
|
-
import React from 'react'
|
114
|
-
import helloIcon from '../hello_react/images/icon.png'
|
115
|
-
import styles from '../hello_react/styles/hello-react'
|
116
|
-
|
117
|
-
const Hello = (props) => (
|
118
|
-
<div className={styles.helloReact}>
|
119
|
-
<img src={helloIcon} alt='hello-icon' />
|
120
|
-
<p>Hello {props.name}!</p>
|
121
|
-
</div>
|
122
|
-
)
|
123
|
-
```
|
124
|
-
|
125
|
-
**Note:** Declared class is referenced as object property in JavaScript.
|
126
|
-
|
127
|
-
## Import scoped styles into your TypeScript app
|
128
|
-
|
129
|
-
Using CSS modules with a TypeScript application requires a few differences from a JavaScript app. The CSS / Sass files are the same:
|
130
|
-
|
131
|
-
```sass
|
132
|
-
// app/javascript/hello_react/styles/hello-react.module.sass
|
133
|
-
|
134
|
-
.helloReact
|
135
|
-
padding: 20px
|
136
|
-
font-size: 12px
|
137
|
-
```
|
138
|
-
|
139
|
-
There must also be a type definition file for these styles:
|
140
|
-
|
141
|
-
```typescript
|
142
|
-
export const helloReact: string
|
143
|
-
```
|
144
|
-
|
145
|
-
You can then import the styles like this:
|
146
|
-
|
147
|
-
```typescript
|
148
|
-
// React component example
|
149
|
-
// app/javascripts/packs/hello_react.tsx
|
150
|
-
|
151
|
-
import React from 'react'
|
152
|
-
import helloIcon from '../hello_react/images/icon.png'
|
153
|
-
import * as styles from '../hello_react/styles/hello-react.module.sass'
|
154
|
-
|
155
|
-
const Hello = (props) => (
|
156
|
-
<div className={styles.helloReact}>
|
157
|
-
<img src={helloIcon} alt='hello-icon' />
|
158
|
-
<p>Hello {props.name}!</p>
|
159
|
-
</div>
|
160
|
-
)
|
161
|
-
```
|
162
|
-
|
163
|
-
You can automatically generate type definitions for the styles by installing the `typed-scss-modules` as a development dependency:
|
164
|
-
|
165
|
-
```
|
166
|
-
yarn add typed-scss-modules --dev
|
167
|
-
```
|
168
|
-
|
169
|
-
Then by adding these lines to your `package.json`:
|
170
|
-
|
171
|
-
```
|
172
|
-
"scripts": {
|
173
|
-
"gen-typings": "yarn run tsm app/javascript/**/*.sass",
|
174
|
-
"watch-typings": "yarn run tsm app/javascript/**/*.sass -w"
|
175
|
-
},
|
176
|
-
```
|
177
|
-
|
178
|
-
You can generate the typings for the stylesheet by running the command `yarn gen-typings` when you've finished writing CSS, or run `yarn watch-typings` to have it automatically generate them as you go.
|
179
|
-
|
180
|
-
## Link styles from your Rails views
|
181
|
-
|
182
|
-
Under the hood webpack uses
|
183
|
-
[mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-extract-plugin) plugin to extract all the referenced styles within your app and compile it into
|
184
|
-
a separate `[pack_name].css` bundle so that in your view you can use the
|
185
|
-
`stylesheet_pack_tag` helper.
|
186
|
-
|
187
|
-
```erb
|
188
|
-
<%= stylesheet_pack_tag 'hello_react' %>
|
189
|
-
```
|
190
|
-
|
191
|
-
## Add bootstrap
|
192
|
-
|
193
|
-
You can use Yarn to add bootstrap or any other modules available on npm:
|
194
|
-
|
195
|
-
```bash
|
196
|
-
yarn add bootstrap
|
197
|
-
```
|
198
|
-
|
199
|
-
Import Bootstrap and theme (optional) CSS in your app/javascript/packs/application.js file:
|
200
|
-
|
201
|
-
```js
|
202
|
-
import 'bootstrap/dist/css/bootstrap'
|
203
|
-
import 'bootstrap/dist/css/bootstrap-theme'
|
204
|
-
```
|
205
|
-
|
206
|
-
Or in your app/javascript/packs/application.sass file:
|
207
|
-
|
208
|
-
```sass
|
209
|
-
// ~ to tell that this is not a relative import
|
210
|
-
|
211
|
-
@import '~bootstrap/dist/css/bootstrap'
|
212
|
-
@import '~bootstrap/dist/css/bootstrap-theme'
|
213
|
-
```
|
214
|
-
|
215
|
-
## Post-Processing CSS
|
216
|
-
|
217
|
-
Webpacker out-of-the-box provides CSS post-processing using
|
218
|
-
[postcss-loader](https://github.com/postcss/postcss-loader)
|
219
|
-
and the installer sets up a standard `postcss.config.js`
|
220
|
-
file in your app root with standard plugins.
|
221
|
-
|
222
|
-
```js
|
223
|
-
module.exports = {
|
224
|
-
plugins: [
|
225
|
-
require('postcss-import'),
|
226
|
-
require('postcss-flexbugs-fixes'),
|
227
|
-
require('postcss-preset-env')({
|
228
|
-
autoprefixer: {
|
229
|
-
flexbox: 'no-2009'
|
230
|
-
},
|
231
|
-
stage: 3
|
232
|
-
})
|
233
|
-
]
|
234
|
-
}
|
235
|
-
```
|
236
|
-
|
237
|
-
## Using CSS with [vue-loader](https://github.com/vuejs/vue-loader)
|
238
|
-
|
239
|
-
Vue templates require loading the stylesheet in your application in
|
240
|
-
order for CSS to work. This is in addition to loading the JavaScript
|
241
|
-
file for the entry point. Loading the stylesheet will also load the
|
242
|
-
CSS for any nested components.
|
243
|
-
|
244
|
-
```erb
|
245
|
-
<%= stylesheet_pack_tag 'hello_vue' %>
|
246
|
-
<%= javascript_pack_tag 'hello_vue' %>
|
247
|
-
```
|
248
|
-
|
249
|
-
## Resolve url loader
|
250
|
-
|
251
|
-
Since `Sass/libsass` does not provide url rewriting, all linked assets must be relative to the output. Add the missing url rewriting using the resolve-url-loader. Place it directly after the sass-loader in the loader chain.
|
252
|
-
|
253
|
-
```bash
|
254
|
-
yarn add resolve-url-loader
|
255
|
-
```
|
256
|
-
|
257
|
-
```js
|
258
|
-
// webpack/environment.js
|
259
|
-
const { environment } = require('@rails/webpacker')
|
260
|
-
|
261
|
-
// resolve-url-loader must be used before sass-loader
|
262
|
-
environment.loaders.get('sass').use.splice(-1, 0, {
|
263
|
-
loader: 'resolve-url-loader'
|
264
|
-
})
|
265
|
-
|
266
|
-
module.exports = environment
|
267
|
-
```
|
268
|
-
|
269
|
-
## Working with TypeScript
|
270
|
-
|
271
|
-
In order to get CSS to work with typescript you have two options.
|
272
|
-
You can either use `require` to bypass typescript special `import`.
|
273
|
-
|
274
|
-
```ts
|
275
|
-
const styles = require('../hello_react/styles/hello-react')
|
276
|
-
```
|
277
|
-
|
278
|
-
You may also use the package [typings-for-css-modules-loader](https://github.com/Jimdo/typings-for-css-modules-loader) instead of `css-loader` to automatically generate typescript `.d.ts` files in order to help resolve any css/scss styles. To do that:
|
279
|
-
|
280
|
-
```js
|
281
|
-
// app/javascript/packs/hello_react.jsx
|
282
|
-
import * as styles from '../hello_react.styles/hello-react.module.scss'
|
283
|
-
```
|
284
|
-
|
285
|
-
```bash
|
286
|
-
yarn add --dev typings-for-css-modules-loader
|
287
|
-
```
|
288
|
-
|
289
|
-
```js
|
290
|
-
// webpack/environment.js
|
291
|
-
const { environment } = require('@rails/webpacker')
|
292
|
-
|
293
|
-
// replace css-loader with typings-for-css-modules-loader
|
294
|
-
environment.loaders.get('moduleSass').use = environment.loaders
|
295
|
-
.get('moduleSass')
|
296
|
-
.use.map((u) => {
|
297
|
-
if (u.loader == 'css-loader') {
|
298
|
-
return { ...u, loader: 'typings-for-css-modules-loader' }
|
299
|
-
} else {
|
300
|
-
return u
|
301
|
-
}
|
302
|
-
})
|
303
|
-
```
|
data/docs/docker.md
DELETED
@@ -1,68 +0,0 @@
|
|
1
|
-
# Docker
|
2
|
-
|
3
|
-
To setup webpacker with a dockerized Rails application.
|
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
|
-
environment:
|
13
|
-
- NODE_ENV=development
|
14
|
-
- RAILS_ENV=development
|
15
|
-
- WEBPACKER_DEV_SERVER_HOST=0.0.0.0
|
16
|
-
command: ./bin/webpack-dev-server
|
17
|
-
volumes:
|
18
|
-
- .:/webpacker-example-app
|
19
|
-
ports:
|
20
|
-
- '127.0.0.1:3035:3035'
|
21
|
-
```
|
22
|
-
|
23
|
-
add nodejs and yarn as dependencies in Dockerfile,
|
24
|
-
|
25
|
-
```dockerfile
|
26
|
-
FROM ruby:2.4.1
|
27
|
-
|
28
|
-
RUN apt-get update -qq && apt-get install -y build-essential nodejs \
|
29
|
-
&& rm -rf /var/lib/apt/lists/* \
|
30
|
-
&& curl -o- -L https://yarnpkg.com/install.sh | bash
|
31
|
-
|
32
|
-
# Rest of the commands....
|
33
|
-
```
|
34
|
-
|
35
|
-
Please note: if using `assets:precompile` in the Dockerfile or have issues with the snippet above then try:
|
36
|
-
|
37
|
-
```dockerfile
|
38
|
-
FROM ruby:2.4.1
|
39
|
-
|
40
|
-
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash \
|
41
|
-
&& apt-get update && apt-get install -y nodejs && rm -rf /var/lib/apt/lists/* \
|
42
|
-
&& curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
|
43
|
-
&& echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list \
|
44
|
-
&& apt-get update && apt-get install -y yarn && rm -rf /var/lib/apt/lists/*
|
45
|
-
|
46
|
-
# Rest of the commands....
|
47
|
-
```
|
48
|
-
|
49
|
-
then add the webpacker host name environment variable to the web/app service:
|
50
|
-
|
51
|
-
```Dockerfile
|
52
|
-
web:
|
53
|
-
build:
|
54
|
-
context: .
|
55
|
-
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
|
56
|
-
volumes:
|
57
|
-
- .:/usr/src/app
|
58
|
-
ports:
|
59
|
-
- "127.0.0.1:3000:3000"
|
60
|
-
environment:
|
61
|
-
- WEBPACKER_DEV_SERVER_HOST=webpacker
|
62
|
-
```
|
63
|
-
|
64
|
-
Lastly, rebuild your container:
|
65
|
-
|
66
|
-
```bash
|
67
|
-
docker-compose up --build
|
68
|
-
```
|
data/docs/engines.md
DELETED
@@ -1,213 +0,0 @@
|
|
1
|
-
# Using in Rails engines
|
2
|
-
|
3
|
-
If the application UI consists of multiple frontend application, you'd probably like to isolate their building too (e.g. if you use different frameworks/versions). Hence we needed our webpack(-er) to be isolated too: separate `package.json`, dev server, compilation process.
|
4
|
-
|
5
|
-
You can do this by adding another Webpacker instance to your application.
|
6
|
-
|
7
|
-
This guide describes how to do that using [Rails engines](https://guides.rubyonrails.org/engines.html).
|
8
|
-
|
9
|
-
|
10
|
-
## Step 1: create Rails engine.
|
11
|
-
|
12
|
-
First, you create a Rails engine (say, `MyEngine`). See the official [Rails guide](https://guides.rubyonrails.org/engines.html).
|
13
|
-
|
14
|
-
## Step 2: install Webpacker within the engine.
|
15
|
-
|
16
|
-
There is no built-in tasks to install Webpacker within the engine, thus you have to add all the require files manually (you can copy them from the main app):
|
17
|
-
- Add `config/webpacker.yml` and `config/webpack/*.js` files
|
18
|
-
- Add `bin/webpack` and `bin/webpack-dev-server` files
|
19
|
-
- Add `package.json` with required deps.
|
20
|
-
|
21
|
-
|
22
|
-
## Step 3: configure Webpacker instance.
|
23
|
-
|
24
|
-
- File `lib/my_engine.rb`
|
25
|
-
|
26
|
-
```ruby
|
27
|
-
module MyEngine
|
28
|
-
ROOT_PATH = Pathname.new(File.join(__dir__, ".."))
|
29
|
-
|
30
|
-
class << self
|
31
|
-
def webpacker
|
32
|
-
@webpacker ||= ::Webpacker::Instance.new(
|
33
|
-
root_path: ROOT_PATH,
|
34
|
-
config_path: ROOT_PATH.join("config/webpacker.yml")
|
35
|
-
)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
```
|
40
|
-
|
41
|
-
## Step 4: Configure dev server proxy.
|
42
|
-
|
43
|
-
- File `lib/my_engine/engine.rb`
|
44
|
-
|
45
|
-
```ruby
|
46
|
-
module MyEngine
|
47
|
-
class Engine < ::Rails::Engine
|
48
|
-
initializer "webpacker.proxy" do |app|
|
49
|
-
insert_middleware = begin
|
50
|
-
MyEngine.webpacker.config.dev_server.present?
|
51
|
-
rescue
|
52
|
-
nil
|
53
|
-
end
|
54
|
-
next unless insert_middleware
|
55
|
-
|
56
|
-
app.middleware.insert_before(
|
57
|
-
0, Webpacker::DevServerProxy, # "Webpacker::DevServerProxy" if Rails version < 5
|
58
|
-
ssl_verify_none: true,
|
59
|
-
webpacker: MyEngine.webpacker
|
60
|
-
)
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
```
|
65
|
-
|
66
|
-
If you have multiple webpackers, you would probably want to run multiple dev servers at a time, and hence be able to configure their setting through env vars (e.g. within a `docker-compose.yml` file):
|
67
|
-
|
68
|
-
```yml
|
69
|
-
# webpacker.yml
|
70
|
-
# ...
|
71
|
-
development:
|
72
|
-
# ...
|
73
|
-
dev_server:
|
74
|
-
env_prefix: "MY_ENGINE_WEBPACKER_DEV_SERVER"
|
75
|
-
# ...
|
76
|
-
```
|
77
|
-
|
78
|
-
## Step 5: configure helper.
|
79
|
-
|
80
|
-
- File `app/helpers/my_engine/application_helper.rb`
|
81
|
-
|
82
|
-
```ruby
|
83
|
-
require "webpacker/helper"
|
84
|
-
|
85
|
-
module MyEngine
|
86
|
-
module ApplicationHelper
|
87
|
-
include ::Webpacker::Helper
|
88
|
-
|
89
|
-
def current_webpacker_instance
|
90
|
-
MyEngine.webpacker
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
94
|
-
```
|
95
|
-
|
96
|
-
Now you can use `stylesheet_pack_tag` and `javascript_pack_tag` from within your engine.
|
97
|
-
|
98
|
-
## Step 6: rake tasks.
|
99
|
-
|
100
|
-
Add Rake task to compile assets in production (`rake my_engine:webpacker:compile`)
|
101
|
-
|
102
|
-
- File `lib/tasks/my_engine_tasks.rake`
|
103
|
-
|
104
|
-
```ruby
|
105
|
-
def ensure_log_goes_to_stdout
|
106
|
-
old_logger = Webpacker.logger
|
107
|
-
Webpacker.logger = ActiveSupport::Logger.new(STDOUT)
|
108
|
-
yield
|
109
|
-
ensure
|
110
|
-
Webpacker.logger = old_logger
|
111
|
-
end
|
112
|
-
|
113
|
-
|
114
|
-
namespace :my_engine do
|
115
|
-
namespace :webpacker do
|
116
|
-
desc "Install deps with yarn"
|
117
|
-
task :yarn_install do
|
118
|
-
Dir.chdir(File.join(__dir__, "../..")) do
|
119
|
-
system "yarn install --no-progress --production"
|
120
|
-
end
|
121
|
-
end
|
122
|
-
|
123
|
-
desc "Compile JavaScript packs using webpack for production with digests"
|
124
|
-
task compile: [:yarn_install, :environment] do
|
125
|
-
Webpacker.with_node_env("production") do
|
126
|
-
ensure_log_goes_to_stdout do
|
127
|
-
if MyEngine.webpacker.commands.compile
|
128
|
-
# Successful compilation!
|
129
|
-
else
|
130
|
-
# Failed compilation
|
131
|
-
exit!
|
132
|
-
end
|
133
|
-
end
|
134
|
-
end
|
135
|
-
end
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
def yarn_install_available?
|
140
|
-
rails_major = Rails::VERSION::MAJOR
|
141
|
-
rails_minor = Rails::VERSION::MINOR
|
142
|
-
|
143
|
-
rails_major > 5 || (rails_major == 5 && rails_minor >= 1)
|
144
|
-
end
|
145
|
-
|
146
|
-
def enhance_assets_precompile
|
147
|
-
# yarn:install was added in Rails 5.1
|
148
|
-
deps = yarn_install_available? ? [] : ["my_engine:webpacker:yarn_install"]
|
149
|
-
Rake::Task["assets:precompile"].enhance(deps) do
|
150
|
-
Rake::Task["my_engine:webpacker:compile"].invoke
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
# Compile packs after we've compiled all other assets during precompilation
|
155
|
-
skip_webpacker_precompile = %w(no false n f).include?(ENV["WEBPACKER_PRECOMPILE"])
|
156
|
-
|
157
|
-
unless skip_webpacker_precompile
|
158
|
-
if Rake::Task.task_defined?("assets:precompile")
|
159
|
-
enhance_assets_precompile
|
160
|
-
else
|
161
|
-
Rake::Task.define_task("assets:precompile" => "my_engine:webpacker:compile")
|
162
|
-
end
|
163
|
-
end
|
164
|
-
```
|
165
|
-
|
166
|
-
## Step 7: serving compiled packs.
|
167
|
-
|
168
|
-
There are two approaches on serving compiled assets.
|
169
|
-
|
170
|
-
### Put engine's assets to the root app's public/ folder
|
171
|
-
|
172
|
-
You can serve engine's assets using the main app's static files server which serves files from `public/` folder.
|
173
|
-
|
174
|
-
For that you must configure your engine's webpacker to put compiled assets to the app's `public/` folder:
|
175
|
-
|
176
|
-
```yml
|
177
|
-
# my_engine/config/webpacker.yml
|
178
|
-
default: &default
|
179
|
-
# ...
|
180
|
-
# public_root_path could be used to override the path to `public/` folder
|
181
|
-
# (relative to the engine root)
|
182
|
-
public_root_path: ../public
|
183
|
-
# use a different sub-folder name
|
184
|
-
public_output_path: my-engine-packs
|
185
|
-
```
|
186
|
-
|
187
|
-
### Use a separate middleware
|
188
|
-
|
189
|
-
To serve static assets from the engine's `public/` folder you must add a middleware and point it to your engine's webpacker output path:
|
190
|
-
|
191
|
-
```ruby
|
192
|
-
# application.rb
|
193
|
-
|
194
|
-
config.middleware.use(
|
195
|
-
Rack::Static,
|
196
|
-
urls: ["/my-engine-packs"], root: "my_engine/public"
|
197
|
-
)
|
198
|
-
```
|
199
|
-
or if you prefer to keep your engine-related configuration within the engine itself
|
200
|
-
|
201
|
-
```ruby
|
202
|
-
# my-engine-root/lib/my-engine/engine.rb
|
203
|
-
module MyEngine
|
204
|
-
class Engine < ::Rails:Engine
|
205
|
-
config.app_middleware.use(
|
206
|
-
Rack::Static,
|
207
|
-
urls: ["/my-engine-packs"], root: "my_engine/public"
|
208
|
-
)
|
209
|
-
end
|
210
|
-
end
|
211
|
-
```
|
212
|
-
|
213
|
-
**NOTE:** in the example above we assume that your `public_output_path` is set to `my-engine-packs` in your engine's `webpacker.yml`.
|