How can I exclude code path when bundling with webpack/browserify?

后端 未结 4 704
再見小時候
再見小時候 2021-02-12 09:27

I have a library that can be used with both node.js and the browser. I am using CommonJS then publishing for the web version using webpack. My code looks like this:



        
4条回答
  •  鱼传尺愫
    2021-02-12 09:57

    This worked best for me

    var _process;
    
    try {
        _process = eval("process");  // avoid browserify shim
    } catch (e) {}
    
    var isNode = typeof _process==="object" && _process.toString()==="[object process]";
    

    as Node will return true and not only will the browser return false, but browserify will not include its process shim when compiling your code. As a result, your browserified code will be smaller.

    Edit: I wrote a package to handle this more cleanly: broquire

提交回复
热议问题