Here is a repository created to demonstrate the issue:
brianmhunt/karma-rollup-preprocessor-issue-3
我正在尝试让 karma-rollup-preprocessor 与 Karma 的内置 watch 一起工作,即解决 showpad/karma-rollup-preprocessor#3
换句话说,在预处理器中,我想将文件添加到 Karma 的监视列表中。
获取 Rollup 用于编译的文件列表很容易。 Rollup 返回它读取的文件列表(那些想要观看的文件),因此在预处理器中我试图将文件添加到列表中 karma watches。
基本上我想将这个(或等效的工作)添加到预处理器中:
bundle.modules.forEach((module) => {
files.unshift({
pattern: module.id, /* The full file path, from Rollup */
watched: true,
included: false,
nocache: false,
served: false,
})
})
files
是 Karma 的 config.files
或 fileList
或任何需要放置正在监视的文件的地方。
使用 config.files
执行上述操作,文件确实被添加到观察程序,但是 .on(fileList.changeFile)
失败 _isIncluded
.
所以看起来这些文件也(或者替代地)必须添加到 fileList
。
不幸的是,当我尝试将 fileList
添加到 $inject
时,我得到错误:
Error: Can not resolve circular dependency! (Resolving: preprocess -> preprocessor:rollup -> fileList -> preprocess).
我基本上已经查看了所有其他看起来也可以添加包含的预处理器,但我没有发现如何做的迹象。
是否有一种规范的方式来添加 Karma 应该从预处理器观看的文件?否则怎么可能做到这一点?这对于 Karma 中的预处理器来说似乎非常关键,因此令人惊讶的是它在其他预处理器中没有记录、明显或有问题。
编辑这里还有一些尝试:
我尝试将监视的模式添加到 karma.conf
中的 config.files
即
files: [
"spec/**/*.js",
{pattern:"src/**/*.js", included: false, watched: true}
]
但是 src/*
在更改时不会重新编译。测试只是重新运行。
所以我尝试了这样的 chokidar:
var server = new karma.Server(options)...
chokidar.watch("src/**/*.js")
.on('add', server.refreshFiles.bind(server))
.on('change', server.refreshFiles.bind(server))
我还尝试了去抖动,以防 Karma 在刷新时变慢,但似乎测试不会重新运行。
我在 karma-browserify 周围寻找灵感,但它有点太复杂了,不深入研究就无法接受。
最佳答案
我已经 issued a pull request to resolve this .
在合并之前,可以使用我的 repo,即将 "karma-rollup-preprocessor": "brianmhunt/karma-rollup-preprocessor"
放在 package.json
依赖项中或 devDependencies。
关于javascript - 在 karma 预处理器中将文件添加到监视列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37795680/