汇总不排除已排除的包裹

时间:2018-05-31 17:55:20

标签: javascript reactjs styled-components rollupjs

我正在使用Rollup捆绑一些React组件。组件使用styled-components设置样式。我在styled-components数组中添加了reactoptions.external和其他一些包。

当我将组件导入我的其他应用程序时,我遇到错误,指出styled-components正在页面上实例化两次。在我看来,这是一个与我的Rollup配置或Rollup本身相关的问题,因为我在运行构建过程时遇到了一些令人困惑的输出。

汇总配置/构建文件

const babel = require('rollup-plugin-babel');
const commonjs = require('rollup-plugin-commonjs');
const nodeResolve = require('rollup-plugin-node-resolve');
const replace = require('rollup-plugin-replace');
const sass = require('rollup-plugin-sass');
const autoprefixer = require('autoprefixer');
const path = require('path');
const rollup = require('rollup');

const cwd = process.cwd();
const { name, moduleName } = require(path.join(cwd, 'package.json'));

const build = opts => {
  rollup
    .rollup({
      input: opts.input || 'src/index.js',
      external: [
        'react',
        'react-proptypes',
        'styled-components',
      ],
      output: {
        globals: {
          'react': 'React',
          'react-proptypes': 'PropTypes',
          'styled-components': 'styled',
        }
      },
      globals: {
        'react': 'React',
        'react-proptypes': 'PropTypes',
        'styled-components': 'styled',
      },
      plugins: [
        babel({
          exclude: 'node_modules/**',
          plugins: ['external-helpers']
        }),
        sass({
          insert: false,
          output: false,
          include: '**/*.scss',
          exclude: [],
          options: {
            importer( url /*, prev */ ) {
              if ( url.startsWith( '~' ) ) {
                const path = process.cwd() + '/node_modules/' + url.slice( 1 );

                return {
                  file: path
                };
              }
            }
          }
        }),
        nodeResolve({
          jsnext: true
        }),
        commonjs({
          include: 'node_modules/**',
          namedExports: {
            './node_modules/react/react.js': [
              'cloneElement',
              'createElement',
              'PropTypes',
              'Children',
              'Component'
            ]
          }
        }),
        replace({
          'process.env.NODE_ENV': JSON.stringify('production')
        })
      ]
    })
    .then(bundle => {
      const format = opts.format || 'umd';
      const formatMod = opts.formatMod || format;
      const exports = opts.exports || 'named';
      const dest = `dist/${name}.${formatMod}.js`;

      console.log(dest);
      bundle.write({
        exports,
        format,
        name: moduleName || name,
        file: dest
      });
    })
    .catch(err => {
      console.error(err);
    });
};

build({
  format: 'umd'
});

build({
  format: 'es',
  formatMod: 'esm'
});

build({
  format: 'cjs'
});

简单组件

import React from 'react';
import styled from 'styled-components';

const StyledTest = styled.div`
  font-size: 24px;
  color: red;
  font-family: sans-serif;
`;

const Test = ({ children }) => (
  <StyledTest>{children}</StyledTest>
);

export default Test;

已编译的简单组件

import React from 'react';
import styled from 'styled-components';

var taggedTemplateLiteral = function (strings, raw) {
  return Object.freeze(Object.defineProperties(strings, {
    raw: {
      value: Object.freeze(raw)
    }
  }));
};

var _templateObject = taggedTemplateLiteral(['\n  font-size: 24px;\n  color: red;\n  font-family: sans-serif;\n'], ['\n  font-size: 24px;\n  color: red;\n  font-family: sans-serif;\n']);

var StyledTest = styled.div(_templateObject);

var Test = function Test(_ref) {
  var children = _ref.children;
  return React.createElement(
    StyledTest,
    null,
    children
  );
};

export default Test;

我从Rollup观察到的令人困惑的错误如下:

The following options have been renamed — please update your config: globals -> output.globals
The following options have been renamed — please update your config: globals -> output.globals
The following options have been renamed — please update your config: globals -> output.globals
dist/@vz-react/test.umd.js
dist/@vz-react/test.esm.js
dist/@vz-react/test.cjs.js
No name was provided for external module 'react' in options.globals – guessing 'React'
No name was provided for external module 'styled-components' in options.globals – guessing 'styled'

尽管定义了options.globalsoptions.output.globals,但我看到了这些错误。如果我删除options.globals我不再收到错误,要求我将options.globals移至options.output.globals,但我仍然会收到以下错误。

包版本

"rollup": "^0.59.4",
"styled-components": "^3.2.5",

非常感谢任何帮助或见解。提前谢谢。

2 个答案:

答案 0 :(得分:0)

重新阅读(样式化组件常见问题解答)后[https://www.styled-components.com/docs/faqs#why-am-i-getting-a-warning-about-several-instances-of-module-on-the-page]我意识到我有一个重复的styled-components模块。

我通过在webpack配置中添加以下内容解决了这个问题:

  resolve: {
+   modules: [path.resolve(appFolder, 'node_modules'), 'node_modules'],
  }

我仍然不确定为什么我会从Rollup收到这些警告但是样式组件的问题不再是问题。

答案 1 :(得分:0)

我已经这样声明了自己的外部组件:

const externals = {
  'react' : 'React',
  'react-dom' : 'ReactDOM',
  'react-scripts' : 'ReactScripts',
};

然后将变量的键添加到汇总配置的默认导出。

  external: Object.keys(externals),

这应该可以帮助您摆脱警告(: