svg-to-icon-component-runtime-generator.js 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. const path = require('path');
  2. const pascalCase = require('pascal-case');
  3. const { stringifyRequest } = require('loader-utils');
  4. const { stringifySymbol, stringify } = require('../../lib/utils');
  5. module.exports = function runtimeGenerator({ symbol, config, context, loaderContext }) {
  6. const { spriteModule, symbolModule, runtimeOptions } = config;
  7. const compilerContext = loaderContext._compiler.context;
  8. const iconModulePath = path.resolve(compilerContext, runtimeOptions.iconModule);
  9. const iconModuleRequest = stringify(
  10. path.relative(path.dirname(symbol.request.file), iconModulePath)
  11. );
  12. const spriteRequest = stringifyRequest({ context }, spriteModule);
  13. const symbolRequest = stringifyRequest({ context }, symbolModule);
  14. const displayName = `${pascalCase(symbol.id)}Icon`;
  15. return `
  16. import React, {PureComponent} from 'react';
  17. import SpriteSymbol from ${symbolRequest};
  18. import sprite from ${spriteRequest};
  19. import Icon from ${iconModuleRequest};
  20. const symbol = new SpriteSymbol(${stringifySymbol(symbol)});
  21. sprite.add(symbol);
  22. export default class ${displayName} extends Icon {}
  23. ${displayName}.defaultProps.glyph = '${symbol.id}';
  24. `;
  25. };