Last active
December 22, 2015 01:53
-
-
Save erguotou520/6c723124bfa1fcd571a4 to your computer and use it in GitHub Desktop.
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
// 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