Home

Awesome

grunt-premailer

Grunt wrapper task for Premailer

Requirements

This plugin is a Grunt wrapper around the Premailer Ruby gem developed by Alex Dunae. In order to run it you will need the following packages installed on your system:

Getting Started

This plugin is compatible with Grunt 1.x but requires Grunt >=0.4.2

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-premailer --save-dev

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

grunt.loadNpmTasks('grunt-premailer');

The "premailer" task

Overview

In your project's Gruntfile, add a section named premailer to the data object passed into grunt.initConfig().

grunt.initConfig({
  premailer: {
    options: {
      // Task-specific options go here.
    },
    your_target: {
      // Target-specific file lists and/or options go here.
    },
  },
})

Options

options.mode

Type: String Default value: 'html'

Output format. Either 'html' (HTML formatted email) or 'txt' (plaintext email).

options.baseUrl

Type: String Default value: ''

Base URL to append to relative links.

options.bundleExec

Type: Boolean Default value: false

Run premailer with bundle exec ruby.

options.queryString

Type: String Default value: ''

Query string to append to links.

options.css

Type: Array Default value: []

Additional CSS stylesheets to process. Paths are relative to the Gruntfile.js file. Any Grunt compatible globbing and template syntax is supported.

options.removeClasses

Type: Boolean Default value: false

Removes HTML classes.

options.removeScripts

Type: Boolean Default value: false

Removes HTML scripts. _(was true by default until v0.2.5)

options.removeComments

Type: Boolean Default value: false

Removes HTML comments.

options.removeIds

Type: Boolean Default value: false

Removes HTML ids.

options.preserveStyles

Type: Boolean Default value: false

Preserve any link rel=stylesheet and style elements.

options.preserveStyleAttribute

Type: Boolean Default value: false

Preserve original style attribute.

options.lineLength

Type: Number Default value: 65

Line length for plaintext version.

options.ioException

Type: Boolean Default value: false

Aborts on I/O errors.

options.verbose

Type: Boolean Default value: false

Prints additional information at runtime.

options.warnLevel

Type: String Default value: 'safe'

What level of CSS compatibility warnings to show (either 'none', 'safe', 'poor', 'risky').

Usage Examples

Default Options

In this example, the default options are used to inline CSS styles in into the markup.

grunt.initConfig({
  premailer: {
    simple: {
      options: {},
      files: {
        'dest/email.html': ['src/email.html']
      }
    }
  }
})

Custom Options

In this example, custom options are used to append a query string (such as a google campaign click tracking) to every link.

grunt.initConfig({
  premailer: {
    withqs: {
      options: {
        queryString: 'utm_source=premailer&utm_medium=email&utm_campaign=test'
      },
      files: {
        'dest/email.html': ['src/email.html']
      }
    }
  }
})

Usage Notes

BaseUrl option and stylesheets parsing

Be aware that the base URL gets applied before inlining styles. This process will convert all relative linked stylesheets to absolute ones, possibly preventing the parser to retrieve the resources (since link tags point to a different location). In this scenario it's advisable to use the css option since it's uneffected from BaseUrl.

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.

Release History

1.0.0 Updated dependency and Grunt 1.x ready

0.2.13 Added option warnLevel and removeIds

0.2.12 Dependencies updated. Fixing a bug when source and destination files are the same (#25)

0.2.11 Fixing EventEmitter error (#21)

0.2.10 Merging #20 (thanks to @Iszak)

0.2.9 Merging #19 to add Bundler support (thanks to @Iszak)

0.2.8 Merging #18 to fix #16 and #17.

0.2.7 Minor bug fixing

0.2.6 Added option removeScriptsto preserve/remove script tags (was true by default until v0.2.5)

0.2.5 Added ability to remove temp folders even outside project folder (thanx to @fadomire)

0.2.4 Updated dependencies to Nodejs 0.10+, Grunt 0.4.2+ and Premailer 1.8.0+ (media query support). Added postinstall check for Premailer gem version.

0.2.3 Check for large HTML emails (see https://github.com/dwightjack/grunt-premailer/issues/1)

0.2.2 Bugfix when passing multiple file targets (see #5)

0.2.1 Bugfix in custom premailer script

0.2.0 - Moved to custom ruby script to execute premailer instead of premailer binary. Added removeComments and preserveStyles options. Premailer not requires v1.7.8 or greater.

0.1.1 - Replaced deprecated reference to grunt.util._ with lodash npm module

0.1.0 - Initial release