Skip to content

Commit b7ed255

Browse files
refactor: runtime code (#412)
1 parent ad82d5b commit b7ed255

File tree

5 files changed

+104
-89
lines changed

5 files changed

+104
-89
lines changed

.eslintrc.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
module.exports = {
22
root: true,
33
extends: ['@webpack-contrib/eslint-config-webpack', 'prettier'],
4+
overrides: [
5+
{
6+
files: ['src/runtime/**/*.js'],
7+
env: {
8+
browser: true,
9+
node: true,
10+
},
11+
globals: {
12+
__webpack_nonce__: 'readonly',
13+
},
14+
rules: {
15+
'no-plusplus': 'off',
16+
'consistent-return': 'off',
17+
'no-param-reassign': 'off',
18+
camelcase: [
19+
'error',
20+
{ properties: 'never', allow: ['__webpack_nonce__'] },
21+
],
22+
// avoid unnecessary `babel` helpers
23+
'prefer-destructuring': 'off',
24+
'prefer-rest-params': 'off',
25+
},
26+
},
27+
],
428
};

src/runtime/injectStylesIntoLinkTag.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
/* eslint-env browser */
2-
/* eslint-disable */
3-
4-
module.exports = function addStyleUrl(url, options) {
1+
module.exports = function injectStylesIntoLinkTag(url, options) {
52
options = options || {};
63
options.attributes =
74
typeof options.attributes === 'object' ? options.attributes : {};
85

9-
if (options.attributes.nonce === undefined) {
10-
var nonce =
6+
if (typeof options.attributes.nonce === 'undefined') {
7+
const nonce =
118
typeof __webpack_nonce__ !== 'undefined' ? __webpack_nonce__ : null;
129

1310
if (nonce) {
@@ -29,9 +26,9 @@ module.exports = function addStyleUrl(url, options) {
2926
head.appendChild(link);
3027

3128
if (module.hot) {
32-
return (url) => {
33-
if (typeof url === 'string') {
34-
link.href = url;
29+
return (newUrl) => {
30+
if (typeof newUrl === 'string') {
31+
link.href = newUrl;
3532
} else {
3633
head.removeChild(link);
3734
}

src/runtime/injectStylesIntoStyleTag.js

Lines changed: 72 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,34 @@
1-
/* eslint-env browser */
2-
/* eslint-disable */
1+
const stylesInDom = {};
32

4-
var stylesInDom = {};
3+
const isOldIE = (function isOldIE() {
4+
let memo;
55

6-
var memoize = function(fn) {
7-
var memo;
8-
9-
return function() {
6+
return function memorized() {
107
if (typeof memo === 'undefined') {
11-
memo = fn.apply(this, arguments);
8+
// Test for IE <= 9 as proposed by Browserhacks
9+
// @see http://browserhacks.com/#hack-e71d8692f65334173fee715c222cb805
10+
// Tests for existence of standard globals is to allow style-loader
11+
// to operate correctly into non-standard environments
12+
// @see https://github.com/webpack-contrib/style-loader/issues/177
13+
memo = Boolean(window && document && document.all && !window.atob);
1214
}
1315

1416
return memo;
1517
};
16-
};
17-
18-
var isOldIE = memoize(function() {
19-
// Test for IE <= 9 as proposed by Browserhacks
20-
// @see http://browserhacks.com/#hack-e71d8692f65334173fee715c222cb805
21-
// Tests for existence of standard globals is to allow style-loader
22-
// to operate correctly into non-standard environments
23-
// @see https://github.com/webpack-contrib/style-loader/issues/177
24-
return window && document && document.all && !window.atob;
25-
});
18+
})();
2619

27-
var getTarget = function(target, parent) {
20+
function getTarget(target, parent) {
2821
if (parent) {
2922
return parent.querySelector(target);
3023
}
3124

3225
return document.querySelector(target);
33-
};
26+
}
3427

35-
var getElement = (function() {
36-
var memo = {};
28+
const getElement = (function getElement() {
29+
const memo = {};
3730

38-
return function(target, parent) {
31+
return function memorized(target, parent) {
3932
// If passing function in options, then use it for resolve "head" element.
4033
// Useful for Shadow Root style i.e
4134
// {
@@ -46,7 +39,7 @@ var getElement = (function() {
4639
}
4740

4841
if (typeof memo[target] === 'undefined') {
49-
var styleTarget = getTarget.call(this, target, parent);
42+
let styleTarget = getTarget.call(this, target, parent);
5043

5144
// Special case to return head of iframe instead of iframe itself
5245
if (
@@ -70,11 +63,11 @@ var getElement = (function() {
7063
};
7164
})();
7265

73-
var singleton = null;
74-
var singletonCounter = 0;
75-
var stylesInsertedAtTop = [];
66+
let singleton = null;
67+
let singletonCounter = 0;
68+
const stylesInsertedAtTop = [];
7669

77-
module.exports = function(list, options) {
70+
module.exports = (list, options) => {
7871
options = options || {};
7972

8073
options.attributes =
@@ -96,16 +89,16 @@ module.exports = function(list, options) {
9689
options.insertAt = 'bottom';
9790
}
9891

99-
var styles = listToStyles(list, options);
92+
const styles = listToStyles(list, options);
10093

10194
addStylesToDom(styles, options);
10295

10396
return function update(newList) {
104-
var mayRemove = [];
97+
const mayRemove = [];
10598

106-
for (var i = 0; i < styles.length; i++) {
107-
var item = styles[i];
108-
var domStyle = stylesInDom[item.id];
99+
for (let i = 0; i < styles.length; i++) {
100+
const item = styles[i];
101+
const domStyle = stylesInDom[item.id];
109102

110103
if (domStyle) {
111104
domStyle.refs--;
@@ -114,16 +107,16 @@ module.exports = function(list, options) {
114107
}
115108

116109
if (newList) {
117-
var newStyles = listToStyles(newList, options);
110+
const newStyles = listToStyles(newList, options);
118111

119112
addStylesToDom(newStyles, options);
120113
}
121114

122-
for (var i = 0; i < mayRemove.length; i++) {
123-
var domStyle = mayRemove[i];
115+
for (let i = 0; i < mayRemove.length; i++) {
116+
const domStyle = mayRemove[i];
124117

125118
if (domStyle.refs === 0) {
126-
for (var j = 0; j < domStyle.parts.length; j++) {
119+
for (let j = 0; j < domStyle.parts.length; j++) {
127120
domStyle.parts[j]();
128121
}
129122

@@ -134,46 +127,47 @@ module.exports = function(list, options) {
134127
};
135128

136129
function addStylesToDom(styles, options) {
137-
for (var i = 0; i < styles.length; i++) {
138-
var item = styles[i];
139-
var domStyle = stylesInDom[item.id];
130+
for (let i = 0; i < styles.length; i++) {
131+
const item = styles[i];
132+
const domStyle = stylesInDom[item.id];
133+
let j = 0;
140134

141135
if (domStyle) {
142136
domStyle.refs++;
143137

144-
for (var j = 0; j < domStyle.parts.length; j++) {
138+
for (; j < domStyle.parts.length; j++) {
145139
domStyle.parts[j](item.parts[j]);
146140
}
147141

148142
for (; j < item.parts.length; j++) {
149143
domStyle.parts.push(addStyle(item.parts[j], options));
150144
}
151145
} else {
152-
var parts = [];
146+
const parts = [];
153147

154-
for (var j = 0; j < item.parts.length; j++) {
148+
for (; j < item.parts.length; j++) {
155149
parts.push(addStyle(item.parts[j], options));
156150
}
157151

158-
stylesInDom[item.id] = { id: item.id, refs: 1, parts: parts };
152+
stylesInDom[item.id] = { id: item.id, refs: 1, parts };
159153
}
160154
}
161155
}
162156

163157
function listToStyles(list, options) {
164-
var styles = [];
165-
var newStyles = {};
158+
const styles = [];
159+
const newStyles = {};
166160

167-
for (var i = 0; i < list.length; i++) {
168-
var item = list[i];
169-
var id = options.base ? item[0] + options.base : item[0];
170-
var css = item[1];
171-
var media = item[2];
172-
var sourceMap = item[3];
173-
var part = { css: css, media: media, sourceMap: sourceMap };
161+
for (let i = 0; i < list.length; i++) {
162+
const item = list[i];
163+
const id = options.base ? item[0] + options.base : item[0];
164+
const css = item[1];
165+
const media = item[2];
166+
const sourceMap = item[3];
167+
const part = { css, media, sourceMap };
174168

175169
if (!newStyles[id]) {
176-
styles.push((newStyles[id] = { id: id, parts: [part] }));
170+
styles.push((newStyles[id] = { id, parts: [part] }));
177171
} else {
178172
newStyles[id].parts.push(part);
179173
}
@@ -183,10 +177,10 @@ function listToStyles(list, options) {
183177
}
184178

185179
function insertStyleElement(options) {
186-
var style = document.createElement('style');
180+
const style = document.createElement('style');
187181

188-
if (options.attributes.nonce === undefined) {
189-
var nonce =
182+
if (typeof options.attributes.nonce === 'undefined') {
183+
const nonce =
190184
typeof __webpack_nonce__ !== 'undefined' ? __webpack_nonce__ : null;
191185

192186
if (nonce) {
@@ -198,7 +192,7 @@ function insertStyleElement(options) {
198192
style.setAttribute(key, options.attributes[key]);
199193
});
200194

201-
var target = getElement(options.insertInto);
195+
const target = getElement(options.insertInto);
202196

203197
if (!target) {
204198
throw new Error(
@@ -207,7 +201,7 @@ function insertStyleElement(options) {
207201
}
208202

209203
if (options.insertAt === 'top') {
210-
var lastStyleElementInsertedAtTop =
204+
const lastStyleElementInsertedAtTop =
211205
stylesInsertedAtTop[stylesInsertedAtTop.length - 1];
212206

213207
if (!lastStyleElementInsertedAtTop) {
@@ -222,7 +216,7 @@ function insertStyleElement(options) {
222216
} else if (options.insertAt === 'bottom') {
223217
target.appendChild(style);
224218
} else if (typeof options.insertAt === 'object' && options.insertAt.before) {
225-
var nextSibling = getElement(options.insertAt.before, target);
219+
const nextSibling = getElement(options.insertAt.before, target);
226220

227221
target.insertBefore(style, nextSibling);
228222
} else {
@@ -241,18 +235,20 @@ function removeStyleElement(style) {
241235

242236
style.parentNode.removeChild(style);
243237

244-
var idx = stylesInsertedAtTop.indexOf(style);
238+
const idx = stylesInsertedAtTop.indexOf(style);
245239

246240
if (idx >= 0) {
247241
stylesInsertedAtTop.splice(idx, 1);
248242
}
249243
}
250244

251245
function addStyle(obj, options) {
252-
var style, update, remove, result;
246+
let style;
247+
let update;
248+
let remove;
253249

254250
if (options.singleton) {
255-
var styleIndex = singletonCounter++;
251+
const styleIndex = singletonCounter++;
256252

257253
style = singleton || (singleton = insertStyleElement(options));
258254

@@ -262,7 +258,7 @@ function addStyle(obj, options) {
262258
style = insertStyleElement(options);
263259

264260
update = applyToTag.bind(null, style, options);
265-
remove = function() {
261+
remove = () => {
266262
removeStyleElement(style);
267263
};
268264
}
@@ -287,26 +283,26 @@ function addStyle(obj, options) {
287283
}
288284

289285
/* istanbul ignore next */
290-
var replaceText = (function() {
291-
var textStore = [];
286+
const replaceText = (function replaceText() {
287+
const textStore = [];
292288

293-
return function(index, replacement) {
289+
return function replace(index, replacement) {
294290
textStore[index] = replacement;
295291

296292
return textStore.filter(Boolean).join('\n');
297293
};
298294
})();
299295

300296
function applyToSingletonTag(style, index, remove, obj) {
301-
var css = remove ? '' : obj.css;
297+
const css = remove ? '' : obj.css;
302298

303299
// For old IE
304300
/* istanbul ignore if */
305301
if (style.styleSheet) {
306302
style.styleSheet.cssText = replaceText(index, css);
307303
} else {
308-
var cssNode = document.createTextNode(css);
309-
var childNodes = style.childNodes;
304+
const cssNode = document.createTextNode(css);
305+
const childNodes = style.childNodes;
310306

311307
if (childNodes[index]) {
312308
style.removeChild(childNodes[index]);
@@ -321,20 +317,18 @@ function applyToSingletonTag(style, index, remove, obj) {
321317
}
322318

323319
function applyToTag(style, options, obj) {
324-
var css = obj.css;
325-
var media = obj.media;
326-
var sourceMap = obj.sourceMap;
320+
let css = obj.css;
321+
const media = obj.media;
322+
const sourceMap = obj.sourceMap;
327323

328324
if (media) {
329325
style.setAttribute('media', media);
330326
}
331327

332328
if (sourceMap && btoa) {
333-
css +=
334-
// http://stackoverflow.com/a/26603875
335-
'\n/*# sourceMappingURL=data:application/json;base64,' +
336-
btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap)))) +
337-
' */';
329+
css += `\n/*# sourceMappingURL=data:application/json;base64,${btoa(
330+
unescape(encodeURIComponent(JSON.stringify(sourceMap)))
331+
)} */`;
338332
}
339333

340334
// For old IE

test/helpers/compiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const output = (config) => {
5353
export default function(fixture, config = {}, options = {}) {
5454
// webpack Config
5555
config = {
56-
mode: 'development',
56+
mode: config.mode || 'development',
5757
devtool: config.devtool || false,
5858
context: path.resolve(__dirname, '..', 'fixtures'),
5959
entry: `./${fixture}`,

0 commit comments

Comments
 (0)