Giter Club home page Giter Club logo

assets-webpack-plugin's Introduction

assets-webpack-plugin

Webpack plugin that emits a json file with assets paths.

Why is this useful?

When working with Webpack will probably want to generate you bundles with a generate hash in them (for cache busting).

This plug-in generates a json file with the generated assets to be used somewhere else, I use this with Rails so it can find the assets.

Install

npm install assets-webpack-plugin --save

Configuration

In you webpack config include the plug-in. And add it to your config:

var path = require("path");
var SaveAssetsJson = require('assets-webpack-plugin');

module.exports = {
  ...
  output: {
    path: path.join(__dirname, "public", "js"),
    filename: "[name]-bundle-[hash].js",
    publicPath: "/js/"
  },
  ....
  plugins: [new SaveAssetsJson()]
};  

Options

You can pass the following options:

path:

Path where to save the created json file. Defaults to the current directory.

new SaveHashes({path: path.join(__dirname, 'app', 'views')})

filename:

Name for the created json file. Defaults to webpack-assets.json

new SaveHashes({filename: 'assets.json'})

Using this with Rails

I use this with Rails, so it can find my Webpack assets. In my application controller I have:

def script_for(bundle)
  path = Rails.root.join('app', 'views', 'webpack-assets.json') # This is the file generated by the plug-in
  file = File.read(path)
  json = JSON.parse(file)
  json[bundle]
end

Then in the actions:

def show
  @script = script_for('clients') # this will retrive the bundle named 'clients'
end

And finally in the views:

<div id="app">
  <script src="<%= @script %>"></script>
</div>

Test

npm test

assets-webpack-plugin's People

Contributors

tulios avatar

Watchers

 avatar  avatar  avatar

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.