shakapacker 6.0.0.rc.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (147) hide show
  1. checksums.yaml +7 -0
  2. data/.eslintignore +4 -0
  3. data/.eslintrc.js +14 -0
  4. data/.github/ISSUE_TEMPLATE/bug_report.md +20 -0
  5. data/.github/ISSUE_TEMPLATE/feature-request.md +18 -0
  6. data/.github/workflows/jest.yml +30 -0
  7. data/.github/workflows/js-lint.yml +31 -0
  8. data/.github/workflows/rubocop.yml +39 -0
  9. data/.github/workflows/ruby.yml +48 -0
  10. data/.gitignore +13 -0
  11. data/.node-version +1 -0
  12. data/.rubocop.yml +229 -0
  13. data/CHANGELOG.md +32 -0
  14. data/CONTRIBUTING.md +62 -0
  15. data/Gemfile +13 -0
  16. data/Gemfile.lock +183 -0
  17. data/MIT-LICENSE +20 -0
  18. data/README.md +666 -0
  19. data/Rakefile +11 -0
  20. data/config/README.md +3 -0
  21. data/config/webpacker.yml +1 -0
  22. data/docs/customizing_babel_config.md +59 -0
  23. data/docs/deployment.md +116 -0
  24. data/docs/developing_webpacker.md +29 -0
  25. data/docs/troubleshooting.md +212 -0
  26. data/docs/v6_upgrade.md +158 -0
  27. data/gemfiles/Gemfile-rails-edge +12 -0
  28. data/gemfiles/Gemfile-rails.5.2.x +9 -0
  29. data/gemfiles/Gemfile-rails.6.0.x +9 -0
  30. data/gemfiles/Gemfile-rails.6.1.x +12 -0
  31. data/lib/install/application.js +15 -0
  32. data/lib/install/bin/webpacker +15 -0
  33. data/lib/install/bin/webpacker-dev-server +18 -0
  34. data/lib/install/bin/yarn +18 -0
  35. data/lib/install/binstubs.rb +4 -0
  36. data/lib/install/config/webpack/webpack.config.js +5 -0
  37. data/lib/install/config/webpacker.yml +64 -0
  38. data/lib/install/package.json +15 -0
  39. data/lib/install/template.rb +100 -0
  40. data/lib/shakapacker/utils/git_utils.rb +23 -0
  41. data/lib/shakapacker/utils/version_syntax_converter.rb +24 -0
  42. data/lib/tasks/webpacker/binstubs.rake +15 -0
  43. data/lib/tasks/webpacker/check_binstubs.rake +12 -0
  44. data/lib/tasks/webpacker/check_node.rake +31 -0
  45. data/lib/tasks/webpacker/check_yarn.rake +33 -0
  46. data/lib/tasks/webpacker/clean.rake +25 -0
  47. data/lib/tasks/webpacker/clobber.rake +20 -0
  48. data/lib/tasks/webpacker/compile.rake +45 -0
  49. data/lib/tasks/webpacker/info.rake +21 -0
  50. data/lib/tasks/webpacker/install.rake +17 -0
  51. data/lib/tasks/webpacker/verify_config.rake +14 -0
  52. data/lib/tasks/webpacker/verify_install.rake +4 -0
  53. data/lib/tasks/webpacker/yarn_install.rake +18 -0
  54. data/lib/tasks/webpacker.rake +19 -0
  55. data/lib/tasks/yarn.rake +38 -0
  56. data/lib/webpacker/commands.rb +79 -0
  57. data/lib/webpacker/compiler.rb +130 -0
  58. data/lib/webpacker/configuration.rb +111 -0
  59. data/lib/webpacker/dev_server.rb +72 -0
  60. data/lib/webpacker/dev_server_proxy.rb +33 -0
  61. data/lib/webpacker/dev_server_runner.rb +96 -0
  62. data/lib/webpacker/env.rb +43 -0
  63. data/lib/webpacker/helper.rb +161 -0
  64. data/lib/webpacker/instance.rb +41 -0
  65. data/lib/webpacker/manifest.rb +120 -0
  66. data/lib/webpacker/railtie.rb +63 -0
  67. data/lib/webpacker/runner.rb +23 -0
  68. data/lib/webpacker/version.rb +4 -0
  69. data/lib/webpacker/webpack_runner.rb +58 -0
  70. data/lib/webpacker.rb +46 -0
  71. data/package/__tests__/config.js +34 -0
  72. data/package/__tests__/dev_server.js +45 -0
  73. data/package/__tests__/development.js +35 -0
  74. data/package/__tests__/env.js +58 -0
  75. data/package/__tests__/index.js +9 -0
  76. data/package/__tests__/production.js +29 -0
  77. data/package/__tests__/staging.js +30 -0
  78. data/package/__tests__/test.js +25 -0
  79. data/package/babel/preset.js +41 -0
  80. data/package/config.js +32 -0
  81. data/package/configPath.js +3 -0
  82. data/package/dev_server.js +20 -0
  83. data/package/env.js +27 -0
  84. data/package/environments/__tests__/base.js +69 -0
  85. data/package/environments/base.js +116 -0
  86. data/package/environments/development.js +55 -0
  87. data/package/environments/production.js +79 -0
  88. data/package/environments/test.js +3 -0
  89. data/package/index.js +33 -0
  90. data/package/inliningCss.js +7 -0
  91. data/package/rules/babel.js +30 -0
  92. data/package/rules/coffee.js +6 -0
  93. data/package/rules/css.js +3 -0
  94. data/package/rules/erb.js +15 -0
  95. data/package/rules/file.js +23 -0
  96. data/package/rules/index.js +18 -0
  97. data/package/rules/less.js +22 -0
  98. data/package/rules/raw.js +5 -0
  99. data/package/rules/sass.js +16 -0
  100. data/package/rules/stylus.js +26 -0
  101. data/package/utils/get_style_rule.js +37 -0
  102. data/package/utils/helpers.js +51 -0
  103. data/package.json +71 -0
  104. data/rakelib/release.rake +57 -0
  105. data/test/command_test.rb +109 -0
  106. data/test/compiler_test.rb +68 -0
  107. data/test/configuration_test.rb +78 -0
  108. data/test/dev_server_runner_test.rb +81 -0
  109. data/test/dev_server_test.rb +47 -0
  110. data/test/engine_rake_tasks_test.rb +39 -0
  111. data/test/env_test.rb +23 -0
  112. data/test/helper_test.rb +159 -0
  113. data/test/manifest_test.rb +89 -0
  114. data/test/mounted_app/Rakefile +4 -0
  115. data/test/mounted_app/test/dummy/Rakefile +3 -0
  116. data/test/mounted_app/test/dummy/bin/rails +3 -0
  117. data/test/mounted_app/test/dummy/bin/rake +3 -0
  118. data/test/mounted_app/test/dummy/config/application.rb +10 -0
  119. data/test/mounted_app/test/dummy/config/environment.rb +3 -0
  120. data/test/mounted_app/test/dummy/config/webpacker.yml +75 -0
  121. data/test/mounted_app/test/dummy/config.ru +5 -0
  122. data/test/mounted_app/test/dummy/package.json +7 -0
  123. data/test/rake_tasks_test.rb +71 -0
  124. data/test/test_app/Rakefile +3 -0
  125. data/test/test_app/app/packs/entrypoints/application.js +10 -0
  126. data/test/test_app/app/packs/entrypoints/multi_entry.css +4 -0
  127. data/test/test_app/app/packs/entrypoints/multi_entry.js +4 -0
  128. data/test/test_app/bin/webpacker +14 -0
  129. data/test/test_app/bin/webpacker-dev-server +14 -0
  130. data/test/test_app/config/application.rb +11 -0
  131. data/test/test_app/config/environment.rb +4 -0
  132. data/test/test_app/config/initializers/inspect_autoload_paths.rb +1 -0
  133. data/test/test_app/config/webpack/webpack.config.js +0 -0
  134. data/test/test_app/config/webpacker.yml +77 -0
  135. data/test/test_app/config/webpacker_other_location.yml +79 -0
  136. data/test/test_app/config/webpacker_public_root.yml +18 -0
  137. data/test/test_app/config.ru +5 -0
  138. data/test/test_app/package.json +13 -0
  139. data/test/test_app/public/packs/manifest.json +50 -0
  140. data/test/test_app/some.config.js +0 -0
  141. data/test/test_app/yarn.lock +11 -0
  142. data/test/test_helper.rb +33 -0
  143. data/test/webpack_runner_test.rb +57 -0
  144. data/test/webpacker_test.rb +34 -0
  145. data/webpacker.gemspec +31 -0
  146. data/yarn.lock +4029 -0
  147. metadata +331 -0
