简体   繁体   中英

How to use redux-form in peer dependency

I am trying to create a ES6 module of a login form using rollup and react-redud.

I have a rollup with the following configuration:

const plugins = [
  // Unlike Webpack and Browserify, Rollup doesn't automatically shim Node
  // builtins like `process`. This ad-hoc plugin creates a 'virtual module'
  // which includes a shim containing just the parts the bundle needs.
  {
    resolveId(importee) {
      if (importee === processShim) return importee;
      return null;
    },
    load(id) {
      if (id === processShim) return 'export default { argv: [], env: {} }';
      return null;
    },
  },
  nodeResolve(),
  commonjs({
    include: 'node_modules/**',
    namedExports: {
      './node_modules/immutable/dist/immutable.js': ['fromJS', 'Map', 'List', 'Record', 'Iterable'],
      './node_modules/redux/dist/redux.js': ['createStore', 'combineReducers', 'bindActionCreators', 'applyMiddleware', 'compose'],
      './node_modules/react-redux/dist/react-redux.js': [' Provider', 'createProvider', 'connectAdvanced', 'connect'],
    },
  }),
  replace({
    'process.env.NODE_ENV': JSON.stringify(prod ? 'production' : 'development'),
  }),
  inject({
    process: processShim,
  }),
  json(),
  babel({
    plugins: ['external-helpers'],
    exclude: 'node_modules/**',
  }),
  cleanup(),
];

if (prod) plugins.push(uglify(), visualizer({ filename: './bundle-stats.html' }));

export default {
  input: 'src/index.js',
  sourcemap: true,
  name: pkg.name,
  external: ['react', 'react-dom', 'prop-types', 'styled-components', 'bootstrap-styled', 'classnames', 'react-transition-group', 'loaders', 'redux-form', 'redux', 'react-redux', 'react-intl', 'message-common', 'bootstrap-styled-motion'],
  exports: 'named',
  output,
  plugins,
  globals: { react: 'React', 'react-dom': 'ReactDom', 'prop-types': 'PropTypes', 'styled-components': 'styled', classnames: 'cn', 'react-transition-group': 'ReactTransitionGroup', loaders: 'loaders', 'redux-form': 'redux-form', 'react-intl': 'react-intl', 'message-common': 'message-common' },
};

My rollup bundle fine, no warnings.

I have tried every possibility of import :

import reduxForm from 'redux-form/lib/immutable/reduxForm';
import Field from 'redux-form/lib/immutable/Field';

and

import { Field, reduxForm } from 'redux-form/es/immutable';

and

import { Field, reduxForm } from 'redux-form/es/immutable';

But nothing work, everytime I install this module somewhere, the transpiled ES replace this line with these imports

import reactRedux from 'react-redux';
import redux from 'redux';

I assume this is happening because redux-form depend on these two. Because these two modules doesn't have default export , this throw an error :

WARNING in ./node_modules/login-form/dist/login-form.es.js 3471:19-24 "export 'default' (imported as 'redux') was not found in 'redux'

WARNING in ./node_modules/login-form/dist/login-form.es.js 3524:26-36 "export 'default' (imported as 'reactRedux') was not found in 'react-redux'

I have tried to play with globals , namedExports . I haven't found a way to make redux-form a peer of my project.

我终于解决了它,我不得不将外部设置为redux-form/immutable而不是redux-form

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM