Code Monkey home page Code Monkey logo

grunt-hash's Introduction

grunt-hash

Append a unique hash to the end of a filename for cache busting. For example:

examples/test1.js => examples/dist/test1.b93fd451.js

##Grunt 0.4

This task now depends on grunt 0.4.x. Please see the grunt 0.3 to 0.4 migration guide for more details.

If you are still using grunt 0.3, please install grunt-hash 0.1x

Getting Started

Install this grunt plugin next to your project's grunt.js gruntfile with: npm install grunt-hash

Then add this line to your project's grunt.js gruntfile:

grunt.loadNpmTasks('grunt-hash');

Documentation

grunt.initConfig({
	hash: {
		options: {
			mapping: 'examples/assets.json', //mapping file so your server can serve the right files
			srcBasePath: 'examples/', // the base Path you want to remove from the `key` string in the mapping file
			destBasePath: 'out/', // the base Path you want to remove from the `value` string in the mapping file
			flatten: false, // Set to true if you don't want to keep folder structure in the `key` value in the mapping file
			hashLength: 8, // hash length, the max value depends on your hash function
			hashFunction: function(source, encoding){ // default is md5
				return require('crypto').createHash('sha1').update(source, encoding).digest('hex');
			}
		},
        js: {
            src: 'examples/*.js',  //all your js that needs a hash appended to it
            dest: 'examples/dist/js/' //where the new files will be created
        },
        css: {
            src: 'examples/*.css',  //all your css that needs a hash appended to it
            dest: 'examples/dist/css/' //where the new files will be created
        }
	}
});
grunt.loadNpmTasks('grunt-hash');

Configuration follow the multi-task standard configuration format: http://gruntjs.com/configuring-tasks

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.

License

Copyright (c) 2012 Greg Allen
Licensed under the MIT license.

grunt-hash's People

Contributors

abimaelmartell avatar daithiocrualaoich avatar dessibelle avatar eltimn avatar jgallen23 avatar michaellopez avatar phamann avatar pushpak avatar sboudrias avatar solarissmoke 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  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

grunt-hash's Issues

NPM 0.4.1 does not point to your tagged 0.4.1 version

I have noticed that the version 0.4.1 that you published to NPM does not contain the latest changes we pull requested this week which you have tagged correctly as 0.4.1 in the repo.

This includes the parameterization of the hash character length, which we now depend on in our project.

Would it be possible for you to re-publish to npm?

Steps to reproduce

In a clean directory, run:
npm install grunt-hash

Inside grunt-hash/tasks/hash.js line 42 should read as per the original:

var hash = getHash(source, 'utf8').substr(0, options.hashLength);

Files not being copied to the destination folder

I have the following configuration:
hash: {
options: {
mapping: "./public/assets.json"
},
src: ["public/*/.js"],
dest: 'public/dist/'
}

The hashed files are being created in the src folder in the root directory of the application rather than the folder specified in the configuration.

Does not work

I'm using node v4.3 and npm 2.14 and the default example copied and pasted does not work. An empty source map is created and that's all... I've tried the bare bones, etc. and no dice. Any recs?

Remove the source file

The source file is useless since I have a new file with the hash in the name, is it possible to remove the source file?

Update Src Page?

Stilly question maybe, but how do you update the HTML file (i.e. index.html or other) to link to the new hash-containing file names? Manually?

Create a new tag?

Hi @jgallen23! Could it be possible to tag and publish a new release to use with our grunt tasks? The expand true bug was solved after the last 0.5.0 tag. thanks!

Paths are not respecting folders

given an input

cfg.hash = {
    options: {
        mapping: cfg.Ss.path.temp + 'assets.json',
        hashLength: 8
        flatten: true
        // hashFunction: function(source, encoding){ // default is md5
        //     return require('crypto').createHash('sha1').update(source, encoding).digest('hex');
        // }
    },
    js: {
        options: {
            srcBasePath : cfg.Ss.path.dev + 'js/',
                    destBasePath: cfg.Ss.path.temp + 'hash/js/'
        },
        src: cfg.Ss.path.dev + 'js/**/*.js',
        dest: cfg.Ss.path.temp + 'hash/js'
    },

I expect the output to be

.../hash/js/folder/path/filename.js

Instead it's

.../hash/js/filename.js

Following up with a PR to fix this.

Support multitask format

Hi!

Is there a reason why this task isn't a grunt "multitask"?

That'd allow the setting of multiple configs for multiple files types or sub-app, etc.

Let me know what you think about it, I could even work on a PR.

Grunt 0.4 Release

I'm posting this issue to let you know that we will be publishing Grunt 0.4 on Monday, February 18th.

If your plugin is not already Grunt 0.4 compatible, would you please consider updating it? For an overview of what's changed, please see our migration guide.

If you'd like to develop against the final version of Grunt before Monday, please specify "grunt": "0.4.0rc8" as a devDependency in your project. After Monday's release, you'll be able to use "grunt": "~0.4.0" to actually publish your plugin. If you depend on any plugins from the grunt-contrib series, please see our list of release candidates for compatible versions. All of these will be updated to final status when Grunt 0.4 is published.

Also, in an effort to reduce duplication of effort and fragmentation in the developer community, could you review the grunt-contrib series of plugins to see if any of your functionality overlaps significantly with them? Grunt-contrib is community maintained with 40+ contributors—we'd love to discuss any additions you'd like to make.

Finally, we're working on a new task format that doesn't depend on Grunt: it's called node-task. Once this is complete, there will be one more conversion, and then we'll never ask you to upgrade your plugins to support our changes again. Until that happens, thanks for bearing with us!

If you have any questions about how to proceed, please respond here, or join us in #grunt on irc.freenode.net.

Thanks, we really appreciate your work!

Are you open to add new exports formats for assets.json?

I use grunt-hash with Ruby On Rails (I don't like sprockets) and I have made a very small view helper to open the assets.json mapping and load it into a variable accessible everywhere. Are you open to add some kind of templates for the mapping output?
I saw you already have a PHP output with json_decode. Could be nice if we have a dedicated simple template output format with Ruby, PHP, JavaScript, .NET and others. I can start with Ruby, let me know if you're interested in doing so.

Thanks.
Damian

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.