Giter Club home page Giter Club logo

external_asset_pipeline's Introduction

ExternalAssetPipeline

The external_asset_pipeline gem provides a lightweight and flexible integration between rails and any independent asset processor(s) that can produce an asset manifest.

It can be used in conjunction with sprockets or as a complete replacement that manages all assets. This is perhaps best illustrated through examples, so don't hesitate to check out some demo apps:

Installation

Add this line to your application's Gemfile:

gem 'external_asset_pipeline'

And then execute:

$ bundle

Usage

In config/application.rb, after Bundler.require(*Rails.groups), add:

require 'external_asset_pipeline/railtie'

See an example in examples/demo_app/config/application.rb

Additionally, in any environments where you don't want the asset manifest to be cached (e.g. in development), you should set

config.external_asset_pipeline.cache_manifest = false

See an example in examples/demo_app/config/environments/development.rb

Usage together with sprockets

If you'd like to use the external_asset_pipeline together with sprockets, you must also set

config.external_asset_pipeline.fall_back_to_sprockets = true

either in config/application.rb or in an initializer (e.g. config/initializers/assets.rb). This tells your app to first look for an asset in the external asset pipeline manifest, and if no asset is found then fall back to looking in the sprockets pipeline.

See an example in examples/demo_app/config/application.rb

Configuration

By default, the external_asset_pipeline will look for an asset manifest in public/packs/manifest.json. You can configure both the public subdirectory and the manifest filename:

config.external_asset_pipeline.assets_prefix = '/static'
config.external_asset_pipeline.manifest_filename = '.asset-manifest.json'

Usage with a manifest whose values already include the assets_prefix

By default, the external_asset_pipeline assumes that the manifest file contains values which are not already prefixed by the assets_prefix. For example, it assumes the manifest looks like:

{
  "application.css": "application-2ea8c3891d.css",
  "application.js": "application-7b3dc2436f7956c77987.js"
}

rather than:

{
  "application.css": "/packs/application-2ea8c3891d.css",
  "application.js": "/packs/application-7b3dc2436f7956c77987.js"
}

This assumption aligns with the default behavior of the webpack-assets-manifest plugin when no options are passed. However, if your manifest values include the assets prefix, as in the latter example (e.g. if you set the publicPath option to true or to '/packs/' when instantiating the WebpackAssetsManifest plugin), then you should set the prepend_assets_prefix_to_manifest_values configuration option to false:

config.external_asset_pipeline.prepend_assets_prefix_to_manifest_values = false

This will instruct the external_asset_pipeline to not prepend the assets prefix to your manifest values (otherwise, it would return doubly-prefixed paths like /packs//packs/application-2ea8c3891d.css).

Using with a dev server

You may also connect the external_asset_pipeline to a dev server (e.g. webpack-dev-server). To do so, enable the dev_server in the relevant environments:

config.external_asset_pipeline.dev_server.enabled = true

You may also optionally configure the dev_server host and port (their default values are 'localhost' and 3035, respectively):

config.external_asset_pipeline.dev_server.host = 'dev-server'
config.external_asset_pipeline.dev_server.port = 9000

Finally, you may optionally configure the dev_server.public_origin setting:

config.external_asset_pipeline.dev_server.public_origin = 'http://localhost:9000'

This will be used to generate the URLs in your asset tags. If this isn't set, the asset origin defaults to "http://#{dev_server.host}:#{dev_server.port}", so public_origin only needs to be set if that default doesn't work 1.

If the dev server is enabled but not running (i.e. we can't establish a connection to that port), the app will automatically fall back to returning assets from disk.

See an example in examples/demo_app/config/environments/development.rb

1 For example, if you're running everything in docker containers then the origin from which the browser requests assets may be different than the origin that the rails container uses to request the manifest from the dev server container.

Development

After checking out the repo, run script/bootstrap to install dependencies. Then, run bundle exec rake test:integration:prepare to set up the integration test environment and run script/test to run the tests. By default, that will exercise the demo_app example application. To exercise a different example application, set the EXAMPLE_APP environment variable to the corresponding directory name (e.g. EXAMPLE_APP=demo_app-rollup). To lint the code, run script/lint. You can also run script/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/rails-front-end/external_asset_pipeline.

License

The gem is available as open source under the terms of the MIT License.

external_asset_pipeline's People

Contributors

cjlarose avatar dependabot[bot] avatar rmacklin avatar thijsnado avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

external_asset_pipeline's Issues

Images?

I couldn't see any documentation on how to deal with images processed by webpack.

As far as I could see all your examples only use images as background, not as an <img src='what?' /> element.

Any clues - or write my own helper?

Errno::ENOENT No such file or directory manifest.json

For some applications where I use WDS, WDS is capable of serving all assets (its manifest contains all entries I need to use), so it's not necessary to generate the disk-based manifest file. On a fresh clone of my project, the on-disk manifest file doesn't exist.

This leads to the following unexpected scenario:

  1. Start WDS
  2. Try to include (via the asset helpers) an asset that does not exist

Expected:

ExternalAssetPipeline::AssetNotFound is raised telling me that the asset doesn't exist

Observed:

Errno::ENOENT No such file or directory @ rb_sysopen - /Users/chris.larose/dev/mycase/login/public/assets/manifest.json

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.