Skip to content

Instantly share code, notes, and snippets.

@erguotou520
Last active December 22, 2015 01:53
Show Gist options
  • Save erguotou520/6c723124bfa1fcd571a4 to your computer and use it in GitHub Desktop.
Save erguotou520/6c723124bfa1fcd571a4 to your computer and use it in GitHub Desktop.
// init, append require.css method
(function() {
var list = {};
// 移除css
var remove = function remove() {
this.count --;
if (this.count <= 0) {
this.$style.remove();
}
}
require.css = function(src) {
if (list[src]) {
list[src].count ++;
} else {
list[src] = {
count: 0,
remove: remove
};
var $style = document.createElement('style');
$style.textContent = require(src, 'css');
var $head = document.getElementByTag('head')[0];
$head.appendChild($style);
list[src].$style = $head;
}
return list[src];
}
})(require);
// in vue demo
var $css = require.css('./style.css');
new Vue({
template: require('./index.html', 'html'),
destroyed: function() {
$css.remove();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment