Some refactor: added getStyle and separated server code

master
Istvan Jano 2016-07-26 09:49:11 +01:00
parent d6e4da2565
commit b8edfa7f17
5 changed files with 55 additions and 29 deletions

View File

@ -61,6 +61,12 @@ Example with React:
</head>
```
From version 0.14.4 the captured styles can be get by using `getStyles()` as well:
``` javascript
import { getStyles } from 'simple-universal-style-loader'
```
### Options
#### `insertAt`

View File

@ -1,22 +0,0 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Istvan Jano janoist1@gmail.com
*/
module.exports.addStylesServer = function(list, options) {
global.__styles__ = global.__styles__ || [];
var newStyles = {};
for(var i = 0; i < list.length; i++) {
var item = list[i];
var id = item[0];
var css = item[1];
var media = item[2];
var sourceMap = item[3];
var part = {css: css, media: media, sourceMap: sourceMap};
if(!newStyles[id])
global.__styles__.push(newStyles[id] = {id: id, parts: [part]});
else
newStyles[id].parts.push(part);
}
}

View File

@ -1,9 +1,10 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var loaderUtils = require("loader-utils"),
path = require("path");
path = require("path"),
universal = require('./universal');
module.exports = function() {};
module.exports.pitch = function(remainingRequest) {
if(this.cacheable) this.cacheable();
@ -16,7 +17,7 @@ module.exports.pitch = function(remainingRequest) {
"if(typeof content === 'string') content = [[module.id, content, '']];",
"if(content.locals) module.exports = content.locals;",
"if (typeof window === 'undefined') {",
" require(" + loaderUtils.stringifyRequest(this, "!" + path.join(__dirname, "addStylesServer.js")) + ")(content, " + JSON.stringify(query) + ");",
" require(" + loaderUtils.stringifyRequest(this, "!" + path.join(__dirname, "universal.js")) + ").addStyles(content, " + JSON.stringify(query) + ");",
"} else {",
"// add the styles to the DOM",
"var update = require(" + loaderUtils.stringifyRequest(this, "!" + path.join(__dirname, "addStyles.js")) + ")(content, " + JSON.stringify(query) + ");",
@ -36,3 +37,5 @@ module.exports.pitch = function(remainingRequest) {
"}"
].join("\n");
};
module.exports.getStyles = universal.getStyles

View File

@ -1,6 +1,6 @@
{
"name": "simple-universal-style-loader",
"version": "0.14.3",
"version": "0.14.4",
"author": "Tobias Koppers @sokra & Istvan Jano @janoist1",
"description": "style loader module for webpack",
"devDependencies": {
@ -8,7 +8,7 @@
},
"repository": {
"type": "git",
"url": "git@github.com:webpack/style-loader.git"
"url": "git@github.com:janoist1/simple-universal-style-loader.git"
},
"license": "MIT",
"dependencies": {

39
universal.js Normal file
View File

@ -0,0 +1,39 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Istvan Jano janoist1@gmail.com
*/
/**
* Add styles - used by the loader
*
* @param list
* @param options
*/
module.exports.addStyles = function (list, options) {
global.__styles__ = global.__styles__ || []
var newStyles = {}
for (var i = 0; i < list.length; i++) {
var item = list[i]
var id = item[0]
var css = item[1]
var media = item[2]
var sourceMap = item[3]
var part = {css: css, media: media, sourceMap: sourceMap}
if (!newStyles[id]){
global.__styles__.push(newStyles[id] = {id: id, parts: [part]})
} else {
newStyles[id].parts.push(part)
}
}
}
/**
* Return the styles that have been collected so far
*
* @returns {*}
*/
module.exports.getStyles = function () {
return global.__styles__
}