Last active
August 29, 2015 14:12
-
-
Save garthk/e468e708d1c3a4c06571 to your computer and use it in GitHub Desktop.
docpad-plugin-autoprefix-with-configuration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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; | |
} | |
} | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} |
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.
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
This docpad plugin can be dropped into your
plugins
directory to perform much the same job as docpad-plugin-autoprefix, except:browsers
You can't trigger it with document metadataThe latter would require arenderDocument
method doing much the same asrender
, only checking foropts.file.autoprefix
rather thanopts.inExtension
andopts.outExtension
. Good luck!