source: trip-planner-front/node_modules/mini-css-extract-plugin/dist/hmr/hotModuleReplacement.js@ e29cc2e

Last change on this file since e29cc2e was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.4 KB
RevLine 
[6a3a178]1"use strict";
2
3/* eslint-env browser */
4
5/*
6 eslint-disable
7 no-console,
8 func-names
9*/
10var normalizeUrl = require("./normalize-url");
11
12var srcByModuleId = Object.create(null);
13var noDocument = typeof document === "undefined";
14var forEach = Array.prototype.forEach;
15
16function debounce(fn, time) {
17 var timeout = 0;
18 return function () {
19 var self = this; // eslint-disable-next-line prefer-rest-params
20
21 var args = arguments;
22
23 var functionCall = function functionCall() {
24 return fn.apply(self, args);
25 };
26
27 clearTimeout(timeout);
28 timeout = setTimeout(functionCall, time);
29 };
30}
31
32function noop() {}
33
34function getCurrentScriptUrl(moduleId) {
35 var src = srcByModuleId[moduleId];
36
37 if (!src) {
38 if (document.currentScript) {
39 src = document.currentScript.src;
40 } else {
41 var scripts = document.getElementsByTagName("script");
42 var lastScriptTag = scripts[scripts.length - 1];
43
44 if (lastScriptTag) {
45 src = lastScriptTag.src;
46 }
47 }
48
49 srcByModuleId[moduleId] = src;
50 }
51
52 return function (fileMap) {
53 if (!src) {
54 return null;
55 }
56
57 var splitResult = src.split(/([^\\/]+)\.js$/);
58 var filename = splitResult && splitResult[1];
59
60 if (!filename) {
61 return [src.replace(".js", ".css")];
62 }
63
64 if (!fileMap) {
65 return [src.replace(".js", ".css")];
66 }
67
68 return fileMap.split(",").map(function (mapRule) {
69 var reg = new RegExp("".concat(filename, "\\.js$"), "g");
70 return normalizeUrl(src.replace(reg, "".concat(mapRule.replace(/{fileName}/g, filename), ".css")));
71 });
72 };
73}
74
75function updateCss(el, url) {
76 if (!url) {
77 if (!el.href) {
78 return;
79 } // eslint-disable-next-line
80
81
82 url = el.href.split("?")[0];
83 }
84
85 if (!isUrlRequest(url)) {
86 return;
87 }
88
89 if (el.isLoaded === false) {
90 // We seem to be about to replace a css link that hasn't loaded yet.
91 // We're probably changing the same file more than once.
92 return;
93 }
94
95 if (!url || !(url.indexOf(".css") > -1)) {
96 return;
97 } // eslint-disable-next-line no-param-reassign
98
99
100 el.visited = true;
101 var newEl = el.cloneNode();
102 newEl.isLoaded = false;
103 newEl.addEventListener("load", function () {
104 if (newEl.isLoaded) {
105 return;
106 }
107
108 newEl.isLoaded = true;
109 el.parentNode.removeChild(el);
110 });
111 newEl.addEventListener("error", function () {
112 if (newEl.isLoaded) {
113 return;
114 }
115
116 newEl.isLoaded = true;
117 el.parentNode.removeChild(el);
118 });
119 newEl.href = "".concat(url, "?").concat(Date.now());
120
121 if (el.nextSibling) {
122 el.parentNode.insertBefore(newEl, el.nextSibling);
123 } else {
124 el.parentNode.appendChild(newEl);
125 }
126}
127
128function getReloadUrl(href, src) {
129 var ret; // eslint-disable-next-line no-param-reassign
130
131 href = normalizeUrl(href, {
132 stripWWW: false
133 }); // eslint-disable-next-line array-callback-return
134
135 src.some(function (url) {
136 if (href.indexOf(src) > -1) {
137 ret = url;
138 }
139 });
140 return ret;
141}
142
143function reloadStyle(src) {
144 if (!src) {
145 return false;
146 }
147
148 var elements = document.querySelectorAll("link");
149 var loaded = false;
150 forEach.call(elements, function (el) {
151 if (!el.href) {
152 return;
153 }
154
155 var url = getReloadUrl(el.href, src);
156
157 if (!isUrlRequest(url)) {
158 return;
159 }
160
161 if (el.visited === true) {
162 return;
163 }
164
165 if (url) {
166 updateCss(el, url);
167 loaded = true;
168 }
169 });
170 return loaded;
171}
172
173function reloadAll() {
174 var elements = document.querySelectorAll("link");
175 forEach.call(elements, function (el) {
176 if (el.visited === true) {
177 return;
178 }
179
180 updateCss(el);
181 });
182}
183
184function isUrlRequest(url) {
185 // An URL is not an request if
186 // It is not http or https
187 if (!/^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(url)) {
188 return false;
189 }
190
191 return true;
192}
193
194module.exports = function (moduleId, options) {
195 if (noDocument) {
196 console.log("no window.document found, will not HMR CSS");
197 return noop;
198 }
199
200 var getScriptSrc = getCurrentScriptUrl(moduleId);
201
202 function update() {
203 var src = getScriptSrc(options.filename);
204 var reloaded = reloadStyle(src);
205
206 if (options.locals) {
207 console.log("[HMR] Detected local css modules. Reload all css");
208 reloadAll();
209 return;
210 }
211
212 if (reloaded) {
213 console.log("[HMR] css reload %s", src.join(" "));
214 } else {
215 console.log("[HMR] Reload all css");
216 reloadAll();
217 }
218 }
219
220 return debounce(update, 50);
221};
Note: See TracBrowser for help on using the repository browser.