Skip to content

Instantly share code, notes, and snippets.

@necolas
Created September 8, 2014 20:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save necolas/c11c1698295b405ebe71 to your computer and use it in GitHub Desktop.
Save necolas/c11c1698295b405ebe71 to your computer and use it in GitHub Desktop.
webpack-pre-css-loader.js
var path = require('path');
/**
* CSS transform dependencies
*/
var autoprefixer = require('autoprefixer-core');
var calc = require('rework-calc');
var color = require('rework-color-function');
var conformance = require('rework-suit-conformance');
var customMedia = require('rework-custom-media');
var rework = require('rework');
var utils = require('loader-utils');
var vars = require('rework-vars');
// config files
var cssConfig = require('../css.config');
/**
* Transform CSS source
*
* @param {String} source
*/
module.exports = function (source) {
if (this.cacheable) {
this.cacheable();
}
var file = utils.getRemainingRequest(this);
var query = utils.parseQuery(this.query);
var varsMap = cssConfig.vars;
var mediaMap = cssConfig.media;
var browsers = cssConfig.browsers || [];
// rework css
var output = rework(source, { source: file })
.use(conformance)
// W3C-style variables
.use(vars({ map: varsMap }))
// W3C-style color function
.use(color)
// basic calc resolution
.use(calc)
// W3C-style custom media queries
.use(customMedia({ map: mediaMap }))
.toString({ sourcemap: (query.minimize ? false : true) });
// add vendor prefixes
output = autoprefixer({
browsers: browsers,
cascade: false
}).process(output, {
from: file,
map: (query.minimize ? false : 'inline')
});
this.callback(null, output.css);
};
@irvinebroque
Copy link

Stumbled on this after Googling for a better rework loader -- have you figured out how to get webpack to play nice with rework-npm?

Banging my head against the wall trying to migrate a bunch of code built on suit-rework from gulp to webpack.

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