简体   繁体   中英

How browserify makes Node.js modules to work in the browser?

Ii really can't understand some work behind browserify . When using pure JavaScript modules it seems "easy" to convert it to browser code, even if with many dependencies.

But browserify does more:

Many npm modules that don't do IO will just work after being browserified. Others take more work.

Many node built-in modules have been wrapped to work in the browser, but only when you explicitly require() or use their functionality.

When you require() any of these modules, you will get a browser-specific shim.

And again:

  • process.nextTick(), __dirname, and __filename node-isms work
  • get browser versions of the node core libraries events, stream, path, url, assert, buffer, util, querystring, http, vm, and crypto when you require() them

So... process.nextTick() , __dirname inside the browser... even http ? This doesn't make sense to me... how __dirname is supposed to work inside a browser?

process.nextTick() simply runs a callback later.
In a browser, this is simulated using setTimeout(func, 0) , setImmediate , or requestAnimationFrame() .

__dirnamne and __filename are simply variables that browserify injects at compile-time based on the original path to the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM