Skip to content

Commit

Permalink
Add helper pack_path to avoid calling asset_path
Browse files Browse the repository at this point in the history
The `pack_path` is the same as the `asset_pack_path` except that the
Rails `asset_path` is not called on the file name. This is used by
server rendering, as the `asset_path` is designed for browsers to access
assets.
  • Loading branch information
justin808 committed Jul 19, 2017
1 parent d96379d commit 4797510
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ Contributors: please follow the recommendations outlined at [keepachangelog.com]

## [Unreleased]
*Please add entries here for your pull requests.*
## [2.1.0] - 2017-07-18
### Added
* Expose helper pack_path for server rendering so asset_path is not called, so that a CDN is never used for server rendering in React on Rails. [#23](https://github.com/shakacode/webpacker_lite/pull/23) by [justin808](https://github.com/justin808).

## [2.0.4] - 2017-05-29
### Fixed
* Code handles case of missing file and mtime. [#15](https://github.com/shakacode/webpacker_lite/pull/15) by [justin808](https://github.com/justin808).
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ for an asset used in your pack code you can reference them like this in your vie
<% # real file path "public/webpack/calendar.png" /> %>
```

The `pack_path` is the same as the `asset_pack_path` except that the Rails `asset_path` is not called on the file name. This is used by server rendering, as the `asset_path` is designed for browsers to access assets.

## Webpack Helper
You may use the [React on Rails NPM Package](https://www.npmjs.com/package/react-on-rails), [react-on-rails/webpackConfigLoader](https://github.com/shakacode/react_on_rails/blob/master/webpackConfigLoader.js) to provide your Webpack config with easy access to the YAML settings. Even if you don't use the NPM package, you can use that file to inspire your Webpack configuration.

Expand Down
13 changes: 12 additions & 1 deletion lib/webpacker_lite/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,20 @@ module WebpackerLite::Helper
# In production mode:
# <%= asset_pack_path 'calendar.css' %> # => "/public/webpack/production/calendar-1016838bab065ae1e122.css"
def asset_pack_path(name, **options)
pack_path = pack_path(name)
asset_path(pack_path, **options)
end

# Computes the full path for a given webpacker asset.
# Return relative path using manifest.json and passes it to asset_url helper
# Examples:
#
# In production mode:
# <%= asset_pack_path 'calendar.css' %> # => "webpack/production/calendar-1016838bab065ae1e122.css"
def pack_path(name)
path = WebpackerLite::Configuration.base_path
file = WebpackerLite::Manifest.lookup(name)
asset_path("#{path}/#{file}", **options)
"#{path}/#{file}"
end

# Creates a script tag that references the named pack file, as compiled by Webpack.
Expand Down
5 changes: 5 additions & 0 deletions test/helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ def setup
@view.extend WebpackerLite::Helper
end

def test_pack_path
assert_equal @view.pack_path("bootstrap.js"), "/webpack/test/bootstrap-300631c4f0e0f9c865bc.js"
assert_equal @view.pack_path("bootstrap.css"), "/webpack/test/bootstrap-c38deda30895059837cf.css"
end

def test_asset_pack_path
assert_equal @view.asset_pack_path("bootstrap.js"), "/webpack/test/bootstrap-300631c4f0e0f9c865bc.js"
assert_equal @view.asset_pack_path("bootstrap.css"), "/webpack/test/bootstrap-c38deda30895059837cf.css"
Expand Down

0 comments on commit 4797510

Please sign in to comment.