Skip to content

Commit

Permalink
[FIX] Merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
Sébastien LeBlanc committed Aug 31, 2022
2 parents 15b16e8 + b77275c commit b2d5447
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 11 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ svgLoader({
})
```

### Skip SVGO for a single file
SVGO can be explicitly disabled for one file by adding the `?skipsvgo` suffix:
```js
import IconWithoutOptimizer from './my-icon.svg?skipsvgo'
// <IconWithoutOptimizer />
```

### Use with TypeScript
If you use the loader in a Typescript project, you'll need to import your svg files with the `?component` param: `import MyIcon from './my-icon.svg?component'`.

Expand Down
10 changes: 10 additions & 0 deletions cypress/integration/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ describe('Vite SVG Loader', () => {
cy.get('#raw').contains('<?xml version="1.0"?>')
})

it('uses svgo', () => {
cy.get('#component svg')
.should('not.have.attr', 'id') // Should be stripped by svgo
})

it('supports ?skipsvgo param', () => {
cy.get('#skipsvgo svg')
.should('have.attr', 'id')
})

it('ignores root files references', () => {
cy.get('#root img')
.should('exist')
Expand Down
5 changes: 5 additions & 0 deletions examples/vue/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { defineAsyncComponent } from 'vue'
import HelloWorld from './components/HelloWorld.vue'
import Test from './assets/test.svg'
import TestSkipSvgo from './assets/test.svg?skipsvgo'
import testUrl from './assets/test.svg?url'
import testRaw from './assets/test.svg?raw'
Expand All @@ -23,6 +24,10 @@ const Async = defineAsyncComponent(() => import(`./assets/${name}.svg`))
<Async />
</div>

<div id="skipsvgo">
<TestSkipSvgo />
</div>

<div id="url">
{{ testUrl }}
</div>
Expand Down
5 changes: 3 additions & 2 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.
12 changes: 9 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Plugin } from 'vite'
import { OptimizeOptions } from 'svgo'
import { FunctionalComponent, SVGAttributes } from 'vue'

declare module 'vite-svg-loader' {
import { Plugin } from 'vite'
import { OptimizeOptions } from 'svgo'
function svgLoader(options?: { svgoConfig?: OptimizeOptions, svgo?: boolean, defaultImport?: 'url' | 'raw' | 'component' }): Plugin
export default svgLoader
}

declare module '*.svg?component' {
import { FunctionalComponent, SVGAttributes } from 'vue'
const src: FunctionalComponent<SVGAttributes>
export default src
}
Expand All @@ -20,3 +21,8 @@ declare module '*.svg?raw' {
const src: string
export default src
}

declare module '*.svg?skipsvgo' {
const src: FunctionalComponent<SVGAttributes>
export default src
}
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { optimize } = require('svgo')
module.exports = function svgLoader (options = {}) {
const { svgoConfig, svgo, defaultImport } = options

const svgRegex = /\.svg(\?(raw|component))?$/
const svgRegex = /\.svg(\?(raw|component|skipsvgo))?$/

return {
name: 'svg-loader',
Expand All @@ -29,14 +29,15 @@ module.exports = function svgLoader (options = {}) {
try {
svg = await fs.readFile(path, 'utf-8')
} catch (ex) {
return // File couldn't be loaded, fallback to default loader
console.warn('\n', `${id} couldn't be loaded by vite-svg-loader, fallback to default loader`)
return
}

if (importType === 'raw') {
return `export default ${JSON.stringify(svg)}`
}

if (svgo !== false) {
if (svgo !== false && query !== 'skipsvgo') {
svg = optimize(svg, {
...svgoConfig,
path
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite-svg-loader",
"version": "3.4.0",
"version": "3.5.0",
"description": "Vite plugin to load SVG files as Vue components",
"keywords": [
"vite",
Expand Down

0 comments on commit b2d5447

Please sign in to comment.