style-loader-insert-top 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -0
- package/insert-function.js +17 -0
- package/package.json +15 -0
package/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# style-loader-insert-top
|
|
2
|
+
|
|
3
|
+
Custom webpack style-loader insert function to inject style tags to the top of the head element.
|
|
4
|
+
|
|
5
|
+
This package simply packages the `insert` function example from [webpack's style-loader documentation](https://webpack.js.org/loaders/style-loader/#insert). Instead of copying the function into your project, you can use this package directly.
|
|
6
|
+
|
|
7
|
+
## Why?
|
|
8
|
+
|
|
9
|
+
By default, `style-loader` appends `<style>` elements to the end of the `<head>` tag. This means your bundled styles will override any existing stylesheets. Sometimes you want the opposite behavior - inserting styles at the top so they can be overridden by existing stylesheets or inline styles.
|
|
10
|
+
|
|
11
|
+
## Usage:
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
module.exports = {
|
|
15
|
+
module: {
|
|
16
|
+
rules: [
|
|
17
|
+
{
|
|
18
|
+
test: /\.css$/i,
|
|
19
|
+
use: [
|
|
20
|
+
{
|
|
21
|
+
loader: "style-loader",
|
|
22
|
+
options: {
|
|
23
|
+
insert: require.resolve('style-loader-insert-top')
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
"css-loader",
|
|
27
|
+
],
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Install
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
npm install style-loader-insert-top
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## License
|
|
41
|
+
|
|
42
|
+
MIT
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
function insertAtTop(element) {
|
|
2
|
+
const parent = document.querySelector("head");
|
|
3
|
+
|
|
4
|
+
const lastInsertedElement = window._lastElementInsertedByStyleLoader;
|
|
5
|
+
|
|
6
|
+
if (!lastInsertedElement) {
|
|
7
|
+
parent.insertBefore(element, parent.firstChild);
|
|
8
|
+
} else if (lastInsertedElement.nextSibling) {
|
|
9
|
+
parent.insertBefore(element, lastInsertedElement.nextSibling);
|
|
10
|
+
} else {
|
|
11
|
+
parent.appendChild(element);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
window._lastElementInsertedByStyleLoader = element;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
module.exports = insertAtTop;
|
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "style-loader-insert-top",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "insert-function.js",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"test": "echo \"No test specified\" && exit 0"
|
|
7
|
+
},
|
|
8
|
+
"keywords": [
|
|
9
|
+
"webpack",
|
|
10
|
+
"style-loader"
|
|
11
|
+
],
|
|
12
|
+
"author": "Viljami Kuosmanen <viljami@viljami.io>",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"description": "Custom webpack style-loader insert function to inject style tags to the top of the head element"
|
|
15
|
+
}
|