Different main entry point in package.json for node and browser

后端 未结 4 1866
我在风中等你
我在风中等你 2021-02-20 16:02

In isomorphic react app I have myModule which should behave differently on node and browser environments. I would like configure this split point i

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-20 16:21

    If you look at tools/webpack.config.js in React Starter Kit you will see that it exports two Webpack configurations that slightly differ, e.g. module.exports = [clientConfig, sererConfig]. The server-side bundle config has this field target set to node (by default it's web).

    https://webpack.github.io/docs/configuration.html#target

    The approach that you described works great for modules that have exactly the same API but different implementations, like in the case with HTTP client utility that uses XMLHttpRequest in its browser-specific implementation and Node's http module in its server implementation:

    https://github.com/kriasoft/react-starter-kit/tree/master/src/core/fetch

提交回复
热议问题