Last active
August 29, 2015 14:20
-
-
Save MadLittleMods/59916ac4f22d24bba8fc to your computer and use it in GitHub Desktop.
A custom System.js plugin to load in post css. Usage: `import 'somestyles.css!mypostcss';`
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
// Based off of geelen's gists: https://github.com/systemjs/plugin-css/issues/22#issuecomment-89491851 | |
import postcss from 'postcss'; | |
import inlineComments from 'postcss-inline-comment'; | |
import mixins from 'postcss-mixins'; | |
import nestedcss from 'postcss-nested'; | |
import cssvariables from 'postcss-css-variables'; | |
//import autoprefixer from 'autoprefixer-core'; | |
import cursorHandMixin from './custom-postcss-mixins/cursor-hand'; | |
import toggleCheckboxEnclosedMixin from './custom-postcss-mixins/toggle-checkbox-enclosed'; | |
let processors = [ | |
inlineComments(), | |
mixins({ | |
mixins: { | |
'cursor-hand': cursorHandMixin, | |
'toggle-checkbox-enclosed': toggleCheckboxEnclosedMixin | |
} | |
}), | |
nestedcss, | |
cssvariables(), | |
//autoprefixer({browsers: ['last 10 versions']}) | |
]; | |
let myProcessor = postcss(processors); | |
function escape(source) { | |
return source | |
.replace(/(["\\])/g, '\\$1') | |
.replace(/[\f]/g, "\\f") | |
.replace(/[\b]/g, "\\b") | |
.replace(/[\n]/g, "\\n") | |
.replace(/[\t]/g, "\\t") | |
.replace(/[\r]/g, "\\r") | |
.replace(/[\u2028]/g, "\\u2028") | |
.replace(/[\u2029]/g, "\\u2029"); | |
} | |
export default { | |
translate: function(load) { | |
return new Promise(function(resolve, reject) { | |
//console.log(load.source); | |
myProcessor.process(load.source).then(function(result) { | |
console.log('Success'); | |
let runInBrowser = ` | |
(function(css) { | |
var blob = new Blob([css], {type: 'text/css'}), | |
url = URL.createObjectURL(blob), | |
elem = document.createElement('link'), | |
head = document.getElementsByTagName('head')[0]; | |
elem.setAttribute('href', url) | |
elem.setAttribute('rel', 'stylesheet') | |
head.appendChild(elem) | |
})("${escape(result.css)}"); | |
`; | |
resolve(runInBrowser); | |
}).catch(function(e) { | |
reject('Error processing PostCSS:', e); | |
}); | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment