shakapacker 6.5.6 → 7.0.0.rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.eslintrc.js +1 -0
- data/.github/FUNDING.yml +1 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +18 -0
- data/.github/workflows/ruby-backward-compatibility.yml +43 -0
- data/CHANGELOG.md +56 -8
- data/CONTRIBUTING.md +4 -4
- data/Gemfile.development_dependencies +1 -1
- data/README.md +113 -88
- data/Rakefile +5 -0
- data/config/shakapacker.yml +1 -0
- data/docs/deployment.md +12 -10
- data/docs/developing_webpacker.md +5 -5
- data/docs/react.md +10 -6
- data/docs/style_loader_vs_mini_css.md +2 -2
- data/docs/troubleshooting.md +22 -18
- data/docs/using_esbuild_loader.md +5 -3
- data/docs/using_swc_loader.md +2 -2
- data/docs/v6_upgrade.md +3 -3
- data/docs/v7_upgrade.md +56 -0
- data/lib/install/bin/shakapacker +13 -0
- data/lib/install/bin/shakapacker-dev-server +13 -0
- data/lib/install/config/{webpacker.yml → shakapacker.yml} +13 -8
- data/lib/install/template.rb +7 -7
- data/lib/{webpacker → shakapacker}/base_strategy.rb +2 -2
- data/lib/{webpacker → shakapacker}/commands.rb +4 -4
- data/lib/{webpacker → shakapacker}/compiler.rb +43 -15
- data/lib/{webpacker → shakapacker}/compiler_strategy.rb +6 -6
- data/lib/{webpacker → shakapacker}/configuration.rb +52 -21
- data/lib/shakapacker/deprecation_helper.rb +88 -0
- data/lib/{webpacker → shakapacker}/dev_server.rb +27 -4
- data/lib/{webpacker → shakapacker}/dev_server_proxy.rb +4 -4
- data/lib/shakapacker/dev_server_runner.rb +104 -0
- data/lib/{webpacker → shakapacker}/digest_strategy.rb +6 -6
- data/lib/{webpacker → shakapacker}/env.rb +8 -8
- data/lib/{webpacker → shakapacker}/helper.rb +20 -20
- data/lib/shakapacker/instance.rb +50 -0
- data/lib/{webpacker → shakapacker}/manifest.rb +14 -14
- data/lib/{webpacker → shakapacker}/mtime_strategy.rb +5 -5
- data/lib/shakapacker/railtie.rb +70 -0
- data/lib/shakapacker/runner.rb +28 -0
- data/lib/shakapacker/utils/version_syntax_converter.rb +2 -2
- data/lib/{webpacker → shakapacker}/version.rb +2 -2
- data/lib/{webpacker → shakapacker}/version_checker.rb +8 -8
- data/lib/shakapacker/webpack_runner.rb +67 -0
- data/lib/shakapacker.rb +51 -1
- data/lib/tasks/shakapacker/binstubs.rake +15 -0
- data/lib/tasks/shakapacker/check_binstubs.rake +25 -0
- data/lib/tasks/shakapacker/check_node.rake +31 -0
- data/lib/tasks/shakapacker/check_yarn.rake +33 -0
- data/lib/tasks/shakapacker/clean.rake +23 -0
- data/lib/tasks/shakapacker/clobber.rake +18 -0
- data/lib/tasks/shakapacker/compile.rake +31 -0
- data/lib/tasks/shakapacker/info.rake +21 -0
- data/lib/tasks/shakapacker/install.rake +17 -0
- data/lib/tasks/shakapacker/verify_config.rake +12 -0
- data/lib/tasks/shakapacker/verify_install.rake +4 -0
- data/lib/tasks/shakapacker/yarn_install.rake +24 -0
- data/lib/tasks/shakapacker.rake +18 -0
- data/lib/tasks/webpacker/binstubs.rake +5 -11
- data/lib/tasks/webpacker/check_binstubs.rake +6 -9
- data/lib/tasks/webpacker/check_node.rake +5 -27
- data/lib/tasks/webpacker/check_yarn.rake +5 -29
- data/lib/tasks/webpacker/clean.rake +5 -19
- data/lib/tasks/webpacker/clobber.rake +6 -13
- data/lib/tasks/webpacker/compile.rake +5 -33
- data/lib/tasks/webpacker/info.rake +5 -17
- data/lib/tasks/webpacker/install.rake +5 -13
- data/lib/tasks/webpacker/verify_config.rake +6 -11
- data/lib/tasks/webpacker/verify_install.rake +7 -2
- data/lib/tasks/webpacker/yarn_install.rake +5 -20
- data/lib/tasks/webpacker.rake +15 -13
- data/lib/webpacker/dev_server_runner.rb +9 -96
- data/lib/webpacker/webpack_runner.rb +9 -58
- data/lib/webpacker.rb +3 -47
- data/package/__tests__/config-bc.js +27 -0
- data/package/__tests__/config.js +6 -5
- data/package/__tests__/dev_server-bc.js +46 -0
- data/package/__tests__/dev_server.js +9 -8
- data/package/__tests__/development-bc.js +66 -0
- data/package/__tests__/development.js +36 -5
- data/package/__tests__/env-bc.js +59 -0
- data/package/__tests__/env.js +3 -2
- data/package/__tests__/index.js +13 -0
- data/package/__tests__/production-bc.js +51 -0
- data/package/__tests__/production.js +25 -3
- data/package/__tests__/staging-bc.js +53 -0
- data/package/__tests__/staging.js +27 -4
- data/package/__tests__/test-bc.js +43 -0
- data/package/__tests__/test.js +22 -4
- data/package/babel/preset.js +1 -4
- data/package/config.js +25 -7
- data/package/dev_server.js +7 -4
- data/package/env.js +22 -3
- data/package/environments/__tests__/base-bc.js +107 -0
- data/package/environments/__tests__/base.js +13 -13
- data/package/environments/base.js +1 -1
- data/package/environments/development.js +4 -46
- data/package/environments/production.js +1 -1
- data/package/index.js +11 -4
- data/package/rules/__tests__/__utils__/webpack.js +1 -1
- data/package/rules/__tests__/file.js +27 -0
- data/package/rules/__tests__/swc.js +1 -2
- data/package/rules/babel.js +11 -11
- data/package/rules/file.js +2 -2
- data/package/rules/jscommon.js +16 -16
- data/package/utils/configPath.js +19 -0
- data/package/utils/defaultConfigPath.js +2 -0
- data/package/utils/get_style_rule.js +5 -2
- data/package/utils/helpers.js +24 -1
- data/package/utils/inliningCss.js +7 -0
- data/package/utils/snakeToCamelCase +7 -0
- data/package/webpackDevServerConfig.js +68 -0
- data/package.json +2 -2
- data/{webpacker.gemspec → shakapacker.gemspec} +3 -3
- data/spec/backward_compatibility_specs/command_spec_bc.rb +116 -0
- data/spec/backward_compatibility_specs/compiler_spec_bc.rb +59 -0
- data/spec/backward_compatibility_specs/compiler_strategy_spec_bc.rb +22 -0
- data/spec/backward_compatibility_specs/configuration_spec_bc.rb +286 -0
- data/spec/backward_compatibility_specs/dev_server_runner_spec_bc.rb +79 -0
- data/spec/backward_compatibility_specs/dev_server_spec_bc.rb +47 -0
- data/spec/backward_compatibility_specs/digest_strategy_spec_bc.rb +35 -0
- data/spec/backward_compatibility_specs/engine_rake_tasks_spec_bc.rb +44 -0
- data/spec/backward_compatibility_specs/env_spec_bc.rb +23 -0
- data/spec/backward_compatibility_specs/helper_spec_bc.rb +243 -0
- data/spec/backward_compatibility_specs/instance_spec_bc.rb +31 -0
- data/spec/backward_compatibility_specs/manifest_spec_bc.rb +100 -0
- data/spec/backward_compatibility_specs/mtime_strategy_spec_bc.rb +55 -0
- data/spec/backward_compatibility_specs/rake_tasks_spec_bc.rb +37 -0
- data/spec/backward_compatibility_specs/spec_helper_initializer.rb +24 -0
- data/spec/backward_compatibility_specs/webpack_runner_spec_bc.rb +56 -0
- data/spec/backward_compatibility_specs/webpacker_spec_bc.rb +41 -0
- data/spec/backward_compatibility_specs/webpacker_test_app/.gitignore +2 -0
- data/spec/backward_compatibility_specs/webpacker_test_app/Rakefile +3 -0
- data/spec/{test_app → backward_compatibility_specs/webpacker_test_app}/app/packs/entrypoints/application.js +1 -1
- data/{lib/install → spec/backward_compatibility_specs/webpacker_test_app}/bin/webpacker +0 -0
- data/spec/backward_compatibility_specs/webpacker_test_app/config/application.rb +11 -0
- data/spec/backward_compatibility_specs/webpacker_test_app/config/environment.rb +4 -0
- data/spec/backward_compatibility_specs/webpacker_test_app/config/initializers/inspect_autoload_paths.rb +1 -0
- data/spec/backward_compatibility_specs/webpacker_test_app/config/webpack/webpack.config.js +0 -0
- data/spec/{test_app → backward_compatibility_specs/webpacker_test_app}/config/webpacker.yml +1 -1
- data/spec/{test_app → backward_compatibility_specs/webpacker_test_app}/config/webpacker_css_extract_ignore_order_warnings.yml +1 -1
- data/spec/{test_app → backward_compatibility_specs/webpacker_test_app}/config/webpacker_defaults_fallback.yml +1 -1
- data/spec/backward_compatibility_specs/webpacker_test_app/config/webpacker_no_precompile.yml +7 -0
- data/spec/{test_app → backward_compatibility_specs/webpacker_test_app}/config/webpacker_other_location.yml +1 -1
- data/spec/{test_app → backward_compatibility_specs/webpacker_test_app}/config/webpacker_public_root.yml +1 -1
- data/spec/backward_compatibility_specs/webpacker_test_app/config.ru +5 -0
- data/spec/backward_compatibility_specs/webpacker_test_app/package.json +13 -0
- data/spec/backward_compatibility_specs/webpacker_test_app/public/packs/manifest.json +58 -0
- data/spec/backward_compatibility_specs/webpacker_test_app/some.config.js +0 -0
- data/spec/backward_compatibility_specs/webpacker_test_app/tmp/shakapacker/last-compilation-digest-production +1 -0
- data/spec/backward_compatibility_specs/webpacker_test_app/yarn.lock +11 -0
- data/spec/command_spec.rb +18 -16
- data/spec/compiler_spec.rb +19 -17
- data/spec/compiler_strategy_spec.rb +9 -7
- data/spec/configuration_spec.rb +108 -66
- data/spec/dev_server_runner_spec.rb +9 -8
- data/spec/dev_server_spec.rb +97 -9
- data/spec/digest_strategy_spec.rb +4 -2
- data/spec/engine_rake_tasks_spec.rb +12 -16
- data/spec/env_spec.rb +7 -5
- data/spec/helper_spec.rb +4 -2
- data/spec/instance_spec.rb +31 -0
- data/spec/manifest_spec.rb +21 -19
- data/spec/mounted_app/test/dummy/config/application.rb +1 -1
- data/spec/mounted_app/test/dummy/config/webpacker.yml +3 -3
- data/spec/mtime_strategy_spec.rb +4 -2
- data/spec/rake_tasks_spec.rb +20 -18
- data/spec/spec_helper.rb +0 -25
- data/spec/spec_helper_initializer.rb +24 -0
- data/spec/test_app/.gitignore +2 -0
- data/spec/test_app/app/javascript/entrypoints/application.js +10 -0
- data/spec/test_app/app/javascript/entrypoints/generated/something.js +2 -0
- data/spec/test_app/app/javascript/entrypoints/multi_entry.css +4 -0
- data/spec/test_app/app/javascript/entrypoints/multi_entry.js +4 -0
- data/spec/test_app/bin/{webpacker-dev-server → shakapacker} +3 -3
- data/spec/test_app/bin/{webpacker → shakapacker-dev-server} +3 -3
- data/spec/test_app/config/application.rb +1 -1
- data/spec/test_app/config/shakapacker.yml +82 -0
- data/spec/test_app/config/shakapacker_css_extract_ignore_order_warnings.yml +84 -0
- data/spec/test_app/config/shakapacker_defaults_fallback.yml +11 -0
- data/spec/test_app/config/shakapacker_manifest_path.yml +80 -0
- data/spec/test_app/config/shakapacker_nested_entries.yml +83 -0
- data/spec/test_app/config/shakapacker_no_precompile.yml +7 -0
- data/spec/test_app/config/shakapacker_other_location.yml +85 -0
- data/spec/test_app/config/shakapacker_public_root.yml +18 -0
- data/spec/version_checker_spec.rb +16 -15
- data/spec/webpack_runner_spec.rb +4 -3
- data/spec/webpacker_spec.rb +9 -7
- metadata +121 -44
- data/config/webpacker.yml +0 -1
- data/lib/webpacker/instance.rb +0 -45
- data/lib/webpacker/railtie.rb +0 -70
- data/lib/webpacker/runner.rb +0 -23
- data/package/configPath.js +0 -3
- data/package/inliningCss.js +0 -7
- data/spec/test_app/config/webpacker_no_precompile.yml +0 -7
- /data/spec/{test_app → backward_compatibility_specs/webpacker_test_app}/app/packs/entrypoints/generated/something.js +0 -0
- /data/spec/{test_app → backward_compatibility_specs/webpacker_test_app}/app/packs/entrypoints/multi_entry.css +0 -0
- /data/spec/{test_app → backward_compatibility_specs/webpacker_test_app}/app/packs/entrypoints/multi_entry.js +0 -0
- /data/{lib/install → spec/backward_compatibility_specs/webpacker_test_app}/bin/webpacker-dev-server +0 -0
- /data/spec/{test_app → backward_compatibility_specs/webpacker_test_app}/config/webpacker_manifest_path.yml +0 -0
- /data/spec/{test_app → backward_compatibility_specs/webpacker_test_app}/config/webpacker_nested_entries.yml +0 -0
data/README.md
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
# Shakapacker
|
|
1
|
+
# Shakapacker (v7)
|
|
2
2
|
|
|
3
|
-
_Official, actively maintained successor to [rails/webpacker](https://github.com/rails/webpacker).
|
|
3
|
+
_Official, actively maintained successor to [rails/webpacker](https://github.com/rails/webpacker).ShakaCode stands behind the long-term maintenance and development of this project for the Rails community._
|
|
4
4
|
|
|
5
|
+
* ⚠️ See the [6-stable](https://github.com/shakacode/shakapacker/tree/6-stable) branch for Shakapacker v6.x code and documentation. :warning:
|
|
6
|
+
* See [V7 Upgrade](./docs/v7_upgrade.md) for upgrading from the v6 release.
|
|
5
7
|
* See [V6 Upgrade](./docs/v6_upgrade.md) for upgrading from v5 or prior v6 releases.
|
|
6
8
|
|
|
7
9
|
[](https://github.com/shakacode/shakapacker/actions)
|
|
@@ -15,18 +17,18 @@ _Official, actively maintained successor to [rails/webpacker](https://github.com
|
|
|
15
17
|
|
|
16
18
|
Shakpacker makes it easy to use the JavaScript pre-processor and bundler [Webpack v5+](https://webpack.js.org/)
|
|
17
19
|
to manage frontend JavaScript in Rails. It can coexist with the asset pipeline,
|
|
18
|
-
leaving Webpack responsible solely for frontend JavaScript, or
|
|
20
|
+
leaving Webpack responsible solely for frontend JavaScript, or can be used exclusively, making it also responsible for images, fonts, and CSS.
|
|
19
21
|
|
|
20
22
|
Check out 6.1.1+ for [SWC](https://swc.rs/) and [esbuild-loader](https://github.com/privatenumber/esbuild-loader) support! They are faster than Babel!
|
|
21
23
|
|
|
22
|
-
See a comparison of [Shakapacker with jsbundling-rails](https://github.com/rails/jsbundling-rails/blob/main/docs/comparison_with_webpacker.md).
|
|
24
|
+
See a comparison of [Shakapacker with jsbundling-rails](https://github.com/rails/jsbundling-rails/blob/main/docs/comparison_with_webpacker.md). For a in-depth discussion of choosing between `shakapacker` and `jsbundling-rails`, see the discussion [Webpacker alternatives - which path should we go to? #8783](https://github.com/decidim/decidim/discussions/8783) and the resulting PR [Switch away from Webpacker to Shakapacker #10389](https://github.com/decidim/decidim/pull/10389).
|
|
23
25
|
|
|
24
26
|
For discussions, see our [Slack Channel](https://reactrails.slack.com/join/shared_invite/enQtNjY3NTczMjczNzYxLTlmYjdiZmY3MTVlMzU2YWE0OWM0MzNiZDI0MzdkZGFiZTFkYTFkOGVjODBmOWEyYWQ3MzA2NGE1YWJjNmVlMGE).
|
|
25
27
|
|
|
26
28
|
---
|
|
27
29
|
### ShakaCode Support
|
|
28
30
|
|
|
29
|
-
[ShakaCode](https://www.shakacode.com) offers support for upgrading from
|
|
31
|
+
[ShakaCode](https://www.shakacode.com) offers support for upgrading from Webpacker and using Shakapacker. If interested, contact Justin Gordon, [justin@shakacode.com](mailto:justin@shakacode.com). We're also [hiring](https://jobs.lever.co/shakacode/3bdbfdb3-4495-4611-a279-01dddb351abe)!
|
|
30
32
|
|
|
31
33
|
Here's a testimonial of how ShakaCode can help, from [Florian Gößler](https://github.com/FGoessler) of [Blinkist](https://www.blinkist.com/), January 2, 2023:
|
|
32
34
|
> Hey Justin 👋
|
|
@@ -37,6 +39,8 @@ Here's a testimonial of how ShakaCode can help, from [Florian Gößler](https://
|
|
|
37
39
|
>
|
|
38
40
|
> Have a great 2023 and maybe we get to work together again later in the year! 🙌
|
|
39
41
|
|
|
42
|
+
Read the [full review here](https://clutch.co/profile/shakacode#reviews?sort_by=date_DESC#review-2118154). Here's [another review of a Shakapacker migration that led to more work](https://clutch.co/profile/shakacode#reviews?sort_by=date_DESC#review-2096078).
|
|
43
|
+
|
|
40
44
|
---
|
|
41
45
|
|
|
42
46
|
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
|
@@ -100,8 +104,8 @@ Here's a testimonial of how ShakaCode can help, from [Florian Gößler](https://
|
|
|
100
104
|
|
|
101
105
|
## Features
|
|
102
106
|
- Rails view helpers that fully support Webpack output, including HMR and code splitting.
|
|
103
|
-
- Convenient but not required webpack configuration. The only requirement is that your webpack configuration
|
|
104
|
-
- HMR with the
|
|
107
|
+
- Convenient but not required webpack configuration. The only requirement is that your webpack configuration creates a manifest.
|
|
108
|
+
- HMR with the `shakapacker-dev-server`, such as for hot-reloading React!
|
|
105
109
|
- Automatic code splitting using multiple entry points to optimize JavaScript downloads.
|
|
106
110
|
- [Webpack v5+](https://webpack.js.org/)
|
|
107
111
|
- ES6 with [babel](https://babeljs.io/), [SWC](https://swc.rs/), or [Esbuild](https://github.com/privatenumber/esbuild-loader)
|
|
@@ -133,14 +137,14 @@ Add `shakapacker` gem to your `Gemfile`:
|
|
|
133
137
|
bundle add shakapacker --strict
|
|
134
138
|
```
|
|
135
139
|
|
|
136
|
-
Then
|
|
140
|
+
Then run the following to install Shakapacker:
|
|
137
141
|
|
|
138
142
|
```bash
|
|
139
143
|
./bin/bundle install
|
|
140
|
-
./bin/rails
|
|
144
|
+
./bin/rails shakapacker:install
|
|
141
145
|
```
|
|
142
146
|
|
|
143
|
-
When `package.json` and/or `yarn.lock` changes, such as when pulling down changes to your local environment in
|
|
147
|
+
When `package.json` and/or `yarn.lock` changes, such as when pulling down changes to your local environment in team settings, be sure to keep your NPM packages up-to-date:
|
|
144
148
|
|
|
145
149
|
```bash
|
|
146
150
|
yarn
|
|
@@ -154,32 +158,32 @@ yarn add @babel/core @babel/plugin-transform-runtime @babel/preset-env @babel/ru
|
|
|
154
158
|
webpack webpack-assets-manifest webpack-cli webpack-merge webpack-sources webpack-dev-server
|
|
155
159
|
```
|
|
156
160
|
|
|
157
|
-
Previously, these "webpack" and "babel" packages were direct dependencies for `
|
|
161
|
+
Previously, these "webpack" and "babel" packages were direct dependencies for `shakapacker`. By
|
|
158
162
|
making these peer dependencies, you have control over the versions used in your webpack and babel configs.
|
|
159
163
|
|
|
160
164
|
### Note for Yarn v2 usage
|
|
161
165
|
|
|
162
166
|
If you are using Yarn v2 (berry), please note that PnP modules are not supported.
|
|
163
167
|
|
|
164
|
-
|
|
168
|
+
To use Shakapacker with Yarn v2, make sure you set `nodeLinker: node-modules` in your `.yarnrc.yml` file as per the [Yarn docs](https://yarnpkg.com/getting-started/migration#step-by-step) to opt out of Plug'n'Play behavior.
|
|
165
169
|
|
|
166
170
|
## Concepts
|
|
167
171
|
|
|
168
|
-
At
|
|
172
|
+
At its core, Shakapacker's essential function is to:
|
|
169
173
|
|
|
170
174
|
1. Provide configuration by a single file used by both Rails view helpers and JavaScript webpack compilation code.
|
|
171
|
-
2. Provide Rails view helpers, utilizing this configuration file
|
|
172
|
-
3. Provide a community
|
|
175
|
+
2. Provide Rails view helpers, utilizing this configuration file so that a webpage can load JavaScript, CSS, and other static assets compiled by webpack, supporting bundle splitting, fingerprinting, and HMR.
|
|
176
|
+
3. Provide a community-supported, default webpack compilation that generates the necessary bundles and manifest, using the same configuration file. This compilation can be extended for any needs.
|
|
173
177
|
|
|
174
178
|
## Usage
|
|
175
179
|
|
|
176
180
|
### Configuration and Code
|
|
177
181
|
|
|
178
|
-
You will need your file system to correspond to the setup of your `
|
|
182
|
+
You will need your file system to correspond to the setup of your `config/shakapacker.yml` file.
|
|
179
183
|
|
|
180
184
|
Suppose you have the following configuration:
|
|
181
185
|
|
|
182
|
-
`
|
|
186
|
+
`shakapacker.yml`
|
|
183
187
|
```yml
|
|
184
188
|
default: &default
|
|
185
189
|
source_path: app/javascript
|
|
@@ -210,16 +214,20 @@ Webpack intelligently includes only necessary files. In this example, the file `
|
|
|
210
214
|
|
|
211
215
|
`nested_entries` allows you to have webpack entry points nested in subdirectories. This defaults to false so you don't accidentally create entry points for an entire tree of files. In other words, with `nested_entries: false`, you can have your entire `source_path` used for your source (using the `source_entry_path: /`) and you place files at the top level that you want as entry points. `nested_entries: true` allows you to have entries that are in subdirectories. This is useful if you have entries that are generated, so you can have a `generated` subdirectory and easily separate generated files from the rest of your codebase.
|
|
212
216
|
|
|
217
|
+
#### Setting custom config path
|
|
218
|
+
|
|
219
|
+
You can use the environment variable `SHAKAPACKER_CONFIG` to enforce a particular path to the config file rather than the default `config/shakapacker.yml`.
|
|
220
|
+
|
|
213
221
|
### View Helpers
|
|
214
222
|
The Shakapacker view helpers generate the script and link tags to get the webpack output onto your views.
|
|
215
223
|
|
|
216
|
-
Be sure to consult the API documentation in the source code of [helper.rb](./lib/
|
|
224
|
+
Be sure to consult the API documentation in the source code of [helper.rb](./lib/shakapacker/helper.rb).
|
|
217
225
|
|
|
218
|
-
**Note:**
|
|
226
|
+
**Note:** 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.
|
|
219
227
|
|
|
220
228
|
#### View Helpers `javascript_pack_tag` and `stylesheet_pack_tag`
|
|
221
229
|
|
|
222
|
-
These view helpers take your `
|
|
230
|
+
These view helpers take your `shakapacker.yml` configuration file and the resulting webpack compilation `manifest.json` and generate the HTML to load the assets.
|
|
223
231
|
|
|
224
232
|
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`:
|
|
225
233
|
|
|
@@ -229,24 +237,24 @@ You can then link the JavaScript pack in Rails views using the `javascript_pack_
|
|
|
229
237
|
```
|
|
230
238
|
|
|
231
239
|
The `javascript_pack_tag` and `stylesheet_pack_tag` helpers will include all the transpiled
|
|
232
|
-
packs with the chunks in your view, which creates
|
|
240
|
+
packs with the chunks in your view, which creates HTML tags for all the chunks.
|
|
233
241
|
|
|
234
242
|
You can provide multiple packs and other attributes. Note, `defer` defaults to showing.
|
|
235
243
|
|
|
236
244
|
```erb
|
|
237
|
-
<%= javascript_pack_tag 'calendar', 'map', 'data-
|
|
245
|
+
<%= javascript_pack_tag 'calendar', 'map', 'data-turbo-track': 'reload' %>
|
|
238
246
|
```
|
|
239
247
|
|
|
240
|
-
The resulting HTML would look like:
|
|
248
|
+
The resulting HTML would look like this:
|
|
241
249
|
```
|
|
242
|
-
<script src="/packs/vendor-16838bab065ae1e314.js" data-
|
|
243
|
-
<script src="/packs/calendar~runtime-16838bab065ae1e314.js" data-
|
|
244
|
-
<script src="/packs/calendar-1016838bab065ae1e314.js" data-
|
|
245
|
-
<script src="/packs/map~runtime-16838bab065ae1e314.js" data-
|
|
246
|
-
<script src="/packs/map-16838bab065ae1e314.js" data-
|
|
250
|
+
<script src="/packs/vendor-16838bab065ae1e314.js" data-turbo-track="reload" defer></script>
|
|
251
|
+
<script src="/packs/calendar~runtime-16838bab065ae1e314.js" data-turbo-track="reload" defer></script>
|
|
252
|
+
<script src="/packs/calendar-1016838bab065ae1e314.js" data-turbo-track="reload" defer"></script>
|
|
253
|
+
<script src="/packs/map~runtime-16838bab065ae1e314.js" data-turbo-track="reload" defer></script>
|
|
254
|
+
<script src="/packs/map-16838bab065ae1e314.js" data-turbo-track="reload" defer></script>
|
|
247
255
|
```
|
|
248
256
|
|
|
249
|
-
In this output, both the calendar and map codes might refer to other common libraries. Those get placed something like the vendor bundle. The view helper removes any duplication.
|
|
257
|
+
In this output, both the calendar and map codes might refer to other common libraries. Those get placed in something like the vendor bundle. The view helper removes any duplication.
|
|
250
258
|
|
|
251
259
|
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/javascript/application.js`, pass the option `defer: false` to your `javascript_pack_tag`.
|
|
252
260
|
|
|
@@ -269,7 +277,7 @@ While this also generally applies to `stylesheet_pack_tag`, you may use multiple
|
|
|
269
277
|
|
|
270
278
|
#### View Helper `append_javascript_pack_tag`, `prepend_javascript_pack_tag` and `append_stylesheet_pack_tag`
|
|
271
279
|
|
|
272
|
-
If you need configure your script pack names or stylesheet pack names from the view for a route or partials, then you will need some logic to ensure you call the helpers only once with multiple arguments. The new view helpers, `append_javascript_pack_tag` and `append_stylesheet_pack_tag` can solve this problem. The helper `append_javascript_pack_tag` will queue up script packs when the `javascript_pack_tag` is finally used. Similarly,`append_stylesheet_pack_tag` will queue up style packs when the `stylesheet_pack_tag` is finally used.
|
|
280
|
+
If you need to configure your script pack names or stylesheet pack names from the view for a route or partials, then you will need some logic to ensure you call the helpers only once with multiple arguments. The new view helpers, `append_javascript_pack_tag` and `append_stylesheet_pack_tag` can solve this problem. The helper `append_javascript_pack_tag` will queue up script packs when the `javascript_pack_tag` is finally used. Similarly,`append_stylesheet_pack_tag` will queue up style packs when the `stylesheet_pack_tag` is finally used.
|
|
273
281
|
|
|
274
282
|
Main view:
|
|
275
283
|
```erb
|
|
@@ -337,7 +345,7 @@ is the same as using this in the main layout:
|
|
|
337
345
|
<%= javascript_pack_tag 'main', 'calendar', 'map', application' %>
|
|
338
346
|
```
|
|
339
347
|
|
|
340
|
-
For alternative options
|
|
348
|
+
For alternative options for setting the additional packs, [see this discussion](https://github.com/shakacode/shakapacker/issues/39).
|
|
341
349
|
|
|
342
350
|
#### View Helper: `asset_pack_path`
|
|
343
351
|
|
|
@@ -378,17 +386,17 @@ If you want to use images in your stylesheets:
|
|
|
378
386
|
|
|
379
387
|
### Server-Side Rendering (SSR)
|
|
380
388
|
|
|
381
|
-
Note, if you are using server-side rendering of JavaScript with dynamic code-splitting, as is often done with extensions to
|
|
389
|
+
Note, if you are using server-side rendering of JavaScript with dynamic code-splitting, as is often done with extensions to Shakapacker, 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.
|
|
382
390
|
|
|
383
391
|
### Development
|
|
384
392
|
|
|
385
|
-
|
|
393
|
+
Shakapacker ships with two binstubs: `./bin/shakapacker` and `./bin/shakapacker-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.
|
|
386
394
|
|
|
387
395
|
_Note: older Shakapacker installations had set a missing NODE_ENV in the binstubs. Please remove this for versions 6.5.2 and newer._
|
|
388
396
|
|
|
389
397
|
#### Automatic Webpack Code Building
|
|
390
398
|
|
|
391
|
-
Shakapacker can be configured to automatically compile on demand when needed using
|
|
399
|
+
Shakapacker can be configured to automatically compile on demand when needed using `compile` option in the `shakapacker.yml`. This happens when you refer to any of the pack assets using the Shakapacker 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/shakapacker --watch` or run `./bin/shakapacker-dev-server` during development.
|
|
392
400
|
|
|
393
401
|
The `compile: true` option can be more useful for test and production builds.
|
|
394
402
|
|
|
@@ -397,43 +405,43 @@ The `compile: true` option can be more useful for test and production builds.
|
|
|
397
405
|
Shakapacker ships with two different strategies that are used to determine whether assets need recompilation per the `compile: true` option:
|
|
398
406
|
|
|
399
407
|
- `digest` - This strategy calculates SHA1 digest of files in your watched paths (see below). The calculated digest is then stored in a temp file. To check whether the assets need to be recompiled, Shakapacker calculates the SHA1 of the watched files and compares it with the one stored. If the digests are equal, no recompilation occurs. If the digests are different or the temp file is missing, files are recompiled.
|
|
400
|
-
- `mtime` - This strategy looks at last modified at timestamps of both files AND directories in your watched paths. The timestamp of the most recent file or directory is then compared with the timestamp of `manifest.json` file generated. If the manifest file timestamp is newer than one of the most recently modified
|
|
408
|
+
- `mtime` - This strategy looks at the last "modified at" timestamps of both files AND directories in your watched paths. The timestamp of the most recent file or directory is then compared with the timestamp of `manifest.json` file generated. If the manifest file timestamp is newer than one of the most recently modified files or directories in the watched paths, no recompilation occurs. If the manifest file is older, files are recompiled.
|
|
401
409
|
|
|
402
410
|
The `mtime` strategy is generally faster than the `digest` one, but it requires stable timestamps, this makes it perfect for a development environment, such as needing to rebuild bundles for tests, or if you're not changing frontend assets much.
|
|
403
411
|
|
|
404
412
|
In production or CI environments, the `digest` strategy is more suitable, unless you are using incremental builds or caching and can guarantee that the timestamps will not change after e.g. cache restore. However, many production or CI environments will explicitly compile assets, so `compile: false` is more appropriate. Otherwise, you'll waste time either checking file timestamps or computing digests.
|
|
405
413
|
|
|
406
|
-
You can control what strategy is used by `compiler_strategy` option in `
|
|
414
|
+
You can control what strategy is used by the `compiler_strategy` option in `shakapacker.yml` config file. By default `mtime` strategy is used in development environment, `digest` is used elsewhere.
|
|
407
415
|
|
|
408
|
-
**Note:** If you are not using the
|
|
416
|
+
**Note:** If you are not using the `shakapacker-dev-server`, your packs will be served by the Rails public file server. If you've enabled caching (Rails application `config.action_controller.perform_caching` setting), your changes will likely not be picked up due to `Cache-Control` header being set and assets being cached in the browser memory. For more details see [issue](https://github.com/shakacode/shakapacker/issues/88)[ #88](https://github.com/shakacode/shakapacker/issues/88)](https://github.com/shakacode/shakapacker/issues/88).
|
|
409
417
|
|
|
410
|
-
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/
|
|
418
|
+
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/shakapacker-dev-server`. This process will watch for changes in the relevant files, defined by `shakapacker.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/).
|
|
411
419
|
|
|
412
420
|
#### Common Development Commands
|
|
413
421
|
|
|
414
422
|
```bash
|
|
415
423
|
# webpack dev server
|
|
416
|
-
./bin/
|
|
424
|
+
./bin/shakapacker-dev-server
|
|
417
425
|
|
|
418
426
|
# watcher
|
|
419
|
-
./bin/
|
|
427
|
+
./bin/shakapacker --watch --progress
|
|
420
428
|
|
|
421
429
|
# standalone build
|
|
422
|
-
./bin/
|
|
430
|
+
./bin/shakapacker --progress
|
|
423
431
|
```
|
|
424
432
|
|
|
425
|
-
Once you start this webpack development server,
|
|
433
|
+
Once you start this webpack development server, Shakapacker 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/shakapacker.yml`
|
|
426
434
|
|
|
427
|
-
You can use environment variables as options supported by [webpack-dev-server](https://webpack.js.org/configuration/dev-server/) in the form `
|
|
435
|
+
You can use environment variables as options supported by [webpack-dev-server](https://webpack.js.org/configuration/dev-server/) in the form `SHAKAPACKER_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.
|
|
428
436
|
|
|
429
437
|
```bash
|
|
430
|
-
|
|
438
|
+
SHAKAPACKER_DEV_SERVER_PORT=4305 SHAKAPACKER_DEV_SERVER_HOST=example.com SHAKAPACKER_DEV_SERVER_INLINE=true SHAKAPACKER_DEV_SERVER_HOT=false ./bin/shakapacker-dev-server
|
|
431
439
|
```
|
|
432
440
|
|
|
433
|
-
By default, the webpack dev server listens on `localhost:3035` in development for security purposes. However, if you want your app to be available on port 4035 over local LAN IP or a VM instance like vagrant, you can set the `port` and `host` when running `./bin/
|
|
441
|
+
By default, the webpack dev server listens on `localhost:3035` in development for security purposes. However, if you want your app to be available on port 4035 over local LAN IP or a VM instance like vagrant, you can set the `port` and `host` when running `./bin/shakapacker-dev-server` binstub:
|
|
434
442
|
|
|
435
443
|
```bash
|
|
436
|
-
|
|
444
|
+
SHAKAPACKER_DEV_SERVER_PORT=4305 SHAKAPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/shakapacker-dev-server
|
|
437
445
|
```
|
|
438
446
|
|
|
439
447
|
**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:
|
|
@@ -449,17 +457,19 @@ end
|
|
|
449
457
|
|
|
450
458
|
### Webpack Configuration
|
|
451
459
|
|
|
452
|
-
First, you don't _need_ to use Shakapacker's webpack configuration. However, the `shakapacker` NPM package provides convenient access to configuration code that reads the `config/
|
|
460
|
+
First, you don't _need_ to use Shakapacker's webpack configuration. However, the `shakapacker` NPM package provides convenient access to configuration code that reads the `config/shakapacker.yml` file which the view helpers also use. If you have your customized webpack configuration, at the minimum, you must ensure:
|
|
453
461
|
|
|
454
|
-
1. Your output files go the right directory
|
|
455
|
-
2. Your output includes a manifest, via package [`webpack-assets-manifest`](https://github.com/webdeveric/webpack-assets-manifest) that maps output names (your 'packs') to the fingerprinted versions, including bundle-splitting dependencies. That's the main secret sauce of
|
|
462
|
+
1. Your output files go to the right directory
|
|
463
|
+
2. Your output includes a manifest, via package [`webpack-assets-manifest`](https://github.com/webdeveric/webpack-assets-manifest) that maps output names (your 'packs') to the fingerprinted versions, including bundle-splitting dependencies. That's the main secret sauce of Shakapacker!
|
|
456
464
|
|
|
457
465
|
The most practical webpack configuration is to take the default from Shakapacker and then use [webpack-merge](https://github.com/survivejs/webpack-merge) to merge your customizations with the default. For example, suppose you want to add some `resolve.extensions`:
|
|
458
466
|
|
|
459
467
|
```js
|
|
460
468
|
// use the new NPM package name, `shakapacker`.
|
|
461
469
|
// merge is webpack-merge from https://github.com/survivejs/webpack-merge
|
|
462
|
-
const {
|
|
470
|
+
const { generateWebpackConfig, merge } = require('shakapacker')
|
|
471
|
+
|
|
472
|
+
const baseWebpackConfig = generateWebpackConfig()
|
|
463
473
|
|
|
464
474
|
const options = {
|
|
465
475
|
resolve: {
|
|
@@ -499,7 +509,9 @@ Then `require` this file in your `config/webpack/webpack.config.js`:
|
|
|
499
509
|
```js
|
|
500
510
|
// config/webpack/webpack.config.js
|
|
501
511
|
// use the new NPM package name, `shakapacker`.
|
|
502
|
-
const {
|
|
512
|
+
const { generateWebpackConfig, merge } = require('shakapacker')
|
|
513
|
+
|
|
514
|
+
const webpackConfig = generateWebpackConfig()
|
|
503
515
|
const customConfig = require('./custom')
|
|
504
516
|
|
|
505
517
|
module.exports = merge(webpackConfig, customConfig)
|
|
@@ -509,7 +521,9 @@ If you need access to configs within Shakapacker's configuration, you can import
|
|
|
509
521
|
|
|
510
522
|
```js
|
|
511
523
|
// config/webpack/webpack.config.js
|
|
512
|
-
const {
|
|
524
|
+
const { generateWebpackConfig } = require('shakapacker')
|
|
525
|
+
|
|
526
|
+
const webpackConfig = generateWebpackConfig()
|
|
513
527
|
|
|
514
528
|
console.log(webpackConfig.output_path)
|
|
515
529
|
console.log(webpackConfig.source_path)
|
|
@@ -518,7 +532,7 @@ console.log(webpackConfig.source_path)
|
|
|
518
532
|
console.log(JSON.stringify(webpackConfig, undefined, 2))
|
|
519
533
|
```
|
|
520
534
|
|
|
521
|
-
You may want to modify rules in the default configuration. For instance, if you are using a custom svg loader, you may want to remove `.svg` from the default file loader rules. You can search and filter the default rules like so:
|
|
535
|
+
You may want to modify the rules in the default configuration. For instance, if you are using a custom svg loader, you may want to remove `.svg` from the default file loader rules. You can search and filter the default rules like so:
|
|
522
536
|
|
|
523
537
|
```js
|
|
524
538
|
const svgRule = config.module.rules.find(rule => rule.test.test('.svg'));
|
|
@@ -536,7 +550,7 @@ By default, you will find the Shakapacker preset in your `package.json`. Note, y
|
|
|
536
550
|
]
|
|
537
551
|
},
|
|
538
552
|
```
|
|
539
|
-
Optionally, you can change your Babel configuration by removing these lines in your `package.json` and
|
|
553
|
+
Optionally, you can change your Babel configuration by removing these lines in your `package.json` and adding [a Babel](https://babeljs.io/docs/en/config-files) configuration file](https://babeljs.io/docs/en/config-files) to your project. For an example of customization based on the original, see [Customizing Babel Config](./docs/customizing_babel_config.md).
|
|
540
554
|
|
|
541
555
|
|
|
542
556
|
### SWC configuration
|
|
@@ -598,7 +612,9 @@ Then modify the webpack config to use it as a plugin:
|
|
|
598
612
|
|
|
599
613
|
```js
|
|
600
614
|
// config/webpack/webpack.config.js
|
|
601
|
-
const {
|
|
615
|
+
const { generateWebpackConfig, merge } = require("shakapacker");
|
|
616
|
+
|
|
617
|
+
const webpackConfig = generateWebpackConfig()
|
|
602
618
|
const ForkTSCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
|
|
603
619
|
|
|
604
620
|
module.exports = merge(webpackConfig, {
|
|
@@ -608,7 +624,7 @@ module.exports = merge(webpackConfig, {
|
|
|
608
624
|
|
|
609
625
|
#### CSS
|
|
610
626
|
|
|
611
|
-
To enable CSS support in your application, add following packages:
|
|
627
|
+
To enable CSS support in your application, add the following packages:
|
|
612
628
|
|
|
613
629
|
```bash
|
|
614
630
|
yarn add css-loader style-loader mini-css-extract-plugin css-minimizer-webpack-plugin
|
|
@@ -618,7 +634,10 @@ Optionally, add the `CSS` extension to webpack config for easy resolution.
|
|
|
618
634
|
|
|
619
635
|
```js
|
|
620
636
|
// config/webpack/webpack.config.js
|
|
621
|
-
const {
|
|
637
|
+
const { generateWebpackConfig, merge } = require('shakapacker')
|
|
638
|
+
|
|
639
|
+
const webpackConfig = generateWebpackConfig()
|
|
640
|
+
|
|
622
641
|
const customConfig = {
|
|
623
642
|
resolve: {
|
|
624
643
|
extensions: ['.css']
|
|
@@ -668,7 +687,7 @@ yarn add coffeescript coffee-loader
|
|
|
668
687
|
|
|
669
688
|
#### Other frameworks
|
|
670
689
|
|
|
671
|
-
Please follow
|
|
690
|
+
Please follow Webpack integration guide for the relevant framework or library,
|
|
672
691
|
|
|
673
692
|
1. [Svelte](https://github.com/sveltejs/svelte-loader#install)
|
|
674
693
|
2. [Angular](https://v2.angular.io/docs/ts/latest/guide/webpack.html#!#configure-webpack)
|
|
@@ -697,7 +716,10 @@ module.exports = {
|
|
|
697
716
|
|
|
698
717
|
```js
|
|
699
718
|
// config/webpack/webpack.config.js
|
|
700
|
-
const {
|
|
719
|
+
const { generateWebpackConfig, merge } = require('shakapacker')
|
|
720
|
+
|
|
721
|
+
const webpackConfig = generateWebpackConfig()
|
|
722
|
+
|
|
701
723
|
const vueConfig = require('./rules/vue')
|
|
702
724
|
|
|
703
725
|
module.exports = merge(vueConfig, webpackConfig)
|
|
@@ -705,9 +727,9 @@ module.exports = merge(vueConfig, webpackConfig)
|
|
|
705
727
|
|
|
706
728
|
### Custom Rails environments
|
|
707
729
|
|
|
708
|
-
Out of the box Shakapacker ships with - development, test and production environments in `config/
|
|
730
|
+
Out of the box Shakapacker ships with - development, test and production environments in `config/shakapacker.yml` however, in most production apps extra environments are needed as part of the deployment workflow. Shakapacker supports this out of the box from version 3.4.0+ onwards.
|
|
709
731
|
|
|
710
|
-
You can choose to define additional environment configurations in
|
|
732
|
+
You can choose to define additional environment configurations in shakapacker.yml,
|
|
711
733
|
|
|
712
734
|
```yml
|
|
713
735
|
staging:
|
|
@@ -723,15 +745,15 @@ staging:
|
|
|
723
745
|
public_output_path: packs-staging
|
|
724
746
|
```
|
|
725
747
|
|
|
726
|
-
Otherwise Shakapacker 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/
|
|
748
|
+
Otherwise, Shakapacker will use the 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/shakapacker/*` and instead use shakapacker.yml to load different configurations using `RAILS_ENV`.
|
|
727
749
|
|
|
728
|
-
For example, the below command will compile assets in production mode but will use staging configurations from `config/
|
|
750
|
+
For example, the below command will compile assets in production mode but will use staging configurations from `config/shakapacker.yml` if available or use fallback production environment configuration:
|
|
729
751
|
|
|
730
752
|
```bash
|
|
731
753
|
RAILS_ENV=staging bundle exec rails assets:precompile
|
|
732
754
|
```
|
|
733
755
|
|
|
734
|
-
And, this will compile in development mode and load configuration for cucumber environment if defined in
|
|
756
|
+
And, this will compile in development mode and load configuration for the cucumber environment if defined in `shakapacker.yml` or fallback to production configuration
|
|
735
757
|
|
|
736
758
|
```bash
|
|
737
759
|
RAILS_ENV=cucumber NODE_ENV=development bundle exec rails assets:precompile
|
|
@@ -741,24 +763,24 @@ Please note, binstubs compiles in development mode however rake tasks compiles i
|
|
|
741
763
|
|
|
742
764
|
```bash
|
|
743
765
|
# Compiles in development mode unless NODE_ENV is specified, per the binstub source
|
|
744
|
-
./bin/
|
|
745
|
-
./bin/
|
|
766
|
+
./bin/shakapacker
|
|
767
|
+
./bin/shakapacker-dev-server
|
|
746
768
|
|
|
747
|
-
# Compiles in production mode by default unless NODE_ENV is specified, per `lib/tasks/
|
|
769
|
+
# Compiles in production mode by default unless NODE_ENV is specified, per `lib/tasks/shakapacker/compile.rake`
|
|
748
770
|
bundle exec rails assets:precompile
|
|
749
|
-
bundle exec rails
|
|
771
|
+
bundle exec rails shakapacker:compile
|
|
750
772
|
```
|
|
751
773
|
|
|
752
774
|
### Upgrading
|
|
753
775
|
|
|
754
|
-
You can run following commands to upgrade Shakapacker to the latest stable version. This process involves upgrading the gem and related JavaScript packages:
|
|
776
|
+
You can run the following commands to upgrade Shakapacker to the latest stable version. This process involves upgrading the gem and related JavaScript packages:
|
|
755
777
|
|
|
756
778
|
```bash
|
|
757
779
|
# check your Gemfile for version restrictions
|
|
758
780
|
bundle update shakapacker
|
|
759
781
|
|
|
760
782
|
# overwrite your changes to the default install files and revert any unwanted changes from the install
|
|
761
|
-
rails
|
|
783
|
+
rails shakapacker:install
|
|
762
784
|
|
|
763
785
|
# yarn 1 instructions
|
|
764
786
|
yarn upgrade shakapacker --latest
|
|
@@ -776,22 +798,22 @@ Also, consult the [CHANGELOG](./CHANGELOG.md) for additional upgrade links.
|
|
|
776
798
|
|
|
777
799
|
### Paths
|
|
778
800
|
|
|
779
|
-
By default, Shakapacker 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/
|
|
801
|
+
By default, Shakapacker 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/shakapacker.yml` file.
|
|
780
802
|
|
|
781
|
-
The configuration for what webpack is supposed to compile by default rests on the convention that every file in `app/javascript/`**(default)** or whatever path you set for `source_entry_path` in the `
|
|
803
|
+
The configuration for what webpack is supposed to compile by default rests on the convention that every file in `app/javascript/`**(default)** or whatever path you set for `source_entry_path` in the `shakapacker.yml` configuration is turned into their own output files (or entry points, as webpack calls it). Therefore you don't want to put any file inside `app/javascript` 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 "app/javascript/" directory and keep everything else under subdirectories like `app/javascript/controllers`.
|
|
782
804
|
|
|
783
805
|
Suppose you want to change the source directory from `app/javascript` to `frontend` and output to `assets/packs`. This is how you would do it:
|
|
784
806
|
|
|
785
807
|
```yml
|
|
786
|
-
# config/
|
|
808
|
+
# config/shakapacker.yml
|
|
787
809
|
source_path: frontend # packs are the files in frontend/
|
|
788
810
|
public_output_path: assets/packs # outputs to => public/assets/packs
|
|
789
811
|
```
|
|
790
812
|
|
|
791
|
-
Similarly you can also control and configure `webpack-dev-server` settings from `config/
|
|
813
|
+
Similarly, you can also control and configure `webpack-dev-server` settings from `config/shakapacker.yml` file:
|
|
792
814
|
|
|
793
815
|
```yml
|
|
794
|
-
# config/
|
|
816
|
+
# config/shakapacker.yml
|
|
795
817
|
development:
|
|
796
818
|
dev_server:
|
|
797
819
|
host: localhost
|
|
@@ -804,7 +826,7 @@ If you want to have HMR and separate link tags, set `hmr: true` and `inline_css:
|
|
|
804
826
|
|
|
805
827
|
### Additional paths
|
|
806
828
|
|
|
807
|
-
If you are adding Shakapacker 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/
|
|
829
|
+
If you are adding Shakapacker 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/shakapacker.yml`. This lets you
|
|
808
830
|
add additional paths that webpack should look up when resolving modules:
|
|
809
831
|
|
|
810
832
|
```yml
|
|
@@ -819,20 +841,20 @@ import 'stylesheets/main'
|
|
|
819
841
|
import 'images/rails.png'
|
|
820
842
|
```
|
|
821
843
|
|
|
822
|
-
**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
|
|
844
|
+
**Note:** Please be careful when adding paths here otherwise it will make the compilation slow, consider adding specific paths instead of the whole parent directory if you just need to reference one or two modules
|
|
823
845
|
|
|
824
|
-
**Also note:** While importing assets living outside your `source_path` defined in
|
|
846
|
+
**Also note:** While importing assets living outside your `source_path` defined in shakapacker.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, Shakapacker won't recompile the bundle in production unless a file that lives in one of it's watched paths has changed (check out `Shakapacker::MtimeStrategy#latest_modified_timestamp` or `Shakapacker::DigestStrategy#watched_files_digest` depending on strategy configured by `compiler_strategy` option in `shakapacker.yml`). That's why you'd need to add `app/assets` to the additional_paths as stated above and use `import 'javascripts/file.js'` instead.
|
|
825
847
|
|
|
826
848
|
|
|
827
849
|
## Deployment
|
|
828
850
|
|
|
829
|
-
Shakapacker hooks up a new `
|
|
851
|
+
Shakapacker hooks up a new `shakapacker:compile` task to `assets:precompile`, which gets run whenever you run `assets:precompile`. If you are not using Sprockets, `shakapacker: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/shakapacker.yml` (if available).
|
|
830
852
|
|
|
831
|
-
This behavior is optional & can be disabled by either setting
|
|
853
|
+
This behavior is optional & can be disabled by either setting a `SHAKAPACKER_PRECOMPILE` environment variable to `false`, `no`, `n`, or `f`, or by setting a `shakapacker_precompile` key in your `shakapacker.yml` to `false`. ([source code](./lib/shakapacker/configuration.rb#L34))
|
|
832
854
|
|
|
833
855
|
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.
|
|
834
856
|
|
|
835
|
-
If you are using a CDN setup,
|
|
857
|
+
If you are using a CDN setup, Shakapacker 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 `SHAKAPACKER_ASSET_HOST` environment variable.
|
|
836
858
|
|
|
837
859
|
## Example Apps
|
|
838
860
|
* [React on Rails Tutorial With SSR, HMR fast refresh, and TypeScript](https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh)
|
|
@@ -844,14 +866,19 @@ See the doc page for [Troubleshooting](./docs/troubleshooting.md).
|
|
|
844
866
|
|
|
845
867
|
## Contributing
|
|
846
868
|
|
|
847
|
-
We encourage you to contribute to Shakapacker
|
|
869
|
+
We encourage you to contribute to Shakapacker! See [CONTRIBUTING](CONTRIBUTING.md) for guidelines about how to proceed. We have a [Slack discussion channel](https://reactrails.slack.com/join/shared_invite/enQtNjY3NTczMjczNzYxLTlmYjdiZmY3MTVlMzU2YWE0OWM0MzNiZDI0MzdkZGFiZTFkYTFkOGVjODBmOWEyYWQ3MzA2NGE1YWJjNmVlMGE).
|
|
848
870
|
|
|
849
871
|
## License
|
|
850
872
|
|
|
851
|
-
|
|
873
|
+
Shakapacker is released under the [MIT License](https://opensource.org/licenses/MIT).
|
|
852
874
|
|
|
853
875
|
## Supporters
|
|
854
876
|
|
|
877
|
+
The following companies support our Open Source projects, and ShakaCode uses their products!
|
|
878
|
+
|
|
879
|
+
<br />
|
|
880
|
+
<br />
|
|
881
|
+
|
|
855
882
|
<a href="https://www.jetbrains.com">
|
|
856
883
|
<img src="https://user-images.githubusercontent.com/4244251/184881139-42e4076b-024b-4b30-8c60-c3cd0e758c0a.png" alt="JetBrains" height="120px">
|
|
857
884
|
</a>
|
|
@@ -876,8 +903,6 @@ Webpacker is released under the [MIT License](https://opensource.org/licenses/MI
|
|
|
876
903
|
<a href="https://www.honeybadger.io">
|
|
877
904
|
<img src="https://user-images.githubusercontent.com/4244251/184881133-79ee9c3c-8165-4852-958e-31687b9536f4.png" alt="Honeybadger" height="55px">
|
|
878
905
|
</a>
|
|
879
|
-
|
|
880
|
-
<
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
The following companies support our open source projects, and ShakaCode uses their products!
|
|
906
|
+
<a href="https://reviewable.io">
|
|
907
|
+
<img src="https://user-images.githubusercontent.com/20628911/230848305-c94510a4-82d7-468f-bf9f-eeb81d3f2ce0.png" alt="Reviewable" height="55px">
|
|
908
|
+
</a>
|
data/Rakefile
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
../lib/install/config/shakapacker.yml
|
data/docs/deployment.md
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
# Deployment
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
If you are not using Sprockets `
|
|
3
|
+
Shakapacker hooks up a new `shakapacker:compile` task to `assets:precompile`, which gets run whenever you run `assets:precompile`.
|
|
4
|
+
If you are not using Sprockets `shakapacker:compile` is automatically aliased to `assets:precompile`.
|
|
5
5
|
|
|
6
6
|
```
|
|
7
7
|
|
|
8
8
|
## Heroku
|
|
9
9
|
|
|
10
|
-
In order for your
|
|
10
|
+
In order for your Shakapacker app to run on Heroku, you'll need to do a bit of configuration before hand.
|
|
11
11
|
|
|
12
12
|
```bash
|
|
13
|
-
heroku create my-
|
|
13
|
+
heroku create my-shakapacker-heroku-app
|
|
14
14
|
heroku addons:create heroku-postgresql:hobby-dev
|
|
15
15
|
heroku buildpacks:add heroku/nodejs
|
|
16
16
|
heroku buildpacks:add heroku/ruby
|
|
@@ -24,13 +24,15 @@ We're essentially doing the following here:
|
|
|
24
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
25
|
* Pushing your code to Heroku and kicking off the deployment
|
|
26
26
|
|
|
27
|
+
Your production build process is responsible for running `yarn install` before `rake assets:precompile`. For example, if you are on Heroku, the `heroku/nodejs` buildpack must run **prior** to the `heroku/ruby` buildpack for precompilation to run successfully.
|
|
28
|
+
|
|
27
29
|
## Nginx
|
|
28
30
|
|
|
29
|
-
|
|
31
|
+
Shakapacker doesn't serve anything in production. You’re expected to configure your web server to serve files in public/ directly.
|
|
30
32
|
|
|
31
33
|
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
34
|
|
|
33
|
-
Here's a sample nginx site config for a Rails app using
|
|
35
|
+
Here's a sample nginx site config for a Rails app using Shakapacker:
|
|
34
36
|
|
|
35
37
|
```nginx
|
|
36
38
|
upstream app {
|
|
@@ -86,17 +88,17 @@ Now, you can set `brotli_static on;` in your nginx site config, as per the confi
|
|
|
86
88
|
|
|
87
89
|
## CDN
|
|
88
90
|
|
|
89
|
-
|
|
90
|
-
you don't need to do anything extra for
|
|
91
|
+
Shakapacker 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
|
|
92
|
+
you don't need to do anything extra for Shakapacker, it just works.
|
|
91
93
|
|
|
92
94
|
## Capistrano
|
|
93
95
|
|
|
94
96
|
### Assets compiling on every deployment even if JavaScript and CSS files are not changed
|
|
95
97
|
|
|
96
|
-
Make sure you have your public outputh path (default `public/packs`), the
|
|
98
|
+
Make sure you have your public outputh path (default `public/packs`), the shakapacker cache path (default `tmp/shakapacker`) and `node_modules` in `:linked_dirs`
|
|
97
99
|
|
|
98
100
|
```ruby
|
|
99
|
-
append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "tmp/
|
|
101
|
+
append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "tmp/shakapacker", "public/packs", ".bundle", "node_modules"
|
|
100
102
|
```
|
|
101
103
|
|
|
102
104
|
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
|