@d0whc3r/stencil-inline-svg

A Stenciljs plugin to insert inline SVGs into components on build time.

Usage no npm install needed!

<script type="module">
  import d0whc3rStencilInlineSvg from 'https://cdn.skypack.dev/@d0whc3r/stencil-inline-svg';
</script>

README

stencil-inline-svg

A Stenciljs plugin to insert inline SVGs into components on build time.

Installation

npm install -D @d0whc3r/stencil-inline-svg

Usage

First of all, you need to declare the global *.svg module on your TypeScript project, if you haven't done that yet so it will be possible to directly import SVG files import Icon from './my-icon.svg'.

// src/typings.d.ts (example name)
declare module '*.svg' {
  const svgContent: string;

  export default svgContent;
}

Import the plugin on your stencil.config.ts file and add the inlineSvg function to your plugins list.

import { Config } from '@stencil/core';
import { inlineSvg } from 'stencil-inline-svg';

export const config: Config = {
  namespace: 'mycomponent',
  outputTargets: [
    { type: 'dist' },
    { type: 'docs' },
    {
      type: 'www',
      serviceWorker: null, // disable service workers
    },
  ],
  plugins: [inlineSvg()],
};

Now, just import your file and set it as the innerHTML property of an element inside your component.

import { Component } from '@stencil/core';
import Lambo from '../../assets/img/gallardo.svg';

@Component({
  tag: 'my-component',
  styleUrl: 'my-component.css',
  shadow: true,
})
export class MyComponent {
  render() {
    return <img src={Lambo} />;
  }
}

Links

Stenciljs - https://stenciljs.com/

Lamborghini SVG - https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/gallardo.svg