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

Prevent webpack from resolving modules called with require #373

Merged
merged 5 commits into from Jul 17, 2019

Conversation

southpolesteve
Copy link
Contributor

@southpolesteve southpolesteve commented Jul 17, 2019

This fixes 3.0.2 which is currently broken for webpack users

  1. In nodejs envs, we need to set a default agent with keepAlive: true. It results in a ~2x perf improvement
  2. The only way to do this with node API is to dynamically require http or https and construct the agent
  3. This doesn't matter for browser users, but we need to prevent them from loading the code and node builtin modules
  4. By default, webpack will see require and attempt to resolve the module
  5. We can trick webpack into not doing this with eval('require')

@southpolesteve southpolesteve changed the title Eval require calls to prevent webpack from loading modules Use global.require to prevent webpack from resolving modules Jul 17, 2019
@southpolesteve southpolesteve changed the title Use global.require to prevent webpack from resolving modules Prevent webpack from resolving modules called with require Jul 17, 2019
@southpolesteve
Copy link
Contributor Author

southpolesteve commented Jul 17, 2019

Looks like eval is the only way to do this webpack/webpack#8826

Tried:

  • module.require. Not the same as require
  • global.require. doesn't exist in node. some of the time 😜
  • typeof __non_webpack_require__ === "function" ? __non_webpack_require__ : require. Some how webpack is able to figure this trick out

package.json Outdated Show resolved Hide resolved
@southpolesteve
Copy link
Contributor Author

Final update: @bterlson suggested to use the package.json browser field to accomplish this and it works great

@christopheranderson
Copy link
Contributor

This either needs to get merged then added to the master PR, or this should target master once that PR closes.

@southpolesteve southpolesteve changed the base branch from v3 to master July 17, 2019 19:38
@southpolesteve southpolesteve merged commit 0964024 into master Jul 17, 2019
@southpolesteve southpolesteve deleted the eval-requires branch July 17, 2019 20:01
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

Successfully merging this pull request may close these issues.

None yet

3 participants