jlengstorf / learn-rollup

This is an example project to accompany a tutorial on using Rollup.
https://code.lengstorf.com/learn-rollup-js/
ISC License
191 stars 61 forks source link

Question about excluding node_modules #23

Closed helenb closed 7 years ago

helenb commented 7 years ago

I've found your video and associated code examples really useful indeed.

I've got a question on why you need to exclude the 'node_modules' folder when you've explicitly set your entry point as 'src/scripts/main.js' - surely that then means it wouldn't be trying to compile node modules as these are not on that path?

jlengstorf commented 7 years ago

In the tutorial config, there are two places where we exclude the node_modules directory:

  1. The babel plugin — we do this to prevent Babel from attempting to transpile external code, which would most likely cause problems.
  2. The replace plugin — we do this to prevent altering external code by replacing strings, which is a recipe for chaos. :)

I'm going to close this, but feel free to follow up with any additional questions.

Thanks!

helenb commented 7 years ago

Thanks - I think I wasn't quite clear in my question. Why would babel try to compile stuff in the node_modules directory when we're telling it to look only at 'src/scripts/main.js as a starting point:

entry: 'src/scripts/main.js',

I expected that this meant the rollup babel plugin would only be compiling stuff referenced in that main.js file - and therefore wouldn't be looking at node_modues which are not anywhere on that path.

I'm guessing I must misunderstand what that entry point means though, as I can see from the example with debug that it can end up compiling the content in node_modules.

jlengstorf commented 7 years ago

Oh, I see what you're asking.

So, the way Rollup works — as well as other bundlers — is to combine all referenced code into a single file. So in the case of this tutorial, we include debug, which is a Node module. As it's pulled through Rollup, the plugins execute on all the included code unless we explicitly ignore it.

Because of this, if we don't ignore node_modules and other paths where third-party code is stored, Rollup will try to process code that we should assume already went through processing, and that causes problems. By ignoring it, we process only the code we wrote, and not the code we're importing to make our lives easier.

Does that make sense?

helenb commented 7 years ago

Yes, thank you!