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

Regex in external doesn't exclude local import #3811

Closed
Naeoth opened this issue Oct 7, 2020 · 2 comments · Fixed by RocketChat/EmbeddedChat#406
Closed

Regex in external doesn't exclude local import #3811

Naeoth opened this issue Oct 7, 2020 · 2 comments · Fixed by RocketChat/EmbeddedChat#406

Comments

@Naeoth
Copy link

Naeoth commented Oct 7, 2020

  • Rollup Version: 2.28.2
  • Operating System (or Browser): MacOS (10.15.7)
  • Node Version (if applicable): 14.8.0
  • Link to reproduction (IMPORTANT, read below): https://github.com/Naeoth/rollup-issue

To avoid unresolved dependencies warning on compilation, I use a regex (/^[^.].+/) for the external rollup configuration to exclude all the imports which starts with "." (it means all the local import in fact).
It worked perfectly before 2.26.8 but a change in 2.26.8 broke the trick.
It seems that now, it's impossible to exclude the local import with a regex if you have a least a regex with /.+/ or /.*/.

Expected Behavior

Include the import like "./..." in the bundle with the regex in the external.

Actual Behavior

Treat the import like "./..." as external module with the regex in the external.

@lukastaegert
Copy link
Member

While I understand your pain, the previous behaviour was both inconsistent and contradicted what was written in the documentation. But I think there is still a way to achieve what you want.

So what happens is that when you import e.g. ./foo.js, first the string "./foo.js" is checked against the regular expression. Then it is resolved to a full path, e.g. "/Users/myself/project/foo.js", and checked against the regular expression again.

So what you need to do is extend your regular expression to also exclude absolute paths. On non-Windows systems, this should be as easy as

/^[^./].+/

while on Windows, you need something more involved. Another variant would be to use a function:

import path from 'path';

// ...

external(id) {
  return id[0] !== '.' && !path.isAbsolute(id);
}

Hope this helps.

@Naeoth
Copy link
Author

Naeoth commented Oct 7, 2020

It works like a charm thank you so much ! I'm using the function variant to be compatible with all environments.

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