25

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?

Stretch0
  • 8,362
  • 13
  • 71
  • 133

2 Answers2

48

If you care about type checking your test files, instead of excluding them in tsconfig.json, do that exclusion as a param to the rollup typescript plugin in rollup.config.js.

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

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

  })
]
hello_luke
  • 837
  • 12
  • 15
  • 2
    You need to nest the `exclude` property under the `tsconfigOverride` key. Refer [docs](https://github.com/ezolenko/rollup-plugin-typescript2/blob/master/README.md#plugin-options) – Ansh Saini Sep 18 '21 at 20:06
  • Thanks @AnshSaini, I've updated the answer. – hello_luke Sep 19 '21 at 22:36
12

You can exclude tests in tsconfig.json e.g.

"exclude": [
    "**/tests",
    "**/*.test.js",
  ]
Porok12
  • 262
  • 2
  • 10