UNPKG

2.17 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.default = plugin;
5
6var t = _interopRequireWildcard(require("@babel/types"));
7
8var _getName = _interopRequireDefault(require("../utils/getName"));
9
10function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
12function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
13
14function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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; }
15
16function plugin() {
17 return {
18 name: 'rename-styled-import',
19 visitor: {
20 ImportDeclaration(path) {
21 if (path.get('source').node.value !== 'astroturf') {
22 return;
23 }
24
25 const styled = path.get('specifiers').find(s => s.isImportDefaultSpecifier() || s.isImportSpecifier() && (0, _getName.default)(s.node.imported) === 'styled');
26 if (!styled) return; // used to be exported as both a named and default
27
28 if (styled.isImportSpecifier()) {
29 // @ts-ignore
30 path.unshiftContainer('specifiers', t.importDefaultSpecifier(t.identifier(styled.node.local.name)));
31 styled.remove();
32 }
33
34 path.get('source').replaceWith(t.stringLiteral('astroturf/react'));
35 }
36
37 }
38 };
39}
\No newline at end of file