Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[@rollup/plugin-node-resolve] Extend resolveOnly property to allow for denylists / miscellaneous use cases. #1150

Closed
TotalTechGeek opened this issue Mar 26, 2022 · 3 comments

Comments

@TotalTechGeek
Copy link
Contributor

TotalTechGeek commented Mar 26, 2022

  • Rollup Plugin Name: node-resolve
  • Rollup Plugin Version: 13.1.3

Feature Use Case

Currently, it appears that the resolveOnly property allows you to pass in an array of strings or regular expressions to validate if a plugin should be bundled in.

I had a use case where I wanted to bundle in everything except for certain dependencies. (node-rdkafka, sqlite3).

I would like to extend the resolveOnly property to allow developers more control.

This feature may already exist under another property, and if so, I apologize for the unnecessary recommendation.

Feature Proposal

While it is semi-possible to work around this by creating a complex regular expression to attempt to match everything except the given string, there are a few ways to introduce this.

Suggestion 1
I believe the best way to introduce this is to also allow resolveOnly to be a (plugin: string) => boolean function, thus allowing a developer to write something like:

function forbid (...plugins) {
    const set = new Set(plugins)
    return plugin => !set.has(plugins)
}

nodeResolve({
    resolveOnly: forbid('sqlite3', 'node-rdkafka')
})

By allowing the user to provide their own function, it gives them complete control to customize however they see fit.

Suggestion 2
While I believe suggestion 1 should be simple enough to implement, if you wanted to change just literally 1 type / 1 line of code, all you'd need to do is change out RegExp for { test: (plugin: string) => boolean }. I do not like this as much :)

function forbid (...plugins) {
    const set = new Set(plugins)
    return [{ test: plugin => !set.has(plugins) }]
}

nodeResolve({
    resolveOnly: forbid('sqlite3', 'node-rdkafka')
})

My current workaround

In order to work around this, I ended up doing the following:

function forbid (...plugins) {
    const regex = new RegExp('');
    const set = new Set(plugins)
    regex.test = (pattern) => !set.has(pattern)
    return [regex]
}

While this works for now, I know it's a hack and could break at any minute :P

If someone likes the idea, I'm 100% willing to file the MR!

@shellscape
Copy link
Collaborator

tagging a few folks that might have some input @guybedford @tjenkinson @lukastaegert

@tjenkinson
Copy link
Member

Supporting a function too that returns a boolean sounds good to me

shellscape pushed a commit that referenced this issue Apr 11, 2022
…ly (#1152)

* feat(node-resolve): Add the ability to pass a function into resolveOnly to resolve issue #1150

* Add a missing piece of text to the readme.
@TotalTechGeek
Copy link
Contributor Author

Closing the issue as the pull request has been merged :)

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

No branches or pull requests

3 participants