Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from floorish/custom-default
Feature: Add default import config option
- Loading branch information
Showing
6 changed files
with
81 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<script setup> | ||
// this component can only be used when defaultImport === 'url' | ||
import { defineAsyncComponent } from 'vue' | ||
import HelloWorld from './components/HelloWorld.vue' | ||
import Test from './assets/test.svg?component' | ||
import testUrl from './assets/test.svg?url' | ||
import testRaw from './assets/test.svg?raw' | ||
// variables are not supported in dynamic imports | ||
// because https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars | ||
// does not support query params | ||
const Async = defineAsyncComponent(() => import(`./assets/circle.svg?component`)) | ||
</script> | ||
|
||
<template> | ||
<div id="component"> | ||
<Test class="test-svg" data-animal="bird" aria-hidden="true" /> | ||
</div> | ||
|
||
<div id="image"> | ||
<img src="./assets/test.svg"> | ||
</div> | ||
|
||
<div id="async"> | ||
<Async /> | ||
</div> | ||
|
||
<div id="url"> | ||
{{ testUrl }} | ||
</div> | ||
|
||
<div id="raw"> | ||
{{ testRaw }} | ||
</div> | ||
|
||
<HelloWorld msg="Hello Vue 3 + Vite" /> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { createApp } from 'vue' | ||
import App from './App.vue' | ||
import App from 'App.vue' // note: this is dynamically resolved in vite.config.js | ||
|
||
createApp(App).mount('#app') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,25 @@ | ||
import { defineConfig } from 'vite' | ||
import { defineConfig, loadEnv } from 'vite' | ||
import vue from '@vitejs/plugin-vue' | ||
import viteSvgLoader from '../../' | ||
|
||
// https://vitejs.dev/config/ | ||
export default defineConfig({ | ||
plugins: [vue(), viteSvgLoader()] | ||
export default defineConfig(({ mode }) => { | ||
const env = { ...process.env, ...loadEnv(mode, process.cwd()) } | ||
// define in .env: | ||
// VITE_SVG_DEFAULT_IMPORT=url | ||
// or as npm param: | ||
// npm --svg_default_import=url run build | ||
const DEFAULT_IMPORT = env.VITE_SVG_DEFAULT_IMPORT || env.npm_config_svg_default_import | ||
|
||
return { | ||
plugins: [vue(), viteSvgLoader({ defaultImport: DEFAULT_IMPORT })], | ||
|
||
resolve: { | ||
alias: { | ||
// dynamically change App component based on the default import | ||
'App.vue': DEFAULT_IMPORT === 'url' ? './App-url.vue' : './App.vue' | ||
} | ||
} | ||
|
||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters