UNPKG

4.24 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports["default"] = void 0;
5
6var _babelPluginMacros = require("babel-plugin-macros");
7
8var _babelExternal = require("./babel-external");
9
10var _utils = require("./_utils");
11
12var _constants = require("./_constants");
13
14var _default = (0, _babelPluginMacros.createMacro)(styledJsxMacro);
15
16exports["default"] = _default;
17
18function styledJsxMacro(_ref) {
19 var references = _ref.references,
20 state = _ref.state;
21 (0, _utils.setStateOptions)(state); // Holds a reference to all the lines where strings are tagged using the `css` tag name.
22 // We print a warning at the end of the macro in case there is any reference to css,
23 // because `css` is generally used as default import name for 'styled-jsx/css'.
24 // People who want to migrate from this macro to pure styled-jsx might have name conflicts issues.
25
26 var cssReferences = []; // references looks like this
27 // {
28 // default: [path, path],
29 // resolve: [path],
30 // }
31
32 Object.keys(references).forEach(function (refName) {
33 // Enforce `resolve` as named import so people
34 // can only import { resolve } from 'styled-jsx/macro'
35 // or an alias of it eg. { resolve as foo }
36 if (refName !== 'default' && refName !== 'resolve') {
37 throw new _babelPluginMacros.MacroError("Imported an invalid named import: " + refName + ". Please import: resolve");
38 } // Start processing the references for refName
39
40
41 references[refName].forEach(function (path) {
42 // We grab the parent path. Eg.
43 // path -> css
44 // path.parenPath -> css`div { color: red }`
45 var templateExpression = path.parentPath; // templateExpression member expression?
46 // path -> css
47 // path.parentPath -> css.resolve
48
49 if (templateExpression.isMemberExpression()) {
50 // grab .resolve
51 var tagPropertyName = templateExpression.get('property').node.name; // Member expressions are only valid on default imports
52 // eg. import css from 'styled-jsx/macro'
53
54 if (refName !== 'default') {
55 throw new _babelPluginMacros.MacroError("Can't use named import " + path.node.name + " as a member expression: " + path.node.name + "." + tagPropertyName + "`div { color: red }` Please use it directly: " + path.node.name + "`div { color: red }`");
56 } // Otherwise enforce `css.resolve`
57
58
59 if (tagPropertyName !== 'resolve') {
60 throw new _babelPluginMacros.MacroError("Using an invalid tag: " + tagPropertyName + ". Please use " + templateExpression.get('object').node.name + ".resolve");
61 } // Grab the TaggedTemplateExpression
62 // i.e. css.resolve`div { color: red }`
63
64
65 templateExpression = templateExpression.parentPath;
66 } else {
67 if (refName === 'default') {
68 var name = path.node.name;
69 throw new _babelPluginMacros.MacroError("Can't use default import directly eg. " + name + "`div { color: red }`. Please use " + name + ".resolve`div { color: red }` instead.");
70 }
71
72 if (path.node.name === 'css') {
73 // If the path node name is `css` we push it to the references above to emit a warning later.
74 cssReferences.push(path.node.loc.start.line);
75 }
76 } // Finally transform the path :)
77
78
79 (0, _babelExternal.processTaggedTemplateExpression)({
80 type: 'resolve',
81 path: templateExpression,
82 file: state.file,
83 splitRules: typeof state.opts.optimizeForSpeed === 'boolean' ? state.opts.optimizeForSpeed : process.env.NODE_ENV === 'production',
84 plugins: state.plugins,
85 vendorPrefixes: state.opts.vendorPrefixes,
86 sourceMaps: state.opts.sourceMaps
87 });
88
89 if (!state.hasInjectedJSXStyle && !path.scope.hasBinding(_constants.STYLE_COMPONENT)) {
90 state.hasInjectedJSXStyle = true;
91 (0, _utils.createReactComponentImportDeclaration)(state);
92 }
93 });
94 });
95
96 if (cssReferences.length > 0) {
97 console.warn("styled-jsx - Warning - We detected that you named your tag as `css` at lines: " + 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.');
98 }
99}
\No newline at end of file