1 | "use strict";
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports["default"] = _default;
|
5 |
|
6 | var _babelPluginSyntaxJsx = _interopRequireDefault(require("babel-plugin-syntax-jsx"));
|
7 |
|
8 | var _babelExternal = require("./babel-external");
|
9 |
|
10 | var _utils = require("./_utils");
|
11 |
|
12 | var _constants = require("./_constants");
|
13 |
|
14 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
15 |
|
16 | function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
17 |
|
18 | function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } it = o[Symbol.iterator](); return it.next.bind(it); }
|
19 |
|
20 | function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
21 |
|
22 | function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
23 |
|
24 | function _default(_ref) {
|
25 | var t = _ref.types;
|
26 | var jsxVisitors = {
|
27 | JSXOpeningElement: function JSXOpeningElement(path, state) {
|
28 | var el = path.node;
|
29 |
|
30 | var _ref2 = el.name || {},
|
31 | name = _ref2.name;
|
32 |
|
33 | if (!state.hasJSXStyle) {
|
34 | return;
|
35 | }
|
36 |
|
37 | if (state.ignoreClosing === null) {
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | state.ignoreClosing = 0;
|
44 | }
|
45 |
|
46 | var tag = path.get('name');
|
47 |
|
48 | if (name && name !== 'style' && name !== _constants.STYLE_COMPONENT && (name.charAt(0) !== name.charAt(0).toUpperCase() || Object.values(path.scope.bindings).some(function (binding) {
|
49 | return binding.referencePaths.some(function (r) {
|
50 | return r === tag;
|
51 | });
|
52 | }))) {
|
53 | if (state.className) {
|
54 | (0, _utils.addClassName)(path, state.className);
|
55 | }
|
56 | }
|
57 |
|
58 | state.ignoreClosing++;
|
59 | },
|
60 | JSXElement: {
|
61 | enter: function enter(path, state) {
|
62 | if (state.hasJSXStyle !== null) {
|
63 | return;
|
64 | }
|
65 |
|
66 | var styles = (0, _utils.findStyles)(path);
|
67 |
|
68 | if (styles.length === 0) {
|
69 | return;
|
70 | }
|
71 |
|
72 | state.styles = [];
|
73 | state.externalStyles = [];
|
74 | var scope = (0, _utils.getScope)(path);
|
75 |
|
76 | for (var _iterator = _createForOfIteratorHelperLoose(styles), _step; !(_step = _iterator()).done;) {
|
77 | var style = _step.value;
|
78 |
|
79 | var children = style.get('children').filter(function (c) {
|
80 | return t.isJSXExpressionContainer(c.node) ||
|
81 | t.isJSXText(c.node) && c.node.value.trim() !== '';
|
82 | });
|
83 |
|
84 | if (children.length !== 1) {
|
85 | throw path.buildCodeFrameError("Expected one child under " + ("JSX Style tag, but got " + children.length + " ") + "(eg: <style jsx>{`hi`}</style>)");
|
86 | }
|
87 |
|
88 | var child = children[0];
|
89 |
|
90 | if (!t.isJSXExpressionContainer(child)) {
|
91 | throw path.buildCodeFrameError("Expected a child of " + "type JSXExpressionContainer under JSX Style tag " + ("(eg: <style jsx>{`hi`}</style>), got " + child.type));
|
92 | }
|
93 |
|
94 | var expression = child.get('expression');
|
95 |
|
96 | if (t.isIdentifier(expression)) {
|
97 | var idName = expression.node.name;
|
98 |
|
99 | if (expression.scope.hasBinding(idName)) {
|
100 | var externalStylesIdentifier = t.identifier(idName);
|
101 | var isGlobal = (0, _utils.isGlobalEl)(style.get('openingElement').node);
|
102 | state.externalStyles.push([t.memberExpression(externalStylesIdentifier, t.identifier('__hash')), externalStylesIdentifier, isGlobal]);
|
103 | continue;
|
104 | }
|
105 |
|
106 | throw path.buildCodeFrameError("The Identifier " + ("`" + expression.getSource() + "` is either `undefined` or ") + "it is not an external StyleSheet reference i.e. " + "it doesn't come from an `import` or `require` statement");
|
107 | }
|
108 |
|
109 | if (!t.isTemplateLiteral(expression) && !t.isStringLiteral(expression)) {
|
110 | throw path.buildCodeFrameError("Expected a template " + "literal or String literal as the child of the " + "JSX Style tag (eg: <style jsx>{`some css`}</style>)," + (" but got " + expression.type));
|
111 | }
|
112 |
|
113 | state.styles.push((0, _utils.getJSXStyleInfo)(expression, scope));
|
114 | }
|
115 |
|
116 | var externalJsxId;
|
117 |
|
118 | if (state.externalStyles.length > 0) {
|
119 | var expressions = state.externalStyles
|
120 | .filter(function (s) {
|
121 | return !s[2];
|
122 | }).map(function (s) {
|
123 | return s[0];
|
124 | });
|
125 | var expressionsLength = expressions.length;
|
126 |
|
127 | if (expressionsLength === 0) {
|
128 | externalJsxId = null;
|
129 | } else {
|
130 |
|
131 |
|
132 | externalJsxId = t.templateLiteral([t.templateElement({
|
133 | raw: 'jsx-',
|
134 | cooked: 'jsx-'
|
135 | })].concat([].concat(new Array(expressionsLength - 1).fill(null)).map(function () {
|
136 | return t.templateElement({
|
137 | raw: ' jsx-',
|
138 | cooked: ' jsx-'
|
139 | });
|
140 | }), [t.templateElement({
|
141 | raw: '',
|
142 | cooked: ''
|
143 | }, true)]), expressions);
|
144 | }
|
145 | }
|
146 |
|
147 | if (state.styles.length > 0 || externalJsxId) {
|
148 | var _computeClassNames = (0, _utils.computeClassNames)(state.styles, externalJsxId),
|
149 | staticClassName = _computeClassNames.staticClassName,
|
150 | className = _computeClassNames.className;
|
151 |
|
152 | state.className = className;
|
153 | state.staticClassName = staticClassName;
|
154 | }
|
155 |
|
156 | state.hasJSXStyle = true;
|
157 | state.file.hasJSXStyle = true;
|
158 | },
|
159 | exit: function exit(path, state) {
|
160 | var isGlobal = (0, _utils.isGlobalEl)(path.node.openingElement);
|
161 |
|
162 | if (state.hasJSXStyle && ! --state.ignoreClosing && !isGlobal) {
|
163 | state.hasJSXStyle = null;
|
164 | state.className = null;
|
165 | state.externalJsxId = null;
|
166 | }
|
167 |
|
168 | if (!state.hasJSXStyle || !(0, _utils.isStyledJsx)(path)) {
|
169 | return;
|
170 | }
|
171 |
|
172 | if (state.ignoreClosing > 1) {
|
173 | var styleTagSrc;
|
174 |
|
175 | try {
|
176 | styleTagSrc = path.getSource();
|
177 | } catch (error) {}
|
178 |
|
179 | throw path.buildCodeFrameError('Detected nested style tag' + (styleTagSrc ? ": \n\n" + styleTagSrc + "\n\n" : ' ') + 'styled-jsx only allows style tags ' + 'to be direct descendants (children) of the outermost ' + 'JSX element i.e. the subtree root.');
|
180 | }
|
181 |
|
182 | if (state.externalStyles.length > 0 && path.get('children').filter(function (child) {
|
183 | if (!t.isJSXExpressionContainer(child)) {
|
184 | return false;
|
185 | }
|
186 |
|
187 | var expression = child.get('expression');
|
188 | return expression && expression.isIdentifier();
|
189 | }).length === 1) {
|
190 | var _state$externalStyles = state.externalStyles.shift(),
|
191 | id = _state$externalStyles[0],
|
192 | _css = _state$externalStyles[1];
|
193 |
|
194 | path.replaceWith((0, _utils.makeStyledJsxTag)(id, _css));
|
195 | return;
|
196 | }
|
197 |
|
198 | var _state$opts = state.opts,
|
199 | vendorPrefixes = _state$opts.vendorPrefixes,
|
200 | sourceMaps = _state$opts.sourceMaps;
|
201 |
|
202 | var stylesInfo = _extends({}, state.styles.shift(), {
|
203 | file: state.file,
|
204 | staticClassName: state.staticClassName,
|
205 | isGlobal: isGlobal,
|
206 | plugins: state.plugins,
|
207 | vendorPrefixes: vendorPrefixes,
|
208 | sourceMaps: sourceMaps
|
209 | });
|
210 |
|
211 | var splitRules = typeof state.opts.optimizeForSpeed === 'boolean' ? state.opts.optimizeForSpeed : process.env.NODE_ENV === 'production';
|
212 |
|
213 | var _processCss = (0, _utils.processCss)(stylesInfo, {
|
214 | splitRules: splitRules
|
215 | }),
|
216 | hash = _processCss.hash,
|
217 | css = _processCss.css,
|
218 | expressions = _processCss.expressions;
|
219 |
|
220 | path.replaceWith((0, _utils.makeStyledJsxTag)(hash, css, expressions));
|
221 | }
|
222 | }
|
223 | };
|
224 |
|
225 | if (t.isJSXFragment) {
|
226 | jsxVisitors.JSXFragment = jsxVisitors.JSXElement;
|
227 | jsxVisitors.JSXOpeningFragment = {
|
228 | enter: function enter(path, state) {
|
229 | if (!state.hasJSXStyle) {
|
230 | return;
|
231 | }
|
232 |
|
233 | if (state.ignoreClosing === null) {
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 | state.ignoreClosing = 0;
|
240 | }
|
241 |
|
242 | state.ignoreClosing++;
|
243 | }
|
244 | };
|
245 | }
|
246 |
|
247 | var visitors = {
|
248 | inherits: _babelPluginSyntaxJsx["default"],
|
249 | visitor: {
|
250 | Program: {
|
251 | enter: function enter(path, state) {
|
252 | state.hasJSXStyle = null;
|
253 | state.ignoreClosing = null;
|
254 | state.file.hasJSXStyle = false;
|
255 | (0, _utils.setStateOptions)(state);
|
256 |
|
257 |
|
258 |
|
259 | path.traverse(jsxVisitors, state);
|
260 |
|
261 | path.traverse(_babelExternal.visitor, state);
|
262 | },
|
263 | exit: function exit(_ref3, state) {
|
264 | var scope = _ref3.scope;
|
265 |
|
266 | if (!(state.file.hasJSXStyle && !state.hasInjectedJSXStyle && !scope.hasBinding(_constants.STYLE_COMPONENT))) {
|
267 | return;
|
268 | }
|
269 |
|
270 | state.hasInjectedJSXStyle = true;
|
271 | (0, _utils.createReactComponentImportDeclaration)(state);
|
272 | }
|
273 | }
|
274 | }
|
275 | };
|
276 | return visitors;
|
277 | } |
\ | No newline at end of file |