4a345a0eae9357f3b95838a2a497e62114f8077d78df7249d81ce827aca3226056ce148fe88c3370b7b99c4581c296e5f6dfff97fb50af4f047d07ed292d95 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # Custom runtime generating example
  2. Runtime code generated by loader could be overridden by providing custom generator via `runtimeGenerator` option.
  3. For example you can return React component preconfigured with imported symbol data instead of default [symbol instance](https://github.com/JetBrains/svg-baker/blob/master/packages/svg-baker-runtime/src/symbol.js).
  4. ### [Demo](demo.html)
  5. ### Config
  6. - [webpack.config.js](webpack.config.js)
  7. - [svg-to-icon-component-runtime-generator.js](svg-to-icon-component-runtime-generator.js)
  8. ### Input
  9. - [main.jsx](main.jsx)
  10. - [icon.jsx](icon.jsx)
  11. This import:
  12. ```js
  13. import TwitterIcon from '../assets/twitter.svg';
  14. ```
  15. Will be generated to:
  16. ```js
  17. import React from 'react';
  18. import SpriteSymbol from 'svg-sprite-loader/runtime/symbol';
  19. import sprite from 'svg-sprite-loader/runtime/browser-sprite';
  20. import SpriteSymbolComponent from './icon.jsx';
  21. const symbol = new SpriteSymbol({ /* symbol data */ });
  22. sprite.add(symbol);
  23. export default class TwitterIcon extends React.Component {
  24. render() {
  25. return <SpriteSymbolComponent glyph="${symbol.id}" {...this.props} />;
  26. }
  27. }
  28. ```
  29. So when you import SVG, actually React component returns with configured glyph:
  30. ```js
  31. import TwitterIcon from '../assets/twitter.svg';
  32. render(
  33. <div>
  34. <TwitterIcon width="100" />
  35. <TwitterIcon fill="red" style={{width: 300}} />
  36. <TwitterIcon fill="blue" style={{width: 600}} />
  37. </div>,
  38. document.querySelector('.app')
  39. );
  40. ```
  41. ### Output
  42. - [build/main.js](build/main.js)