Giter Club home page Giter Club logo

grunt-static-versioning's Introduction

grunt-static-versioning Build Status

Version static assets

Getting Started

This plugin requires Grunt ~0.4.0

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-static-versioning --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-static-versioning');

NOTE: This plugin requires some extra work on your end. It will create the versioned assets and a config file (PHP and JSON at the moment with more to come), but you will need to write an adapter to use that file. There will be examples in this repo under the examples directory.

Options

cwd

Type: String
Default: ''

Specify the location of the outputted files. This is generally the root folder of the web project, e.g. the public directory. This path is excluded from the final file output.

env

Type: String
Default: 'prod'

Specify which files the task will copy over to the destination directory. Can be: prod or dev.

output

Type: String
Default: 'json'

Specify the type of config file to generate. This will generate an assets.config.[ EXTENSION ] file which can be used to insert the proper files in the source. The config file will only contain paths to development or minified files based on the env option. Can be: json or php.

outputConfigDir

Type: String
Default: ''

Specify the location where the config file will be generated. If left empty, it will used the cwd option.

namespace

Type: String
Default: 'static.assets'

Namespace wrapping the file assets config generated file.

Files array options

assets

Type: Array
Default: null

An array of files that will be versioned. They can be manually entered or leveraged from the uglify and cssmin tasks. NOTE: If the files are provided manually, they MUST exist. This task does NOT concat/minify any files, it simply renames and copies existing files to proper destination.

dest

Type: String
Default: ''

Offers an additional level of organization. The task options' dest paramater provided access to the web public folder while this option offers the organizational directories. E.g. js and css.

type

Type: String
Default: ''

This option is used to create structure to the config object the task creates. Can be: js or css.

ext

Type: String
Default: ''

The extension to apply to the files. Files will follow the naming pattern: [filename].[md5_hash].[ext]. E.g.: .js, .css, .min.js, .min.css.

key

Type: String
Default: ''

The key is used to organize the files in the config file. The same key can be used multiple times to organized multiples files together.

bypass

Type: Boolean
Default: false

If set to true, the file will bypass versioning and simply append the given extension.

Usage Examples

Example config

grunt.initConfig({
  versioning: {               // Task
    options: {                // Task options
      cwd: ''
    },
    dist: {                   // Target
      options: {              // Target options
      },
      files: [{
        assets: [{
            src: [ 'file1.js', 'file2.js' ],
            dest: 'tmp/js/main.min.js'
        }, {
            src: [ 'plugin1.js', 'plugin2.js' ],
            dest: 'tmp/js/plugin.min.js'
        }],
        key: 'global',
        dest: 'js',
        type: 'js',
        ext: '.min.js'
      }, {
        assets: [{
            src: [ 'main.css' ],
            dest: 'tmp/css/main.min.css'
        }],
        key: 'global',
        dest: 'css',
        type: 'css',
        ext: '.min.css'
      }]
    }
  }
});

grunt.loadNpmTasks('grunt-static-versioning');

grunt.registerTask('default', ['versioning']);

Leveraging other tasks

This task is built to leverage other existing tasks. Instead of manually providing the files array, it can be read from the uglify and cssmin tasks.

grunt.initConfig({
  cssmin: {
    main: {
      files: [{
        src: [ 'main.css' ],
        dest: 'tmp/main.min.css'
      }]
    }
  },

  uglify: {
    main: {
      files: [{
        src: [
          'file1.js',
          'file2.js',
          'components/test/test.js'
        ],
        dest: 'tmp/main.min.js'
      }]
    },
    plugin: {
      files: [{
        src: [
          'file3.js',
          'file4.js'
        ],
        dest: 'tmp/plugin.min.js'
      }]
    }
  },

  versioning: {
    options: {
      cwd: 'public',
      outputConfigDir: 'public/config'
    },
    dist: {
      files: [{
        assets: '<%= uglify.main.files %>',
        key: 'global',
        dest: 'js',
        type: 'js',
        ext: '.min.js'
      }, {
        assets: '<%= uglify.plugin.files %>',
        key: 'global',
        bypass: true,
        dest: 'js',
        type: 'js',
        ext: '.min.js'
      }, {
        assets: '<%= cssmin.main.files %>',
        key: 'global',
        dest: 'css',
        type: 'css',
        ext: '.min.css'
      }]
    }
  }
});

grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-static-versioning');

grunt.registerTask('default', ['uglify', 'cssmin', 'versioning']);

The above example would output:

  • public/js/main.[ MD5_HASH ].min.js
  • public/js/plugin.min.js (no hash because of bypass option)
  • public/css/main.[ MD5_HASH ].min.css
  • public/config/assets.config.json

Sample assets.config

JSON

Prod

{
  "staticAssets": {
    "global": {                // <--- 'key' option
      "js": [
        "/js/main.c2e864c8.min.js",
        "/js/plugin.min.js"
      ],
      "css": [
        "/css/main.b6f17edb.min.css"
      ]
    },
    "all": {                   // <--- 'key' option
      "js": [
        "/js/all.625a4fd0.min.js"
      ],
      "css": []
    }
  }
}

Dev

{
  "staticAssets": {
    "global": {                // <--- 'key' option
      "js": [
        "/js/file1.js",
        "/js/file2.js",
        "/js/test.js",
        "/js/file3.js",
        "/js/file4.js"
      ],
      "css": [
        "/css/main.css"
      ]
    },
    "all": {                   // <--- 'key' option
      "js": [
        "/js/file1.js",
        "/js/file2.js",
        "/js/file3.js",
        "/js/file4.js"
      ],
      "css": []
    }
  }
}

PHP

Prod

<?php
return array(
    'staticAssets' => array(
        'global' => array(
            'css' => array(
                'css/main.b6f17edb.css',
            ),
            'js' => array(
                'js/main.c2e864c8.js',
                'js/plugin.24d54461.js',
            ),
        ),
        'all' => array(
            'css' => array(
            ),
            'js' => array(
                'js/all.625a4fd0.js',
            ),
        ),
    )
);

Dev

<?php
return array(
    'staticAssets' => array(
        'global' => array(
            'css' => array(
                '/css/main.css',
            ),
            'js' => array(
                '/js/file1.js',
                '/js/file2.js',
                '/js/test.js',
                '/js/file3.js',
                '/js/file4.js',
            ),
        ),
        'all' => array(
            'css' => array(
            ),
            'js' => array(
                '/js/file1.js',
                '/js/file2.js',
                '/js/file3.js',
                '/js/file4.js',
            ),
        ),
    )
);

Release history

  • 2014-04-01 0.2.2
  • 2013-12-03 0.2.1
  • 2013-09-17 0.2.0
  • 2013-08-26 0.1.1 Initial release. Not yet officially released.
  • 2013-08-26 0.1.0 Initial release. Not yet officially released.

License

Copyright (c) 2013 CanvasPop

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

grunt-static-versioning's People

Contributors

fabien-d avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

grunt-static-versioning's Issues

Use the generated PHP file

I'm usually a JavaScript /node.js guy but have to use PHP for this project I'm working on.

How do I get the values from the generated array in the PHP config file?

Add flag to allow files to bypass versioning

e.g.

versioning: {
    options: {
      cwd: 'public',
      outputConfigDir: 'public/config'
    },
    dist: {
      files: [{
        assets: '<%= uglify.main.files %>',
        bypass: true, // <-- flag to skip versioning for files
        key: 'global',
        dest: 'js',
        type: 'js',
        ext: '.min.js'
      }]
    }
}

More details to come.

wildcard for src files

Would be cool if you could use a wildcard for src files... ie:

files: [
      assets: [
        src: ["tmp/js/**/*.js"]
      ]
      key: "main"
      dest: "js"
      type: "js"
      ext: ".js"
]

The reason is that I have a number of files that I am compiling with coffeescript, a whole folder that just gets compiled automatically. It would be sweet if in "dev mode" if versioning it listed out all those files in the assets.config file so it's easier to debug. It's just full of custom jquery plugins and other randoms that's that have no importance of precedence

maybe there's already a way to do this?

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.