|
| 1 | +var defaults = require('lodash/defaults'); |
| 2 | +var loaderUtils = require('loader-utils'); |
| 3 | +var dangerousStyleValue = require('react/lib/dangerousStyleValue'); |
| 4 | +var hyphenateStyleName = require('fbjs/lib/hyphenateStyleName'); |
| 5 | + |
| 6 | +function indent(pretty, depth) { |
| 7 | + return pretty ? Array(depth).join(' ') : ''; |
| 8 | +}; |
| 9 | + |
| 10 | +function space(pretty) { |
| 11 | + return pretty ? ' ' : ''; |
| 12 | +}; |
| 13 | + |
| 14 | +function line(pretty) { |
| 15 | + return pretty ? '\n' : ''; |
| 16 | +} |
| 17 | + |
| 18 | +function parse(config, styles, level) { |
| 19 | + level = level === undefined ? 0 : level; |
| 20 | + |
| 21 | + var pretty = config.pretty; |
| 22 | + var css = ''; |
| 23 | + |
| 24 | + for (var styleName in styles) { |
| 25 | + if (!styles.hasOwnProperty(styleName)) { |
| 26 | + continue; |
| 27 | + } |
| 28 | + |
| 29 | + // Extract the style definition or nested block. |
| 30 | + var styleValue = styles[styleName]; |
| 31 | + |
| 32 | + if (styleValue === null) { |
| 33 | + continue; |
| 34 | + } |
| 35 | + |
| 36 | + // Remove whitespace from selector/block. |
| 37 | + styleName = styleName.trim(); |
| 38 | + |
| 39 | + var block = Object.prototype.toString.call(styleValue) === '[object Object]'; |
| 40 | + |
| 41 | + if (block) { |
| 42 | + css += indent(pretty, level) + styleName + space(pretty) + '{' + line(pretty); |
| 43 | + css += parse(config, styleValue, level + 1); |
| 44 | + css += indent(pretty, level) + '}' + line(pretty) + line(pretty); |
| 45 | + continue; |
| 46 | + } |
| 47 | + |
| 48 | + css += indent(pretty, level + 1) + hyphenateStyleName(styleName) + ':' + space(pretty); |
| 49 | + css += dangerousStyleValue(styleName, styleValue) + ';' + line(pretty); |
| 50 | + } |
| 51 | + |
| 52 | + return css; |
| 53 | +} |
| 54 | + |
| 55 | +module.exports = function(content) { |
| 56 | + this.cacheable(); |
| 57 | + |
| 58 | + config = defaults(loaderUtils.getLoaderConfig(this, 'jsCssLoader'), {pretty: process.env.NODE_ENV !== 'production'}); |
| 59 | + |
| 60 | + var styles = this.exec(content, this.resourcePath); |
| 61 | + |
| 62 | + return parse(config, styles.__esModule ? styles.default : styles); |
| 63 | +}; |
0 commit comments