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

Trouble working with webpack aliases #533

Closed
mattkime opened this issue May 31, 2019 · 4 comments
Closed

Trouble working with webpack aliases #533

mattkime opened this issue May 31, 2019 · 4 comments

Comments

@mattkime
Copy link

I'm working with a project that has a number of webpack aliases defined. I'm unclear how to make importjs understand these aliases or even whether its possible. Relative imports seem to be working fine. If I manually translate a webpack alias into a relative path everything is resolved correctly. It seems like moduleNameFormatter should be able to help here but it doesn't appear to be executed - a console.log statement doesn't display anything. Perhaps its executed after a module path is resolved, not before.

Any advice on resolving webpack aliases?

@trotzig
Copy link
Collaborator

trotzig commented May 31, 2019

I suggest looking into using the aliases configuration option:
https://github.com/Galooshi/import-js/blob/master/README.md#aliases

@mattkime
Copy link
Author

mattkime commented Jun 1, 2019

I need to ui/* => src/legacy/ui/public/* - is that possible with aliases? If so, it doesn't appear to be documented.

@iahu
Copy link

iahu commented Jun 26, 2019

need support webpack aliases +1

@mikabytes
Copy link
Collaborator

I could achieve this using moduleNameFormatter. Here's what I did:

cd /tmp ; mkdir test ; cd test
mkdir -p src/legacy/ui/public
echo "export const b = 'hello'" > src/legacy/ui/public/b.js
echo "console.log(b)" > src/a.js
touch .importjs-root

.importjs.js:

module.exports = {
  importStatementFormatter({ importStatement }) {
    if (importStatement.includes("legacy/ui/public/")) {
      return importStatement.replace(/'.*legacy\/ui\/public/, "'ui");
    }
    return importStatement;
  },
};

And then importjs fix src/a.js

The result is:

import { b } from 'ui/b';

console.log(b);

As this is a quite old issue I'll close it now. But if there's any need for clarification, or if I missed some problem here, then please reopen it. Thanks!

This was referenced Feb 4, 2024
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

4 participants