webpacker 4.2.0 → 5.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/.node-version +1 -1
  3. data/.travis.yml +7 -20
  4. data/CHANGELOG.md +90 -55
  5. data/Gemfile +1 -0
  6. data/Gemfile.lock +8 -5
  7. data/README.md +52 -280
  8. data/docs/css.md +3 -4
  9. data/docs/deployment.md +2 -2
  10. data/docs/docker.md +17 -17
  11. data/docs/engines.md +13 -0
  12. data/docs/env.md +0 -2
  13. data/docs/integrations.md +220 -0
  14. data/docs/testing.md +1 -2
  15. data/docs/typescript.md +4 -21
  16. data/lib/install/config/webpacker.yml +0 -4
  17. data/lib/install/examples/react/tsconfig.json +2 -1
  18. data/lib/install/examples/typescript/tsconfig.json +2 -1
  19. data/lib/install/loaders/svelte.js +2 -2
  20. data/lib/install/template.rb +2 -2
  21. data/lib/install/typescript.rb +8 -12
  22. data/lib/tasks/webpacker/check_node.rake +14 -7
  23. data/lib/tasks/webpacker/check_yarn.rake +16 -9
  24. data/lib/tasks/webpacker/clean.rake +16 -6
  25. data/lib/tasks/webpacker/clobber.rake +8 -4
  26. data/lib/tasks/webpacker/compile.rake +1 -9
  27. data/lib/tasks/webpacker/yarn_install.rake +5 -16
  28. data/lib/webpacker.rb +8 -0
  29. data/lib/webpacker/commands.rb +45 -19
  30. data/lib/webpacker/compiler.rb +2 -3
  31. data/lib/webpacker/configuration.rb +1 -5
  32. data/lib/webpacker/dev_server_proxy.rb +3 -1
  33. data/lib/webpacker/dev_server_runner.rb +4 -4
  34. data/lib/webpacker/helper.rb +8 -8
  35. data/lib/webpacker/manifest.rb +4 -4
  36. data/lib/webpacker/railtie.rb +0 -43
  37. data/lib/webpacker/version.rb +1 -1
  38. data/package.json +35 -35
  39. data/package/__tests__/dev_server.js +2 -0
  40. data/package/dev_server.js +1 -1
  41. data/package/environments/__tests__/base.js +10 -0
  42. data/package/environments/base.js +12 -1
  43. data/package/environments/development.js +0 -4
  44. data/package/rules/babel.js +1 -1
  45. data/package/rules/sass.js +7 -1
  46. data/test/manifest_test.rb +37 -6
  47. data/test/rake_tasks_test.rb +11 -0
  48. data/test/test_app/app/javascript/packs/multi_entry.css +4 -0
  49. data/test/test_app/app/javascript/packs/multi_entry.js +4 -0
  50. data/test/test_app/config/application.rb +0 -1
  51. data/webpacker.gemspec +4 -3
  52. data/yarn.lock +2627 -1547
  53. metadata +28 -13
  54. data/gemfiles/Gemfile-rails.4.2.x +0 -9
  55. data/gemfiles/Gemfile-rails.5.0.x +0 -9
  56. data/gemfiles/Gemfile-rails.5.1.x +0 -9
  57. data/lib/install/loaders/typescript.js +0 -11
@@ -213,11 +213,10 @@ const { environment } = require('@rails/webpacker')
213
213
 
214
214
  // resolve-url-loader must be used before sass-loader
215
215
  environment.loaders.get('sass').use.splice(-1, 0, {
216
- loader: 'resolve-url-loader',
217
- options: {
218
- attempts: 1
219
- }
216
+ loader: 'resolve-url-loader'
220
217
  });
