Skip to content

Commit f3a6dba

Browse files
refactor: use babel for compiling client code (#396)
1 parent 7d022b3 commit f3a6dba

File tree

2 files changed

+60
-48
lines changed

2 files changed

+60
-48
lines changed

Diff for: babel.config.js

+15
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,20 @@ module.exports = (api) => {
1515
},
1616
],
1717
],
18+
overrides: [
19+
{
20+
test: '**/hmr/**/*.js',
21+
presets: [
22+
[
23+
'@babel/preset-env',
24+
{
25+
targets: {
26+
node: '0.12',
27+
},
28+
},
29+
],
30+
],
31+
},
32+
],
1833
};
1934
};

Diff for: src/hmr/hotModuleReplacement.js

+45-48
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,27 @@
1-
/* global document, window */
1+
/* eslint-env browser */
22
/*
33
eslint-disable
4-
func-names,
5-
no-var,
6-
vars-on-top,
7-
prefer-arrow-func,
8-
prefer-rest-params,
9-
prefer-arrow-callback,
10-
prefer-template,
11-
prefer-destructuring,
12-
no-param-reassign,
13-
no-console
4+
no-console,
5+
func-names
146
*/
157

16-
var normalizeUrl = require('normalize-url');
8+
const normalizeUrl = require('normalize-url');
179

18-
var srcByModuleId = Object.create(null);
10+
const srcByModuleId = Object.create(null);
1911

20-
var noDocument = typeof document === 'undefined';
12+
const noDocument = typeof document === 'undefined';
2113

22-
var forEach = Array.prototype.forEach;
14+
const { forEach } = Array.prototype;
2315

