汇总监视包含目录

问题描述

我正在尝试使用以下rollup.config.js文件

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 */"
      }
    ]
  }
];

当src中的任何内容更改时,我想重建。我有几个未导入.js和.ts文件的文件,但我希望它们复制到dist文件夹中。副本工作正常,但手表没有拾取其他文件中的更改。在chokidar选项上尝试了很多变化,但还没有运气。

有人知道如何解决吗?

解决方法

watch.include仅适用于与模块图有关的文件,因此,如果未导入它们,则将不包含这些文件(https://rollupjs.org/guide/en/#watchinclude)。

您可以通过创建一个小的插件来解决此问题,该插件在构建开始时会在这些外部文件上调用this.addWatchFile。这是一个示例:

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

使用诸如fast-glob之类的通配工具将其合并,然后为要复制的每个文件调用this.addWatchFile

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);
               }
           }
       } 
    ]
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...