Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rollup watch include directory

I am trying with following rollup.config.js file

import typescript from "rollup-plugin-typescript2";
import pkg from "./package.json";
import copy from 'rollup-plugin-copy'
import clean from 'rollup-plugin-clean';

export default [
  {
    input: "src/index.ts",
    external: Object.keys(pkg.peerDependencies || {}),
    watch: {
        skipWrite: false,
        clearScreen: false,
        include: 'src/**/*',
        //exclude: 'node_modules/**',
        // chokidar: {
        //     paths: 'src/**/*',
        //     usePolling: false
        // }
    },
    plugins: [
      clean(),
      copy({
        targets: [
          { src: 'src/*', dest: 'dist' }
        ]
      }),
      typescript({
        typescript: require("typescript"),
        include: [ "*.ts+(|x)", "**/*.ts+(|x)", "*.d.ts", "**/*.d.ts" ]
      }),
    ],
    output: [
      { file: pkg.main, format: "cjs" },
      { file: pkg.module, format: "esm" },
      {
        file: "example/src/reactComponentLib/index.js",
        format: "es",
        banner: "/* eslint-disable */"
      }
    ]
  }
];

I want to rebuild when anything in src changes. I have couple of files which are not imported in .js and .ts files but I want them to copy in dist folder. copy is working fine but the watch is not picking up changes in those other files. Tried alot of variations on chokidar options but no luck yet.

Anyone have any idea how to resolve this?

like image 603
Saurabh Nemade Avatar asked May 09 '26 11:05

Saurabh Nemade


1 Answers

watch.include only works on files pertaining to the module graph so if they are not imported they won't be included (https://rollupjs.org/guide/en/#watchinclude).

You can solve this by creating a small plugin that calls this.addWatchFile on those external files when the build starts. Here is an example:

plugins: [
       {
           name: 'watch-external',
           buildStart(){
               this.addWatchFile(path.resolve(__dirname, 'foo.js'))
           }
       }
    ]

Combine it with some globbing utility such as fast-glob and just call this.addWatchFile for every file you want to copy:

import fg from 'fast-glob';

export default {
    // ...
    plugins: [
       {
           name: 'watch-external',
           async buildStart(){
               const files = await fg('src/**/*');
               for(let file of files){
                   this.addWatchFile(file);
               }
           }
       } 
    ]
}
like image 153
Isidrok Avatar answered May 11 '26 01:05

Isidrok



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!