简体   繁体   中英

How to prevent tests being bundled by rollup?

I am building a react component package and want to exclude my tests folder from being bundled in my dist file that is built from rollup.

My file structure looks like this after running rollup -c

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

My rollup config looks like this:

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']
}

How can I exclude my tests directory from being bundled into the dist file when runnning rollup?

You can exclude tests in tsconfig.json eg

"exclude": [
    "**/tests",
    "**/*.test.js",
  ]

The accepted answer works if you don't care about type checking your test files. If you do, instead of excluding them in tsconfig.json , do that exclusion as a param to the rollup typescript plugin in rollup.config.js .

plugins: [
  typescript({
    exclude: ["**/__tests__", "**/*.test.ts"]
  })
]

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