简体   繁体   English

如何从生产汇总构建中删除 React PropTypes?

[英]How to remove React PropTypes from production rollup build?

I have created an npm library, where I am exploring using rollup for bundling.我创建了一个 npm 库,我正在探索使用 rollup 进行捆绑。 Here is the config:这是配置:

import babel from 'rollup-plugin-babel';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import { terser } from 'rollup-plugin-terser';
import { sizeSnapshot } from 'rollup-plugin-size-snapshot';
import pkg from './package.json';

const input = 'src/index.js';
const name = 'TooltipTrigger';
const globals = {
  react: 'React',
  'react-dom': 'ReactDOM',
  'prop-types': 'PropTypes',
  'react-popper': 'ReactPopper'
};
const external = id => !id.startsWith('.') && !id.startsWith('/');
const getBabelOptions = ({ useESModules = true } = {}) => ({
  exclude: 'node_modules/**',
  runtimeHelpers: true,
  plugins: [['@babel/plugin-transform-runtime', { useESModules }]]
});

export default [
  {
    input,
    output: {
      name,
      file: 'dist/react-popper-tooltip.js',
      format: 'iife',
      globals
    },
    external: Object.keys(globals),
    plugins: [
      resolve({
        browser: true,
        modulesOnly: true
      }),
      commonjs({
        include: 'node_modules/**'
      }),
      babel(getBabelOptions()),
      sizeSnapshot()
    ]
  },
  {
    input,
    output: {
      name,
      file: 'dist/react-popper-tooltip.min.js',
      format: 'iife',
      globals
    },
    external: Object.keys(globals),
    plugins: [
      resolve({
        browser: true,
        modulesOnly: true
      }),
      commonjs({
        include: 'node_modules/**'
      }),
      babel(getBabelOptions()),
      terser(),
      sizeSnapshot()
    ]
  },
  {
    input,
    output: { file: pkg.main, format: 'cjs' },
    external,
    plugins: [babel(getBabelOptions({ useESModules: false })), sizeSnapshot()]
  },
  {
    input,
    output: { file: pkg.module, format: 'esm' },
    external,
    plugins: [babel(getBabelOptions()), sizeSnapshot()]
  }
];

I want to remove prop types from prod builds in the IIFE build.我想从 IIFE 构建中的 prod 构建中删除 prop 类型。 The babel-plugin-transform-react-remove-prop-types removes the prop types declarations well, but since I have declared prop-types as global in rollup config, it keeps it as a dependency. babel-plugin-transform-react-remove-prop-types很好地删除了 prop 类型声明,但是由于我在汇总配置中将 prop-types 声明为全局,因此它将其保留为依赖项。 When I remove it from globals, it resolves the package and bundles it inside the final build.当我从全局变量中删除它时,它会解析包并将其捆绑在最终构建中。 What should I do here?我应该在这里做什么? Also is my build config optimal wrt creating iife, cjs and esm builds?我的构建配置是否最佳 wrt 创建 iife、cjs 和 esm 构建?

You can use transform-react-remove-prop-types babel plugin with options removeImport: true .您可以使用带有选项removeImport: true transform-react-remove-prop-types babel 插件。 So this condition finally remove prop-types from build.所以这个条件最终从构建中删除了道具类型。

['transform-react-remove-prop-types', { removeImport: true }] for example. ['transform-react-remove-prop-types', { removeImport: true }]例如。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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