UNPKG

1.82 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5var path = require('path');
6
7var loaderUtils = require("loader-utils");
8var validateOptions = require('schema-utils');
9
10module.exports = function () {};
11
12module.exports.pitch = function (request) {
13 if (this.cacheable) this.cacheable();
14
15 var options = loaderUtils.getOptions(this) || {};
16
17 validateOptions(require('./options.json'), options, 'Style Loader (Useable)');
18
19 options.hmr = typeof options.hmr === 'undefined' ? true : options.hmr;
20
21 var hmr = [
22 // Hot Module Replacement
23 "if(module.hot) {",
24 " var lastRefs = module.hot.data && module.hot.data.refs || 0;",
25 "",
26 " if(lastRefs) {",
27 " exports.ref();",
28 " if(!content.locals) {",
29 " refs = lastRefs;",
30 " }",
31 " }",
32 "",
33 " if(!content.locals) {",
34 " module.hot.accept();",
35 " }",
36 "",
37 " module.hot.dispose(function(data) {",
38 " data.refs = content.locals ? 0 : refs;",
39 "",
40 " if(dispose) {",
41 " dispose();",
42 " }",
43 " });",
44 "}"
45 ].join("\n");
46
47 return [
48 "var refs = 0;",
49 "var dispose;",
50 "var content = require(" + loaderUtils.stringifyRequest(this, "!!" + request) + ");",
51 "",
52 "if(typeof content === 'string') content = [[module.id, content, '']];",
53 // Export CSS Modules
54 "if(content.locals) exports.locals = content.locals;",
55 "",
56 "exports.use = exports.ref = function() {",
57 " if(!(refs++)) {",
58 " dispose = require(" + loaderUtils.stringifyRequest(this, "!" + path.join(__dirname, "lib", "addStyles.js")) + ")(content, " + JSON.stringify(options) + ");",
59 " }",
60 "",
61 " return exports;",
62 "};",
63 "",
64 "exports.unuse = exports.unref = function() {",
65 " if(refs > 0 && !(--refs)) {",
66 " dispose();",
67 " dispose = null;",
68 " }",
69 "};",
70 options.hmr ? hmr : ""
71 ].join("\n");
72};