UNPKG

3.23 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.default = void 0;
5
6var _template = _interopRequireDefault(require("@babel/template"));
7
8var t = _interopRequireWildcard(require("@babel/types"));
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 _Symbols = require("../utils/Symbols");
19
20function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
21
22function _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; }
23
24function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
26const buildImport = (0, _template.default)('require(FILENAME);');
27
28function buildStyleRequire(path, opts) {
29 const {
30 tagName
31 } = opts.pluginOptions;
32 const {
33 styles
34 } = opts.file.get(_Symbols.STYLES);
35 const nodeMap = opts.file.get(_Symbols.COMPONENTS);
36 const style = (0, _createStyleNode.default)(path, (0, _getDisplayName.default)(path, opts), opts);
37 style.code = `require('${style.relativeFilePath}')`;
38 const {
39 text,
40 imports
41 } = (0, _buildTaggedTemplate.default)({
42 quasiPath: path.get('quasi'),
43 nodeMap,
44 style,
45 useCssProperties: false,
46 ...opts.pluginOptions
47 });
48 style.value = `${imports}${text}`;
49 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`);
50 styles.set(style.absoluteFilePath, style);
51 const runtimeNode = buildImport({
52 FILENAME: t.StringLiteral(style.relativeFilePath)
53 });
54 nodeMap.set(runtimeNode.expression, style);
55 return runtimeNode;
56}
57
58var _default = {
59 TaggedTemplateExpression(path, state) {
60 const pluginOptions = state.defaultedOptions;
61 const tagPath = path.get('tag');
62
63 if ((0, _isCssTag.default)(tagPath, pluginOptions)) {
64 path.replaceWith(buildStyleRequire(path, {
65 pluginOptions,
66 file: state.file
67 }));
68 }
69 }
70
71};
72exports.default = _default;
\No newline at end of file