rollup/rollup-plugin-node-resolve

node modules imports converted to relative paths

FezVrasta opened this issue · 5 comments

Hi, I'm having a problem with Rollup and this plugin.

If I have an import of this kind:

import _ from 'lodash';

it gets converted to:

import _ from '../node_modules/lodash/lib/index.js';

This breaks the bundle when I try to use it inside a project which uses Webpack because the node_modules folder is not child of the folder of my Rollup-bundled package obviously (because npm flats the dependencies).

I am already defining all the node_modules as external.

This is my relevant config:

{
  entry: 'dist/components-index.js',
  external: id => id.indexOf('node_modules') >= 0,
  plugins: [
    resolve({
      extensions: ['.jsx', '.js', '.json'],
    }),
    commonjs({
      namedExports: { '../xxx-styles/lib/index.js': ['common', 'dark', 'light' ] },
    }),
  ],
}

How can I make the build keep the node modules import preserve the absolute paths? I'd like to keep them as I write, so that node.js/webpack/wathever can resolve the imports properly.

Thank you.

I tried with rollup-plugin-local-resolve and I got the expected result (even if it doesn't support multiple extensions and I had to patch it).

Is this plugin simply unsuitable for my needs or there's something wrong?

yeah I find it odd that this isn't a straightforward thing to accomplish. The jail config makes it even more confusing. Reading the docs for it made me think that was what I was missing which it wasn't

Actually if you want to look for node_modules and mark them as external that is not the correct way, and in fact most likely rollup is warning you! You should get;

'foo' is imported by /libs/bar but could not be resolved – treating it as an external dependency

Looking into rollup code: https://github.com/rollup/rollup/blob/4f5f8483b9cf6f1d47b27c8b50b116663bc72e5f/src/Bundle.js#L73

this is what is happening, when you have an import to an external module example

import { foo } from "bar";

At the time the external method is called rollup isn't aware of the path yet. Thus the id of the module would be still bar rather than node_modules/bar so this will be marked as non-external and all plugins will be called including rollup-plugin-node-resolve which rightly so will resolve the path to ../node_modules/bar

Thus in the case you want to mark all node_modules as external I'd suggest you use something like;

external: id => !(path.isAbsolute(id) || id.startsWith("."));

Basically saying that all paths that don't have with a . are non-relative paths and treat them as external. Like this non-of the plugin will be invoked for the real externals and thus the paths will not be resolved.

Can be closed now. Current output: import _ from 'lodash';

Hello All,
I have used rollup and i have imported custom file (utils,constant, etc.)using absolute path. When i compiled using rollup -c. absolute path not resolve and it compiled like var abc=requires('services/abc/endpoint').

import abc from '../service/abc/endpoint'; -- Works
import abc from 'service/abc/endpoint'; -- Does not Works

Any one can help ?