UNPKG

5.89 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; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; 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)(`TAG(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 TAG: pluginOptions.styledTag,
81 ELEMENTTYPE: elementType,
82 ATTRS: (0, _normalizeAttrs.default)(styledAttrs),
83 OPTIONS: styledOptions || t.NullLiteral(),
84 DISPLAYNAME: t.StringLiteral(displayName),
85 VARS: (0, _toVarsArray.default)(dynamicInterpolations),
86 IMPORT: buildImport({
87 FILENAME: t.StringLiteral(style.relativeFilePath)
88 }).expression
89 });
90
91 if (pluginOptions.generateInterpolations) {
92 style.code = `${PURE_COMMENT}${(0, _generator.default)(runtimeNode).code}`;
93 }
94
95 cssState.styles.set(style.absoluteFilePath, style);
96 nodeMap.set(runtimeNode.expression, style);
97 return runtimeNode;
98}
99
100var _default = {
101 TaggedTemplateExpression(path, state) {
102 const pluginOptions = state.defaultedOptions;
103 const tagPath = path.get('tag');
104
105 if ((0, _isStyledTag.default)(tagPath, pluginOptions)) {
106 let styledOptions, componentType, styledAttrs;
107
108 if ((0, _hasAttrs.default)(tagPath.get('callee'))) {
109 styledAttrs = (0, _get.default)(tagPath, 'node.arguments[0]');
110 const styled = tagPath.get('callee.object');
111 componentType = (0, _get.default)(styled, 'node.arguments[0]');
112 styledOptions = (0, _get.default)(styled, 'node.arguments[1]');
113 } else {
114 componentType = (0, _get.default)(tagPath, 'node.arguments[0]');
115 styledOptions = (0, _get.default)(tagPath, 'node.arguments[1]');
116 }
117
118 path.replaceWith(buildStyledComponent(path, componentType, {
119 pluginOptions,
120 styledAttrs,
121 styledOptions,
122 file: state.file
123 }));
124 path.addComment('leading', '#__PURE__'); // styled.button` ... `
125 } else if ((0, _isStyledTagShorthand.default)(tagPath, pluginOptions)) {
126 const componentType = t.StringLiteral(tagPath.get('property').node.name);
127 path.replaceWith(buildStyledComponent(path, componentType, {
128 pluginOptions,
129 file: state.file
130 }));
131 path.addComment('leading', '#__PURE__');
132 }
133 }
134
135};
136exports.default = _default;
\No newline at end of file