-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,22 +7,33 @@ function createSymbol(code, id) { | |
const markup = cheerio.load(code, { xmlMode: true }) | ||
const svgMarkup = markup('svg') | ||
const symbolId = svgMarkup.find('title').text() || id | ||
const viewBox = svgMarkup.attr('viewBox') | ||
|
||
markup('svg').replaceWith('<symbol/>') | ||
markup('symbol') | ||
.attr('id', symbolId) | ||
.attr('viewBox', svgMarkup.attr('viewBox')) | ||
.attr('viewBox', viewBox) | ||
.append(svgMarkup.children()) | ||
|
||
return markup.xml('symbol') | ||
return { | ||
content: markup.xml('symbol'), | ||
id: symbolId, | ||
viewBox, | ||
} | ||
} | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
} | ||
|
||
export default function svgSprite(options = {}) { | ||
const { minify = true, outputFolder, ...rest } = options | ||
const { | ||
minify = true, | ||
outputFolder, | ||
publicPath = '', | ||
spriteFilename = 'sprites.svg', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets go with just There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ) |
||
...rest | ||
} = options | ||
|
||
if (!outputFolder) { | ||
throw new Error('"outputFolder" must be set') | ||
|
@@ -58,11 +69,12 @@ export default function svgSprite(options = {}) { | |
} | ||
|
||
const filename = path.basename(id, '.svg') | ||
const { content, viewBox, id: symbolId } = createSymbol(code, filename) | ||
|
||
convertedSvgs.set(id, createSymbol(code, filename)) | ||
convertedSvgs.set(id, content) | ||
|
||
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 commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well,
|
||
} | ||
}, | ||
async writeBundle() { | ||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, lets go with just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just for compatibility of configs with webpack. |
||
|
||
loadedSvgs.clear() | ||
} | ||
|
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 ofcontent
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.