newer-universal-style-loader/useable.js

43 lines
1.2 KiB
JavaScript
Raw Normal View History

2014-01-27 14:41:56 +04:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2015-04-14 15:47:10 +03:00
var loaderUtils = require("loader-utils"),
path = require("path");
2014-01-27 14:41:56 +04:00
module.exports = function() {};
module.exports.pitch = function(remainingRequest) {
this.cacheable && this.cacheable();
return [
"var refs = 0;",
"var dispose;",
"exports.use = exports.ref = function() {",
" if(!(refs++)) {",
2015-04-14 15:47:10 +03:00
" var content = require(" + loaderUtils.stringifyRequest(this, "!!" + remainingRequest) + ")",
2014-09-30 01:43:33 +04:00
" if(typeof content === 'string') content = [[module.id, content, '']];",
" dispose = require(" + JSON.stringify("!" + path.join(__dirname, "addStyles.js")) + ")(content);",
2014-01-27 14:41:56 +04:00
" }",
" return exports",
"};",
"exports.unuse = exports.unref = function() {",
" if(!(--refs)) {",
" dispose();",
" dispose = null;",
" }",
"};",
"if(module.hot) {",
2015-03-23 02:14:58 +03:00
" var lastRefs = module.hot.data && module.hot.data.refs || 0;",
" if(lastRefs) {",
2014-01-27 14:41:56 +04:00
" exports.ref();",
2015-03-23 02:14:58 +03:00
" refs = lastRefs;",
2014-01-27 14:41:56 +04:00
" }",
" module.hot.accept();",
" module.hot.dispose(function(data) {",
" data.refs = refs;",
" if(dispose) {",
" dispose();",
" }",
" });",
"}",
].join("\n");
};