Epimodev / svg-sprite-html-webpack

Webpack loader and plugin to generate a SVG sprite with <symbol> elements and inject it in html built by html-webpack-plugin
48 stars 4 forks source link

How to use without html-webpack-plugin ? #12

Open ryuran opened 5 years ago

ryuran commented 5 years ago

I love you plugin but I have an issue.

I need to insert the svg sprite inline in some html without htmlWebpackPlugin.

By exemple using it in KSS documentation or insert sprite in Html from a php website (webpack is only used to make a bundle of a front module inserted in a bigger php website).

How to do, can we export svgsprite string or insert it by js only ?

Epimodev commented 5 years ago

Hello !

I haven't use KSS yet so I'm not sure the solution I suggest will work.

I've created another module to create svg sprite without webpack : https://github.com/Epimodev/sprite-svg-symbol You just need to call this module with the folder containing svg files and it returns a promise with the svg sprite. Then you can use posthtml (https://github.com/posthtml/posthtml) to inject this sprite in a html file.

For the moment this module doesn't set aria attribute but if this solution solves your problem I can update it.

ryuran commented 5 years ago

Hello, I have try several solution for svg symboles

Epimodev commented 5 years ago

I haven't try those plugins but according to their documentation I've got same conclusion.

If you want to insert a sprite in a html file without html-webpack-plugin the only solution I've got in mind is to inject the sprite after webpack build.

ryuran commented 5 years ago

Yes, same conclusion but svg-sprite-html-webpack do not allow to export sprite as a string variable or as a file. The only way is by html-webpack-plugin hook.

Epimodev commented 5 years ago

does an option onSpriteGenerated: (sprite: string) => void which will be a callback with the svg sprite as parameter is a solution which help you in your case ?

ryuran commented 5 years ago

Yes I think it can help, I'm not sure it it's enought to work with an othe webpack plugin like kss-webpack plugin because it will create an asynchrone issue. I think the way to do that is using a webpack hook (but I‘m not sure).

Epimodev commented 5 years ago

Ok, for the moment this plugin doesn't emits any value for webpack hook. But I can add a callback and emit a value with webpack hook, even if it doesn't solve your issue it can help other developers which use this plugin.

I'll work on this today or tomorrow.

Epimodev commented 5 years ago

Hello !

Sorry I've got more work than I expected during last days so I haven't implement callback and webpack hook yet.

But tomorrow I will have the morning to work on this.

ryuran commented 5 years ago
const { createSprite } = require('svg-sprite-html-webpack/src/spriteUtils')

I find a way using your createSprite function to get a sprite before webpack build.

Epimodev commented 5 years ago

ok happy to read you find a solution.

Sorry, I forgot to add onSpriteGenerated and try to add webpack hook.

I keep this issue open until I make those changes.