As of vite@2.1.5
I could solve resolution of @
and ~
aliases in the following way:
add vite-aliases
modify vite.config.js
as described in the readme.md
add an alias for the referenced scss file using the ~
alias as follows:
aliases.push({ find: '~bootstrap', replacement: 'bootstrap' })
now vite.config.js
looks as follows:
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { getAliases } from 'vite-aliases'
const aliases = getAliases();
// add aliases to import scss from node_modules here
aliases.push({ find: '~bootstrap', replacement: 'bootstrap' })
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
resolve: {
alias: aliases
},
css: {
preprocessorOptions: {
scss: {
additionalData: '@import "@scss/shared.scss";'
}
}
}
})
this allowed me to import scss files as follows:
in src/main.ts
:
import '@scss/main.scss'
in src/scss/main.scss
:
@import "~bootstrap/scss/bootstrap";
Obviously, if you need to import more scss files from a subdirectory of node_modules
, sou need to add some more aliases to aliases
. Leaving out the tilde alias for importing bootstrap.scss
worked, but it would not be recognized by my IDE. Adding the css.preprocessorOptions.scss.additionalData
causes the shared.scss
to be imported in each vue SFC.