How can I include a svg in my Kotlin/JS resources?...
# javascript
m
How can I include a svg in my Kotlin/JS resources? I followed this (https://kotlinlang.slack.com/archives/C0B8L3U69/p1637057339393600?thread_ts=1637054144.390200&cid=C0B8L3U69) but that only seems to include the file in the webserver, instead of embedding it in the JS file.
Copy code
@JsModule("./star.svg")
@JsNonModule
external val star: dynamic
console.log(star)
=
<http://localhost:8080/images/8879ae8fb00946dae2fd.svg>
, instead of printing the svg content. I want the svg to be inlined (because animations) so that's why I can't just reference the svg in a <img> tag
Found out how 🙂
Project Root Folder/webpack.config.d
Copy code
config.module.rules.push(
    {
        test: /\.(svg)$/i,
        type: 'asset/source'
    }
);
Copy code
@JsModule("./star.svg")
@JsNonModule
external val star: dynamic

console.log(star)
Prints...
Copy code
<svg xmlns="<http://www.w3.org/2000/svg>" viewBox="0 0 576 512"><!-- Font Awesome Free 5.15.1 by @fontawesome - <https://fontawesome.com> License - <https://fontawesome.com/license/free> (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) --><path fill="#ffffff" d="M528.1 171.5L382 150.2 316.7 17.8c-11.7-23.6-45.6-23.9-57.4 0L194 150.2 47.9 171.5c-26.2 3.8-36.7 36.1-17.7 54.6l105.7 103-25 145.5c-4.5 26.3 23.2 46 46.4 33.7L288 439.6l130.7 68.7c23.2 12.2 50.9-7.4 46.4-33.7l-25-145.5 105.7-103c19-18.5 8.5-50.8-17.7-54.6zM388.6 312.3l23.7 138.4L288 385.4l-124.3 65.3 23.7-138.4-100.6-98 139-20.2 62.2-126 62.2 126 139 20.2-100.6 98z"/></svg>
If you are curious about other available "type", check out https://webpack.js.org/guides/asset-modules/
t
You need loader
Old configuration But it works 🙂
Copy code
fun main() {
    val icon = require("icons/star.svg")
}
m
@turansky what's the difference between your solution and mine? Yours use "svg-inline-loader" so that's why I'm wondering
t
Optimization expected from
svg-inline-loader
🙏 1
f
@turansky Hi, I am new to kotlin (multiplattform + JS) and I guess your plugin kfc-plugins ( I added 5.45.0) is something I might need but I don’t understand yet how I can add the solution in WebpackLoadersPlugin above for example, to add inline svg. Could you maybe explain me a little bit more about the plugin and how to use it? Thank you in advance
t
svg-inline-loader
will be configured by default, if you will apply
kfc.application
plugin
f
@turansky I am getting a Run error when I try to use
id("io.github.turansky.kfc.application") version "5.45.0"
:
Cannot add extension with name 'kotlin', as there is an extension already registered with that name.
Do you have an idea where this could come from? Setup is Multiplatform & Springboot
t
Looks like you apply JS plugin before
193 Views