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

Adding an "only" option #83

Merged
merged 2 commits into from Mar 17, 2018
Merged

Adding an "only" option #83

merged 2 commits into from Mar 17, 2018

Conversation

arantes555
Copy link
Contributor

Hello,
However, for my project, I needed the ability to limit the resolution of node_modules to only modules matching a pattern, so I added this option. The implementation is very straightforward, using String.prototype.match.

Using it is very simple. It works with both strings and regexes:

{
  entry: 'samples/only/main.js',
  plugins: [
    nodeResolve({
      only: [ 'test' ]
    })
  ]
}
import test from 'test' // will be bundled
import test2 from 'test2' // /!\ will be bundled
import mytest from 'mytest' // will NOT be bundled
{
  entry: 'samples/only/main.js',
  plugins: [
    nodeResolve({
      only: [ /@scoped\/.*/ ]
    })
  ]
}
import test from 'test' // will NOT bundled
import mytest from '@scoped/mytest' // will be bundled
import mytest2 from '@otherscope/mytest' // will NOT be bundled

@keithamus
Copy link
Contributor

Hey @arantes555 - this looks like a good PR. My question is, does jail not help you here?

Also this PR has some conflicts - so if you think we should really add this it would be great to get it rebased 😃

@keithamus keithamus self-requested a review March 7, 2018 10:36
@arantes555
Copy link
Contributor Author

Hi @keithamus ! Thanks for checking this out 😄

Nope, the jail option does not cut it for me. For example, if I want to rollup a node module called foo and only it, if I set the jail option to ./node_modules/foo, it is also going to rollup what is under ./node_modules/foo/node_modules, which I do not want (and what's actually in there depends on how npm decides to dedupe packages...). Maybe I missed something and jail is actually enough for this usecase?

Indeed, there are some merge conflicts, which is to be expected as the PR is more than a year old 😜 .
I'll get around to rebasing it sometime next week! Thanks :)

@arantes555 arantes555 force-pushed the master branch 2 times, most recently from b6dcc5d to 4387b8a Compare March 13, 2018 23:44
@arantes555
Copy link
Contributor Author

@keithamus : branch has been rebased, and some cleanup has been done :) I believe it's good to merge on my end!

Copy link
Contributor

@keithamus keithamus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im happy with this - but I'll ask @lukastaegert for a second 👍 before we go ahead and merge.

Copy link
Member

@lukastaegert lukastaegert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be some overlap with rollup's own "externals" function but I still see some use-cases for this feature. One minor performance suggestion, otherwise 👍

src/index.js Outdated
@@ -99,6 +100,8 @@ export default function nodeResolve ( options = {} ) {
id = resolve( importer, '..', importee );
}

if (only && !only.some(pattern => id.match(pattern))) return null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For performance reasons it might also make sense to initially convert all entries to RegExp (in L.47 where it will only happen once) and then use pattern.test(id) instead (which will make all invocations faster, cf. e.g. https://stackoverflow.com/a/10940138)

@arantes555
Copy link
Contributor Author

@lukastaegert : I just pushed a minor edit with your suggestion !

Copy link
Member

@lukastaegert lukastaegert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@keithamus keithamus merged commit 3927778 into rollup:master Mar 17, 2018
@keithamus
Copy link
Contributor

3.3.0 😄

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

Successfully merging this pull request may close these issues.

None yet

3 participants