Skip to content

Instantly share code, notes, and snippets.

@adjavaherian
Created July 28, 2015 01:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adjavaherian/3c665d143244ffd1fbe3 to your computer and use it in GitHub Desktop.
Save adjavaherian/3c665d143244ffd1fbe3 to your computer and use it in GitHub Desktop.
Webpack plugin to collect different stylesheets.
var ModuleParserHelpers = require('webpack/lib/ModuleParserHelpers');
var NullFactory = require('webpack/lib/NullFactory');
var _ = require('lodash');
var path = require('path');
var sass = require('node-sass');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var ExtractTextPlugin__dirname = path.dirname(
require.resolve('extract-text-webpack-plugin'));
var visited = {};
var currentCounter = 0;
var cssOutput = {};
function getNodes(root) {
if (root.dependencies) {
root.dependencies.forEach(function (child) {
if ( child.module && child.module.userRequest && !visited[child.module.userRequest]) {
//console.log('child request', child.module.userRequest);
visited[child.module.userRequest] = true;
getNodes(child.module);
}
});
}
}
function apply(options, compiler) {
var testString = 'scss';
var re = new RegExp('' + testString + '', 'g');
function filterFunction (collection, key, regex) {
return _.filter(collection, function(obj){ return obj[key].match(regex);});
}
compiler.plugin("compile", function(params) {
//console.log("in compile", params);
});
compiler.plugin("compilation", function(compilation) {
console.log('in compilation');
compilation.plugin('revive-modules', function(modules, records) {
//handle to the module during tree optimization
console.log('revive-modules length', modules.length);
var cssKeys = _.keys(cssOutput);
console.log(cssKeys);
modules.forEach(function(value){
//console.log(value.userRequest, value.debugId);
if (cssKeys.indexOf(value.debugId.toString()) > -1) {
console.log('found match', value);
}
});
});
compilation.plugin("optimize-chunks", function (chunks) {
//there's only one chunk at this point
chunks.forEach(function (chunk) {
console.log('\noutput chunk name', chunk.name);
chunk.modules.forEach(function (module){
if (module.loaders && module.loaders.join().indexOf('collect-sass') > -1) {
//console.log(module);
var moduleRequest = module.debugId;
console.log('found module', moduleRequest);
getNodes(module);
//get paths for match
var foundPaths = [];
_.mapKeys(visited, function(value, key) {
if (re.test(key.toString())) {
//console.log('scss', key);
foundPaths.push(key);
}
});
cssOutput[moduleRequest] = [];
for (var i = 0; i < foundPaths.length; i++) {
var result = sass.renderSync({
file: foundPaths[i]
});
cssOutput[moduleRequest].push(result.css.toString());
}
//console.log(cssOutput);
compiler.cssOutput = cssOutput;
//console.log(this);
}
});
});
});
});
}
module.exports = function(options) {
if (options instanceof Array) {
options = {
include: options
};
}
if (!Array.isArray(options.include)) {
options.include = [ options.include ];
}
return {
apply: apply.bind(this, options)
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment