问题描述
我正在尝试使用以下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);
}
}
}
]
}