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

Send path to svgo #61

Merged
merged 5 commits into from Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions cypress/integration/spec.js
Expand Up @@ -62,4 +62,8 @@ describe('Vite SVG Loader', () => {
expect($img[0].width).to.equal(355)
})
})

it("it send path to svgo", () => {
cy.get("#component svg .test_svg__rectangle").should("exist");
});
})
12 changes: 5 additions & 7 deletions examples/vue/src/assets/test.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 12 additions & 1 deletion examples/vue/vite.config.js
Expand Up @@ -12,7 +12,18 @@ export default defineConfig(({ mode }) => {
const DEFAULT_IMPORT = env.VITE_SVG_DEFAULT_IMPORT || env.npm_config_svg_default_import

return {
plugins: [vue(), viteSvgLoader({ defaultImport: DEFAULT_IMPORT })],
plugins: [
vue(),
viteSvgLoader({
defaultImport: DEFAULT_IMPORT,
svgoConfig: {
plugins: [
'preset-default',
{ name: 'prefixIds' },
],
}
})
],

resolve: {
alias: {
Expand Down
7 changes: 5 additions & 2 deletions index.js
@@ -1,6 +1,6 @@
const fs = require('fs').promises
const { compileTemplate } = require('@vue/compiler-sfc')
const { optimize: optimizeSvg } = require('svgo')
const { optimize } = require('svgo')

module.exports = function svgLoader (options = {}) {
const { svgoConfig, svgo, defaultImport } = options
Expand Down Expand Up @@ -38,7 +38,10 @@ module.exports = function svgLoader (options = {}) {
}

if (svgo !== false && query !== 'skipsvgo') {
svg = optimizeSvg(svg, svgoConfig).data
svg = optimize(svg, {
...svgoConfig,
path
}).data
}

const { code } = compileTemplate({
Expand Down