|
| 1 | +/** |
| 2 | + * @copyright 2016-present, React CSS Components team |
| 3 | + * @flow |
| 4 | + */ |
| 5 | + |
| 6 | +import * as types from 'babel-types'; |
| 7 | +import * as postcss from 'postcss'; |
| 8 | +import * as LoaderUtils from 'loader-utils'; |
| 9 | +import generate from 'babel-generator'; |
| 10 | + |
| 11 | +const LOADER = require.resolve('../webpack'); |
| 12 | + |
| 13 | +export type RenderConfig = { |
| 14 | + requestCSS: string; |
| 15 | +}; |
| 16 | + |
| 17 | +export function loader(source: string): string { |
| 18 | + this.cacheable(); |
| 19 | + let query = LoaderUtils.parseQuery(this.query); |
| 20 | + if (query.css) { |
| 21 | + let result = renderToCSS(source); |
| 22 | + return result; |
| 23 | + } else { |
| 24 | + let requestCSS = `!!style-loader!css-loader?module!${LOADER}?css!${this.resource}`; |
| 25 | + let result = renderToJS(source, {requestCSS}); |
| 26 | + return result; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +export function render(source: string, config: RenderConfig = {}): {js: string; css: string}{ |
| 31 | + let js = renderToJS(source, {requestCSS: config.requestCSS}); |
| 32 | + let css = renderToCSS(source); |
| 33 | + return {js, css}; |
| 34 | +} |
| 35 | + |
| 36 | +function renderToCSS(source: string, config: RenderConfig): string { |
| 37 | + let root = postcss.parse(source); |
| 38 | + root.walkRules(node => { |
| 39 | + let cssNode = node.clone(); |
| 40 | + cssNode.selector = `:local(.${node.selector})`; |
| 41 | + node.replaceWith(cssNode); |
| 42 | + }); |
| 43 | + return root.toString(); |
| 44 | +} |
| 45 | + |
| 46 | +function renderToJS(source: string, config: RenderConfig): string { |
| 47 | + let root = postcss.parse(source); |
| 48 | + let statements = []; |
| 49 | + root.walkRules(node => { |
| 50 | + statements.push(exportComponent(node.selector, 'div', node.selector)); |
| 51 | + }); |
| 52 | + statements.unshift( |
| 53 | + types.importDeclaration( |
| 54 | + [types.importDefaultSpecifier(types.identifier('styles'))], |
| 55 | + types.stringLiteral(config.requestCSS) |
| 56 | + ) |
| 57 | + ); |
| 58 | + statements.unshift( |
| 59 | + types.importDeclaration( |
| 60 | + [types.importDefaultSpecifier(types.identifier('React'))], |
| 61 | + types.stringLiteral('react') |
| 62 | + ) |
| 63 | + ); |
| 64 | + return generate(types.program(statements)).code; |
| 65 | +} |
| 66 | + |
| 67 | +function exportComponent(name: string, component: string, className: string) { |
| 68 | + let propsNode = types.objectExpression([ |
| 69 | + types.spreadProperty(types.identifier('props')), |
| 70 | + types.objectProperty( |
| 71 | + types.identifier('className'), |
| 72 | + types.memberExpression(types.identifier('styles'), types.identifier(name)) |
| 73 | + ) |
| 74 | + ]); |
| 75 | + let elementNode = types.callExpression( |
| 76 | + types.memberExpression( |
| 77 | + types.identifier('React'), |
| 78 | + types.identifier('createElement')), |
| 79 | + [types.stringLiteral(component), propsNode] |
| 80 | + ); |
| 81 | + let componentNode = types.functionDeclaration( |
| 82 | + types.identifier(name), |
| 83 | + [types.identifier('props')], |
| 84 | + types.blockStatement([types.returnStatement(elementNode)]) |
| 85 | + ); |
| 86 | + return types.exportNamedDeclaration(componentNode, [], null); |
| 87 | +} |
0 commit comments