UNPKG

5.78 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.default = void 0;
5
6var _get = _interopRequireDefault(require("lodash/get"));
7
8var _generator = _interopRequireDefault(require("@babel/generator"));
9
10var _template = _interopRequireDefault(require("@babel/template"));
11
12var t = _interopRequireWildcard(require("@babel/types"));
13
14var _buildTaggedTemplate = _interopRequireDefault(require("../utils/buildTaggedTemplate"));
15
16var _createStyleNode = _interopRequireDefault(require("../utils/createStyleNode"));
17
18var _getDisplayName = _interopRequireDefault(require("../utils/getDisplayName"));
19
20var _hasAttrs = _interopRequireDefault(require("../utils/hasAttrs"));
21
22var _isStyledTag = _interopRequireDefault(require("../utils/isStyledTag"));
23
24var _isStyledTagShorthand = _interopRequireDefault(require("../utils/isStyledTagShorthand"));
25
26var _normalizeAttrs = _interopRequireDefault(require("../utils/normalizeAttrs"));
27
28var _Symbols = require("../utils/Symbols");
29
30var _toVarsArray = _interopRequireDefault(require("../utils/toVarsArray"));
31
32var _trimExpressions = _interopRequireDefault(require("../utils/trimExpressions"));
33
34var _wrapInClass = _interopRequireDefault(require("../utils/wrapInClass"));
35
36function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
37
38function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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; }
39
40function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
41
42const PURE_COMMENT = '/*#__PURE__*/';
43const buildImport = (0, _template.default)('require(FILENAME);');
44const buildComponent = (0, _template.default)(`styled(ELEMENTTYPE, OPTIONS, {
45 displayName: DISPLAYNAME,
46 styles: IMPORT,
47 attrs: ATTRS,
48 vars: VARS
49 })`);
50
51function buildStyledComponent(path, elementType, opts) {
52 const {
53 file,
54 pluginOptions,
55 styledAttrs,
56 styledOptions
57 } = opts;
58 const cssState = file.get(_Symbols.STYLES);
59 const nodeMap = file.get(_Symbols.COMPONENTS);
60 const displayName = (0, _getDisplayName.default)(path, opts, null);
61 if (!displayName) throw path.buildCodeFrameError( // the expression case should always be the problem but just in case, let's avoid a potentially weird error.
62 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');
63 const style = (0, _createStyleNode.default)(path, displayName, opts);
64 style.isStyledComponent = true;
65 const {
66 text,
67 dynamicInterpolations,
68 imports
69 } = (0, _buildTaggedTemplate.default)({
70 style,
71 nodeMap,
72 ...opts.pluginOptions,
73 quasiPath: path.get('quasi'),
74 useCssProperties: pluginOptions.customCssProperties === true
75 });
76 style.imports = imports;
77 style.interpolations = (0, _trimExpressions.default)(dynamicInterpolations);
78 style.value = imports + (0, _wrapInClass.default)(text);
79 const runtimeNode = buildComponent({
80 ELEMENTTYPE: elementType,
81 ATTRS: (0, _normalizeAttrs.default)(styledAttrs),
82 OPTIONS: styledOptions || t.NullLiteral(),
83 DISPLAYNAME: t.StringLiteral(displayName),
84 VARS: (0, _toVarsArray.default)(dynamicInterpolations),
85 IMPORT: buildImport({
86 FILENAME: t.StringLiteral(style.relativeFilePath)
87 }).expression
88 });
89
90 if (pluginOptions.generateInterpolations) {
91 style.code = `${PURE_COMMENT}${(0, _generator.default)(runtimeNode).code}`;
92 }
93
94 cssState.styles.set(style.absoluteFilePath, style);
95 nodeMap.set(runtimeNode.expression, style);
96 return runtimeNode;
97}
98
99var _default = {
100 TaggedTemplateExpression(path, state) {
101 const pluginOptions = state.defaultedOptions;
102 const tagPath = path.get('tag');
103
104 if ((0, _isStyledTag.default)(tagPath, pluginOptions)) {
105 let styledOptions, componentType, styledAttrs;
106
107 if ((0, _hasAttrs.default)(tagPath.get('callee'))) {
108 styledAttrs = (0, _get.default)(tagPath, 'node.arguments[0]');
109 const styled = tagPath.get('callee.object');
110 componentType = (0, _get.default)(styled, 'node.arguments[0]');
111 styledOptions = (0, _get.default)(styled, 'node.arguments[1]');
112 } else {
113 componentType = (0, _get.default)(tagPath, 'node.arguments[0]');
114 styledOptions = (0, _get.default)(tagPath, 'node.arguments[1]');
115 }
116
117 path.replaceWith(buildStyledComponent(path, componentType, {
118 pluginOptions,
119 styledAttrs,
120 styledOptions,
121 file: state.file
122 }));
123 path.addComment('leading', '#__PURE__'); // styled.button` ... `
124 } else if ((0, _isStyledTagShorthand.default)(tagPath, pluginOptions)) {
125 const componentType = t.StringLiteral(tagPath.get('property').node.name);
126 path.replaceWith(buildStyledComponent(path, componentType, {
127 pluginOptions,
128 file: state.file
129 }));
130 path.addComment('leading', '#__PURE__');
131 }
132 }
133
134};
135exports.default = _default;
\No newline at end of file