Skip to content

Commit 71dbb73

Browse files
General upgrades (#97)
* fix: Fix CSS ordering for config with multiple entry points * some updates to the hot loding system
1 parent a0ae649 commit 71dbb73

File tree

2 files changed

+32
-40
lines changed

2 files changed

+32
-40
lines changed

src/hotLoader.js

+8-15
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,19 @@ const loaderUtils = require('loader-utils');
44
const defaultOptions = {
55
fileMap: '{fileName}',
66
};
7-
8-
function hotReload(content) {
7+
module.exports = function (content) {
98
this.cacheable();
109
const options = Object.assign(
11-
{},
12-
defaultOptions,
13-
loaderUtils.getOptions(this),
14-
);
10+
{},
11+
defaultOptions,
12+
loaderUtils.getOptions(this),
13+
);
1514

16-
return `${content}
15+
return content + `
1716
if(module.hot) {
1817
// ${Date.now()}
19-
var cssReload = require(${loaderUtils.stringifyRequest(
20-
this,
21-
`!${path.join(__dirname, 'hotModuleReplacement.js')}`,
22-
)})(module.id, ${JSON.stringify(options)});
18+
var cssReload = require(${loaderUtils.stringifyRequest(this, '!' + path.join(__dirname, 'hotModuleReplacement.js'))})(module.id, ${JSON.stringify(options)});
2319
module.hot.dispose(cssReload);
24-
module.hot.accept(undefined, cssReload);
2520
}
2621
`;
27-
}
28-
29-
module.exports = hotReload;
22+
};

src/hotModuleReplacement.js

+24-25
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ const getCurrentScriptUrl = function (moduleId) {
3333
}
3434
return fileMap.split(',').map(function (mapRule) {
3535
const reg = new RegExp(filename + '\\.js$', 'g');
36-
return normalizeUrl(
37-
src.replace(reg, mapRule.replace(/{fileName}/g, filename) + '.css'),
38-
{ stripWWW: false },
39-
);
36+
return normalizeUrl(src.replace(reg, mapRule.replace(/{fileName}/g, filename) + '.css'), { stripWWW: false });
4037
});
4138
};
4239
};
@@ -46,8 +43,8 @@ function updateCss(el, url) {
4643
url = el.href.split('?')[0];
4744
}
4845
if (el.isLoaded === false) {
49-
// We seem to be about to replace a css link that hasn't loaded yet.
50-
// We're probably changing the same file more than once.
46+
// We seem to be about to replace a css link that hasn't loaded yet.
47+
// We're probably changing the same file more than once.
5148
return;
5249
}
5350
if (!url || !(url.indexOf('.css') > -1)) return;
@@ -58,29 +55,18 @@ function updateCss(el, url) {
5855
newEl.isLoaded = false;
5956
newEl.addEventListener('load', function () {
6057
newEl.isLoaded = true;
61-
el.parentNode.removeChild(el);
58+
el.remove();
6259
});
6360
newEl.addEventListener('error', function () {
6461
newEl.isLoaded = true;
65-
el.parentNode.removeChild(el);
62+
el.remove();
6663
});
6764

6865
newEl.href = url + '?' + Date.now();
6966
el.parentNode.appendChild(newEl);
7067
}
7168

72-
function getReloadUrl(href, src) {
73-
href = normalizeUrl(href, { stripWWW: false });
74-
let ret;
75-
src.some(function (url) { // eslint-disable-line array-callback-return
76-
if (href.indexOf(src) > -1) {
77-
ret = url;
78-
}
79-
});
80-
return ret;
81-
}
82-
83-
function reloadStyle(src) { // eslint-disable-line no-unused-vars
69+
function reloadStyle(src) {
8470
const elements = document.querySelectorAll('link');
8571
let loaded = false;
8672

@@ -97,6 +83,17 @@ function reloadStyle(src) { // eslint-disable-line no-unused-vars
9783
return loaded;
9884
}
9985

86+
function getReloadUrl(href, src) {
87+
href = normalizeUrl(href, { stripWWW: false });
88+
let ret;
89+
src.some(function (url) {
90+
if (href.indexOf(src) > -1) {
91+
ret = url;
92+
}
93+
});
94+
return ret;
95+
}
96+
10097
function reloadAll() {
10198
const elements = document.querySelectorAll('link');
10299
forEach.call(elements, function (el) {
@@ -106,19 +103,21 @@ function reloadAll() {
106103
}
107104

108105
module.exports = function (moduleId, options) {
106+
let getScriptSrc;
107+
109108
if (noDocument) {
110109
return noop;
111110
}
112111

113-
const getScriptSrc = getCurrentScriptUrl(moduleId);
112+
getScriptSrc = getCurrentScriptUrl(moduleId);
114113

115114
function update() {
116115
const src = getScriptSrc(options.fileMap);
117-
const reloaded = false; // hack of all hacks...for now
118-
if (reloaded) {
119-
console.log('[HMR] css reload %s', src.join(' ')); // eslint-disable-line no-console
116+
const reloaded = reloadStyle(src);
117+
if (reloaded && !options.reloadAll) {
118+
console.log('[HMR] css reload %s', src.join(' '));
120119
} else {
121-
console.log('[HMR] Reload all css'); // eslint-disable-line no-console
120+
console.log('[HMR] Reload all css');
122121
reloadAll();
123122
}
124123
}

0 commit comments

Comments
 (0)