Skip to content

Commit c09f80d

Browse files
refactor: code hmr
1 parent 89e7a0a commit c09f80d

File tree

11 files changed

+9
-24
lines changed

11 files changed

+9
-24
lines changed

src/CssLoadingRuntimeModule.js

-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ module.exports = class CssLoadingRuntimeModule extends RuntimeModule {
5757
'var linkTag = document.createElement("link");',
5858
this.runtimeOptions.attributes,
5959
'linkTag.rel = "stylesheet";',
60-
'linkTag.type = "text/css";',
6160
'linkTag.onload = resolve;',
6261
'linkTag.onerror = function(event) {',
6362
Template.indent([

src/index.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@ class MiniCssExtractPlugin {
4444
`var target = document.querySelector("${options.insert}");`,
4545
`target.parentNode.insertBefore(linkTag, target.nextSibling);`,
4646
])
47-
: Template.asString([
48-
'var head = document.getElementsByTagName("head")[0];',
49-
'head.appendChild(linkTag);',
50-
]);
47+
: Template.asString(['document.head.appendChild(linkTag);']);
5148

5249
const attributes =
5350
typeof options.attributes === 'object' ? options.attributes : {};
@@ -398,7 +395,6 @@ class MiniCssExtractPlugin {
398395
'var linkTag = document.createElement("link");',
399396
this.runtimeOptions.attributes,
400397
'linkTag.rel = "stylesheet";',
401-
'linkTag.type = "text/css";',
402398
'linkTag.onload = resolve;',
403399
'linkTag.onerror = function(event) {',
404400
Template.indent([

test/__snapshots__/attributes-option.test.js.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`attributes option should work with attributes option: DOM 1`] = `
44
"<!DOCTYPE html><html><head>
55
<title>style-loader test</title>
66
<style id=\\"existing-style\\">.existing { color: yellow }</style>
7-
<link id=\\"target\\" data-target=\\"example\\" rel=\\"stylesheet\\" type=\\"text/css\\" href=\\"simple.css\\"><script charset=\\"utf-8\\" src=\\"simple.bundle.js\\"></script></head>
7+
<link id=\\"target\\" data-target=\\"example\\" rel=\\"stylesheet\\" href=\\"simple.css\\"><script charset=\\"utf-8\\" src=\\"simple.bundle.js\\"></script></head>
88
<body>
99
<h1>Body</h1>
1010
<div class=\\"target\\"></div>
@@ -22,7 +22,7 @@ exports[`attributes option should work without attributes option: DOM 1`] = `
2222
"<!DOCTYPE html><html><head>
2323
<title>style-loader test</title>
2424
<style id=\\"existing-style\\">.existing { color: yellow }</style>
25-
<link rel=\\"stylesheet\\" type=\\"text/css\\" href=\\"simple.css\\"><script charset=\\"utf-8\\" src=\\"simple.bundle.js\\"></script></head>
25+
<link rel=\\"stylesheet\\" href=\\"simple.css\\"><script charset=\\"utf-8\\" src=\\"simple.bundle.js\\"></script></head>
2626
<body>
2727
<h1>Body</h1>
2828
<div class=\\"target\\"></div>

test/__snapshots__/insert-option.test.js.snap

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
exports[`insert option should work when insert option is function: DOM 1`] = `
44
"<!DOCTYPE html><html><head>
55
<title>style-loader test</title>
6-
<link rel=\\"stylesheet\\" type=\\"text/css\\" href=\\"simple.css\\"><style id=\\"existing-style\\">.existing { color: yellow }</style>
6+
<link rel=\\"stylesheet\\" href=\\"simple.css\\"><style id=\\"existing-style\\">.existing { color: yellow }</style>
77
<script charset=\\"utf-8\\" src=\\"simple.bundle.js\\"></script></head>
88
<body>
99
<h1>Body</h1>
@@ -21,7 +21,7 @@ exports[`insert option should work when insert option is function: warnings 1`]
2121
exports[`insert option should work when insert option is string: DOM 1`] = `
2222
"<!DOCTYPE html><html><head>
2323
<title>style-loader test</title>
24-
<style id=\\"existing-style\\">.existing { color: yellow }</style><link rel=\\"stylesheet\\" type=\\"text/css\\" href=\\"simple.css\\">
24+
<style id=\\"existing-style\\">.existing { color: yellow }</style><link rel=\\"stylesheet\\" href=\\"simple.css\\">
2525
<script charset=\\"utf-8\\" src=\\"simple.bundle.js\\"></script></head>
2626
<body>
2727
<h1>Body</h1>
@@ -40,7 +40,7 @@ exports[`insert option should work without insert option: DOM 1`] = `
4040
"<!DOCTYPE html><html><head>
4141
<title>style-loader test</title>
4242
<style id=\\"existing-style\\">.existing { color: yellow }</style>
43-
<link rel=\\"stylesheet\\" type=\\"text/css\\" href=\\"simple.css\\"><script charset=\\"utf-8\\" src=\\"simple.bundle.js\\"></script></head>
43+
<link rel=\\"stylesheet\\" href=\\"simple.css\\"><script charset=\\"utf-8\\" src=\\"simple.bundle.js\\"></script></head>
4444
<body>
4545
<h1>Body</h1>
4646
<div class=\\"target\\"></div>

test/cases/hmr/expected/webpack-5/main.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,6 @@ module.exports = function (urlString) {
826826
/******/ var linkTag = document.createElement("link");
827827
/******/
828828
/******/ linkTag.rel = "stylesheet";
829-
/******/ linkTag.type = "text/css";
830829
/******/ linkTag.onload = resolve;
831830
/******/ linkTag.onerror = function(event) {
832831
/******/ var request = event && event.target && event.target.href || fullhref;
@@ -838,8 +837,7 @@ module.exports = function (urlString) {
838837
/******/ };
839838
/******/ linkTag.href = fullhref;
840839
/******/
841-
/******/ var head = document.getElementsByTagName("head")[0];
842-
/******/ head.appendChild(linkTag);
840+
/******/ document.head.appendChild(linkTag);
843841
/******/ return linkTag;
844842
/******/ };
845843
/******/ var findStylesheet = (href, fullhref) => {

test/cases/insert-function/expected/webpack-4/main.js

-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@
103103
/******/ var linkTag = document.createElement("link");
104104
/******/
105105
/******/ linkTag.rel = "stylesheet";
106-
/******/ linkTag.type = "text/css";
107106
/******/ linkTag.onload = resolve;
108107
/******/ linkTag.onerror = function(event) {
109108
/******/ var request = event && event.target && event.target.href || fullhref;

test/cases/insert-function/expected/webpack-5/main.js

-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@
161161
/******/ var linkTag = document.createElement("link");
162162
/******/
163163
/******/ linkTag.rel = "stylesheet";
164-
/******/ linkTag.type = "text/css";
165164
/******/ linkTag.onload = resolve;
166165
/******/ linkTag.onerror = function(event) {
167166
/******/ var request = event && event.target && event.target.href || fullhref;

test/cases/insert-string/expected/webpack-4/main.js

-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@
103103
/******/ var linkTag = document.createElement("link");
104104
/******/
105105
/******/ linkTag.rel = "stylesheet";
106-
/******/ linkTag.type = "text/css";
107106
/******/ linkTag.onload = resolve;
108107
/******/ linkTag.onerror = function(event) {
109108
/******/ var request = event && event.target && event.target.href || fullhref;

test/cases/insert-string/expected/webpack-5/main.js

-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@
161161
/******/ var linkTag = document.createElement("link");
162162
/******/
163163
/******/ linkTag.rel = "stylesheet";
164-
/******/ linkTag.type = "text/css";
165164
/******/ linkTag.onload = resolve;
166165
/******/ linkTag.onerror = function(event) {
167166
/******/ var request = event && event.target && event.target.href || fullhref;

test/cases/insert-undefined/expected/webpack-4/main.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@
103103
/******/ var linkTag = document.createElement("link");
104104
/******/
105105
/******/ linkTag.rel = "stylesheet";
106-
/******/ linkTag.type = "text/css";
107106
/******/ linkTag.onload = resolve;
108107
/******/ linkTag.onerror = function(event) {
109108
/******/ var request = event && event.target && event.target.href || fullhref;
@@ -116,8 +115,7 @@
116115
/******/ };
117116
/******/ linkTag.href = fullhref;
118117
/******/
119-
/******/ var head = document.getElementsByTagName("head")[0];
120-
/******/ head.appendChild(linkTag);
118+
/******/ document.head.appendChild(linkTag);
121119
/******/ }).then(function() {
122120
/******/ installedCssChunks[chunkId] = 0;
123121
/******/ }));

test/cases/insert-undefined/expected/webpack-5/main.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@
161161
/******/ var linkTag = document.createElement("link");
162162
/******/
163163
/******/ linkTag.rel = "stylesheet";
164-
/******/ linkTag.type = "text/css";
165164
/******/ linkTag.onload = resolve;
166165
/******/ linkTag.onerror = function(event) {
167166
/******/ var request = event && event.target && event.target.href || fullhref;
@@ -173,8 +172,7 @@
173172
/******/ };
174173
/******/ linkTag.href = fullhref;
175174
/******/
176-
/******/ var head = document.getElementsByTagName("head")[0];
177-
/******/ head.appendChild(linkTag);
175+
/******/ document.head.appendChild(linkTag);
178176
/******/ return linkTag;
179177
/******/ };
180178
/******/ var findStylesheet = (href, fullhref) => {

0 commit comments

Comments
 (0)