@@ -0,0 +1,59 @@
1
+ # Customizing Babel Config
2
+
3
+ ## Default Configuration
4
+ The default configuration of babel is done by using `package.json` to use the file within the `@shakacode/shakapacker` package.
5
+
6
+ ```json
7
+ {
8
+ "babel": {
9
+ "presets": [
10
+ "./node_modules/@shakacode/shakapacker/package/babel/preset.js"
11
+ ]
12
+ }
13
+ }
14
+ ```
15
+
16
+ ## Customizing the Babel Config
17
+ This example shows how you can create an object and apply _additional_ presets and plugins on top of the default.
18
+
19
+ ### React Configuration
20
+ To use this example file,
21
+
22
+ ```
23
+ yarn add react react-dom @babel/preset-react
24
+ yarn add --dev @pmmmwh/react-refresh-webpack-plugin react-refresh
25
+ ```
26
+
27
+ ```js
28
+ // babel.config.js
29
+ module.exports = function (api) {
30
+ const defaultConfigFunc = require('@shakacode/shakapacker/package/babel/preset.js')
31
+ const resultConfig = defaultConfigFunc(api)
32
+ const isProductionEnv = api.env('production')
33
+
34
+ const changesOnDefault = {
35
+ presets: [
36
+ [
37
+ '@babel/preset-react',
38
+ {
39
+ development: isDevelopmentEnv || isTestEnv,
40
+ useBuiltIns: true
41
+ }
42
+ ],
43
+ isProductionEnv && ['babel-plugin-transform-react-remove-prop-types',
44
+ {
45
+ removeImport: true
46
+ }
47
+ ]
48
+ ].filter(Boolean),
49
+ plugins: [
50
+ process.env.WEBPACK_SERVE && 'react-refresh/babel'
51
+ ].filter(Boolean),
52
+ }
53
+
54
+ resultConfig.presets = [...resultConfig.presets, ...changesOnDefault.presets]
55
+ resultConfig.plugins = [...resultConfig.plugins, ...changesOnDefault.plugins ]
56
+
57
+ return resultConfig
58
+ }
59
+ ```
@@ -0,0 +1,116 @@
1
+ # Deployment
2
+
3
+ Webpacker hooks up a new `webpacker:compile` task to `assets:precompile`, which gets run whenever you run `assets:precompile`.
4
+ If you are not using Sprockets `webpacker:compile` is automatically aliased to `assets:precompile`.
5
+
6
+ ```
7
+
8
+ ## Heroku
9
+
10
+ In order for your Webpacker app to run on Heroku, you'll need to do a bit of configuration before hand.
11
+
12
+ ```bash
13
+ heroku create my-webpacker-heroku-app
14
+ heroku addons:create heroku-postgresql:hobby-dev
15
+ heroku buildpacks:add heroku/nodejs
16
+ heroku buildpacks:add heroku/ruby
17
+ git push heroku master
18
+ ```
19
+
20
+ We're essentially doing the following here:
21
+
22
+ * Creating an app on Heroku
23
+ * Creating a Postgres database for the app (this is assuming that you're using Heroku Postgres for your app)
24
+ * Adding the Heroku NodeJS and Ruby buildpacks for your app. This allows the `npm` or `yarn` executables to properly function when compiling your app - as well as Ruby.
25
+ * Pushing your code to Heroku and kicking off the deployment
26
+
27
+ ## Nginx
28
+
29
+ Webpacker doesn't serve anything in production. You’re expected to configure your web server to serve files in public/ directly.
30
+
31
+ Some servers support sending precompressed versions of files when they're available. For example, nginx offers a `gzip_static` directive that serves files with the `.gz` extension to supported clients. With an optional module, nginx can also serve Brotli compressed files with the `.br` extension (see below for installation and configuration instructions).
32
+
33
+ Here's a sample nginx site config for a Rails app using Webpacker:
34
+
35
+ ```nginx
36
+ upstream app {
37
+ # server unix:///path/to/app/tmp/puma.sock;
38
+ }
39
+
40
+ server {
41
+ listen 80;
42
+ server_name www.example.com;
43
+ root /path/to/app/public;
44
+
45
+ location @app {
46
+ proxy_pass http://app;
47
+ proxy_redirect off;
48
+
49
+ proxy_set_header Host $host;
50
+ proxy_set_header X-Real-IP $remote_addr;
51
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
52
+ proxy_set_header X-Forwarded-Proto $scheme;
53
+ }
54
+
55
+ location / {
56
+ try_files $uri @app;
57
+ }
58
+
59
+ location = /favicon.ico { access_log off; log_not_found off; }
60
+ location = /robots.txt { access_log off; log_not_found off; }
61
+
62
+ location ~ /\.(?!well-known).* {
63
+ deny all;
64
+ }
65
+
66
+ location ~ ^/(assets|packs)/ {
67
+ gzip_static on;
68
+ brotli_static on; # Optional, see below
69
+ expires max;
70
+ add_header Cache-Control public;
71
+ }
72
+ }
73
+ ```
74
+
75
+ ### Installing the ngx_brotli module
76
+
77
+ If you want to serve Brotli compressed files with nginx, you will need to install the `nginx_brotli` module. Installation instructions from source can be found in the official [google/ngx_brotli](https://github.com/google/ngx_brotli) git repository. Alternatively, depending on your platform, the module might be available via a pre-compiled package.
78
+
79
+ Once installed, you need to load the module. As we want to serve the pre-compressed files, we only need the static module. Add the following line to your `nginx.conf` file and reload nginx:
80
+
81
+ ```
82
+ load_module modules/ngx_http_brotli_static_module.so;
83
+ ```
84
+
85
+ Now, you can set `brotli_static on;` in your nginx site config, as per the config in the last section above.
86
+
87
+ ## CDN
88
+
89
+ Webpacker out-of-the-box provides CDN support using your Rails app `config.action_controller.asset_host` setting. If you already have [CDN](http://guides.rubyonrails.org/asset_pipeline.html#cdns) added in your Rails app
90
+ you don't need to do anything extra for Webpacker, it just works.
91
+
92
+ ## Capistrano
93
+
94
+ ### Assets compiling on every deployment even if JavaScript and CSS files are not changed
95
+
96
+ Make sure you have `public/packs` and `node_modules` in `:linked_dirs`
97
+
98
+ ```ruby
99
+ append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "public/packs", ".bundle", "node_modules"
100
+ ```
101
+
102
+ If you have `node_modules` added to `:linked_dirs` you'll need to run yarn install before `deploy:assets:precompile`, so you can add this code snippet at the bottom deploy.rb
103
+
104
+ ```ruby
105
+ before "deploy:assets:precompile", "deploy:yarn_install"
106
+ namespace :deploy do
107
+ desc "Run rake yarn install"
108
+ task :yarn_install do
109
+ on roles(:web) do
110
+ within release_path do
111
+ execute("cd #{release_path} && yarn install --silent --no-progress --no-audit --no-optional")
112
+ end
113
+ end
114
+ end
115
+ end
116
+ ```
@@ -0,0 +1,29 @@
1
+ # Developing Webpacker
2
+
3
+ It's a little trickier for Rails developers to work on the JS code of a project like shakacode/shakapacker. So here are some tips!
4
+
5
+ ## Use some test app
6
+ For example, for React on Rails Changes, I'm using [shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh](https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh).
7
+ This directory is the `TEST_APP_DIR`.
8
+
9
+ ## Fork shakacode/shakapacker
10
+ Let's call the shakacode/shakapacker directory `WEBPACKER_DIR` which has shakacode/shakapacker's `package.json`.
11
+
12
+ ## Changing the Package
13
+ ### Setup with Yalc
14
+ Use [`yalc`](https://github.com/wclr/yalc) unless you like yak shaving weird errors.
15
+ 1. In `WEBPACKER_DIR`, run `yalc publish`
16
+ 2. In `TEST_APP_DIR`, run `yalc link @shakacode/shakapacker`
17
+
18
+ ## Update the Package Code
19
+ 1. Make some JS change in WEBPACKER_DIR
20
+ 2. Run `yalc push` and your changes will be pushed to your `TEST_APP_DIR`'s node_modules.
21
+ 3. You may need to run `yarn` in `TEST_APP_DIR` if you added or removed dependencies of shakacode/shakapacker.
22
+
23
+ ## Updating the Ruby Code
24
+
25
+ For the Ruby part, just change the gem reference `TEST_APP_DIR`, like:
26
+
27
+ ```ruby
28
+ gem "webpacker", path: "../../forks/webpacker"
29
+ ```
@@ -0,0 +1,212 @@
1
+ # Troubleshooting
2
+
3
+ ## Debugging your webpack config
4
+
5
+ 1. Read the error message carefully. The error message will tell you the precise key value
6
+ that is not matching what Webpack expects.
7
+
8
+ 2. Put a `debugger` statement in your Webpack configuration and run `bin/webpacker --debug-webpacker`.
9
+ If you have a node debugger installed, you'll see the Chrome debugger for your webpack
10
+ config. For example, install the Chrome extension
11
+ [NiM](https://chrome.google.com/webstore/detail/nodejs-v8-inspector-manag/gnhhdgbaldcilmgcpfddgdbkhjohddkj) and
12
+ set the option for the dev tools to open automatically. Or put `chrome://inspect` in the URL bar.
13
+ For more details on debugging, see the official
14
+ [Webpack docs on debugging](https://webpack.js.org/contribute/debugging/#devtools)
15
+
16
+ 3. Any arguments that you add to bin/webpacker get sent to webpack. For example, you can pass `--debug` to switch loaders to debug mode. See [webpack CLI debug options](https://webpack.js.org/api/cli/#debug-options) for more information on the available options.
17
+
18
+ 4. You can also pass additional options to the command to run the webpack-dev-server and start the webpack-dev-server with the option `--debug-webpacker`
19
+
20
+ ## ENOENT: no such file or directory - node-sass
21
+
22
+ If you get the error `ENOENT: no such file or directory - node-sass` on deploy with
23
+ `assets:precompile` or `bundle exec rails webpacker:compile` you may need to
24
+ move Sass to production `dependencies`.
25
+
26
+ Move any packages that related to Sass (e.g. `node-sass` or `sass-loader`) from
27
+ `devDependencies` to `dependencies` in `package.json`. This is because
28
+ webpacker is running on a production system with the Rails workflow to build
29
+ the assets. Particularly on hosting providers that try to detect and do the right
30
+ thing, like Heroku.
31
+
32
+ However, if you get this on local development, or not during a deploy then you
33
+ may need to rebuild `node-sass`. It's a bit of a weird error; basically, it
34
+ can't find the `node-sass` binary. An easy solution is to create a postinstall
35
+ hook to ensure `node-sass` is rebuilt whenever new modules are installed.
36
+
37
+ In `package.json`:
38
+
39
+ ```json
40
+ "scripts": {
41
+ "postinstall": "npm rebuild node-sass"
42
+ }
43
+ ```
44
+
45
+ ## Can't find hello_react.js in manifest.json
46
+
47
+ * If you get this error `Can't find hello_react.js in manifest.json`
48
+ when loading a view in the browser it's because webpack is still compiling packs.
49
+ Webpacker uses a `manifest.json` file to keep track of packs in all environments,
50
+ however since this file is generated after packs are compiled by webpack. So,
51
+ if you load a view in browser whilst webpack is compiling you will get this error.
52
+ Therefore, make sure webpack
53
+ (i.e `./bin/webpacker-dev-server`) is running and has
54
+ completed the compilation successfully before loading a view.
55
+
56
+
57
+ ## throw er; // Unhandled 'error' event
58
+
59
+ * If you get this error while trying to use Elm, try rebuilding Elm. You can do
60
+ so with a postinstall hook in your `package.json`:
61
+
62
+ ```json
63
+ "scripts": {
64
+ "postinstall": "npm rebuild elm"
65
+ }
66
+ ```
67
+
68
+ ## webpack or webpack-dev-server not found
69
+
70
+ * This could happen if `webpacker:install` step is skipped. Please run `bundle exec rails webpacker:install` to fix the issue.
71
+
72
+ * If you encounter the above error on heroku after upgrading from Rails 4.x to 5.1.x, then the problem might be related to missing `yarn` binstub. Please run following commands to update/add binstubs:
73
+
74
+ ```bash
75
+ bundle config --delete bin
76
+ ./bin/rails app:update:bin # or rails app:update:bin
77
+ ```
78
+
79
+ ## Running webpack on Windows
80
+
81
+ If you are running webpack on Windows, your command shell may not be able to interpret the preferred interpreter
82
+ for the scripts generated in `bin/webpacker` and `bin/webpacker-dev-server`. Instead you'll want to run the scripts
83
+ manually with Ruby:
84
+
85
+ ```
86
+ C:\path>ruby bin\webpack
87
+ C:\path>ruby bin\webpack-dev-server
88
+ ```
89
+
90
+ ## Invalid configuration object. webpack has been initialised using a configuration object that does not match the API schema.
91
+
92
+ If you receive this error when running `$ ./bin/webpacker-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`.
93
+
94
+ ## Running Elm on Continuous Integration (CI) services such as CircleCI, CodeShip, Travis CI
95
+
96
+ 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)
97
+
98
+ 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.
99
+
100
+ Basic fix involves:
101
+
102
+ ```bash
103
+ # install sysconfcpus on CI
104
+
105
+ git clone https://github.com/obmarg/libsysconfcpus.git $HOME/dependencies/libsysconfcpus
106
+ cd libsysconfcpus
107
+ .configure --prefix=$HOME/dependencies/sysconfcpus
108
+ make && make install
109
+
110
+ # use sysconfcpus with elm-make
111
+ mv $HOME/your_rails_app/node_modules/.bin/elm-make $HOME/your_rails_app/node_modules/.bin/elm-make-old
112
+ 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
113
+ chmod +x $HOME/your_rails_app/node_modules/.bin/elm-make
114
+ ```
115
+
116
+ ## Rake assets:precompile fails. ExecJS::RuntimeError
117
+ This error occurs because you are trying to minify by `terser` 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:
118
+
119
+ ```ruby
120
+ # In production.rb
121
+
122
+ # From
123
+ Rails.application.config.assets.js_compressor = :uglifier
124
+
125
+ # To
126
+ Rails.application.config.assets.js_compressor = Uglifier.new(harmony: true)
127
+ ```
128
+
129
+ ### Angular: WARNING in ./node_modules/@angular/core/esm5/core.js, Critical dependency: the request of a dependency is an expression
130
+
131
+ To silent these warnings, please update `config/webpack/webpack.config.js`:
132
+ ```js
133
+ const webpack = require('webpack')
134
+ const { resolve } = require('path')
135
+ const { webpackConfig, merge } = require('@shakacode/shakapacker')
136
+
137
+ module.exports = merge(webpackConfig, {
138
+ plugins: [
139
+ new webpack.ContextReplacementPlugin(
140
+ /angular(\\|\/)core(\\|\/)(@angular|esm5)/,
141
+ resolve(config.source_path)
142
+ )
143
+ ]
144
+ })
145
+ ```
146
+
147
+ ### Compilation Fails Silently
148
+
149
+ If compiling is not producing output files and there are no error messages to help troubleshoot. Setting the `webpack_compile_output` configuration variable to `true` in webpacker.yml may add some helpful error information to your log file (Rails `log/development.log` or `log/production.log`)
150
+
151
+ ```yml
152
+ # webpacker.yml
153
+ default: &default
154
+ source_path: app/javascript
155
+ source_entry_path: packs
156
+ public_root_path: public
157
+ public_output_path: complaints_packs
158
+ webpack_compile_output: true
159
+ ```
160
+
161
+ ### Using global variables for dependencies
162
+
163
+ If you want to access any dependency without importing it everywhere or use it directly in your dev tools, please check: [https://webpack.js.org/plugins/provide-plugin/](https://webpack.js.org/plugins/provide-plugin/) and the [webpack docs on shimming globals](https://webpack.js.org/guides/shimming/#shimming-globals).
164
+
165
+ Note, if you are exposing globals, like jQuery, to non-webpack dependencies (like an inline script) via the [expose-loader](https://webpack.js.org/loaders/expose-loader/), you will need to override the default of `defer: true` to be `defer:false` your call to the `javascript_pack_tag` so that the browser will load your bundle to setup the global variable before other code depends on it. However, you really should try to remove the dependendency on such globals.
166
+
167
+ Thus ProvidePlugin manages build-time dependencies to global symbols whereas the expose-loader manages runtime dependencies to global symbols.
168
+
169
+ **You don't need to assign dependencies on `window`.**
170
+
171
+ For instance, with [jQuery](https://jquery.com/):
172
+ ```diff
173
+ // app/packs/entrypoints/application.js
174
+
175
+ - import jQuery from 'jquery'
176
+ - window.jQuery = jQuery
177
+ ```
178
+
179
+ Instead do:
180
+ ```js
181
+ // config/webpack/webpack.config.js
182
+
183
+ const webpack = require('webpack')
184
+ const { webpackConfig, merge } = require('@shakacode/shakapacker')
185
+
186
+ module.exports = merge(webpackConfig, {
187
+ plugins: [
188
+ new webpack.ProvidePlugin({
189
+ $: 'jquery',
190
+ jQuery: 'jquery',
191
+ })
192
+ ],
193
+ })
194
+ ```
195
+
196
+ ## Wrong CDN src from javascript_pack_tag
197
+
198
+ If your deployment doesn't rebuild assets between environments (such as when
199
+ using Heroku's Pipeline promote feature). You might find that your production
200
+ application is using your staging `config.asset_host` host when using
201
+ `javascript_pack_tag`.
202
+
203
+ This can be fixed by setting the environment variable `WEBPACKER_ASSET_HOST` to
204
+ an empty string where your assets are compiled. On Heroku this is done under
205
+ *Settings* -> *Config Vars*.
206
+
207
+ This way webpacker won't hard-code the CDN host into the manifest file used by
208
+ `javascript_pack_tag`, but instead fetch the CDN host at runtime, resolving the
209
+ issue.
210
+
211
+ See [this issue](https://github.com/rails/webpacker/issues/3005) for more
212
+ details.
@@ -0,0 +1,158 @@
1
+ # Upgrading from Webpacker 5 to 6
2
+
3
+ There are several substantial changes in Webpacker 6 that you need to manually account for when coming from Webpacker 5. This guide will help you through it.
4
+
5
+ ## Webpacker has become a slimmer wrapper around Webpack
6
+
7
+ By default, Webpacker 6 is focused on compiling and bundling JavaScript. This pairs with the existing asset pipeline in Rails that's setup to transpile CSS and static images using [Sprockets](https://github.com/rails/sprockets). For most developers, that's the recommended combination. But if you'd like to use Webpacker for CSS and static assets as well, please see [integrations](https://github.com/shakacode/shakapacker#integrations) for more information.
8
+
9
+ Webpacker used to configure Webpack indirectly, which lead to a [complicated secondary configuration process](https://github.com/rails/webpacker/blob/5-x-stable/docs/webpack.md). This was done in order to provide default configurations for the most popular frameworks, but ended up creating more complexity than it cured. So now Webpacker delegates all configuration directly to Webpack's default configuration setup.
10
+
11
+ This means you have to configure integration with frameworks yourself, but webpack-merge helps with this. See this example for [Vue](https://github.com/shakacode/shakapacker#other-frameworks) and scroll to the bottom for [more examples](#examples-of-v5-to-v6).
12
+
13
+ ## How to upgrade to Webpacker v6 from v5
14
+ 1. Ensure you have a clean working git branch. You will be overwriting all your files and reverting the changes that you don't want.
15
+
16
+ 1. Consider changing from the v5 default for `source_entry_path` in `webpacker.yml`.
17
+ ```yml
18
+ source_path: app/javascript
19
+ source_entry_path: packs
20
+ ```
21
+ consider changing to the v6 default:
22
+ ```yml
23
+ source_path: app/javascript
24
+ source_entry_path: /
25
+ ```
26
+ Then consider moving your `app/javascript/packs/*` (including `application.js`) to `app/javascript/` and updating the configuration file.
27
+
28
+ Note, moving your files is optional, as you can stil keep your entries in a separate directory, called something like `packs`, or `entries`. This directory is defined within the source_path.
29
+
30
+ 1. **Ensure no nested directories in your `source_entry_path`.** Check if you had any entry point files in child directories of your `source_entry_path`. Files for entry points in child directories are not supported by shakacode/shakapacker v6. Move those files to the top level, adjusting any imports in those files.
31
+
32
+ The new v6 configuration does not allow nesting, so as to allow placing the entry points at in the root directory of JavaScript. You can find this change [here](https://github.com/rails/webpacker/commit/5de0fbc1e16d3db0c93202fb39f5b4d80582c682#diff-7af8667a3e36201db57c02b68dd8651883d7bfc00dc9653661be11cd31feeccdL19).
33
+
34
+ 1. Upgrade the Webpacker Ruby gem and the NPM package
35
+
36
+ Note: [Check the releases page to verify the latest version](https://github.com/rails/webpacker/releases), and make sure to install identical version numbers of webpacker gem and `@shakacode/shakapacker` npm package. (Gems use a period and packages use a dot between the main version number and the beta version.)
37
+
38
+ Example going to a specific version:
39
+
40
+ ```ruby
41
+ # Gemfile
42
+ gem 'webpacker', '6.0.0.rc.7'
43
+ ```
44
+
45
+ ```bash
46
+ bundle install
47
+ ```
48
+
49
+ ```bash
50
+ yarn add @shakacode/shakapacker@6.0.0-rc.7 --exact
51
+ ```
52
+
53
+ ```bash
54
+ bundle exec rails webpacker:install
55
+ ```
56
+
57
+ Overwrite all files and check what changed.
58
+
59
+ Note, the webpacker:install will install the peer dependencies:
60
+ ```bash
61
+ yarn add @babel/core @babel/plugin-transform-runtime @babel/preset-env @babel/runtime babel-loader compression-webpack-plugin pnp-webpack-plugin terser-webpack-plugin webpack webpack-assets-manifest webpack-cli webpack-merge webpack-sources webpack-dev-server
62
+ ```
63
+
64
+ 1. There is now a single default configuration file of `config/webpack/webpack.config.js`. Previously, the config file was set
65
+ to `config/webpack/#{NODE_ENV}.js`. In the `config/webpack/` directory, you can either refactor your code in `test.js`, `development.js`, and `production.js` to a single file, `webpack.config.js` or you can replace the contents of `config/webpack/config.webpack.js` to conditionally load the old file based on the NODE_ENV with this snippet:
66
+ ```js
67
+ const { env, webpackConfig } = require('@shakacode/shakapacker')
68
+ const { existsSync } = require('fs')
69
+ const { resolve } = require('path')
70
+
71
+ const envSpecificConfig = () => {
72
+ const path = resolve(__dirname, `${env.nodeEnv}.js`)
73
+ if (existsSync(path)) {
74
+ console.log(`Loading ENV specific webpack configuration file ${path}`)
75
+ return require(path)
76
+ } else {
77
+ return webpackConfig
78
+ }
79
+ }
80
+
81
+ module.exports = envSpecificConfig()
82
+ ```
83
+
84
+ 1. Review the new default's changes to `webpacker.yml`. Consider each suggested change carefully, especially the change to have your `source_entry_path` be at the top level of your `source_path`.
85
+ The v5 default used `packs` for `source_entry_path`:
86
+ ```yml
87
+ source_path: app/javascript
88
+ source_entry_path: packs
89
+ ```
90
+ The v6 default uses the top level directory.
91
+ ```yml
92
+ source_path: app/javascript
93
+ source_entry_path: /
94
+ ```
95
+ If you prefer this configuratiom, then you will move your `app/javascript/packs/*` (including `application.js`) to `app/javascript/` and update the configuration file.
96
+
97
+ Note, moving your files is optional, as you can stil keep your entries in a separate directory, called something like `packs`, or `entries`. This directory is defined with the `source_path`.
98
+
99
+ 1. **Ensure no nested directories in your `source_entry_path`.** Check if you had any entry point files in child directories of your `source_entry_path`. Files for entry points in child directories are not supported by shakacode/shakapacker v6. Move those files to the top level, adjusting any imports in those files.
100
+
101
+ The new v6 configuration does not allow nesting, so as to allow placing the entry points at in the root directory of JavaScript. You can find this change [here](https://github.com/rails/webpacker/commit/5de0fbc1e16d3db0c93202fb39f5b4d80582c682#diff-7af8667a3e36201db57c02b68dd8651883d7bfc00dc9653661be11cd31feeccdL19).
102
+
103
+ 1. Update `webpack-dev-server` to the current version, greater than 4.2, updating `package.json`.
104
+
105
+ 1. Update API usage of the view helpers by changing `javascript_packs_with_chunks_tag` and `stylesheet_packs_with_chunks_tag` to `javascript_pack_tag` and `stylesheet_pack_tag`. Ensure that your layouts and views will only have **at most one call** to `javascript_pack_tag` and **at most one call** to `stylesheet_pack_tag`. You can now pass multiple bundles to these view helper methods. If you fail to changes this, you may experience performance issues, and other bugs related to multiple copies of React, like [issue 2932](https://github.com/rails/webpacker/issues/2932). If you expose jquery globally with `expose-loader,` by using `import $ from "expose-loader?exposes=$,jQuery!jquery"` in your `app/javascript/application.js`, pass the option `defer: false` to your `javascript_pack_tag`.
106
+
107
+ 1. If you are using any integrations like `css`, `postcss`, `React` or `TypeScript`. Please see https://github.com/shakacode/shakapacker#integrations section on how they work in v6.
108
+
109
+ 1. Import `environment` changed to `webpackConfig`. For example, the new code looks like:
110
+ ```js
111
+ // config/webpack/webpack.config.js
112
+ const { webpackConfig, merge } = require('@shakacode/shakapacker')
113
+ const customConfig = require('./custom')
114
+
115
+ module.exports = merge(webpackConfig, customConfig)
116
+ ```
117
+
118
+ 1. Copy over custom browserlist config from `.browserslistrc` if it exists into the `"browserslist"` key in `package.json` and remove `.browserslistrc`.
119
+
120
+ 1. Remove `babel.config.js` if you never changed it. Configure your `package.json` to use the default:
121
+ ```json
122
+ "babel": {
123
+ "presets": [
124
+ "./node_modules/shakapacker/package/babel/preset.js"
125
+ ]
126
+ }
127
+ ```
128
+ See customization example the [Customizing Babel Config](./docs/customizing_babel_config.md) for React configuration.
129
+
130
+ 1. `extensions` was removed from the `webpacker.yml` file. Move custom extensions to your configuration by merging an object like this. For more details, see docs for [Webpack Configuration](https://github.com/shakacode/shakapacker/blob/master/README.md#webpack-configuration)
131
+
132
+ ```js
133
+ {
134
+ resolve: {
135
+ extensions: ['.ts', '.tsx', '.vue', '.css']
136
+ }
137
+ }
138
+ ```
139
+ 1. In `webpacker.yml`, check if you had `watched_paths`. That is not `additional_paths`.
140
+
141
+ 1. Some dependencies were removed in [PR 3056](https://github.com/rails/webpacker/pull/3056). If you see the error: `Error: Cannot find module 'babel-plugin-macros'`, or similar, then you need to `yarn add <dependency>` where <dependency> might include: `babel-plugin-macros`, `case-sensitive-paths-webpack-plugin`, `core-js`, `regenerator-runtime`. Or you might want to remove your dependency on those.
142
+
143
+ 1. Review the new default's changes to `webpacker.yml` and `config/webpack`. Consider each suggested change carefully, especially the change to have your `source_entry_path` be at the top level of your `source_path`.
144
+
145
+ 1. Make sure that you can run `bin/webpacker` without errors.
146
+
147
+ 1. Try running `RAILS_ENV=production bin/rails assets:precompile`. If all goes well, don't forget to clean the generated assets with `bin/rails assets:clobber`.
148
+
149
+ 1. Run `yarn add webpack-dev-server` if those are not already in your dev dependencies. Make sure you're using v4+.
150
+
151
+ 1. Update any scripts that called `/bin/webpack` or `bin/webpack-dev-server` to `/bin/webpacker` or `bin/webpacker-dev-server`
152
+
153
+ 1. Try your app!
154
+
155
+ ## Examples of v5 to v6
156
+
157
+ 1. [React on Rails Project with HMR and SSR](https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh/compare/webpacker-5.x...master)
158
+ 2. [Vue and Sass Example](https://github.com/guillaumebriday/upgrade-webpacker-5-to-6)
@@ -0,0 +1,12 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
4
+
5
+ gemspec path: "../"
6
+
7
+ gem "rails", github: "rails/rails", branch: "main"
8
+ gem "arel", github: "rails/arel"
9
+ gem "rake", ">= 11.1"
10
+ gem "rack-proxy", require: false
11
+ gem "minitest", "~> 5.0"
12
+ gem "byebug"
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec path: "../"
4
+
5
+ gem "rails", "~> 5.2.0"
6
+ gem "rake", ">= 11.1"
7
+ gem "rack-proxy", require: false
8
+ gem "minitest", "~> 5.0"
9
+ gem "byebug"
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec path: "../"
4
+
5
+ gem "rails", "~> 6.0.0.rc2"
6
+ gem "rake", ">= 11.1"
7
+ gem "rack-proxy", require: false
8
+ gem "minitest", "~> 5.0"
9
+ gem "byebug"
@@ -0,0 +1,12 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
4
+
5
+ gemspec path: "../"
6
+
7
+ gem "rails", '~>6.1.0'
8
+ gem "arel", github: "rails/arel"
9
+ gem "rake", ">= 11.1"
10
+ gem "rack-proxy", require: false
11
+ gem "minitest", "~> 5.0"
12
+ gem "byebug"
@@ -0,0 +1,15 @@
1
+ /* eslint no-console:0 */
2
+ // This file is automatically compiled by Webpack, along with any other files
3
+ // present in this directory. You're encouraged to place your actual application logic in
4
+ // a relevant structure within app/javascript and only use these pack files to reference
5
+ // that code so it'll be compiled.
6
+ //
7
+ // To reference this file, add <%= javascript_pack_tag 'application' %> to the appropriate
8
+ // layout file, like app/views/layouts/application.html.erb
9
+
10
+ // Uncomment to copy all static images under ./images to the output folder and reference
11
+ // them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>)
12
+ // or the `imagePath` JavaScript helper below.
13
+ //
14
+ // const images = require.context('./images', true)
15
+ // const imagePath = (name) => images(name, true)
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "pathname"
4
+ require "bundler/setup"
5
+ require "webpacker"
6
+ require "webpacker/webpack_runner"
7
+
8
+ ENV["RAILS_ENV"] ||= "development"
9
+ ENV["NODE_ENV"] ||= ENV["RAILS_ENV"]
10
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", Pathname.new(__FILE__).realpath)
11
+
12
+ APP_ROOT = File.expand_path("..", __dir__)
13
+ Dir.chdir(APP_ROOT) do
14
+ Webpacker::WebpackRunner.run(ARGV)
15
+ end