NOTE: This repo is not maintained any more. Feel free to fork and upload to elm-packages by yourself.
A webpack loader that inlines external SVG file into Elm views. It consists of a elm package and a npm package. DEMO.
Inspired by elm-css-modules-loader.
module Main exposing (..)
import Svg.Attributes
import InlineSvg exposing (inline)
{ icon } =
inline
{ github = "./svg/github.svg"
, share = "./svg/share.svg"
}
view =
div
[]
[ icon .github [ Svg.Attributes.class "icon icon--github" ]
, icon .share [ Svg.Attributes.class "icon icon--share" ]
]
Add elm-svg-loader
and raw-loader
to your project.
npm install raw-loader elm-svg-loader --save-dev
or
yarn add raw-loader elm-svg-loader --dev
module.exports = {
⋮
module: {
rules: [
{
test: /\.elm$/,
use: [
{
loader: "elm-svg-loader",
},
{
loader: "elm-webpack",
}
],
},
{
test: /\.svg$/,
loaders: ["raw-loader"]
}
⋮
],
},
};
elm-package install rnons/elm-svg-loader
Then you can import InlineSvg
as in the Overview section.
-
Without
elm-svg-loader
, webpack will compile{ icon } = inline { github = "./svg/github.svg" , share = "./svg/share.svg" }
to
var _user$project$Main$_p0 = _rnons$elm_svg_loader$InlineSvg$inline( {github: './svg/mark-github.svg', share: './svg/share.svg'});
-
elm-svg-loader
will replace the svg file location with arequire
statementvar _user$project$Main$_p0 = _rnons$elm_svg_loader$InlineSvg$inline( {github: require('./svg/mark-github.svg'), share: require('./svg/share.svg')});
-
With the help of
raw-loader
,require('./svg/mark-github.svg')
will become the actual svg file content. -
With the help of elm-svg-parser, the
icon
function inicon .github []
parses the svg file contentString
to aHtml msg
so that it can be used in Elm views.