Skip to content

Commit 8a26186

Browse files
fix: reduce runtime (#519)
1 parent 1556c0b commit 8a26186

7 files changed

+310
-36
lines changed

package-lock.json

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

+5-35
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
getStyleTagTransformFn,
1414
getExportStyleCode,
1515
getExportLazyStyleCode,
16+
getSetAttributesCode,
1617
} from "./utils";
1718

1819
import schema from "./options.json";
@@ -37,39 +38,6 @@ loaderAPI.pitch = function loader(request) {
3738
base: options.base,
3839
};
3940

40-
let setAttributesFn;
41-
42-
if (typeof options.attributes !== "undefined") {
43-
setAttributesFn =
44-
typeof options.attributes.nonce === "undefined"
45-
? `function(style, attributes) {
46-
var nonce =
47-
typeof __webpack_nonce__ !== "undefined" ? __webpack_nonce__ : null;
48-
49-
if (nonce) {
50-
attributes.nonce = nonce;
51-
}
52-
53-
Object.keys(attributes).forEach((key) => {
54-
style.setAttribute(key, attributes[key]);
55-
});
56-
}`
57-
: `function(style, attributes) {
58-
Object.keys(attributes).forEach((key) => {
59-
style.setAttribute(key, attributes[key]);
60-
});
61-
}`;
62-
} else {
63-
setAttributesFn = `function(style) {
64-
var nonce =
65-
typeof __webpack_nonce__ !== "undefined" ? __webpack_nonce__ : null;
66-
67-
if (nonce) {
68-
style.setAttribute("nonce", nonce);
69-
}
70-
}`;
71-
}
72-
7341
const insertFn = insertIsFunction
7442
? options.insert.toString()
7543
: `function(style){
@@ -139,6 +107,7 @@ ${esModule ? "export default {}" : ""}`;
139107
${getImportStyleAPICode(esModule, this)}
140108
${getImportStyleDomAPICode(esModule, this, isSingleton, isAuto)}
141109
${getImportGetTargetCode(esModule, this, insertIsFunction)}
110+
${getSetAttributesCode(esModule, this, options)}
142111
${getImportInsertStyleElementCode(esModule, this)}
143112
${getImportStyleContentCode(esModule, this, request)}
144113
${isAuto ? getImportIsOldIECode(esModule, this) : ""}
@@ -158,7 +127,7 @@ var update;
158127
var options = ${JSON.stringify(runtimeOptions)};
159128
160129
${getStyleTagTransformFn(styleTagTransformFn, isSingleton)};
161-
options.setAttributes = ${setAttributesFn};
130+
options.setAttributes = setAttributes;
162131
options.insert = ${insertFn};
163132
options.domAPI = ${getdomAPI(isAuto)};
164133
options.insertStyleElement = insertStyleElement;
@@ -197,6 +166,7 @@ ${getExportLazyStyleCode(esModule, this, request)}
197166
${getImportStyleAPICode(esModule, this)}
198167
${getImportStyleDomAPICode(esModule, this, isSingleton, isAuto)}
199168
${getImportGetTargetCode(esModule, this, insertIsFunction)}
169+
${getSetAttributesCode(esModule, this, options)}
200170
${getImportInsertStyleElementCode(esModule, this)}
201171
${getImportStyleContentCode(esModule, this, request)}
202172
${isAuto ? getImportIsOldIECode(esModule, this) : ""}
@@ -209,7 +179,7 @@ ${getExportLazyStyleCode(esModule, this, request)}
209179
var options = ${JSON.stringify(runtimeOptions)};
210180
211181
${getStyleTagTransformFn(styleTagTransformFn, isSingleton)};
212-
options.setAttributes = ${setAttributesFn};
182+
options.setAttributes = setAttributes;
213183
options.insert = ${insertFn};
214184
options.domAPI = ${getdomAPI(isAuto)};
215185
options.insertStyleElement = insertStyleElement;
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* istanbul ignore next */
2+
function setAttributesWithoutAttributes(style, attributes) {
3+
const nonce =
4+
typeof __webpack_nonce__ !== "undefined" ? __webpack_nonce__ : null;
5+
6+
if (nonce) {
7+
attributes.nonce = nonce;
8+
}
9+
10+
Object.keys(attributes).forEach((key) => {
11+
style.setAttribute(key, attributes[key]);
12+
});
13+
}
14+
15+
module.exports = setAttributesWithoutAttributes;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* istanbul ignore next */
2+
function setAttributesWithoutAttributes(style, attributes) {
3+
Object.keys(attributes).forEach((key) => {
4+
style.setAttribute(key, attributes[key]);
5+
});
6+
}
7+
8+
module.exports = setAttributesWithoutAttributes;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* istanbul ignore next */
2+
function setAttributesWithoutAttributes(style) {
3+
const nonce =
4+
typeof __webpack_nonce__ !== "undefined" ? __webpack_nonce__ : null;
5+
6+
if (nonce) {
7+
style.setAttribute("nonce", nonce);
8+
}
9+
}
10+
11+
module.exports = setAttributesWithoutAttributes;

src/utils.js

+30
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,35 @@ function getExportLazyStyleCode(esModule, loaderContext, request) {
274274
: "module.exports = exported;";
275275
}
276276

277+
function getSetAttributesCode(esModule, loaderContext, options) {
278+
let modulePath;
279+
280+
if (typeof options.attributes !== "undefined") {
281+
modulePath =
282+
options.attributes.nonce !== "undefined"
283+
? stringifyRequest(
284+
loaderContext,
285+
`!${path.join(
286+
__dirname,
287+
"runtime/setAttributesWithAttributesAndNonce.js"
288+
)}`
289+
)
290+
: stringifyRequest(
291+
loaderContext,
292+
`!${path.join(__dirname, "runtime/setAttributesWithAttributes.js")}`
293+
);
294+
} else {
295+
modulePath = stringifyRequest(
296+
loaderContext,
297+
`!${path.join(__dirname, "runtime/setAttributesWithoutAttributes.js")}`
298+
);
299+
}
300+
301+
return esModule
302+
? `import setAttributes from ${modulePath};`
303+
: `var setAttributes = require(${modulePath});`;
304+
}
305+
277306
// eslint-disable-next-line import/prefer-default-export
278307
export {
279308
stringifyRequest,
@@ -291,4 +320,5 @@ export {
291320
getStyleTagTransformFn,
292321
getExportStyleCode,
293322
getExportLazyStyleCode,
323+
getSetAttributesCode,
294324
};

0 commit comments

Comments
 (0)