styled-components-macro.esm.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { addDefault, addNamed } from '@babel/helper-module-imports';
  2. import traverse from '@babel/traverse';
  3. import { createMacro } from 'babel-plugin-macros';
  4. import babelPlugin from 'babel-plugin-styled-components';
  5. function _extends() {
  6. _extends = Object.assign || function (target) {
  7. for (var i = 1; i < arguments.length; i++) {
  8. var source = arguments[i];
  9. for (var key in source) {
  10. if (Object.prototype.hasOwnProperty.call(source, key)) {
  11. target[key] = source[key];
  12. }
  13. }
  14. }
  15. return target;
  16. };
  17. return _extends.apply(this, arguments);
  18. }
  19. function styledComponentsMacro(_ref) {
  20. var references = _ref.references,
  21. state = _ref.state,
  22. t = _ref.babel.types,
  23. _ref$config = _ref.config,
  24. config = _ref$config === void 0 ? {} : _ref$config;
  25. var program = state.file.path; // FIRST STEP : replace `styled-components/macro` by `styled-components
  26. // references looks like this
  27. // { default: [path, path], css: [path], ... }
  28. var customImportName;
  29. Object.keys(references).forEach(function (refName) {
  30. // generate new identifier
  31. var id;
  32. if (refName === 'default') {
  33. id = addDefault(program, 'styled-components', {
  34. nameHint: 'styled'
  35. });
  36. customImportName = id;
  37. } else {
  38. id = addNamed(program, refName, 'styled-components', {
  39. nameHint: refName
  40. });
  41. } // update references with the new identifiers
  42. references[refName].forEach(function (referencePath) {
  43. // eslint-disable-next-line no-param-reassign
  44. referencePath.node.name = id.name;
  45. });
  46. }); // SECOND STEP : apply babel-plugin-styled-components to the file
  47. var stateWithOpts = _extends({}, state, {
  48. opts: config,
  49. customImportName: customImportName
  50. });
  51. traverse(program.parent, babelPlugin({
  52. types: t
  53. }).visitor, undefined, stateWithOpts);
  54. }
  55. var index = createMacro(styledComponentsMacro, {
  56. configName: 'styledComponents'
  57. });
  58. export default index;
  59. //# sourceMappingURL=styled-components-macro.esm.js.map