Hello!
I have a few inquiries. Is it feasible to obtain the file path of the file utilizing an alias for import in Vite (Vue3)?
Setup
Here's the directory structure I'm using, purely for illustrative purposes:
src/
module_a/
some_script.js
another_script.js
module_b/
some_script.js
another_script.js
vite.config.js
Situations
For instance, if I am in this particular file:
When importing as follows:
src/module_a/some_script.js
import "#/another_script.js"
...then, the #
alias should automatically refer to ./src/module_a
Suppose when I'm in another module location ./src/module_b/some_script.js
.
The code snippet reads:
import '#/another_script.js'
...it should import ./src/module_b/another_script.js
accordingly.
Main Inquiry
Can this be achieved within the vite.config.js
file?
vite.config.js
...
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
'#': (...args) => {
console.log(args); // outputs [#, 0, '@/path/of/script/to/import']
// how to get this information below??? =(
const filepathOfImportingScript =
getImportingFilepath() // <--- should return ./src/module_a/some_script.js
const pathOfModule =
getPathOfModule(filepathOfImportingScript); // returns ./src/module_a/
return path.resolve(__dirname, pathOfModule);
}
}
})
My primary challenges include...
How do I implement the
getImportingFilepath()
function above which retrieves the filepath of the script utilizing the alias#
for import?Developing the
getPathOfModule()
function is more straightforward. However, implementinggetImportingFilepath()
poses a challenge.
I hope someone can provide insights, thank you!