-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Return useful object instead empty string #3
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, looks good to me. I've added a few notes, most are just stylistic. You can correct them or I'll merge and replace later.
The only real question is about export part, can you please explain me. Other than this it's good to merge and ship.
minify = true, | ||
outputFolder, | ||
publicPath = '', | ||
spriteFilename = 'sprites.svg', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets go with just filename
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TLDR: for compatibility with webpack loader config file.
All options names comes from webpack loader. I think about those guys, who switching from webpack to rollup. They can preserve all the options from webpack version. Or (and this my particular case) who support 2 parallel build system when on transition process from one bundler to anoher. Or webpack for dev, rollup for production. Or just for fun )
.append(svgMarkup.children()) | ||
|
||
return markup.xml('symbol') | ||
return { | ||
content: markup.xml('symbol'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets go with markup
instead of content
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For compatibility with webpack options.
} | ||
|
||
function createSprite(symbols) { | ||
return `<svg xmlns="http://www.w3.org/2000/svg">${symbols.join('')}</svg>` | ||
return `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><defs>${symbols.join('')}</defs></svg>` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
|
||
return { | ||
code: '' | ||
code: `export default {id: '${symbolId}_usage', viewBox: '${viewBox}', url: '${publicPath}${spriteFilename}#${symbolId}', toString() { return this.url },}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't used webpack svg loader, can you explain me, why we need to add _usage
to symbol id
here and what are url
and toString()
used for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, _usage
comes from here
https://github.com/nightflash/svg-baker/blob/master/packages/svg-baker/lib/symbol.js#L42
Actually, there is -usage
, I made a typo. So you can refer to <use>
tag. Set fill
to another color or something. And id
is for symbol itself.
url
- is a full path to external svg, including target hash.
like /staticServerPath/img/sprite.svg#logo-usage
toString
is a really nice lifehack when you really not need all the properties of this object, but just url
const spriteObj = {
id: 'aaaId',
url: 'someStatic/svg.sprite#twi-usage',
toString() { return this.url }
}
// I need id!
console.log(spriteObj.id)
// Just give me url, without destructuring or something
console.log(`<svg>${spriteObj}</svg>`)
@@ -71,7 +83,7 @@ export default function svgSprite(options = {}) { | |||
const { data } = await svgo.optimize(createSprite(symbols)) | |||
|
|||
await fs.ensureDir(outputFolder) | |||
await fs.writeFile(`${outputFolder}/sprites.svg`, data) | |||
await fs.writeFile(`${outputFolder}/${spriteFilename}`, data) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above, lets go with just filename
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for compatibility of configs with webpack.
Something wrong? |
@vladshcherbin mb it's time to recall that PR? :) I've been waiting for something similar to webpack's |
Idea from webpack svg-sprite-loader
In addition to create sprite, you can return symbol parameters in same time.
For example, we can use great method with external sprite. Like this:
https://css-tricks.com/svg-sprites-use-better-icon-fonts/