gruntjs / grunt-contrib-compress

Compress files and folders.
http://gruntjs.com/
MIT License
345 stars 114 forks source link
hacktoberfest

grunt-contrib-compress v2.0.0 Build Status

Compress files and folders

Getting Started

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-contrib-compress --save-dev

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

grunt.loadNpmTasks('grunt-contrib-compress');

This plugin was designed to work with Grunt >= 0.4.x. If you're still using grunt v0.3.x it's strongly recommended that you upgrade, but in case you can't please use v0.3.2.

Compress task

Run this task with the grunt compress command.

Task targets, files and options may be specified according to the grunt Configuring tasks guide.

Node Libraries Used: archiver (for zip/tar) zlib (for gzip).

Options

archive

Type: String or Function
Modes: zip tar

This is used to define where to output the archive. Each target can only have one output file. If the type is a Function it must return a String.

This option is only appropriate for many-files-to-one compression modes like zip and tar. For gzip for example, please use grunt's standard src/dest specifications.

mode

Type: String

This is used to define which mode to use, currently supports gzip, deflate, deflateRaw, tar, tgz (tar gzip),zip and brotli.

Automatically detected per dest:src pair, but can be overridden per target if desired.

level

Type: Integer
Modes: zip gzip
Default: 1

Sets the level of archive compression.

brotli

Configure brotli compression settings:

Type: Object
Default:

{
    [zlib.constants.BROTLI_PARAM_MODE]: 0,
    [zlib.constants.BROTLI_PARAM_QUALITY]: 11,
    [zlib.constants.BROTLI_PARAM_LGWIN]: 22,
    [zlib.constants.BROTLI_PARAM_LGBLOCK]: 0
}

Do not forget require zlib for zlib.constants, example:

const zlib = require('zlib');
mode

Type: Integer

Default: 0

quality

Controls the compression-speed vs compression-density tradeoffs. The higher the quality, the slower the compression. Range is 0 to 11.

Type: Integer
Default: 11

lgwin

Base 2 logarithm of the sliding window size. Range is 10 to 24.

Type: Integer
Default: 22

lgblock

Base 2 logarithm of the maximum input block size. Range is 16 to 24. If set to 0, the value will be set based on the quality.

Type: Integer
Default: 0

pretty

Type: Boolean
Default: false

Pretty print file sizes when logging.

createEmptyArchive

Type: Boolean
Default: true

This can be used when you don't want to get an empty archive as a result, if there are no files at the specified paths.

It may be useful, if you don't clearly know if files exist and you don't need an empty archive as a result.

File Data

The following additional keys may be passed as part of a dest:src pair when using an Archiver-backed format. All keys can be defined as a Function that receives the file name and returns in the type specified below.

date

Type: Date
Modes: zip tar tgz

Sets the file date.

mode

Type: Integer
Modes: zip tar tgz

Sets the file permissions.

store

Type: Boolean
Default: false

If true, file contents will be archived without compression.

comment

Type: String
Modes: zip

Sets the file comment.

gid

Type: Integer
Modes: tar tgz

Sets the group of the file in the archive

uid

Type: Integer
Modes: tar tgz

Sets the user of the file in the archive

Usage Examples

// make a zipfile
compress: {
  main: {
    options: {
      archive: 'archive.zip'
    },
    files: [
      {src: ['path/*'], dest: 'internal_folder/', filter: 'isFile'}, // includes files in path
      {src: ['path/**'], dest: 'internal_folder2/'}, // includes files in path and its subdirs
      {expand: true, cwd: 'path/', src: ['**'], dest: 'internal_folder3/'}, // makes all src relative to cwd
      {flatten: true, src: ['path/**'], dest: 'internal_folder4/', filter: 'isFile'} // flattens results to a single level
    ]
  }
}
// gzip assets 1-to-1 for production
compress: {
  main: {
    options: {
      mode: 'gzip'
    },
    expand: true,
    cwd: 'assets/',
    src: ['**/*'],
    dest: 'public/'
  }
}
// compress a file to a different location than its source
// example compresses path/the_file to /the_file inside archive.zip
compress: {
  main: {
    options: {
      archive: 'archive.zip'
    },
    files: [{
      expand: true,
      cwd: 'path/',
      src: ['the_file'],
      dest: '/'
    }]
  }
},
// use custom extension for the output file
compress: {
  main: {
    options: {
      mode: 'gzip'
    },
    // Each of the files in the src/ folder will be output to
    // the dist/ folder each with the extension .gz.js
    files: [{
      expand: true,
      src: ['src/*.js'],
      dest: 'dist/',
      ext: '.gz.js'
    }]
  }
}
// use a function to return the output file
compress: {
  main: {
    options: {
      archive: function () {
        // The global value git.tag is set by another task
        return git.tag + '.zip'
      }
    },
    files: [{
      expand: true,
      src: ['src/*.js'],
      dest: 'dist/'
    }]
  }
}
// brotlify assets 1-to-1 for production using default options
compress: {
  main: {
    options: {
      mode: 'brotli'
    },
    expand: true,
    cwd: 'assets/',
    src: ['**/*.js'],
    dest: 'public/',
    extDot: 'last',
    ext: '.js.br'
  }
}
// brotlify assets 1-to-1 for production specifying text mode
// and using default options otherwise
compress: {
  main: {
    options: {
      mode: 'brotli',
      brotli: {
        mode: 1
      }
    },
    expand: true,
    cwd: 'assets/',
    src: ['**/*.js'],
    dest: 'public/',
    extDot: 'last',
    ext: '.js.br'
  }
}

Release History


Task submitted by Chris Talkington

This file was generated on Sat Dec 12 2020 14:15:27.