1 |
|
2 |
|
3 |
|
4 |
|
5 | var path = require('path');
|
6 |
|
7 | var loaderUtils = require("loader-utils");
|
8 | var validateOptions = require('schema-utils');
|
9 |
|
10 | module.exports = function () {};
|
11 |
|
12 | module.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 |
|
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 |
|
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 | };
|