2416
function debounce(fn, time) {
25-
var timeout = 0;
17+
let timeout = 0;
2618

27-
// eslint-disable-next-line func-names
2819
return function() {
29-
var self = this;
30-
var args = arguments;
31-
20+
const self = this;
3221
// eslint-disable-next-line prefer-rest-params
33-
var functionCall = function functionCall() {
22+
const args = arguments;
23+
24+
const functionCall = function functionCall() {
3425
return fn.apply(self, args);
3526
};
3627

@@ -42,19 +33,20 @@ function debounce(fn, time) {
4233
function noop() {}
4334

4435
function getCurrentScriptUrl(moduleId) {
45-
var src = srcByModuleId[moduleId];
36+
let src = srcByModuleId[moduleId];
4637

4738
if (!src) {
4839
if (document.currentScript) {
49-
src = document.currentScript.src;
40+
({ src } = document.currentScript);
5041
} else {
51-
var scripts = document.getElementsByTagName('script');
52-
var lastScriptTag = scripts[scripts.length - 1];
42+
const scripts = document.getElementsByTagName('script');
43+
const lastScriptTag = scripts[scripts.length - 1];
5344

5445
if (lastScriptTag) {
55-
src = lastScriptTag.src;
46+
({ src } = lastScriptTag);
5647
}
5748
}
49+
5850
srcByModuleId[moduleId] = src;
5951
}
6052

@@ -63,8 +55,8 @@ function getCurrentScriptUrl(moduleId) {
6355
return null;
6456
}
6557

66-
var splitResult = src.split(/([^\\/]+)\.js$/);
67-
var filename = splitResult && splitResult[1];
58+
const splitResult = src.split(/([^\\/]+)\.js$/);
59+
const filename = splitResult && splitResult[1];
6860

6961
if (!filename) {
7062
return [src.replace('.js', '.css')];
@@ -74,11 +66,11 @@ function getCurrentScriptUrl(moduleId) {
7466
return [src.replace('.js', '.css')];
7567
}
7668

77-
return fileMap.split(',').map(function(mapRule) {
78-
var reg = new RegExp(filename + '\\.js$', 'g');
69+
return fileMap.split(',').map((mapRule) => {
70+
const reg = new RegExp(`${filename}\\.js$`, 'g');
7971

8072
return normalizeUrl(
81-
src.replace(reg, mapRule.replace(/{fileName}/g, filename) + '.css'),
73+
src.replace(reg, `${mapRule.replace(/{fileName}/g, filename)}.css`),
8274
{ stripWWW: false }
8375
);
8476
});
@@ -87,6 +79,7 @@ function getCurrentScriptUrl(moduleId) {
8779

8880
function updateCss(el, url) {
8981
if (!url) {
82+
// eslint-disable-next-line
9083
url = el.href.split('?')[0];
9184
}
9285

@@ -104,34 +97,36 @@ function updateCss(el, url) {
10497
return;
10598
}
10699

100+
// eslint-disable-next-line no-param-reassign
107101
el.visited = true;
108102

109-
var newEl = el.cloneNode(); // eslint-disable-line vars-on-top
103+
const newEl = el.cloneNode();
110104

111105
newEl.isLoaded = false;
112106

113-
newEl.addEventListener('load', function() {
107+
newEl.addEventListener('load', () => {
114108
newEl.isLoaded = true;
115109
el.parentNode.removeChild(el);
116110
});
117111

118-
newEl.addEventListener('error', function() {
112+
newEl.addEventListener('error', () => {
119113
newEl.isLoaded = true;
120114
el.parentNode.removeChild(el);
121115
});
122116

123-
newEl.href = url + '?' + Date.now();
117+
newEl.href = `${url}?${Date.now()}`;
124118

125119
el.parentNode.appendChild(newEl);
126120
}
127121

128122
function getReloadUrl(href, src) {
129-
var ret;
123+
let ret;
130124

125+
// eslint-disable-next-line no-param-reassign
131126
href = normalizeUrl(href, { stripWWW: false });
132127

133128
// eslint-disable-next-line array-callback-return
134-
src.some(function(url) {
129+
src.some((url) => {
135130
if (href.indexOf(src) > -1) {
136131
ret = url;
137132
}
@@ -141,11 +136,11 @@ function getReloadUrl(href, src) {
141136
}
142137

143138
function reloadStyle(src) {
144-
var elements = document.querySelectorAll('link');
145-
var loaded = false;
139+
const elements = document.querySelectorAll('link');
140+
let loaded = false;
146141

147-
forEach.call(elements, function(el) {
148-
var url = getReloadUrl(el.href, src);
142+
forEach.call(elements, (el) => {
143+
const url = getReloadUrl(el.href, src);
149144

150145
if (!isUrlRequest(url)) {
151146
return;
@@ -165,9 +160,9 @@ function reloadStyle(src) {
165160
}
166161

167162
function reloadAll() {
168-
var elements = document.querySelectorAll('link');
163+
const elements = document.querySelectorAll('link');
169164

170-
forEach.call(elements, function(el) {
165+
forEach.call(elements, (el) => {
171166
if (el.visited === true) {
172167
return;
173168
}
@@ -200,27 +195,29 @@ function isUrlRequest(url) {
200195
module.exports = function(moduleId, options) {
201196
if (noDocument) {
202197
console.log('no window.document found, will not HMR CSS');
198+
203199
return noop;
204200
}
205201

206-
// eslint-disable-next-line vars-on-top
207-
var getScriptSrc = getCurrentScriptUrl(moduleId);
202+
const getScriptSrc = getCurrentScriptUrl(moduleId);
208203

209204
function update() {
210-
var src = getScriptSrc(options.filename);
211-
212-
var reloaded = reloadStyle(src);
205+
const src = getScriptSrc(options.filename);
206+
const reloaded = reloadStyle(src);
213207

214208
if (options.locals) {
215209
console.log('[HMR] Detected local css modules. Reload all css');
210+
216211
reloadAll();
212+
217213
return;
218214
}
219215

220216
if (reloaded && !options.reloadAll) {
221217
console.log('[HMR] css reload %s', src.join(' '));
222218
} else {
223219
console.log('[HMR] Reload all css');
220+
224221
reloadAll();
225222
}
226223
}

0 commit comments

Comments
 (0)