#same imports into every file

1 messages · Page 1 of 1 (latest)

floral sky
#

I'm trying to fix having to re-write my imports each time I move my files around.
I'm using Svelte.
In each .svelte file I import the same thing - a javascript lib I use for state.
And now whenever I move things around I have to go re-write the import state from '../../../state.js
Is it possible to just say 'please import this file as state into every .svelte file?

Also, as a side note, I'd love to just import every .svelte file into every .svelte file!
Or at least every file in a sub-directory, so again I don't have to keep re-writing the import paths!
Or writing them at all!

Thanks for any help in advance

north igloo
#

You can use resolve.alias setting in the Vite config to write absolute paths and define an alias. For example, this would let @/state.js resolve to src/state.js. Or any other @/random/file/from/src.js resolve relative to the src folder

resolve: {
  alias: {
    '@': path.resolve(__dirname, './src'),
  },
}
floral sky
#

Hey Matt. Thank you for the reply. Would this resolve to different paths depending on which file it was used in?

#

I've just tried it out - it works brilliantly! Thanks so much