Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

warmrobot
Copy link

Idea from webpack svg-sprite-loader
In addition to create sprite, you can return symbol parameters in same time.

import twitterLogo from './logos/twitter.svg';
// twitterLogo === SpriteSymbol<id: string, viewBox: string, content: string>
// Extract mode: SpriteSymbol<id: string, viewBox: string, url: string, toString: Function>

const rendered = `
<svg viewBox="${twitterLogo.viewBox}">
  <use xlink:href="#${twitterLogo.id}" />
</svg>`;

For example, we can use great method with external sprite. Like this:

<svg viewBox="0 0 100 100" class="icon shape-codepen">
  <use xlink:href="/images/svg-defs.svg#shape-codepen"></use>
</svg>

https://css-tricks.com/svg-sprites-use-better-icon-fonts/

Copy link
Owner

@vladshcherbin vladshcherbin left a 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',
Copy link
Owner

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

Copy link
Author

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'),
Copy link
Owner

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

Copy link
Author

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>`
Copy link
Owner

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 },}`
Copy link
Owner

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?

Copy link
Author

@warmrobot warmrobot Dec 1, 2019

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)
Copy link
Owner

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

Copy link
Author

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.

@warmrobot
Copy link
Author

Something wrong?

@klevytskyi
Copy link

klevytskyi commented May 21, 2020

@vladshcherbin mb it's time to recall that PR? :) I've been waiting for something similar to webpack's svg-sprite-loader for ages and till now used naїvely written rollup-plugin-svg-spritesheet but it's methods are deprecated and don't see ant sense to contrib there.
Ping me if any help needed, cause I'd like to move to rollup@^2 with that one plugin)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants