Rollup: CLI: Allow Config and package.json to be in separate directories for module resolution

Created on 12 Oct 2018  ·  3Comments  ·  Source: rollup/rollup

Feature Use Case

I am building a collection of react components I want to be able to be self contained and used in one larger project, and self contained "Example" pages. For example, image with following directory structure

-- components/
-- -- rollup.config.js
-- -- myComponent/
-- -- -- myComponent.js
-- -- -- package.json
-- -- -- ...other files here..
-- package.json
-- ...other files..

So lets say I use the following inside of the myProject/components/myComponent: rollup -c ../rollupconfig.js -i myComponent.js --file build/myComponent.js.

What will happen is that it will use the base project package.json for resolving plugin imports and things myProject/package.json, instead of the component package.json myProject/components/myComponent/package.json. I feel like it should be resolving installed modules in the directory the CLI is run, not relative to the passed config.

Feature Proposal

Can we add something like --cwd where it will use a directory to where CLI plugin module resolution happens? Or something like that.

In the meantime, I've just added a myProject/components/myComponent/rollup.config.js that simple re-exports the rollup config from myProject/components/rollup.config.js.

t³ ✨ enhancement

Most helpful comment

Hey folks. This is a saved-form message, but rest assured we mean every word. The Rollup team is attempting to clean up the Issues backlog in the hopes that the active and still-needed, still-relevant issues bubble up to the surface. With that, we're closing issues that have been open for an eon or two, and have gone stale like pirate hard-tack without activity.

We really appreciate the folks have taken the time to open and comment on this issue. Please don't confuse this closure with us not caring or dismissing your issue, feature request, discussion, or report. The issue will still be here, just in a closed state. If the issue pertains to a bug, please re-test for the bug on the latest version of Rollup and if present, please tag @shellscape and request a re-open, and we'll be happy to oblige.

All 3 comments

Actually, rollup CLI uses the current working directory to resolve files, not the location of some package.json. package.json scripts, on the other hand, have their cwd set to their parent dir (at least to my knowledge).

Nevertheless I see that it would make sense to resolve everything relative to the config file and not the cwd. But this would definitely be a breaking change. Not 100% sure about this.

To achieve cwd-independence, you can write your config like this:

import path from 'path'

export default ({
  input: path.join(__dirname, 'src/main'),
  output: [{
    file: path.join(__dirname, 'dist/bundle.js'),
    format: 'esm'
  }]
});

Hello!

Thanks for taking a look at this! So yes, I agree that that solution would work when resolving where the input and the output should go, but what I'm referring to is actually (in this example): import path from 'path'.

Let's say in my original example, the child package.json has the 'path' dependency, and the parent package.json does not. In this case, when using rollup -c ../rollup.config.json, it will check the parent package.json, and not the child package.json of the cwd that the rollup CLI is run. Therefore, import path from 'path' will not work, because it will think path is not installed.

Hope this helps!

P.S I know it's actually checking the node_modules directory, but I put package.json to hopefully better show how they are nested projects? I may just be making it more confusing haha!

Hey folks. This is a saved-form message, but rest assured we mean every word. The Rollup team is attempting to clean up the Issues backlog in the hopes that the active and still-needed, still-relevant issues bubble up to the surface. With that, we're closing issues that have been open for an eon or two, and have gone stale like pirate hard-tack without activity.

We really appreciate the folks have taken the time to open and comment on this issue. Please don't confuse this closure with us not caring or dismissing your issue, feature request, discussion, or report. The issue will still be here, just in a closed state. If the issue pertains to a bug, please re-test for the bug on the latest version of Rollup and if present, please tag @shellscape and request a re-open, and we'll be happy to oblige.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

adammockor picture adammockor  ·  3Comments

Announcement picture Announcement  ·  3Comments

nanomosfet picture nanomosfet  ·  3Comments

Havunen picture Havunen  ·  3Comments

FruitieX picture FruitieX  ·  3Comments