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

Is it possible to to get the filepath of the file that uses an alias to import in Vite (Vue3)? #10607

Closed
4 tasks done
lvjhn opened this issue Oct 23, 2022 · 3 comments
Closed
4 tasks done

Comments

@lvjhn
Copy link

lvjhn commented Oct 23, 2022

Description

Good day!

I have a couple of questions. Is it possible to to get the filepath of the file that uses an
alias to import in Vite (Vue3) ?

Set-up

I have this directory structure, for rhetorical purposes only:

src/ 
    module_a/ 
        some_script.js 
        another_script.js 
    module_b/ 
        some_script.js 
        another_script.js 
vite.config.js 

Scenarios

For example, say I'm in this file:

If I import using:
src/module_a/some_script.js

import "#/another_script.js" 

... then, the # alias should automatically point to ./src/module_a

Say when I'm in another module location ./src/module_b/some_script.js.

The statement:

import '#/another_script.js' 

... should import ./src/module_b/another_script.js accordingly.

Main Question

Is it possible to do this in vite.config.js?

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 thing 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 main problems are...

  • How do I create the getImportingFilepath() function above which returns the filepath of the script
    that uses the alias # to import?

  • The function getPathOfModule() is easier for me. The import filepath just have to be parsed to identify the module directory. However, the getImportingFilepath() is needed for this..

I hope somebody knows, thanks a lot!

Suggested solution

Why not add additional argument in resolver function?
Is this possible? =D

  export default defineConfig({
        plugins: [vue()],
        resolve: {
            alias: {
            '@': path.resolve(__dirname, './src'),
            '#': (alias, someInt, scriptToImport, scriptThatImports) => {

                // Is this possible??? =D 
                console.log(args);    // example output:  [#, 0, ''@/path/of/script/to/import', '@/path/of/script/that/is/importing']
                
                // how to get this thing below??? =(
                const filepathOfImportingScript = 
                    scriptThatImports  // <--- example output:  full/path/to/src/module_a/some_script.js
                
                const pathOfModule = 
                    getPathOfModule(filepathOfImportingScript);  // example return: ./src/module_a/

                return path.resolve(__dirname, pathOfModule);
            }
        }
    })

Adding this feature might open up a lot of possibilities in module relative imports.

Alternative

No response

Additional context

No response

Validations

@sapphi-red
Copy link
Member

You could use customResolver as documented. (stackblitz)
https://vitejs.dev/config/shared-options.html#resolve-alias

@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Oct 23, 2022
@lvjhn
Copy link
Author

lvjhn commented Oct 23, 2022

Thanks a lot! I'll check that out =D.

How do I know the filepath of the script that imports? It's my main question.

When I use the custom resolver function and try to access the arguments using ..args, it only gives off something like ["@", 0, "@/path/to/import"]. However, I need the file that does the importing. It's not available as an arguments/parameter to the resolver function.

@lvjhn
Copy link
Author

lvjhn commented Oct 23, 2022

Oh... I overlooked the StackBlitz link. Thank you so much for this! Finally found it

More love and power to vite.

@github-actions github-actions bot locked and limited conversation to collaborators Nov 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants