|
4 | 4 | */ |
5 | 5 |
|
6 | 6 | import * as types from 'babel-types'; |
| 7 | +import {identifier, stringLiteral} from 'babel-types'; |
7 | 8 | import * as postcss from 'postcss'; |
8 | 9 | import * as LoaderUtils from 'loader-utils'; |
9 | 10 | import generate from 'babel-generator'; |
@@ -55,59 +56,39 @@ function renderToCSS(source: string, _config: RenderConfig): string { |
55 | 56 |
|
56 | 57 | function renderToJS(source: string, config: RenderConfig): string { |
57 | 58 | let root = postcss.parse(source); |
| 59 | + let imports = stmt` |
| 60 | + import React from "react"; |
| 61 | + import styles from "${stringLiteral(config.requestCSS)}"; |
| 62 | + `; |
58 | 63 | let statements = []; |
59 | | - let component = types.stringLiteral('div'); |
| 64 | + let component = stringLiteral('div'); |
60 | 65 | root.walkRules(node => { |
61 | 66 | if (!Syntax.isComponent(node)) { |
62 | 67 | return; |
63 | 68 | } |
64 | 69 | node.walkDecls(decl => { |
65 | 70 | if (decl.prop === 'base') { |
66 | 71 | if (HTMLTagList[decl.value]) { |
67 | | - component = types.stringLiteral(decl.value); |
| 72 | + component = stringLiteral(decl.value); |
68 | 73 | } else { |
69 | | - component = types.identifier(node.selector + '__Base'); |
70 | | - statements.unshift( |
71 | | - ComponentRef.importDeclaration(component, decl.value) |
72 | | - ); |
| 74 | + component = identifier(node.selector + '__Base'); |
| 75 | + let ref = ComponentRef.parse(decl.value); |
| 76 | + imports.push(stmt` |
| 77 | + import { |
| 78 | + ${identifier(ref.name)} as ${component} |
| 79 | + } from "${stringLiteral(ref.source)}"; |
| 80 | + `); |
73 | 81 | } |
74 | 82 | } |
75 | 83 | }); |
76 | | - statements.push(exportComponent(node.selector, component, node.selector)); |
| 84 | + statements.push(stmt` |
| 85 | + export function ${identifier(node.selector)}(props) { |
| 86 | + return React.createElement( |
| 87 | + ${component}, |
| 88 | + {...props, className: styles.${identifier(node.selector)}} |
| 89 | + ); |
| 90 | + } |
| 91 | + `); |
77 | 92 | }); |
78 | | - statements.unshift( |
79 | | - types.importDeclaration( |
80 | | - [types.importDefaultSpecifier(types.identifier('styles'))], |
81 | | - types.stringLiteral(config.requestCSS) |
82 | | - ) |
83 | | - ); |
84 | | - statements.unshift( |
85 | | - types.importDeclaration( |
86 | | - [types.importDefaultSpecifier(types.identifier('React'))], |
87 | | - types.stringLiteral('react') |
88 | | - ) |
89 | | - ); |
90 | | - return generate(types.program(statements)).code; |
91 | | -} |
92 | | - |
93 | | -function exportComponent(name: string, component: string, className: string) { |
94 | | - let propsNode = types.objectExpression([ |
95 | | - types.spreadProperty(types.identifier('props')), |
96 | | - types.objectProperty( |
97 | | - types.identifier('className'), |
98 | | - types.memberExpression(types.identifier('styles'), types.identifier(className)) |
99 | | - ) |
100 | | - ]); |
101 | | - let elementNode = types.callExpression( |
102 | | - types.memberExpression( |
103 | | - types.identifier('React'), |
104 | | - types.identifier('createElement')), |
105 | | - [component, propsNode] |
106 | | - ); |
107 | | - let componentNode = types.functionDeclaration( |
108 | | - types.identifier(name), |
109 | | - [types.identifier('props')], |
110 | | - types.blockStatement([types.returnStatement(elementNode)]) |
111 | | - ); |
112 | | - return types.exportNamedDeclaration(componentNode, [], null); |
| 93 | + return generate(types.program(imports.concat(statements))).code; |
113 | 94 | } |
0 commit comments