index.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var path = require('path');
  4. var camelCase = require('camelcase');
  5. var cosmiconfig = require('cosmiconfig');
  6. var jsx = require('@svgr/plugin-jsx');
  7. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  8. var camelCase__default = /*#__PURE__*/_interopDefaultLegacy(camelCase);
  9. var jsx__default = /*#__PURE__*/_interopDefaultLegacy(jsx);
  10. var __defProp$1 = Object.defineProperty;
  11. var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
  12. var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
  13. var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
  14. var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
  15. var __spreadValues$1 = (a, b) => {
  16. for (var prop in b || (b = {}))
  17. if (__hasOwnProp$1.call(b, prop))
  18. __defNormalProp$1(a, prop, b[prop]);
  19. if (__getOwnPropSymbols$1)
  20. for (var prop of __getOwnPropSymbols$1(b)) {
  21. if (__propIsEnum$1.call(b, prop))
  22. __defNormalProp$1(a, prop, b[prop]);
  23. }
  24. return a;
  25. };
  26. const VALID_CHAR_REGEX = /[^a-zA-Z0-9 _-]/g;
  27. const getComponentName = (filePath) => {
  28. if (!filePath)
  29. return "SvgComponent";
  30. const pascalCaseFileName = camelCase__default["default"](
  31. path.parse(filePath).name.replace(VALID_CHAR_REGEX, ""),
  32. {
  33. pascalCase: true
  34. }
  35. );
  36. return `Svg${pascalCaseFileName}`;
  37. };
  38. const expandState = (state) => {
  39. return __spreadValues$1({
  40. componentName: state.componentName || getComponentName(state.filePath)
  41. }, state);
  42. };
  43. var __defProp = Object.defineProperty;
  44. var __getOwnPropSymbols = Object.getOwnPropertySymbols;
  45. var __hasOwnProp = Object.prototype.hasOwnProperty;
  46. var __propIsEnum = Object.prototype.propertyIsEnumerable;
  47. var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
  48. var __spreadValues = (a, b) => {
  49. for (var prop in b || (b = {}))
  50. if (__hasOwnProp.call(b, prop))
  51. __defNormalProp(a, prop, b[prop]);
  52. if (__getOwnPropSymbols)
  53. for (var prop of __getOwnPropSymbols(b)) {
  54. if (__propIsEnum.call(b, prop))
  55. __defNormalProp(a, prop, b[prop]);
  56. }
  57. return a;
  58. };
  59. var __objRest = (source, exclude) => {
  60. var target = {};
  61. for (var prop in source)
  62. if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
  63. target[prop] = source[prop];
  64. if (source != null && __getOwnPropSymbols)
  65. for (var prop of __getOwnPropSymbols(source)) {
  66. if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
  67. target[prop] = source[prop];
  68. }
  69. return target;
  70. };
  71. const DEFAULT_CONFIG = {
  72. dimensions: true,
  73. expandProps: "end",
  74. icon: false,
  75. native: false,
  76. typescript: false,
  77. prettier: true,
  78. prettierConfig: void 0,
  79. memo: false,
  80. ref: false,
  81. replaceAttrValues: void 0,
  82. svgProps: void 0,
  83. svgo: true,
  84. svgoConfig: void 0,
  85. template: void 0,
  86. index: false,
  87. titleProp: false,
  88. descProp: false,
  89. runtimeConfig: true,
  90. namedExport: "ReactComponent",
  91. exportType: "default"
  92. };
  93. const explorer = cosmiconfig.cosmiconfig("svgr");
  94. const explorerSync = cosmiconfig.cosmiconfigSync("svgr");
  95. const resolveConfig = async (searchFrom, configFile) => {
  96. if (configFile == null) {
  97. const result2 = await explorer.search(searchFrom);
  98. return result2 ? result2.config : null;
  99. }
  100. const result = await explorer.load(configFile);
  101. return result ? result.config : null;
  102. };
  103. resolveConfig.sync = (searchFrom, configFile) => {
  104. if (configFile == null) {
  105. const result2 = explorerSync.search(searchFrom);
  106. return result2 ? result2.config : null;
  107. }
  108. const result = explorerSync.load(configFile);
  109. return result ? result.config : null;
  110. };
  111. const resolveConfigFile = async (filePath) => {
  112. const result = await explorer.search(filePath);
  113. return result ? result.filepath : null;
  114. };
  115. resolveConfigFile.sync = (filePath) => {
  116. const result = explorerSync.search(filePath);
  117. return result ? result.filepath : null;
  118. };
  119. const loadConfig = async (_a, state = {}) => {
  120. var _b = _a, { configFile } = _b, baseConfig = __objRest(_b, ["configFile"]);
  121. const rcConfig = state.filePath && baseConfig.runtimeConfig !== false ? await resolveConfig(state.filePath, configFile) : {};
  122. return __spreadValues(__spreadValues(__spreadValues({}, DEFAULT_CONFIG), rcConfig), baseConfig);
  123. };
  124. loadConfig.sync = (_c, state = {}) => {
  125. var _d = _c, { configFile } = _d, baseConfig = __objRest(_d, ["configFile"]);
  126. const rcConfig = state.filePath && baseConfig.runtimeConfig !== false ? resolveConfig.sync(state.filePath, configFile) : {};
  127. return __spreadValues(__spreadValues(__spreadValues({}, DEFAULT_CONFIG), rcConfig), baseConfig);
  128. };
  129. const DEFAULT_PLUGINS = [jsx__default["default"]];
  130. const getPlugins = (config, state) => {
  131. var _a;
  132. if (config.plugins) {
  133. return config.plugins;
  134. }
  135. if ((_a = state.caller) == null ? void 0 : _a.defaultPlugins) {
  136. return state.caller.defaultPlugins;
  137. }
  138. return DEFAULT_PLUGINS;
  139. };
  140. const resolvePlugin = (plugin) => {
  141. if (typeof plugin === "function") {
  142. return plugin;
  143. }
  144. if (typeof plugin === "string") {
  145. return loadPlugin(plugin);
  146. }
  147. throw new Error(`Invalid plugin "${plugin}"`);
  148. };
  149. const pluginCache = {};
  150. const resolveModule = (m) => m ? m.default || m : null;
  151. const loadPlugin = (moduleName) => {
  152. if (pluginCache[moduleName]) {
  153. return pluginCache[moduleName];
  154. }
  155. try {
  156. const plugin = resolveModule(require(moduleName));
  157. if (!plugin) {
  158. throw new Error(`Invalid plugin "${moduleName}"`);
  159. }
  160. pluginCache[moduleName] = plugin;
  161. return pluginCache[moduleName];
  162. } catch (error) {
  163. console.log(error);
  164. throw new Error(
  165. `Module "${moduleName}" missing. Maybe \`npm install ${moduleName}\` could help!`
  166. );
  167. }
  168. };
  169. const run = (code, config, state) => {
  170. const expandedState = expandState(state);
  171. const plugins = getPlugins(config, state).map(resolvePlugin);
  172. let nextCode = String(code).replace("\0", "");
  173. for (const plugin of plugins) {
  174. nextCode = plugin(nextCode, config, expandedState);
  175. }
  176. return nextCode;
  177. };
  178. const transform = async (code, config = {}, state = {}) => {
  179. config = await loadConfig(config, state);
  180. return run(code, config, state);
  181. };
  182. transform.sync = (code, config = {}, state = {}) => {
  183. config = loadConfig.sync(config, state);
  184. return run(code, config, state);
  185. };
  186. exports.DEFAULT_CONFIG = DEFAULT_CONFIG;
  187. exports.loadConfig = loadConfig;
  188. exports.resolveConfig = resolveConfig;
  189. exports.resolveConfigFile = resolveConfigFile;
  190. exports.transform = transform;
  191. //# sourceMappingURL=index.js.map