npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

webpack-sprockets-rails-manifest-plugin

v0.0.4

Published

A webpack plugin that allows [`sprockets-rails`](https://github.com/rails/sprockets-rails) to find files from a Webpack build without loading them via the asset pipeline.

Downloads

265

Readme

Webpack Sprockets Rails Manifest Plugin

A Webpack plugin that allows sprockets-rails to find static assets from a Webpack build without requiring them in sprockets directives. We can leverage standard Rails view helpers (i.e. javascript_include_tag, stylesheet_link_tag) to load artifacts supporting finger printing and prepended host urls.

Install

npm install webpack-sprockets-rails-manifest-plugin --save

Usage

# config/initializers/assets.rb
# Avoid creating the manifest file in public/assets as it can be downloaded by anyone
Rails.configuration.assets.manifest = Rails.root.join("config", "sprockets-manifest.json")
# config/environments/development.rb
config.assets.debug = false
<% # app/views/layouts/application.html.erb %>
<% # Use rails helpers in the same way you normally would with sprockets %>
<!DOCTYPE html>
<html lang="en">
  <head></head>
  <body>
    <%= javascript_include_tag "webpack-application-bundle" %>
  </body>
</html>
// client/bundles/webpack-application.js
alert("Howdy, Webpack is working!");
// client/webpack.config.js
var path = require("path");
var WebpackSprocketsRailsManifestPlugin = require("webpack-sprockets-rails-manifest-plugin");

var config = {
  context: __dirname,

  entry: {
    "webpack-application-bundle": "./bundles/webpack-application"
  },

  output: {
    path: path.resolve(__dirname, "../public/assets"),
    filename: "[name]-[chunkhash].js"
  },

  // Other config ...

  plugins: [
    new WebpackSprocketsRailsManifestPlugin(
      // [optional] this is the default location relative to the output directory 
      // that the assets will be built. e.g `public/assets`
      manifestFile: "../../config/sprockets-manifest.json"
    )
  ]
};

module.exports = config;

Heroku

Follow the instructions to install the ruby and node multi-buildpack.

Create a package.json file in the root of the rails project.

{
  "name": "MyProject",
  "version": "1.0.0",
  "description": "A description of MyProject",
  "main": "index.js",
  "engines": {
    "node": "5.12.0",
    "npm": "3.10.6"
  },
  "cacheDirectories": [
    "client/node_modules"
  ],
  "scripts": {
    "preinstall": "cd client && npm install",
    "postinstall": "cd client && npm run build"
  }
}

Hot Module Replacement

Rails provides us with the ability prepend a host to our asset paths generated by the Rails view helpers. This option is normally configured for production environments but we can use it in development to load assets from webpack-dev-server instead of public.

export ASSET_HOST=http://localhost:8080
# config/application.rb
# ...

if ENV["ASSET_HOST"].present?
  Rails.application.config.action_controller.asset_host = proc { |source, _request|
    ENV["ASSET_HOST"]
  }
end

Gradually Replacing the Asset Pipeline with Webpack

The Webpack ecosystem works best when it controls a whole subpath. For greenfield projects this is easy to achieve. However if you're running a legacy Rails app, don't fret! Webpack can copy build files into a location so that they can be required via sprockets directives. We recommend using this technique when you're just getting started, but the end goal should be to have Webpack completely control the build process of your static assets. For more information and setup instructions check out the separate plugin webpack-copy-after-build-plugin.

How Does It Work?

sprockets-rails provides two strategies to map logical paths (webpack-application.js) to file system paths (webpack-application-8f88619b6ef3a358a7ad.js). By using the manifest strategy we can load finger printed static assets via the existing view helpers (i.e. javascript_include_tag, stylesheet_link_tag).

Environment

Searches the default sprockets directory for a file type manifest. By default this is only enabled in the dev and test environments e.g.

// app/assets/javascripts/application.js
//= require_self
alert("Hi, I'm a Javascript manifest in the default Sprockets Asset Pipeline");

Manifest

A json file which stores a mapping between a compiled asset and the original logical path, along with meta information such as size, creation time and integrity hash. webpack-sprockets-rails-manifest-plugin uses this manifest file to make sprockets-rails aware of the files and meta information in the webpack build. Since Rails 3.x sprockets-rails writes it's manifest file to a dynamic location using a randomly generated filename (e.g. public/assets/.sprockets-manifest-b4b2e3829c91f2f867c051a855232bed.json). To get sprockets-rails and webpack talking to each other we need to configure sprockets-rails to write it's manifest file to a static location that webpack knows about.

If Rails.configuration.assets.debug = false the asset pipeline will check the manifest first, if an entry can't be found, it falls back and checks the file system through the environment strategy.

Examples

Head over to https://github.com/rupurt/react_on_rails_simplified_configuration_examples for examples on how to configure:

  • Greenfield Rails projects
  • Legacy Rails projects
  • Hot Module Replacement

TODO

  • [x] - support webpack-dev-server
  • [x] - gradual migration documentation
  • [x] - sample projects
  • [ ] - tests
  • [ ] - script integrity

License

MIT