Webpack2, how to exclude react and react dom from

2019-07-11 02:39发布

问题:

My goal is to create a module for a react components library made in ES6 using webpack.

I am using:

  • webpack: "2.1.0-beta.25"
  • react: "15.4.1"

I need to assign react and react-dom as peerDependencies so it doesn't get downloaded by the developer.

This is the relevant part of my package.json:

"peerDependencies": {
  "react": "15.4.1"
},
"dependencies": {
  "chalk": "1.1.3",
  "compression": "1.6.2",
  "cross-env": "3.1.3",
  "immutable": "3.8.1",
  "invariant": "2.2.1",
  "lodash": "4.16.4",
  "minimist": "1.2.0",
  "sanitize.css": "4.1.0",
  "warning": "3.0.0",
  "react-onclickoutside": "5.8.3",
  "styled-components": "1.1.2",
  "classnames": "2.2.5"
},
"devDependencies": {
  "react": "15.4.1",
  "react-dom": "15.4.1",

According to https://webpack.github.io/docs/configuration.html#externals and https://webpack.github.io/docs/library-and-externals.html,

I have tried the following configuration :

1.

externals: {
  react: {
    root: 'React',
    commonjs2: 'react',
    commonjs: 'react',
    amd: 'react'
  },
  'react-dom': {
    root: 'ReactDOM',
    commonjs2: 'react-dom',
    commonjs: 'react-dom',
    amd: 'react-dom'
  }
},

2. externals: { react: 'react', 'react-dom' : 'react-dom', }

3.

 externals: {
  react: 'React',
  'react-dom' : 'ReactDOM',
 }

4.

externals: {
  react: 'umd react',
  'react-dom' : 'umd react-dom'
}

5.

    externals: {
        // Use external version of React
        "react": "React",
        "react-dom": "ReactDOM"
    },

I always have this error :

 Dynamic page loading failed Error: addComponentAsRefTo(...): Only a ReactOwner can have refs. You might be adding a ref to a component that was not created inside a component's `render` method, or you have multiple copies of React loaded (details: https://bf.me/react-refs-must-have-owner).(…)

So I wonder, the documentation isn't very clear on that.

In december 2017, january 2018 at maximum, how can I exclude React and ReactDOM from being bundled ?

回答1:

One way could be put them into separate file such as reactjs via

  entry: {
      app:'./src/app.jsx',
      reactjs:[
          'react','react-dom'               ,
      ]
  }

  output: {
    path       : path.join(__dirname, 'public'),
    filename   : 'bundle.[name].js',
    publicPath : '/public/'
  },

 plugins : [
     new webpack.optimize.CommonsChunkPlugin({
        name: 'reactjs',
     })
 ]

So now webpack will build two files bundle.app.js and bundle.reactjs.js. Include them in your index.html first react then app.



回答2:

here my webpack configuration, it just the basics since I'm working on a smaller project

const {resolve} = require('path');

module.exports = {
  entry: './demo/react/main.jsx',
  output: {
    filename: 'bundle.js',
    path: resolve(__dirname, 'demo/react/dist')
  }
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        use: [
          {
            loader: 'babel-loader'
          }
        ]

      }
    ]
  },
  externals: {
    // Use external version of React
    'react': 'React',
    'react-dom': 'ReactDOM'
  }
};


回答3:

EDIT:

Did not see this in your tried example. As mentioned by OP, the following JSON config did not work for them.


From GitHub issue #1275, try this:

{
    externals: {
        // Use external version of React
        "react": "React",
        "react-dom": "ReactDOM"
    }
}

If that doesn't work, try reading through that whole Issue page. There are a couple of suggestion that may end up working for you.