How “include” and “exclude” works in webpack loader

后端 未结 2 1624
离开以前
离开以前 2020-12-01 06:10

Update&Answer:

My misunderstand was:

All the imported/required files will be transformed by loader.

However, some

相关标签:
2条回答
  • 2020-12-01 06:52

    The problem is that without that exclude (or an include) webpack would traverse through the dependencies when you point to them at your code and process them. Even though that could work, it would come with a heavy performance penalty.

    I prefer to set up an include myself (whitelist over blacklist) as that gives me more control over the behavior. I include my application directory and then add more items to the include based on the need. This allows me to make exceptions easily and process bits from node_modules if absolutely needed.

    0 讨论(0)
  • 2020-12-01 06:56

    The answers so far haven't really answered your core question. You want to know how your bundled app still manages to function even though its dependencies have been 'excluded'.

    Actually, those 'include' and 'exclude' properties are telling the loaders whether to include/exclude the files described (such as the contents of node_modules), not webpack itself.

    So the 'excluded' modules you import from node_modules will be bundled - but they won't be transformed by babel. This is usually the desired behaviour: most libraries are already transpiled down to ES 5.1 (or even ES 3), and so wasting CPU cycles parsing them with babel (for instance) is pointless at best. At worst, as in the case of large single-file libraries like jQuery, it can throw an error and bring your build to a crashing halt. So we exclude node_modules.

    0 讨论(0)
提交回复
热议问题