@yinxulai/rollup-plugin-less
Version:
SSR 支持 typescript 支持 转换 less 语法并支持 cssModule
42 lines • 1.26 kB
JavaScript
export default function insertStyle(css) {
if (!css)
return;
// 环境检查
if (typeof (window) == 'undefined')
return;
if (typeof (document) == 'undefined')
return;
if (typeof (document.head) == 'undefined')
return;
function hash(input) {
var hash = 5381;
var i = input.length - 1;
const I64BIT_TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'.split('');
if (typeof input == 'string') {
for (; i > -1; i--)
hash += (hash << 5) + input.charCodeAt(i);
}
else {
for (; i > -1; i--)
hash += (hash << 5) + input[i];
}
var value = hash & 0x7FFFFFFF;
var retValue = '';
do {
retValue += I64BIT_TABLE[value & 0x3F];
} while (value >>= 6);
return retValue;
}
// 计算内容哈希
const documentID = hash(css);
if (document.getElementById(documentID))
return;
// 创建 style
const style = document.createElement('style');
style.id = documentID;
style.innerHTML = css;
// 插入 dom
document.head.appendChild(style);
return css;
}
//# sourceMappingURL=insertStyle.js.map