import-js icon indicating copy to clipboard operation
import-js copied to clipboard

Trouble working with webpack aliases

Open mattkime opened this issue 6 years ago • 3 comments

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?

mattkime avatar May 31 '19 15:05 mattkime

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

trotzig avatar May 31 '19 18:05 trotzig

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

mattkime avatar Jun 01 '19 01:06 mattkime

need support webpack aliases +1

iahu avatar Jun 26 '19 06:06 iahu

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!

mikabytes avatar Jan 29 '24 08:01 mikabytes