Skip to content

Commit

Permalink
fix: reduce runtime (#519)
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Jul 12, 2021
1 parent 1556c0b commit 8a26186
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 36 deletions.
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 5 additions & 35 deletions src/index.js
Expand Up @@ -13,6 +13,7 @@ import {
getStyleTagTransformFn,
getExportStyleCode,
getExportLazyStyleCode,
getSetAttributesCode,
} from "./utils";

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

let setAttributesFn;

if (typeof options.attributes !== "undefined") {
setAttributesFn =
typeof options.attributes.nonce === "undefined"
? `function(style, attributes) {
var nonce =
typeof __webpack_nonce__ !== "undefined" ? __webpack_nonce__ : null;
if (nonce) {
attributes.nonce = nonce;
}
Object.keys(attributes).forEach((key) => {
style.setAttribute(key, attributes[key]);
});
}`
: `function(style, attributes) {
Object.keys(attributes).forEach((key) => {
style.setAttribute(key, attributes[key]);
});
}`;
} else {
setAttributesFn = `function(style) {
var nonce =
typeof __webpack_nonce__ !== "undefined" ? __webpack_nonce__ : null;
if (nonce) {
style.setAttribute("nonce", nonce);
}
}`;
}

const insertFn = insertIsFunction
? options.insert.toString()
: `function(style){
Expand Down Expand Up @@ -139,6 +107,7 @@ ${esModule ? "export default {}" : ""}`;
${getImportStyleAPICode(esModule, this)}
${getImportStyleDomAPICode(esModule, this, isSingleton, isAuto)}
${getImportGetTargetCode(esModule, this, insertIsFunction)}
${getSetAttributesCode(esModule, this, options)}
${getImportInsertStyleElementCode(esModule, this)}
${getImportStyleContentCode(esModule, this, request)}
${isAuto ? getImportIsOldIECode(esModule, this) : ""}
Expand All @@ -158,7 +127,7 @@ var update;
var options = ${JSON.stringify(runtimeOptions)};
${getStyleTagTransformFn(styleTagTransformFn, isSingleton)};
options.setAttributes = ${setAttributesFn};
options.setAttributes = setAttributes;
options.insert = ${insertFn};
options.domAPI = ${getdomAPI(isAuto)};
options.insertStyleElement = insertStyleElement;
Expand Down Expand Up @@ -197,6 +166,7 @@ ${getExportLazyStyleCode(esModule, this, request)}
${getImportStyleAPICode(esModule, this)}
${getImportStyleDomAPICode(esModule, this, isSingleton, isAuto)}
${getImportGetTargetCode(esModule, this, insertIsFunction)}
${getSetAttributesCode(esModule, this, options)}
${getImportInsertStyleElementCode(esModule, this)}
${getImportStyleContentCode(esModule, this, request)}
${isAuto ? getImportIsOldIECode(esModule, this) : ""}
Expand All @@ -209,7 +179,7 @@ ${getExportLazyStyleCode(esModule, this, request)}
var options = ${JSON.stringify(runtimeOptions)};
${getStyleTagTransformFn(styleTagTransformFn, isSingleton)};
options.setAttributes = ${setAttributesFn};
options.setAttributes = setAttributes;
options.insert = ${insertFn};
options.domAPI = ${getdomAPI(isAuto)};
options.insertStyleElement = insertStyleElement;
Expand Down
15 changes: 15 additions & 0 deletions src/runtime/setAttributesWithAttributes.js
@@ -0,0 +1,15 @@
/* istanbul ignore next */
function setAttributesWithoutAttributes(style, attributes) {
const nonce =
typeof __webpack_nonce__ !== "undefined" ? __webpack_nonce__ : null;

if (nonce) {
attributes.nonce = nonce;
}

Object.keys(attributes).forEach((key) => {
style.setAttribute(key, attributes[key]);
});
}

module.exports = setAttributesWithoutAttributes;
8 changes: 8 additions & 0 deletions src/runtime/setAttributesWithAttributesAndNonce.js
@@ -0,0 +1,8 @@
/* istanbul ignore next */
function setAttributesWithoutAttributes(style, attributes) {
Object.keys(attributes).forEach((key) => {
style.setAttribute(key, attributes[key]);
});
}

module.exports = setAttributesWithoutAttributes;
11 changes: 11 additions & 0 deletions src/runtime/setAttributesWithoutAttributes.js
@@ -0,0 +1,11 @@
/* istanbul ignore next */
function setAttributesWithoutAttributes(style) {
const nonce =
typeof __webpack_nonce__ !== "undefined" ? __webpack_nonce__ : null;

if (nonce) {
style.setAttribute("nonce", nonce);
}
}

module.exports = setAttributesWithoutAttributes;
30 changes: 30 additions & 0 deletions src/utils.js
Expand Up @@ -274,6 +274,35 @@ function getExportLazyStyleCode(esModule, loaderContext, request) {
: "module.exports = exported;";
}

function getSetAttributesCode(esModule, loaderContext, options) {
let modulePath;

if (typeof options.attributes !== "undefined") {
modulePath =
options.attributes.nonce !== "undefined"
? stringifyRequest(
loaderContext,
`!${path.join(
__dirname,
"runtime/setAttributesWithAttributesAndNonce.js"
)}`
)
: stringifyRequest(
loaderContext,
`!${path.join(__dirname, "runtime/setAttributesWithAttributes.js")}`
);
} else {
modulePath = stringifyRequest(
loaderContext,
`!${path.join(__dirname, "runtime/setAttributesWithoutAttributes.js")}`
);
}

return esModule
? `import setAttributes from ${modulePath};`
: `var setAttributes = require(${modulePath});`;
}

// eslint-disable-next-line import/prefer-default-export
export {
stringifyRequest,
Expand All @@ -291,4 +320,5 @@ export {
getStyleTagTransformFn,
getExportStyleCode,
getExportLazyStyleCode,
getSetAttributesCode,
};

0 comments on commit 8a26186

Please sign in to comment.