https://github.com/davemo/grunt-rails-asset-digest
grunt-rails-asset-digest
https://github.com/davemo/grunt-rails-asset-digest
Last synced: about 1 year ago
JSON representation
grunt-rails-asset-digest
- Host: GitHub
- URL: https://github.com/davemo/grunt-rails-asset-digest
- Owner: davemo
- License: mit
- Created: 2013-10-14T14:08:07.000Z (over 12 years ago)
- Default Branch: main
- Last Pushed: 2024-06-17T11:24:16.000Z (almost 2 years ago)
- Last Synced: 2025-03-08T17:09:23.682Z (about 1 year ago)
- Language: CoffeeScript
- Size: 130 KB
- Stars: 15
- Watchers: 2
- Forks: 7
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE-MIT
Awesome Lists containing this project
README
# grunt-rails-asset-digest
[](https://github.com/davemo/grunt-rails-asset-digest/actions/workflows/node.js.yml)
## Rails Version Support
Rails 4 significantly changed how the asset manifest is generated. As such if you are still on Rails 3.2 (using manifest.yml) you should use `v0.3.2` of this plugin `npm install grunt-rails-asset-digest@0.3.2 --save`; `v1.4.1` tracks support for Rails 4 and above.
> Generates fingerprinted assets and appends entries to a sprockets-rails manifest.json
## Intended Use
This plugin is intended to be used by those who are integrating a grunt workflow alongside of the [sprockets-rails](https://github.com/rails/sprockets-rails). As such, this task expects to be run _after_ the [sprockets-rails](https://github.com/rails/sprockets-rails) has generated an asset manifest.json file.
This approach works well in that it only manages fingerprinting and manifest modification for the files that are explicitly managed in the `files` property and it guarantees that existing entries generated by [sprockets-rails](https://github.com/rails/sprockets-rails) will not be touched.
## Why would I use this?
If you are using something like [Lineman](http://www.linemanjs.com) or [Grunt](http://www.gruntjs.com) and want more control over an advanced front-end workflow to be used alongside a legacy Rails application. A great candidate for this task is to utilize it during construction of a rich-client JavaScript application that lives inside a Rails application. You can let the Rails asset pipeline manage legacy files, and let your Grunt workflow manage your rich-client files.
Ideally this task should not exist; the best way to build a rich-client JavaScript application is to decouple it completely from the serverside. However this is a reasonable _first step_ in refactoring to extract assets from the world of rails/sprockets and still be able to hook into most rails app deploy processes.
## How would I integrate this task?
Adjust your rails deployment lifecycle to run this task prior to deploying/uploading assets (whether to a CDN, or just in `public/assets` which is where this task will dump fingerprinted assets by default). Here's an example configuration in a [Capistrano](https://github.com/capistrano/capistrano) deploy setup that uses [Lineman](http://www.linemanjs.com) to manage the lifecycle of a rich-client application that still lives within a rails source tree. (The same techniques will work with vanilla Grunt, you'll just have to manage the task aliasing yourself)
```coffeescript
# linemans config/application.coffee
appTasks:
common: [
"sass"
"coffee"
"concat"
]
dist: [
"uglify"
"rails_asset_digest"
"clean"
]
```
```ruby
# config/deploy.rb
after 'assets:compress', 'assets:compile_with_lineman', 'assets:upload'
task :compile_with_lineman
logger.important "Compiling other assets with Lineman"
run "lineman build" # run grunt tasks in common and dist phases
end
```
**Note:** this is a _very_ narrowly focused task, and you might not get much use out of it :)
## Getting Started
This plugin requires Grunt `~0.4.1`
If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
```shell
npm install grunt-rails-asset-digest --save-dev
```
Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
```js
grunt.loadNpmTasks('grunt-rails-asset-digest');
```
## The "rails_asset_digest" task
### Overview
In your project's Gruntfile, add a section named `rails_asset_digest` to the data object passed into `grunt.initConfig()`.
```js
grunt.initConfig({
rails_asset_digest: {
options: {
algorithm: 'md5', // default fingerprinting algorithm to use
assetPath: 'public/assets' // default location where manifest.json lives
},
your_target: {
files: {
// dest : src
"public/assets/javascript-file.js" : "input/path/to/javascript-file.js",
"public/assets/sourcemap-file.js.map" : "input/path/to/sourcemap-file.js.map",
"public/assets/style.css" : "input/path/to/style.css"
}
},
},
})
```
### Sample Output
Given the configuration in the Overview section above, you can expect `grunt rails_asset_digest` to output the following to `public/assets`
```shell
ls -la public/assets
manifest.json
javascript-file-a5a14aa0f19b8fe989f3b79fc72b9b36.js
sourcemap-file-365b31e16181703b506e90b57f95b568.js.map
style-1fd9137f040f2440d26da164c65e7f66.css
```
And the associaated fingerprinted entries in manifest.json like so:
```json
{
"files" : {
"javascript-file-a5a14aa0f19b8fe989f3b79fc72b9b36.js" : {
"mtime": "2014-02-04T18:14:52.0",
"digest": "a5a14aa0f19b8fe989f3b79fc72b9b36",
"size": 32,
"logical_path": "javascript-file.js"
},
"sourcemap-file-365b31e16181703b506e90b57f95b568.js.map" : {
"mtime": "2014-02-04T18:14:52.0",
"digest": "365b31e16181703b506e90b57f95b568",
"size": 49,
"logical_path": "sourcemap-file.js.map"
},
"style-1fd9137f040f2440d26da164c65e7f66.css" : {
"mtime": "2014-02-04T18:14:52.0",
"digest": "1fd9137f040f2440d26da164c65e7f66",
"size": 17,
"logical_path": "style.css"
}
},
"assets" : {
"javascript-file.js" : "javascript-file-a5a14aa0f19b8fe989f3b79fc72b9b36.js",
"sourcemap-file.js" : "sourcemap-file-365b31e16181703b506e90b57f95b568.js.map",
"style.css" : "style-1fd9137f040f2440d26da164c65e7f66.css"
}
}
```
### Options
#### options.algorithm
Type: `String`
Default value: `md5`
The digest algorithm used to fingerprint the assets, *note:* Rails 4 (via the [sprockets-rails](https://github.com/rails/sprockets-rails) plugin) doesn't actually use an MD5 hash based on the contents of the file anymore, it just uses a random hex digest. It doesn't matter what value is in the hash of the filename as long as the entry in the manifest matches the name of the file on disk.
#### options.assetPath
Type: `String`
Default value: `public/assets`
The location of the rails asset path
#### options.manifestName
Type: `String`
Default value: `manifest.json`
The name of the manifest file, so you can provide your own secret to prevent people guessing the name if so desired.
## Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).
## Running Specs
* clone this repo
* `npm install`
* `npm test`
## Changelog
## Release History
* 2021-11-19 v1.4.1 update deps for grunt 1.4.0 + refactor tests
* 2014-02-05 v0.4.0 support rails 4 + sprockets-rails manifest.json format
* 2014-02-04 v0.3.2 no-op: align version number with supported rails version
* 2013-10-15 v0.2.0 bugfix: asserts the output of written files actually exist
* 2013-10-15 v0.1.1 bugfix: normalizes asset path to append trailing /
* 2013-10-14 v0.1.0 Initial release.
## License
MIT