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

后端 未结 4 705
再見小時候
再見小時候 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 10:20

    You can use require.ensure for bundle splitting. For example

    if (typeof window !== 'undefined') {
        console.log("Loading browser XMLHttpRequest");
    
        require.ensure([], function(require){
    
            // require.ensure creates a bundle split-point
            require('./adapters/xhr');
        });
    } else if (typeof process !== 'undefined') {
        console.log("Loading node http");
    
        require.ensure([], function(require){
    
            // require.ensure creates a bundle split-point
            require('./adapters/http');
        });
    }
    

    See code splitting for more information and a sample demo usage here

提交回复
热议问题