Last active
December 22, 2018 08:05
-
-
Save dgieselaar/6c0de2adf29ba9e4d10ae4aeb1b80fb6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const RawSource = require('webpack-sources').RawSource; | |
class SplitCssByMediaPlugin { | |
apply(compiler) { | |
// wait until compilation becomes available | |
compiler.hooks.compilation.tap( | |
'SplitByCssMediaPlugin', | |
(compilation) => { | |
// hook into additionalAssets phase | |
compilation.hooks.additionalAssets.tap( | |
'SplitByCssMediaPlugin', | |
() => { | |
// get all about-to-be-emitted css files | |
const cssFiles = Object.keys(compilation.assets) | |
.filter(key => key.endsWith('.css')); | |
cssFiles.forEach((filename) => { | |
const rawSource = compilation.assets[filename]; | |
// extract CSS string | |
const css = rawSource.source(); | |
// create device-type specific css | |
const deviceSpecificCss = '..'; | |
// add output back to compilation | |
compilation.assets[name] = new RawSource(deviceSpecificCss); | |
}); | |
} | |
) | |
} | |
); | |
} | |
} | |
module.exports = SplitCssByMediaPlugin; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment