1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 |
|
8 | var _babelPluginMacros = require("babel-plugin-macros");
|
9 |
|
10 | var _babelExternal = require("./babel-external");
|
11 |
|
12 | var _utils = require("./_utils");
|
13 |
|
14 | var _constants = require("./_constants");
|
15 |
|
16 | var _default = (0, _babelPluginMacros.createMacro)(styledJsxMacro);
|
17 |
|
18 | exports.default = _default;
|
19 |
|
20 | function styledJsxMacro(_ref) {
|
21 | var references = _ref.references,
|
22 | state = _ref.state;
|
23 | (0, _utils.setStateOptions)(state);
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | var cssReferences = [];
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | Object.keys(references).forEach(function (refName) {
|
35 |
|
36 |
|
37 |
|
38 | if (refName !== 'default' && refName !== 'resolve') {
|
39 | throw new _babelPluginMacros.MacroError("Imported an invalid named import: ".concat(refName, ". Please import: resolve"));
|
40 | }
|
41 |
|
42 |
|
43 | references[refName].forEach(function (path) {
|
44 |
|
45 |
|
46 |
|
47 | var templateExpression = path.parentPath;
|
48 |
|
49 |
|
50 |
|
51 | if (templateExpression.isMemberExpression()) {
|
52 |
|
53 | var tagPropertyName = templateExpression.get('property').node.name;
|
54 |
|
55 |
|
56 | if (refName !== 'default') {
|
57 | throw new _babelPluginMacros.MacroError("Can't use named import ".concat(path.node.name, " as a member expression: ").concat(path.node.name, ".").concat(tagPropertyName, "`div { color: red }` Please use it directly: ").concat(path.node.name, "`div { color: red }`"));
|
58 | }
|
59 |
|
60 |
|
61 | if (tagPropertyName !== 'resolve') {
|
62 | throw new _babelPluginMacros.MacroError("Using an invalid tag: ".concat(tagPropertyName, ". Please use ").concat(templateExpression.get('object').node.name, ".resolve"));
|
63 | }
|
64 |
|
65 |
|
66 |
|
67 | templateExpression = templateExpression.parentPath;
|
68 | } else {
|
69 | if (refName === 'default') {
|
70 | var name = path.node.name;
|
71 | throw new _babelPluginMacros.MacroError("Can't use default import directly eg. ".concat(name, "`div { color: red }`. Please use ").concat(name, ".resolve`div { color: red }` instead."));
|
72 | }
|
73 |
|
74 | if (path.node.name === 'css') {
|
75 |
|
76 | cssReferences.push(path.node.loc.start.line);
|
77 | }
|
78 | }
|
79 |
|
80 |
|
81 | (0, _babelExternal.processTaggedTemplateExpression)({
|
82 | type: 'resolve',
|
83 | path: templateExpression,
|
84 | file: state.file,
|
85 | splitRules: typeof state.opts.optimizeForSpeed === 'boolean' ? state.opts.optimizeForSpeed : process.env.NODE_ENV === 'production',
|
86 | plugins: state.plugins,
|
87 | vendorPrefixes: state.opts.vendorPrefixes,
|
88 | sourceMaps: state.opts.sourceMaps
|
89 | });
|
90 |
|
91 | if (!state.hasInjectedJSXStyle && !path.scope.hasBinding(_constants.STYLE_COMPONENT)) {
|
92 | state.hasInjectedJSXStyle = true;
|
93 | var importDeclaration = (0, _utils.createReactComponentImportDeclaration)();
|
94 | path.findParent(function (p) {
|
95 | return p.isProgram();
|
96 | }).node.body.unshift(importDeclaration);
|
97 | }
|
98 | });
|
99 | });
|
100 |
|
101 | if (cssReferences.length > 0) {
|
102 | console.warn("styled-jsx - Warning - We detected that you named your tag as `css` at lines: ".concat(cssReferences.join(', '), ".\n") + 'This tag name is usually used as default import name for `styled-jsx/css`.\n' + 'Porting macro code to pure styled-jsx in the future might be a bit problematic.');
|
103 | }
|
104 | } |
\ | No newline at end of file |