UNPKG

4.46 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _babelPluginMacros = require("babel-plugin-macros");
9
10var _babelExternal = require("./babel-external");
11
12var _utils = require("./_utils");
13
14var _constants = require("./_constants");
15
16var _default = (0, _babelPluginMacros.createMacro)(styledJsxMacro);
17
18exports.default = _default;
19
20function styledJsxMacro(_ref) {
21 var references = _ref.references,
22 state = _ref.state;
23 (0, _utils.setStateOptions)(state); // Holds a reference to all the lines where strings are tagged using the `css` tag name.
24 // We print a warning at the end of the macro in case there is any reference to css,
25 // because `css` is generally used as default import name for 'styled-jsx/css'.
26 // People who want to migrate from this macro to pure styled-jsx might have name conflicts issues.
27
28 var cssReferences = []; // references looks like this
29 // {
30 // default: [path, path],
31 // resolve: [path],
32 // }
33
34 Object.keys(references).forEach(function (refName) {
35 // Enforce `resolve` as named import so people
36 // can only import { resolve } from 'styled-jsx/macro'
37 // or an alias of it eg. { resolve as foo }
38 if (refName !== 'default' && refName !== 'resolve') {
39 throw new _babelPluginMacros.MacroError("Imported an invalid named import: ".concat(refName, ". Please import: resolve"));
40 } // Start processing the references for refName
41
42
43 references[refName].forEach(function (path) {
44 // We grab the parent path. Eg.
45 // path -> css
46 // path.parenPath -> css`div { color: red }`
47 var templateExpression = path.parentPath; // templateExpression member expression?
48 // path -> css
49 // path.parentPath -> css.resolve
50
51 if (templateExpression.isMemberExpression()) {
52 // grab .resolve
53 var tagPropertyName = templateExpression.get('property').node.name; // Member expressions are only valid on default imports
54 // eg. import css from 'styled-jsx/macro'
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 } // Otherwise enforce `css.resolve`
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 } // Grab the TaggedTemplateExpression
64 // i.e. css.resolve`div { color: red }`
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 // If the path node name is `css` we push it to the references above to emit a warning later.
76 cssReferences.push(path.node.loc.start.line);
77 }
78 } // Finally transform the path :)
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