Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Unable to import module as directory #37

Closed
soanvig opened this issue Jan 13, 2018 · 7 comments
Closed

Unable to import module as directory #37

soanvig opened this issue Jan 13, 2018 · 7 comments

Comments

@soanvig
Copy link

soanvig commented Jan 13, 2018

I cannot load modules as import x from '@/directory' (where directory contains index.js).

My config:

alias({
    '@': path.join(__dirname, './src')
})

What doesn't work
import x from '@/directory'

What works
import x from './directory'
import x from '@/directory/index.js'

@lduoduo
Copy link

lduoduo commented Jan 14, 2018

same issue as this, please take a look in, thanks!

@soanvig
Copy link
Author

soanvig commented Jan 20, 2018

I did quick look into the code and it seems to be working only with files. This may be greater modification, than I thought. Unfortunately I have no idea of rollup and rollup plugin system.

I am wondering, if behaviour:

if (isDirectory(path)) {
   path + '/index.js';
}

would do the trick. I am going to experiment a little. But - for me at least - it looks like hackish solution.

@soanvig
Copy link
Author

soanvig commented Jan 20, 2018

@lduoduo you may be interested in my PR.

@frostney
Copy link
Contributor

frostney commented May 2, 2018

Thank you for writing up this issue, we had a similar pull request some time ago: #25

The responsibility of this plugin is to only handle aliases, not to handle Node's resolution mechanism. If you would like to use that functionality, please use rollup-plugin-node-resolve in combination with this plugin.

Please feel free to re-open if you think this is not resolved.

@frostney frostney closed this as completed May 2, 2018
@soanvig
Copy link
Author

soanvig commented May 2, 2018

Can you provide us with working example? Because AFAIR it's impossible to combine these two modules, to achieve appropriate effect.
I strongly agree with your statement of plugin's responsibility though.

@dudiq
Copy link

dudiq commented Jan 22, 2019

as example:

alias({
     '@': path.join(__dirname, './src'),
     resolve: ['.js', '/index.js'],
   }),

@wickkidd
Copy link

Hack

I got it working for the /index.js case by adding the following after this line https://github.com/rollup/rollup-plugin-alias/blob/master/src/index.js#L80

else if (fs.existsSync(path.resolve(filePath, 'index.js'))) {
          updatedId = filePath + '/index.js'
        }

Before you get too excited, this only handles that case, so if your needs are more complex (index.ts for example) this won't be enough.

Thoughts

After reading the issues here, on rollup-plugin-node-resolve, and rollup it seems that we're basically in a stalemate. Both plugin authors assert that their plugin isn't responsible for the duties (even partially so) of the other and I agree with them. Doing so would introduce more complexity and hence more opportunity for bugs–not to mention a higher maintenance cost. The root cause seems to be due to rollup not passing the module id to other plugins once it is resolved.

While I think that rollup having some mechanism for passing the module id to other plugins is likely the better way to go, in the meantime people who use this for their projects have to get things done and so hacks like mine above wind up being the (hopefully temporary) compromise.

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

No branches or pull requests

5 participants