Thinking more about it, elm-assets-loader should be able to load the file and inject it in the elm generated code using svg-inline-loader. I have never tried because I use webpack3 and it is not yet compatible with it.
But I gave it a try using a webpack2 elm test project and it works, and chaining through svgo-loader works also.
Here is my webpack2 config part:
rules: [{
test: /\.elm$/,
exclude: [/elm-stuff/, /node_modules/],
use: [
{
loader: 'elm-assets-loader',
options: {
module: 'My.Assets',
tagger: 'AssetPath',
package: 'test/test'
}
},
'elm-webpack-loader'
]
},
{
test: /\.svg$/i,
use: [{
loader: 'svg-inline-loader',
},
{
loader: 'svgo-loader',
options: {
plugins: [
{ removeTitle: true },
{ convertColors: { shorthex: false } },
{ convertPathData: false }
]
}
}]
}]
}
My My.Assets module is identical to the one in the elm-assets-loader documentation:
module My.Assets exposing (AssetPath(..), path, star)
import Html exposing (..)
type AssetPath
= AssetPath String
star : AssetPath
star =
AssetPath "./star.svg"
path : AssetPath -> String
path (AssetPath str) =
str
and my test view is:
textHtml : String -> Html msg
textHtml t =
div
[ Json.Encode.string t
|> Html.Attributes.property "innerHTML"
, style
[ ( "width", "24px" )
, ( "height", "24px" )
]
]
[]
view : Model -> Html Msg
view model =
div []
[ textHtml <| My.Assets.path <| My.Assets.star
, text "hello"
]
This part is a little dirty to be able to inject the svg node, but I don’t know a better way without modifying elm-assets-loader. However this most likely won’t work with Elm 0.19 given the recent patch that forbids innerHTML, is there another way?
Still, this works fine, the svg file is processed through svgo then correctly inserted, I get:
<div id="main">
<div>
<div style="width: 24px; height: 24px;">
<svg xmlns="http://www.w3.org/2000/svg">
<path d="..."></path>
</svg>
</div>hello</div>
</div>
Pretty cool!
Now if only elm-assets-loader was compatible with webpack3, but maybe someone has began to update it?
PS: I’m not sure why @luke did not use this as this is a NoRedInk project.
Edit: I think the proper way to avoid injecting the SVG node using the innerHTML property in Elm would be to modify elm-assets-loader to make it supporting tagged Svg msg values in addition to String. In this case it would use innerHTML itself in javascript. I don’t think this would be too dangerous as this is done during the build.
Edit2: there is a fork updated for webpack3/4: https://github.com/alvivi/elm-assets-loader