1 | "use strict";
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports.default = void 0;
|
5 |
|
6 | var _generator = _interopRequireDefault(require("@babel/generator"));
|
7 |
|
8 | var t = _interopRequireWildcard(require("@babel/types"));
|
9 |
|
10 | var _get = _interopRequireDefault(require("lodash/get"));
|
11 |
|
12 | var _Symbols = require("../utils/Symbols");
|
13 |
|
14 | var _buildTaggedTemplate = _interopRequireDefault(require("../utils/buildTaggedTemplate"));
|
15 |
|
16 | var _createStyleNode = _interopRequireDefault(require("../utils/createStyleNode"));
|
17 |
|
18 | var _getDisplayName = _interopRequireDefault(require("../utils/getDisplayName"));
|
19 |
|
20 | var _hasAttrs = _interopRequireDefault(require("../utils/hasAttrs"));
|
21 |
|
22 | var _isStyledTag = _interopRequireDefault(require("../utils/isStyledTag"));
|
23 |
|
24 | var _isStyledTagShorthand = _interopRequireDefault(require("../utils/isStyledTagShorthand"));
|
25 |
|
26 | var _normalizeAttrs = _interopRequireDefault(require("../utils/normalizeAttrs"));
|
27 |
|
28 | var _truthy = _interopRequireDefault(require("../utils/truthy"));
|
29 |
|
30 | function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
31 |
|
32 | function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
33 |
|
34 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
35 |
|
36 | const PURE_COMMENT = '/*#__PURE__*/';
|
37 |
|
38 | const buildComponent = nodes => t.callExpression(nodes.TAG, [nodes.ELEMENTTYPE, nodes.OPTIONS, t.objectExpression([t.objectProperty(t.identifier('displayName'), nodes.DISPLAYNAME), t.objectProperty(t.identifier('styles'), nodes.IMPORT), !t.isNullLiteral(nodes.ATTRS) && t.objectProperty(t.identifier('attrs'), nodes.ATTRS), nodes.VARS.elements.length && t.objectProperty(t.identifier('vars'), nodes.VARS), nodes.VARIANTS.elements.length && t.objectProperty(t.identifier('variants'), nodes.VARIANTS)].filter(_truthy.default))]);
|
39 |
|
40 | function buildStyledComponent(path, elementType, opts) {
|
41 | const {
|
42 | file,
|
43 | pluginOptions,
|
44 | styledAttrs,
|
45 | styledOptions,
|
46 | styleImports
|
47 | } = opts;
|
48 | const cssState = file.get(_Symbols.STYLES);
|
49 | const nodeMap = file.get(_Symbols.COMPONENTS);
|
50 | const displayName = (0, _getDisplayName.default)(path, opts, null);
|
51 | if (!displayName) throw path.buildCodeFrameError(
|
52 | path.findParent(p => p.isExpressionStatement()) ? 'The output of this styled component is never used. Either assign it to a variable or export it.' : 'Could not determine a displayName for this styled component. Each component must be uniquely identifiable, either as the default export of the module or by assigning it to a unique identifier');
|
53 | const baseStyle = (0, _createStyleNode.default)(path, displayName, opts);
|
54 | const style = { ...baseStyle,
|
55 | type: 'styled',
|
56 | interpolations: [],
|
57 | imports: '',
|
58 | value: ''
|
59 | };
|
60 | const importId = styleImports.add(style);
|
61 | const {
|
62 | css,
|
63 | vars,
|
64 | variants,
|
65 | interpolations
|
66 | } = (0, _buildTaggedTemplate.default)({
|
67 | style,
|
68 | nodeMap,
|
69 | importId,
|
70 | location: 'COMPONENT',
|
71 | quasiPath: path.get('quasi'),
|
72 | pluginOptions: opts.pluginOptions
|
73 | });
|
74 | style.interpolations = interpolations;
|
75 | style.value = css;
|
76 | const runtimeNode = buildComponent({
|
77 | TAG: t.identifier(pluginOptions.styledTagName),
|
78 | ELEMENTTYPE: elementType,
|
79 | ATTRS: (0, _normalizeAttrs.default)(styledAttrs),
|
80 | OPTIONS: styledOptions || t.nullLiteral(),
|
81 | DISPLAYNAME: t.stringLiteral(displayName),
|
82 | VARS: vars,
|
83 | VARIANTS: variants,
|
84 | IMPORT: importId
|
85 | });
|
86 |
|
87 | if (pluginOptions.generateInterpolations) {
|
88 | style.code = `${PURE_COMMENT}${(0, _generator.default)(runtimeNode).code}`;
|
89 | }
|
90 |
|
91 | style.importIdentifier = importId.name;
|
92 | cssState.styles.set(style.absoluteFilePath, style);
|
93 | nodeMap.set(runtimeNode, style);
|
94 | return runtimeNode;
|
95 | }
|
96 |
|
97 | var _default = {
|
98 | TaggedTemplateExpression(path, state) {
|
99 | const pluginOptions = state.defaultedOptions;
|
100 | const tagPath = path.get('tag');
|
101 |
|
102 | if ((0, _isStyledTag.default)(tagPath, pluginOptions)) {
|
103 | let styledOptions, componentType, styledAttrs;
|
104 |
|
105 | if ((0, _hasAttrs.default)(tagPath.get('callee'))) {
|
106 | styledAttrs = (0, _get.default)(tagPath, 'node.arguments[0]');
|
107 | const styled = tagPath.get('callee.object');
|
108 | componentType = (0, _get.default)(styled, 'node.arguments[0]');
|
109 | styledOptions = (0, _get.default)(styled, 'node.arguments[1]');
|
110 | } else {
|
111 | componentType = (0, _get.default)(tagPath, 'node.arguments[0]');
|
112 | styledOptions = (0, _get.default)(tagPath, 'node.arguments[1]');
|
113 | }
|
114 |
|
115 | path.replaceWith(buildStyledComponent(path, componentType, {
|
116 | pluginOptions,
|
117 | styledAttrs,
|
118 | styledOptions,
|
119 | file: state.file,
|
120 | styleImports: state.styleImports
|
121 | }));
|
122 | path.addComment('leading', '#__PURE__');
|
123 | } else if ((0, _isStyledTagShorthand.default)(tagPath, pluginOptions)) {
|
124 | const prop = tagPath.get('property');
|
125 | const componentType = t.stringLiteral(prop.node.name);
|
126 | path.replaceWith(buildStyledComponent(path, componentType, {
|
127 | pluginOptions,
|
128 | file: state.file,
|
129 | styleImports: state.styleImports
|
130 | }));
|
131 | path.addComment('leading', '#__PURE__');
|
132 | }
|
133 | }
|
134 |
|
135 | };
|
136 | exports.default = _default; |
\ | No newline at end of file |