newer-universal-style-loader/index.js

41 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2012-04-07 05:06:57 +04:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var loaderUtils = require("loader-utils"),
path = require("path"),
universal = require('./universal');
2013-01-28 18:06:19 +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) || {};
return [
"// style-loader: Adds some css to the DOM by adding a <style> tag",
"",
"// load the styles",
2015-04-10 00:46:55 +03:00
"var content = require(" + loaderUtils.stringifyRequest(this, "!!" + remainingRequest) + ");",
2014-09-30 01:22:41 +04:00
"if(typeof content === 'string') content = [[module.id, content, '']];",
"if(content.locals) module.exports = content.locals;",
"if (typeof window === 'undefined') {",
" require(" + loaderUtils.stringifyRequest(this, "!" + path.join(__dirname, "universal.js")) + ").addStyles(content, " + JSON.stringify(query) + ");",
"} else {",
2016-07-24 19:39:27 +03:00
"// add the styles to the DOM",
"var update = require(" + loaderUtils.stringifyRequest(this, "!" + path.join(__dirname, "addStyles.js")) + ")(content, " + JSON.stringify(query) + ");",
"// Hot Module Replacement",
"if(module.hot) {",
" // When the styles change, update the <style> tags",
" if(!content.locals) {",
2015-04-22 00:35:55 +03:00
" module.hot.accept(" + loaderUtils.stringifyRequest(this, "!!" + remainingRequest) + ", function() {",
" var newContent = require(" + loaderUtils.stringifyRequest(this, "!!" + remainingRequest) + ");",
" if(typeof newContent === 'string') newContent = [[module.id, newContent, '']];",
" update(newContent);",
" });",
" }",
" // When the module is disposed, remove the <style> tags",
" module.hot.dispose(function() { update(); });",
2016-07-24 19:39:27 +03:00
"}",
"}"
].join("\n");
2013-01-28 18:06:19 +04:00
};
module.exports.getStyles = universal.getStyles