forked from Anidetrix/rollup-plugin-styles
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(styles): add singleTag option for CSS injection
Allows to select between single/multiple <style> tags mode
- Loading branch information
Showing
11 changed files
with
1,092 additions
and
953 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const path = require("path"); | ||
module.exports = { | ||
root: true, | ||
extends: [path.resolve(__dirname, "..", "..", ".eslintrc.js")], | ||
env: { node: false, browser: true }, | ||
rules: { "prefer-const": "error" }, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** @type {HTMLElement[]} */ | ||
const containers = []; | ||
/** @type {{prepend:HTMLStyleElement,append:HTMLStyleElement}[]} */ | ||
const styleTags = []; | ||
|
||
/** | ||
* @param {string|undefined} css | ||
* @param {object} [options={}] | ||
* @param {boolean} [options.prepend] | ||
* @param {boolean} [options.singleTag] | ||
* @param {HTMLElement} [options.container] | ||
* @returns {void} | ||
*/ | ||
export default (css, options = {}) => { | ||
if (!css || typeof document === "undefined") return; | ||
|
||
const createStyleTag = () => { | ||
const styleTag = document.createElement("style"); | ||
styleTag.type = "text/css"; | ||
if (position === "prepend" && container.firstChild) { | ||
container.insertBefore(styleTag, container.firstChild); | ||
} else { | ||
container.append(styleTag); | ||
} | ||
return styleTag; | ||
}; | ||
|
||
const singleTag = typeof options.singeTag !== "undefined" ? options.singleTag : false; | ||
const container = typeof options.container !== "undefined" ? options.container : document.head; | ||
const position = options.prepend === true ? "prepend" : "append"; | ||
|
||
/** @type {HTMLStyleElement} */ | ||
let styleTag; | ||
let id = containers.indexOf(container); | ||
|
||
if (singleTag) { | ||
if (id === -1) { | ||
id = containers.push(container) - 1; | ||
styleTags[id] = {}; | ||
} | ||
|
||
if (styleTags[id] && styleTags[id][position]) { | ||
styleTag = styleTags[id][position]; | ||
} else { | ||
styleTag = styleTags[id][position] = createStyleTag(); | ||
} | ||
} else { | ||
styleTag = createStyleTag(); | ||
} | ||
|
||
// strip potential UTF-8 BOM if css was read from a file | ||
if (css.charCodeAt(0) === 0xfeff) { | ||
css = css.slice(1, 1 + css.length); | ||
} | ||
|
||
if (styleTag.styleSheet) { | ||
styleTag.styleSheet.cssText += css; | ||
} else { | ||
styleTag.textContent += css; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.