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 this possible with webpack AliasPlugin to automatically switch to a different alias when original file is deleted? #375

Closed
latel opened this issue Mar 30, 2023 · 7 comments

Comments

@latel
Copy link

latel commented Mar 30, 2023

is it possible, if both folder src/servers/overrides/server and Proto/ProtoCPP/HEAD/Proto/server have same file ABC.ts, when I delete src/servers/overrides/server/ABC.ts, webpack automatically use Proto/ProtoCPP/HEAD/Proto/server/ABC.ts

configureWebpack: {
        resolve: {
            plugins: [
                // @see https://github.com/webpack/webpack/issues/6817#issuecomment-542448438
                new AliasPlugin(
                    'described-resolve',
                    [
                        {
                            name: '%',
                            alias: [resolve('src/servers/overrides'), resolve('Proto/ProtoCPP/HEAD/Proto')],
                        },
                    ],
                    'resolve'
                ),
            ],
        },
    },

currently when I delete src/servers/overrides/server/ABC.ts, webpack throws ENOENT errors like this
image

@alexander-akait
Copy link
Member

alexander-akait commented Mar 30, 2023

I think you are looking for https://webpack.js.org/configuration/resolve/#resolveroots

@latel
Copy link
Author

latel commented Mar 31, 2023

I think you are looking for https://webpack.js.org/configuration/resolve/#resolveroots

It's not, the main purpose to use alias is to remove relative paths, reolsve.roots cannot solve this kind of problem.
I try to make my scenario more clearer.

I've folders structure below

/path/to/proj/src/servers/overrides/server
/path/to/proj/Proto/server

I've ABC.ts in both folders

/path/to/proj/src/servers/overrides/server/ABC.ts
/path/to/proj/Proto/server/ABC.ts

I've webpack configs

...
const resolve = dir => path.join(__dirname, dir)
...
configureWebpack: {
        resolve: {
            plugins: [
                // @see https://github.com/webpack/webpack/issues/6817#issuecomment-542448438
                new AliasPlugin(
                    'described-resolve',
                    [
                        {
                            name: '%',
                            alias: [resolve('src/servers/overrides'), resolve('Proto')],
                        },
                    ],
                    'resolve'
                ),
            ],
        },
    }
}
...

I've codes

import ABC from '%/server/ABC';
ABC();

now, when I delete /path/to/proj/src/servers/overrides/server/ABC.ts, webpack will not re-locate ABC to Proto/server/ABC.ts but throw errors, says /path/to/proj/src/servers/overrides/server/ABC.ts not exists.

Actually what I want is

  1. when I delete /path/to/proj/src/servers/overrides/server/ABC.ts, webpack will re-locate ABC to Proto/server/ABC.ts
  2. when I added /path/to/proj/src/servers/overrides/server/ABC.ts, webpack will re-locate ABC from Proto/server/ABC.ts to /path/to/proj/src/servers/overrides/server/ABC.ts

@alexander-akait
Copy link
Member

Because webpack can't find your alternative path, please use roots to provide multiple places to resolve, you don't need AliasPlugin, sorry I don't undestand the problem

@latel
Copy link
Author

latel commented Apr 11, 2023

Actually, chatGPT understands loll, words from chatGPT:

Unfortunately, this is not possible with webpack's default behavior. When resolving aliases, webpack will always prioritize the first matching alias it finds in the list, and will not automatically switch to a different alias if the original file is deleted.
One possible workaround is to use a custom plugin that listens for file changes and updates the alias configuration accordingly. However, this would require significant development effort and may not be practical for your use case.
Alternatively, you could consider restructuring your code to avoid duplicate file names and aliases, or use a different tool that supports more advanced file resolution strategies.

@latel latel changed the title is this possible with AliasPlugin to dynamically locate files again when file is deleted? is this possible with AliasPlugin to dynamically locate files again when original file is deleted? Apr 11, 2023
@latel latel changed the title is this possible with AliasPlugin to dynamically locate files again when original file is deleted? is this possible with webpack AliasPlugin to automatically switch to a different alias when original file is deleted? Apr 11, 2023
@alexander-akait
Copy link
Member

@latel Yeah, it is impossible, you can try roots + alias and it should work, but using only AliasPlugin

@zecka
Copy link

zecka commented Aug 23, 2023

@latel I think my issue #395 is exactly the same than yours.
Did you find a solution ?

@alexander-akait
Copy link
Member

Let's close in favor #250, anyway PR welcome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants