Skip to content

Commit

Permalink
Add config.webpacker_precompile (shakacode#102)
Browse files Browse the repository at this point in the history
* add dont_enhance_asset_precompile configuration value

* Add questions marks to method calls

* Changes per review

* Remove double negative

* per review

* Add test for webpacker_precompile configuration value

* Update with precompile disabling steps

* Update webpacker.yml

Co-authored-by: Tom Dracz <[email protected]>
  • Loading branch information
Judahmeek and Tom Dracz authored Apr 23, 2022
1 parent df704d3 commit f4995d1
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,8 @@ import 'images/rails.png'

Webpacker hooks up a new `webpacker:compile` task to `assets:precompile`, which gets run whenever you run `assets:precompile`. If you are not using Sprockets, `webpacker:compile` is automatically aliased to `assets:precompile`. Similar to sprockets both rake tasks will compile packs in production mode but will use `RAILS_ENV` to load configuration from `config/webpacker.yml` (if available).

This behavior is optional & can be disabled by either setting an `WEBPACKER_PRECOMPILE` environment variable to `false`, `no`, `n`, or `f`, or by setting a `webpacker_precompile` key in your `webpacker.yml` to `false`. ([source code](./lib/webpacker/configuration.rb#L30))

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.

If you are using a CDN setup, webpacker will use the configured [asset host](https://guides.rubyonrails.org/configuring.html#rails-general-configuration) value to prefix URLs for images or font icons which are included inside JS code or CSS. It is possible to override this value during asset compilation by setting the `WEBPACKER_ASSET_HOST` environment variable.
Expand Down
2 changes: 2 additions & 0 deletions lib/install/config/webpacker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ default: &default
public_output_path: packs
cache_path: tmp/webpacker
webpack_compile_output: true
# See https://github.com/shakacode/shakapacker#deployment
webpacker_precompile: true

# Location for manifest.json, defaults to {public_output_path}/manifest.json if unset
# manifest_path: public/packs/manifest.json
Expand Down
4 changes: 1 addition & 3 deletions lib/tasks/webpacker/clean.rake
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ namespace :webpacker do
end
end

skip_webpacker_clean = %w(no false n f).include?(ENV["WEBPACKER_PRECOMPILE"])

unless skip_webpacker_clean
if Webpacker.config.webpacker_precompile?
# Run clean if the assets:clean is run
if Rake::Task.task_defined?("assets:clean")
Rake::Task["assets:clean"].enhance do
Expand Down
4 changes: 1 addition & 3 deletions lib/tasks/webpacker/clobber.rake
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ namespace :webpacker do
end
end

skip_webpacker_clobber = %w(no false n f).include?(ENV["WEBPACKER_PRECOMPILE"])

unless skip_webpacker_clobber
if Webpacker.config.webpacker_precompile?
# Run clobber if the assets:clobber is run
if Rake::Task.task_defined?("assets:clobber")
Rake::Task["assets:clobber"].enhance do
Expand Down
5 changes: 1 addition & 4 deletions lib/tasks/webpacker/compile.rake
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ namespace :webpacker do
end
end

# Compile packs after we've compiled all other assets during precompilation
skip_webpacker_precompile = %w(no false n f).include?(ENV["WEBPACKER_PRECOMPILE"])

unless skip_webpacker_precompile
if Webpacker.config.webpacker_precompile?
if Rake::Task.task_defined?("assets:precompile")
enhance_assets_precompile
else
Expand Down
6 changes: 6 additions & 0 deletions lib/webpacker/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ def ensure_consistent_versioning?
fetch(:ensure_consistent_versioning)
end

def webpacker_precompile?
# ENV of false takes precedence
return false if %w(no false n f).include?(ENV["WEBPACKER_PRECOMPILE"])
return %w(yes true t).include?(ENV["WEBPACKER_PRECOMPILE"]) || fetch(:webpacker_precompile)
end

def source_path
root_path.join(fetch(:source_path))
end
Expand Down
28 changes: 24 additions & 4 deletions test/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ def test_public_output_path
public_output_path = File.expand_path File.join(File.dirname(__FILE__), "test_app/public/packs").to_s
assert_equal @config.public_output_path.to_s, public_output_path

@config = Webpacker::Configuration.new(
@public_root_config = Webpacker::Configuration.new(
root_path: @config.root_path,
config_path: Pathname.new(File.expand_path("./test_app/config/webpacker_public_root.yml", __dir__)),
env: "production"
)

public_output_path = File.expand_path File.join(File.dirname(__FILE__), "public/packs").to_s
assert_equal @config.public_output_path.to_s, public_output_path
assert_equal @public_root_config.public_output_path.to_s, public_output_path
end

def test_public_manifest_path
Expand All @@ -47,14 +47,14 @@ def test_manifest_path
manifest_path = File.expand_path File.join(File.dirname(__FILE__), "test_app/public/packs", "manifest.json").to_s
assert_equal @config.manifest_path.to_s, manifest_path

@config = Webpacker::Configuration.new(
@manifest_config = Webpacker::Configuration.new(
root_path: @config.root_path,
config_path: Pathname.new(File.expand_path("./test_app/config/webpacker_manifest_path.yml", __dir__)),
env: "production"
)

manifest_path = File.expand_path File.join(File.dirname(__FILE__), "test_app/app/packs", "manifest.json").to_s
assert_equal @config.manifest_path.to_s, manifest_path
assert_equal @manifest_config.manifest_path.to_s, manifest_path
end

def test_cache_path
Expand Down Expand Up @@ -100,5 +100,25 @@ def test_ensure_consistent_versioning?
with_rails_env("test") do
refute Webpacker.config.ensure_consistent_versioning?
end

def test_webpacker_precompile
assert @config.webpacker_precompile

ENV["WEBPACKER_PRECOMPILE"] = "false"

refute Webpacker.config.webpacker_precompile?

ENV["WEBPACKER_PRECOMPILE"] = "yes"

assert Webpacker.config.webpacker_precompile?

@no_precompile_config = Webpacker::Configuration.new(
root_path: @config.root_path,
config_path: Pathname.new(File.expand_path("./test_app/config/webpacker_no_precompile.yml", __dir__)),
env: "production"
)

refute @no_precompile_config.webpacker_precompile
end
end
end
7 changes: 7 additions & 0 deletions test/test_app/config/webpacker_no_precompile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Note: You must restart bin/webpacker-dev-server for changes to take effect

default: &default
webpacker_precompile: false

production:
<<: *default

0 comments on commit f4995d1

Please sign in to comment.