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

Feature: Add default import config option #42

Merged
merged 4 commits into from Mar 30, 2022

Conversation

floorish
Copy link
Contributor

Added defaultImport config option, to change the import type when no explicit query param is defined. Can be 'url' | 'raw' | 'component' (default is component).


Testing with cypress
(the following is not necessary when using vite-svg-loader in projects, this is only used for testing vite-svg-loader itself)

vite.config.js dynamically inserts the defaultImport option from environment vars. Can be defined in .env:

VITE_SVG_DEFAULT_IMPORT=url

Or when calling the npm build script:

npm --svg_default_import=url run example:build

App.vue needed to be updated, because when defaultImport === 'url' the following will cause an error while rendering:

import Test from './assets/test.svg' // <--- this is now a url, not a component
<Test class="test-svg" data-animal="bird" aria-hidden="true" /> // <--- this fails to render

So that needs to be:

import Test from './assets/test.svg?component'
<Test class="test-svg" data-animal="bird" aria-hidden="true" />

Therefore vite.config.js also dynamically resolves App.vue to either './App.vue' or './App-url.vue' depending on the SVG_DEFAULT_IMPORT environment setting.

@jpkleemans
Copy link
Owner

Thanks for your work! I'm a little busy at the moment, but I'll look into it soon.

@ppfeiler
Copy link

ppfeiler commented Mar 1, 2022

When this gets merged, do we still need to specify ?component when importing an SVG with Typescript?

@floorish
Copy link
Contributor Author

floorish commented Mar 1, 2022

Yes, you still need to do that. You need to specify ?component anyway if you set the defaultImport to url. If you don't change the defaultImport setting nothing changes.

@jpkleemans jpkleemans merged commit f962b94 into jpkleemans:main Mar 30, 2022
@jpkleemans
Copy link
Owner

Thanks for your work

@floorish
Copy link
Contributor Author

Cheers, thanks for the 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.

None yet

3 participants