Skip to content

Instantly share code, notes, and snippets.

@garthk
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garthk/e468e708d1c3a4c06571 to your computer and use it in GitHub Desktop.
Save garthk/e468e708d1c3a4c06571 to your computer and use it in GitHub Desktop.
docpad-plugin-autoprefix-with-configuration
'use strict';
var autoprefixer = require('autoprefixer');
module.exports = function autoprefix(BasePlugin) {
return BasePlugin.extend({
name: 'autoprefix',
config: {
// fed straight to autoprefixer
},
render: function (opts) {
if (opts.inExtension !== 'autoprefix') {
return;
} else if ((opts.outExtension || 'css') !== 'css') {
return;
} else {
return this._render(opts);
}
},
renderDocument: function (opts) {
if (opts.file.get('autoprefix')) {
return this._render(opts);
}
},
_render: function (opts) {
try {
opts.content = autoprefixer.process(opts.content, this.config).css;
} catch (err) {
return err;
}
}
});
};
{
"name": "docpad-plugin-autoprefix-with-configuration",
"version": "2.1.0",
"description": "apply autoprefix with configuration",
"main": "./src/autoprefix.plugin.js",
"keywords": [
"docpad",
"docpad-plugin",
"autoprefix",
"autoprefixer"
],
"author": "Garth Kidd",
"license": "ISC",
"dependencies": {
"autoprefixer": "^4.0.0"
},
"peerDependencies": {
"docpad": "^6.0.0"
},
"engines": {
"node": ">=0.8.0"
}
}
@garthk
Copy link
Author

garthk commented Jan 5, 2015

This docpad plugin can be dropped into your plugins directory to perform much the same job as docpad-plugin-autoprefix, except:

  • You can feed configuration into it, including browsers
  • You can't trigger it with document metadata

The latter would require a renderDocument method doing much the same as render, only checking for opts.file.autoprefix rather than opts.inExtension and opts.outExtension. Good luck!

@garthk
Copy link
Author

garthk commented Jan 5, 2015

Update: docpad can't render x.css.autoprefix.less without docpad-plugin-less recognising autoprefix as a valid outExtension. It doesn't, so we need to be able to trigger this plugin via autoprefix in the metadata. I've added the required renderDocument method.

@schlaup
Copy link

schlaup commented Feb 28, 2015

Your plugin does not work.

I used your plugin and it always uses the default browsers configuration.

  • browsers is not an option of autoprefixer.process().
  • browsers is a configuration of autoprefixer().

See the autofixer-core documentation:

https://github.com/postcss/autoprefixer-core

To fix it, your code should look like:

'use strict';

var autoprefixer = require('autoprefixer');

module.exports = function autoprefix(BasePlugin) {
    return BasePlugin.extend({
        name: 'autoprefix',

        config: {
            // Options to create the autoprefixer processor
            autoprefixer: null,
            // Options to pass to the process
            options: null
        },

        render: function (opts) {
            if (opts.inExtension !== 'autoprefix') {
                return;
            } else if ((opts.outExtension || 'css') !== 'css') {
                return;
            } else {
                return this._render(opts);
            }
        },

        renderDocument: function (opts) {
            if (opts.file.get('autoprefix')) {
                return this._render(opts);
            }
        },

        _render: function (opts) {
            try {
                var processor = autoprefixer(this.config.autoprefixer);
                opts.content = processor.process(opts.content, this.config.options).css;
            } catch (err) {
                return err;
            }
        }
    });
};

You need to create a processor object with some of the options (like browser) and then you call process() with the rest of the options.

The configuration in docpad.coffee looks like:

        autoprefix:
            autoprefixer:
                browsers: ['> 0.1%', 'last 5 versions', 'IE 8', 'IE 9', 'IE 10']
                cascade: false
            options:
                map:
                    inline: true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment