How to publish a React component without including a reaction

I am creating a React component and want to publish it.

I installed package.json:

  "peerDependencies": {
    "react": "^15.6.1",
    "react-dom": "^15.6.1"
  },

And expect that when creating my module using webpack my component will not yell at this:

 Module not found: Error: Can't resolve 'react'

How can I compile a production component without including React?

I checked a few famous ones:

react-select react-toggle

And they only list the reaction in peerDependecies (and in devDependecies, for testing and development, I think) If I add an answer to my devDeps and run it webpack -p, it compiles successfully, but then I have reactcomponents in my kit.

Here is my simple web package setup

What am I missing?

UPDATE "" webpack ( gist), , React , React , :

"" undefined

React.Component ...

+4
1

peerDependency, devDependency, . , external webpack. ,

. https://webpack.js.org/configuration/externals/#object

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

  output: {
    publicPath: '/',
    filename: '[name]',
    library: "foobar",
    libraryTarget: 'umd',
  }

:

"dependencies": { 
  "foobar": "1.0.0",
  "react": "~16.0.0",
  "react-dom": "~16.0.0",
  "prop-types": "^15.6.0"
}
+2
source

Source: https://habr.com/ru/post/1687559/


All Articles