218
+
219
+ module.exports = environment
221
220
  ```
222
221
 
223
222
  ## Working with TypeScript
@@ -13,8 +13,8 @@ By default the output will look like this in different environments:
13
13
  <script src="http://localhost:8080/calendar-0bd141f6d9360cf4a7f5.js"></script>
14
14
  <link rel="stylesheet" media="screen" href="http://localhost:8080/calendar-dc02976b5f94b507e3b6.css">
15
15
  <!-- In production or development mode -->
16
- <script src="/packs/calendar-0bd141f6d9360cf4a7f5.js"></script>
17
- <link rel="stylesheet" media="screen" href="/packs/calendar-dc02976b5f94b507e3b6.css">
16
+ <script src="/packs/js/calendar-0bd141f6d9360cf4a7f5.js"></script>
17
+ <link rel="stylesheet" media="screen" href="/packs/css/calendar-dc02976b5f94b507e3b6.css">
18
18
  ```
19
19
 
20
20
 
@@ -1,6 +1,6 @@
1
1
  # Docker
2
2
 
3
- To setup webpacker with a dockerized Rails application is trivial.
3
+ To setup webpacker with a dockerized Rails application.
4
4
 
5
5
  First, add a new service for webpacker in docker-compose.yml:
6
6
 
@@ -9,8 +9,10 @@ version: '3'
9
9
  services:
10
10
  webpacker:
11
11
  build: .
12
- env_file:
13
- - '.env.docker'
12
+ environment:
13
+ - NODE_ENV=development
14
+ - RAILS_ENV=development
15
+ - WEBPACKER_DEV_SERVER_HOST=0.0.0.0
14
16
  command: ./bin/webpack-dev-server
15
17
  volumes:
16
18
  - .:/webpacker-example-app
@@ -18,15 +20,6 @@ services:
18
20
  - '3035:3035'
19
21
  ```
20
22
 
21
- Second, change the webpack-dev-server host to the service name of the docker-compose in config/webpacker.yml:
22
-
23
- ```yaml
24
- development:
25
- <<: *default
26
- dev_server:
27
- host: webpacker
28
- ```
29
-
30
23
  add nodejs and yarn as dependencies in Dockerfile,
31
24
 
32
25
  ```dockerfile
@@ -53,12 +46,19 @@ RUN curl -sL https://deb.nodesource.com/setup_8.x | bash \
53
46
  # Rest of the commands....
54
47
  ```
55
48
 
56
- and create an env file to load environment variables from:
49
+ then add the webpacker host name environment variable to the web/app service:
57
50
 
58
- ```env
59
- NODE_ENV=development
60
- RAILS_ENV=development
61
- WEBPACKER_DEV_SERVER_HOST=0.0.0.0
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
+ - "3000:3000"
60
+ environment:
61
+ - WEBPACKER_DEV_SERVER_HOST=webpacker
62
62
  ```
63
63
 
64
64
  Lastly, rebuild your container:
@@ -196,5 +196,18 @@ config.middleware.use(
196
196
  urls: ["/my-engine-packs"], root: "my_engine/public"
197
197
  )
198
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
+ ```
199
212
 
200
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`.
@@ -49,8 +49,6 @@ dotenvFiles.forEach((dotenvFile) => {
49
49
  dotenv.config({ path: dotenvFile, silent: true })
50
50
  })
51
51
 
52
- environment.plugins.prepend('Environment', new webpack.EnvironmentPlugin(JSON.parse(JSON.stringify(process.env))))
53
-
54
52
  module.exports = environment
55
53
  ```
56
54
 
