如何防止测试被rollup捆绑?

Str*_*ch0 27 javascript npm reactjs webpack rollupjs

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

\n

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

\n
.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dist\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 index.js\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 tests\n\xe2\x94\x82      \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 index.test.js\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 index.tsx\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 tests\n\xe2\x94\x82      \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 index.test.tsx\n
Run Code Online (Sandbox Code Playgroud)\n

我的汇总配置如下所示:

\n
import typescript from 'rollup-plugin-typescript2'\n\nimport pkg from './package.json'\n\nexport default {\n  input: 'src/index.tsx',\n  output: [\n    {\n      file: pkg.main,\n      format: 'cjs',\n      exports: 'named',\n      sourcemap: true,\n      strict: false\n    }\n  ],\n  plugins: [typescript()],\n  external: ['react', 'react-dom', 'prop-types']\n}\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n

dra*_*mus 51

如果您关心测试文件的类型检查,请不要在 中排除它们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"]
    }

  })
]
Run Code Online (Sandbox Code Playgroud)

  • 您需要将“exclude”属性嵌套在“tsconfigOverride”键下。请参阅[文档](https://github.com/ezolenko/rollup-plugin-typescript2/blob/master/README.md#plugin-options) (2认同)

Por*_*k12 12

您可以排除tsconfig.json中的测试,例如

"exclude": [
    "**/tests",
    "**/*.test.js",
  ]
Run Code Online (Sandbox Code Playgroud)