What is assets-webpack-plugin?
The assets-webpack-plugin is a Webpack plugin that generates a JSON file containing the paths of the output files. This is useful for server-side rendering or when you need to inject the assets into your HTML manually.
What are assets-webpack-plugin's main functionalities?
Generate JSON file with asset paths
This feature allows you to generate a JSON file that contains the paths of the output files. This is useful for server-side rendering or when you need to inject the assets into your HTML manually.
const AssetsWebpackPlugin = require('assets-webpack-plugin');
module.exports = {
// other webpack configuration
plugins: [
new AssetsWebpackPlugin({
path: path.join(__dirname, 'dist'),
filename: 'assets.json'
})
]
};
Customizing the output format
This feature allows you to customize the format of the generated JSON file. For example, you can enable pretty printing to make the JSON file more readable.
const AssetsWebpackPlugin = require('assets-webpack-plugin');
module.exports = {
// other webpack configuration
plugins: [
new AssetsWebpackPlugin({
path: path.join(__dirname, 'dist'),
filename: 'assets.json',
prettyPrint: true
})
]
};
Including additional information
This feature allows you to include additional information in the generated JSON file. For example, you can specify which file types to include in the JSON file.
const AssetsWebpackPlugin = require('assets-webpack-plugin');
module.exports = {
// other webpack configuration
plugins: [
new AssetsWebpackPlugin({
path: path.join(__dirname, 'dist'),
filename: 'assets.json',
includeAllFileTypes: false,
fileTypes: ['js', 'css']
})
]
};
Other packages similar to assets-webpack-plugin
html-webpack-plugin
The html-webpack-plugin simplifies the creation of HTML files to serve your webpack bundles. It automatically injects the output files into the HTML file. Unlike assets-webpack-plugin, which generates a JSON file with asset paths, html-webpack-plugin directly creates an HTML file with the necessary script and link tags.
webpack-manifest-plugin
The webpack-manifest-plugin generates a manifest file that maps the original filenames to the hashed filenames. This is similar to assets-webpack-plugin, but it focuses on creating a manifest file for cache busting and long-term caching.
webpack-assets-manifest
The webpack-assets-manifest plugin generates a JSON file that maps the original filenames to the hashed filenames, similar to assets-webpack-plugin. However, it provides more customization options and supports merging with existing manifest files.
assets-webpack-plugin
Webpack plugin that emits a json file with assets paths.
Why is this useful?
When working with Webpack you might want to generate you bundles with a generated hash in them (for cache busting).
This plug-in outputs a json file with the generated assets so you can find the assets from somewhere else.
Example output:
As of version 2.0 the output now includes keys for different assets kinds.
{
"one": {
"js": "one-bundle.js",
"jsMap": "one-bundle.js.map"
},
"two": {
"js": "two-bundle.js"
}
}
Install
npm install assets-webpack-plugin --save-dev
Configuration
In you webpack config include the plug-in. And add it to your config:
var path = require('path');
var AssetsPlugin = require('assets-webpack-plugin');
var assetsPluginInstance = new AssetsPlugin();
module.exports = {
...
output: {
path: path.join(__dirname, "public", "js"),
filename: "[name]-bundle-[hash].js",
publicPath: "/js/"
},
....
plugins: [assetsPluginInstance]
};
Options
You can pass the following options:
path:
Path where to save the created json file. Defaults to the current directory.
new AssetsPlugin({path: path.join(__dirname, 'app', 'views')})
filename:
Name for the created json file. Defaults to webpack-assets.json
new AssetsPlugin({filename: 'assets.json'})
Using this with Rails
You can use this with Rails to find the bundled Webpack assets via sprockets. In ApplicationController
you might have:
def script_for(bundle)
path = Rails.root.join('app', 'views', 'webpack-assets.json')
file = File.read(path)
json = JSON.parse(file)
json[bundle]['js']
end
Then in the actions:
def show
@script = script_for('clients')
end
And finally in the views:
<div id="app">
<script src="<%= @script %>"></script>
</div>
Test
npm test