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

preventing vue's compileTemplate from removing style tag #92

Merged
merged 2 commits into from
May 8, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cypress/integration/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ describe('Vite SVG Loader', () => {
expect($svg[0].width.baseVal.value).to.equal(400)
})
})
it('keeps style tag in components', () => {
cy.get('#style-tag svg style')
.should('exist')
})

it('supports ?url param', () => {
cy.get('#url').contains(/^\/assets\/test\..+\.svg/)
Expand Down
6 changes: 6 additions & 0 deletions examples/vue/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { defineAsyncComponent } from 'vue'
import HelloWorld from './components/HelloWorld.vue'
import Test from './assets/test.svg'
import TestSkipSvgo from './assets/test.svg?skipsvgo'
//skipsvgo because svgo with some params will inline styles
import StyleTagTest from './assets/style.svg?skipsvgo'
import testUrl from './assets/test.svg?url'
import testRaw from './assets/test.svg?raw'

Expand All @@ -28,6 +30,10 @@ const Async = defineAsyncComponent(() => import(`./assets/${name}.svg`))
<TestSkipSvgo />
</div>

<div id="style-tag">
<StyleTagTest />
</div>

<div id="url">
{{ testUrl }}
</div>
Expand Down
16 changes: 16 additions & 0 deletions examples/vue/src/assets/style.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ module.exports = function svgLoader (options = {}) {
}).data
}

// To prevent compileTemplate from removing the style tag
svg = svg.replace(/<style/g, '<component is="style"').replace(/<\/style/g, '</component')

const { code } = compileTemplate({
id: JSON.stringify(id),
source: svg,
Expand Down