UNPKG

3.86 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.default = void 0;
5
6var t = _interopRequireWildcard(require("@babel/types"));
7
8var _Symbols = require("../utils/Symbols");
9
10var _buildTaggedTemplate = _interopRequireDefault(require("../utils/buildTaggedTemplate"));
11
12var _createStyleNode = _interopRequireDefault(require("../utils/createStyleNode"));
13
14var _getDisplayName = _interopRequireDefault(require("../utils/getDisplayName"));
15
16var _isCssTag = _interopRequireDefault(require("../utils/isCssTag"));
17
18var _isStylesheetTag = _interopRequireDefault(require("../utils/isStylesheetTag"));
19
20function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
22function _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); }
23
24function _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; }
25
26function buildStyleRequire(path, opts, isSingleClass) {
27 const {
28 cssTagName,
29 stylesheetTagName
30 } = opts.defaultedOptions;
31 const {
32 styles
33 } = opts.file.get(_Symbols.STYLES);
34 const nodeMap = opts.file.get(_Symbols.COMPONENTS);
35 const tagName = isSingleClass ? cssTagName : stylesheetTagName;
36 const baseStyle = (0, _createStyleNode.default)(path, (0, _getDisplayName.default)(path, opts) || undefined, {
37 pluginOptions: opts.defaultedOptions,
38 file: opts.file
39 });
40 const style = { ...baseStyle,
41 type: isSingleClass ? 'class' : 'stylesheet',
42 code: '',
43 value: ''
44 };
45 const {
46 css
47 } = (0, _buildTaggedTemplate.default)({
48 quasiPath: path.get('quasi'),
49 nodeMap,
50 style,
51 location: isSingleClass ? 'RULE' : 'STYLESHEET',
52 pluginOptions: opts.defaultedOptions
53 });
54 style.value = css;
55 if (styles.has(style.absoluteFilePath)) throw path.buildCodeFrameError(path.findParent(p => p.isExpressionStatement()) ? `There are multiple anonymous ${tagName} tags that would conflict. Differentiate each tag by assigning the output to a unique identifier` : `There are multiple ${tagName} tags with the same inferred identifier. Differentiate each tag by assigning the output to a unique identifier`);
56 styles.set(style.absoluteFilePath, style);
57 let runtimeNode = opts.styleImports.add(style);
58 style.code = runtimeNode.name;
59 style.importIdentifier = runtimeNode.name;
60 nodeMap.set(runtimeNode, style);
61
62 if (isSingleClass) {
63 runtimeNode = t.memberExpression(runtimeNode, t.identifier('cls2'));
64 style.code += '.cls2';
65 }
66
67 return runtimeNode;
68}
69
70var _default = {
71 TaggedTemplateExpression(path, state) {
72 const pluginOptions = state.defaultedOptions;
73 const tagPath = path.get('tag');
74 const isCss = (0, _isCssTag.default)(tagPath, pluginOptions);
75 const isStyleSheet = !isCss && (0, _isStylesheetTag.default)(tagPath, pluginOptions);
76
77 if (isCss || isStyleSheet) {
78 path.replaceWith(buildStyleRequire(path, state, isCss));
79 }
80 }
81
82};
83exports.default = _default;
\No newline at end of file