Esbuild plugin for load svg icons to symbol in html for quick and easy import
npm install esbuild-svg-symbol-pluginInspired logic for Esbuild runtime loading and rendering of SVG as symbols
from SVG sprite loader. Contain only runtime part, not sprite
extraction.
- Why it's cool
- Installation
- Configuration
- esModule
- Runtime configuration
- spriteModule
- symbolModule
- runtimeGenerator
- License
- Credits
- Minimum initial configuration. Most of the options are configured automatically.
- Runtime for browser. Sprites are rendered and injected in pages automatically, you just refer to images
via .
- Isomorphic runtime for node/browser. Can render sprites on server or in browser manually.
- Customizable. Write/extend runtime module to implement custom sprite behaviour. Write/extend runtime generator to
produce your own runtime, e.g. React component configured with imported symbol.
``bash`
npm install esbuild-svg-symbol-plugin -Dvia yarn
yarn add esbuild-svg-symbol-plugin -D
`js`
// esbuild
esbuild.build({
entryPoints: ['src/index.js'],
bundle: true,
outfile: 'dist/bundle.js',
plugins: [
svgSymbolLoaderPlugin(config) // options are documented below
],
}).catch(e => (console.error(e), process.exit(1)))
Generated export format:
- when true loader will produce export default ....false
- when the result is module.exports = ....
By default depends on used webpack version: true for webpack >= 2, false otherwise.
When you require an image, loader transforms it to SVG , adds it to the special sprite storage and returnsid
class instance
that represents symbol. It contains , viewBox and content (id, viewBox and url in extract mode)
fields and can later be used for referencing the sprite image, e.g:
`js
import twitterLogo from './logos/twitter.svg';
// twitterLogo === SpriteSymbol
// Extract mode: SpriteSymbol
const rendered = ;`
or for dynamic imports:
`js./logos/${icon}.svg
const dynamicIcon = await import();
const rendered = ;`
When browser event DOMContentLoaded is fired, sprite will be automatically rendered and injected indocument.body
the .spriteModule
If custom behaviour is needed (e.g. a different mounting target) default sprite module could be overridden
via option. Check example below.
Path to sprite module that will be compiled and executed at runtime.
By default it depends on target webpack config option:
- esbuild-svg-symbol-plugin/runtime/browser-sprite.build for 'web' target.esbuild-svg-symbol-plugin/runtime/sprite.build
- for other targets.
If you need custom behavior, use this option to specify a path of your sprite implementation module.
Path will be resolved relative to the current webpack build folder, e.g. utils/sprite.js placed in current project dir./utils/sprite
should be written as .
Example of sprite with custom mounting target (copypasted
from browser-sprite):
`js
import BrowserSprite from 'svg-baker-runtime/src/browser-sprite';
import domready from 'domready';
const sprite = new BrowserSprite();
domready(() => sprite.mount('#my-custom-mounting-target'));
export default sprite; // don't forget to export!
`
Same as spriteModule, but for sprite symbol. By default also depends on target webpack config option:
- svg-baker-runtime/browser-symbol for 'web' target.svg-baker-runtime/symbol` for other targets.
-
Path to node.js script that generates client runtime.
Use this option if you need to produce your own runtime, e.g. React component configured with imported
symbol.
See LICENSE
Huge thanks for all this people.