ember-auto-import icon indicating copy to clipboard operation
ember-auto-import copied to clipboard

[question] can not import files nested in a cjs module

Open gabrielcsapo opened this issue 5 years ago • 7 comments

I have a module that looks like

the module name is moo

--> index.js (this requires dist/cjs/index.js)
--> dist
  --> cjs
     --> index.js
     --> foo/bar.js

I can not do the following

import Bar from 'moo/foo/bar';

Is this expected?

gabrielcsapo avatar Oct 08 '20 19:10 gabrielcsapo

Yes, this is how all node package resolution works. If you want to be able to import from moo/foo/bar, you really need to name it ./foo/bar.js inside the moo package. Even if you customize main in package.json, that only controls what people get when they import from moo, not what they get when they import moo/foo/bar.

ef4 avatar Oct 08 '20 19:10 ef4

Is there a way to tell ember-auto-import to only import dist/cjs to do this without customizing the package being imported? I tried to use alias but that did not yield the right results.

gabrielcsapo avatar Oct 08 '20 19:10 gabrielcsapo

You can do this with alias but you'll need to do it for each module you are aliasing.

{
  autoImport: {
    alias: {
      "moo": "moo/dist/cjs/index.js",
      "moo/foo/bar": "moo/dist/cjs/foo/bar.js",
    }
  }
}

ef4 avatar Oct 08 '20 20:10 ef4

thank you @ef4!

gabrielcsapo avatar Oct 08 '20 20:10 gabrielcsapo

@ef4 sadly this hasn't worked...It might also be that I am trying to alias a node_module coming from a scoped package? Would this be supported?

gabrielcsapo avatar Oct 08 '20 21:10 gabrielcsapo

It should be supported. You're doing the aliasing in the consuming package, right? Not in moo itself.

ef4 avatar Oct 08 '20 21:10 ef4

yeah, I am doing it at the root level ember-cli-build instead of in the in repo addon that is using it. I will try rebuilding with this option set in the addon itself.

gabrielcsapo avatar Oct 08 '20 22:10 gabrielcsapo