newer-universal-style-loader/useable.js

49 lines
1.4 KiB
JavaScript
Raw Permalink 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) {
if(this.cacheable) this.cacheable();
2017-02-24 13:39:44 +03:00
var query = loaderUtils.getOptions(this) || {};
2014-01-27 14:41:56 +04:00
return [
"var refs = 0;",
"var dispose;",
"var content = require(" + loaderUtils.stringifyRequest(this, "!!" + remainingRequest) + ");",
"if(typeof content === 'string') content = [[module.id, content, '']];",
"if(content.locals) exports.locals = content.locals;",
2014-01-27 14:41:56 +04:00
"exports.use = exports.ref = function() {",
" if(!(refs++)) {",
2015-11-06 15:38:13 +03:00
" dispose = require(" + loaderUtils.stringifyRequest(this, "!" + path.join(__dirname, "addStyles.js")) + ")(content, " + JSON.stringify(query) + ");",
2014-01-27 14:41:56 +04:00
" }",
" return exports;",
2014-01-27 14:41:56 +04:00
"};",
"exports.unuse = exports.unref = function() {",
2017-03-27 00:02:00 +03:00
" if(refs > 0 && !(--refs)) {",
2014-01-27 14:41:56 +04:00
" 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();",
" if(!content.locals) {",
" refs = lastRefs;",
" }",
" }",
" if(!content.locals) {",
" module.hot.accept();",
2014-01-27 14:41:56 +04:00
" }",
" module.hot.dispose(function(data) {",
" data.refs = content.locals ? 0 : refs;",
2014-01-27 14:41:56 +04:00
" if(dispose) {",
" dispose();",
" }",
" });",
"}"
2014-01-27 14:41:56 +04:00
].join("\n");
};