首页 > 解决方案 > 如何防止测试被汇总捆绑?

问题描述

我正在构建一个反应组件包,并希望将我的测试文件夹排除在我从汇总构建的 dist 文件中。

运行后我的文件结构如下所示rollup -c

.
├── dist
│   ├── index.js
│   ├── tests
│      ├── index.test.js
├── src
│   ├── index.tsx
│   ├── tests
│      ├── index.test.tsx

我的汇总配置如下所示:

import typescript from 'rollup-plugin-typescript2'

import pkg from './package.json'

export default {
  input: 'src/index.tsx',
  output: [
    {
      file: pkg.main,
      format: 'cjs',
      exports: 'named',
      sourcemap: true,
      strict: false
    }
  ],
  plugins: [typescript()],
  external: ['react', 'react-dom', 'prop-types']
}

dist运行汇总时,如何将我的测试目录从捆绑到文件中排除?

标签: javascriptreactjsnpmwebpackrollupjs

解决方案


如果您不关心类型检查您的测试文件,则接受的答案有效。如果你这样做,而不是将它们tsconfig.json排除在rollup.config.js.

plugins: [
  /* for @rollup/plugin-typescript */
  typescript({
    exclude: ["**/__tests__", "**/*.test.ts"]
  })

  /* or for rollup-plugin-typescript2 */
  typescript({
    tsconfigOverride: {
      exclude: ["**/__tests__", "**/*.test.ts"]
    }

  })
]

推荐阅读