@@ -0,0 +1,220 @@
1
+ # Integrations
2
+
3
+ Webpacker ships with basic out-of-the-box integration for React, Angular, Vue and Elm.
4
+ You can see a list of available commands/tasks by running `bundle exec rails webpacker`:
5
+
6
+ ## React
7
+
8
+ To use Webpacker with [React](https://facebook.github.io/react/), create a
9
+ new Rails 5.1+ app using `--webpack=react` option:
10
+
11
+ ```bash
12
+ # Rails 5.1+
13
+ rails new myapp --webpack=react
14
+ ```
15
+
16
+ (or run `bundle exec rails webpacker:install:react` in an existing Rails app already
17
+ setup with Webpacker).
18
+
19
+ The installer will add all relevant dependencies using Yarn, changes
20
+ to the configuration files, and an example React component to your
21
+ project in `app/javascript/packs` so that you can experiment with React right away.
22
+
23
+
24
+ ## Angular with TypeScript
25
+
26
+ To use Webpacker with [Angular](https://angular.io/), create a
27
+ new Rails 5.1+ app using `--webpack=angular` option:
28
+
29
+ ```bash
30
+ # Rails 5.1+
31
+ rails new myapp --webpack=angular
32
+ ```
33
+
34
+ (or run `bundle exec rails webpacker:install:angular` on a Rails app already
35
+ setup with Webpacker).
36
+
37
+ The installer will add the TypeScript and Angular core libraries using Yarn alongside
38
+ a few changes to the configuration files. An example component written in
39
+ TypeScript will also be added to your project in `app/javascript` so that
40
+ you can experiment with Angular right away.
41
+
42
+ By default, Angular uses a JIT compiler for development environment. This
43
+ compiler is not compatible with restrictive CSP (Content Security
44
+ Policy) environments like Rails 5.2+. You can use Angular AOT compiler
45
+ in development with the [@ngtools/webpack](https://www.npmjs.com/package/@ngtools/webpack#usage) plugin.
46
+
47
+ Alternatively if you're using Rails 5.2+ you can enable `unsafe-eval` rule for your
48
+ development environment. This can be done in the `config/initializers/content_security_policy.rb`
49
+ with the following code:
50
+
51
+ ```ruby
52
+ Rails.application.config.content_security_policy do |policy|
53
+ if Rails.env.development?
54
+ policy.script_src :self, :https, :unsafe_eval
55
+ else
56
+ policy.script_src :self, :https
57
+ end
58
+ end
59
+ ```
60
+
61
+
62
+ ## Vue
63
+
64
+ To use Webpacker with [Vue](https://vuejs.org/), create a
65
+ new Rails 5.1+ app using `--webpack=vue` option:
66
+
67
+ ```bash
68
+ # Rails 5.1+
69
+ rails new myapp --webpack=vue
70
+ ```
71
+ (or run `bundle exec rails webpacker:install:vue` on a Rails app already setup with Webpacker).
72
+
73
+ The installer will add Vue and its required libraries using Yarn alongside
74
+ automatically applying changes needed to the configuration files. An example component will
75
+ be added to your project in `app/javascript` so that you can experiment with Vue right away.
76
+
77
+ If you're using Rails 5.2+ you'll need to enable `unsafe-eval` rule for your development environment.
78
+ This can be done in the `config/initializers/content_security_policy.rb` with the following
79
+ configuration:
80
+
81
+ ```ruby
82
+ Rails.application.config.content_security_policy do |policy|
83
+ if Rails.env.development?
84
+ policy.script_src :self, :https, :unsafe_eval
85
+ else
86
+ policy.script_src :self, :https
87
+ end
88
+ end
89
+ ```
90
+ You can read more about this in the [Vue docs](https://vuejs.org/v2/guide/installation.html#CSP-environments).
91
+
92
+ ### Lazy loading integration
93
+
94
+ See [docs/es6](docs/es6.md) to know more about Webpack and Webpacker configuration.
95
+
96
+ For instance, you can lazy load Vue JS components:
97
+
98
+ Before:
99
+ ```js
100
+ import Vue from 'vue'
101
+ import { VCard } from 'vuetify/lib'
102
+
103
+ Vue.component('VCard', VCard)
104
+ ```
105
+
106
+ After:
107
+ ```js
108
+ import Vue from 'vue'
109
+
110
+ // With destructuring assignment
111
+ Vue.component('VCard', import('vuetify/lib').then(({ VCard }) => VCard)
112
+
113
+ // Or without destructuring assignment
114
+ Vue.component('OtherComponent', () => import('./OtherComponent'))
115
+ ```
116
+
117
+ You can use it in a Single File Component as well:
118
+
119
+ ```html
120
+ <template>
121
+ ...
122
+ </template>
123
+
124
+ <script>
125
+ export default {
126
+ components: {
127
+ OtherComponent: () => import('./OtherComponent')
128
+ }
129
+ }
130
+ </script>
131
+ ```
132
+
133
+ By wrapping the import function into an arrow function, Vue will execute it only when it gets requested, loading the module in that moment.
134
+
135
+ ##### Automatic registration
136
+
137
+ ```js
138
+ /**
139
+ * The following block of code may be used to automatically register your
140
+ * Vue components. It will recursively scan this directory for the Vue
141
+ * components and automatically register them with their "basename".
142
+ *
143
+ * Eg. ./components/OtherComponent.vue -> <other-component></other-component>
144
+ * Eg. ./UI/ButtonComponent.vue -> <button-component></button-component>
145
+ */
146
+
147
+ const files = require.context('./', true, /\.vue$/i)
148
+ files.keys().map(key => {
149
+ const component = key.split('/').pop().split('.')[0]
150
+
151
+ // With Lazy Loading
152
+ Vue.component(component, () => import(`${key}`))
153
+
154
+ // Or without Lazy Loading
155
+ Vue.component(component, files(key).default)
156
+ })
157
+ ```
158
+
159
+ ## Elm
160
+
161
+ To use Webpacker with [Elm](http://elm-lang.org), create a
162
+ new Rails 5.1+ app using `--webpack=elm` option:
163
+
164
+ ```
165
+ # Rails 5.1+
166
+ rails new myapp --webpack=elm
167
+ ```
168
+
169
+ (or run `bundle exec rails webpacker:install:elm` on a Rails app already setup with Webpacker).
170
+
171
+ The Elm library and its core packages will be added via Yarn and Elm.
172
+ An example `Main.elm` app will also be added to your project in `app/javascript`
173
+ so that you can experiment with Elm right away.
174
+
175
+ ## Svelte
176
+
177
+ To use Webpacker with [Svelte](https://svelte.dev), create a
178
+ new Rails 5.1+ app using `--webpack=svelte` option:
179
+
180
+ ```
181
+ # Rails 5.1+
182
+ rails new myapp --webpack=svelte
183
+ ```
184
+
185
+ (or run `bundle exec rails webpacker:install:svelte` on a Rails app already setup with Webpacker).
186
+
187
+ Please play with the [Svelte Tutorial](https://svelte.dev/tutorial/basics) or learn more about its API at https://svelte.dev/docs
188
+
189
+ ## Stimulus
190
+
191
+ To use Webpacker with [Stimulus](http://stimulusjs.org), create a
192
+ new Rails 5.1+ app using `--webpack=stimulus` option:
193
+
194
+ ```
195
+ # Rails 5.1+
196
+ rails new myapp --webpack=stimulus
197
+ ```
198
+
199
+ (or run `bundle exec rails webpacker:install:stimulus` on a Rails app already setup with Webpacker).
200
+
201
+ Please read [The Stimulus Handbook](https://stimulusjs.org/handbook/introduction) or learn more about its source code at https://github.com/stimulusjs/stimulus
202
+
203
+ ## CoffeeScript
204
+
205
+ To add [CoffeeScript](http://coffeescript.org/) support,
206
+ run `bundle exec rails webpacker:install:coffee` on a Rails app already
207
+ setup with Webpacker.
208
+
209
+ An example `hello_coffee.coffee` file will also be added to your project
210
+ in `app/javascript/packs` so that you can experiment with CoffeeScript right away.
211
+
212
+ ## Erb
213
+
214
+ To add [Erb](https://apidock.com/ruby/ERB) support in your JS templates,
215
+ run `bundle exec rails webpacker:install:erb` on a Rails app already
216
+ setup with Webpacker.
217
+
218
+ An example `hello_erb.js.erb` file will also be added to your project
219
+ in `app/javascript/packs` so that you can experiment with Erb-flavoured
220
+ javascript right away.
@@ -10,8 +10,7 @@ Webpacker does not setup `Karma` by default, so you've to manually install it al
10
10
  "test": "NODE_ENV=test karma start"
11
11
  },
12
12
  "dependencies": {
13
- "typescript": "^2.5.2",
14
- "ts-loader": "^2.3.7"
13
+ "typescript": "^2.5.2"
15
14
  },
16
15
  "devDependencies": {
17
16
  "karma": "^1.7.1",
@@ -7,7 +7,6 @@
7
7
 
8
8
  ```bash
9
9
  bundle exec rails webpacker:install:typescript
10
- yarn add @types/react @types/react-dom
11
10
  ```
12
11
 
13
12
  2. Rename the generated `hello_react.js` to `hello_react.tsx`. Make the file valid typescript and
@@ -22,35 +21,19 @@ bundle exec rails webpacker:install:typescript
22
21
  ```
23
22
 
24
23
  2. Rename generated `hello_vue.js` to `hello_vue.ts`.
25
- 3. Add the webpack plug-n-play plugin to your yarn packages with `yarn add pnp-webpack-plugin`.
26
- 4. Change the generated `config/webpack/loaders/typescript.js` from
24
+ 3. Change the generated `babel.config.js` from
27
25
 
28
26
  ```js
29
- module.exports = {
30
- test: /\.(ts|tsx)?(\.erb)?$/,
31
- use: [{
32
- loader: 'ts-loader'
33
- }]
34
- }
27
+ ["@babel/preset-typescript", { "allExtensions": true, "isTSX": true }]
35
28
  ```
36
29
 
37
30
  to
38
31
 
39
32
  ```js
40
- const PnpWebpackPlugin = require('pnp-webpack-plugin');
41
-
42
- module.exports = {
43
- test: /\.(ts|tsx)?(\.erb)?$/,
44
- use: [{
45
- loader: 'ts-loader',
46
- options: PnpWebpackPlugin.tsLoaderOptions({
47
- appendTsSuffixTo: [/\.vue$/]
48
- })
49
- }]
50
- }
33
+ ["babel-preset-typescript-vue", { "allExtensions": true, "isTSX": true }]
51
34
  ```
52
35
 
53
- and now you can use `<script lang="ts">` in your `.vue` component files. See [the pnp-webpack-plugin docs for the `ts-loader` integration](https://github.com/arcanis/pnp-webpack-plugin#ts-loader-integration) for more info.
36
+ 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.
54
37
 
55
38
  ## HTML templates with Typescript and Angular
56
39
 
@@ -6,7 +6,6 @@ default: &default
6
6
  public_root_path: public
7
7
  public_output_path: packs
8
8
  cache_path: tmp/cache/webpacker
9
- check_yarn_integrity: false
10
9
  webpack_compile_output: true
11
10
 
12
11
  # Additional paths webpack should lookup modules
@@ -52,9 +51,6 @@ development:
52
51
  <<: *default
53
52
  compile: true
54
53
 
55
- # Verifies that correct packages and versions are installed by inspecting package.json, yarn.lock, and node_modules
56
- check_yarn_integrity: true
57
-
58
54
  # Reference: https://webpack.js.org/configuration/dev-server/
59
55
  dev_server:
60
56
  https: false
@@ -8,7 +8,8 @@
8
8
  "moduleResolution": "node",
9
9
  "sourceMap": true,
10
10
  "target": "es5",
11
- "jsx": "react"
11
+ "jsx": "react",
12
+ "noEmit": true
12
13
  },
13
14
  "exclude": [
14
15
  "**/*.spec.ts",
@@ -11,7 +11,8 @@
11
11
  "*": ["node_modules/*", "app/javascript/*"]
12
12
  },
13
13
  "sourceMap": true,
14
- "target": "es5"
14
+ "target": "es5",
15
+ "noEmit": true
15
16
  },
16
17
  "exclude": [
17
18
  "**/*.spec.ts",
@@ -1,9 +1,9 @@
1
1
  module.exports = {
2
- test: /\.svelte$/,
2
+ test: /\.svelte(\.erb)?$/,
3
3
  use: [{
4
4
  loader: 'svelte-loader',
5
5
  options: {
6
- hotReload: true
6
+ hotReload: false
7
7
  }
8
8
  }],
9
9
  }