Galooshi / import-js

A tool to simplify importing JS modules
MIT License
526 stars 70 forks source link

Trouble working with webpack aliases #533

Closed mattkime closed 7 months ago

mattkime commented 5 years ago

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 commented 5 years ago

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

mattkime commented 5 years ago

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

iahu commented 5 years ago

need support webpack aliases +1

mikabytes commented 7 months ago

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!