babel-plugin-istanbul
babel-plugin-istanbul copied to clipboard
Cannot change cwd
Is there a way to set the cwd (current working directory) before using the plugin? Right now to get the plugin to work properly with a different cwd I have to manually change cwd as demonstrated below:
const origCwd = process.cwd();
process.chdir(__dirname);
const result = babel.transformSync(fileContent, babelOpts);
process.chdir(origCwd);
If I do not change the cwd then the plugin just returns the untransformed source. This solution seems hacky to me and it would be nice if I could use something like this:
const babelOpts = {
plugins: [
'istanbul'
],
cwd: __dirname,
};
const result = babel.transformSync(fileContent, babelOpts);
I looked at the source code and noticed in 'lib/index.js' that the function 'makeShouldSkip()' has the line below which does not seem to check for a user specified cwd:
const cwd = getRealpath(process.env.NYC_CWD || process.cwd());
I'm not that familiar with babel in general so maybe I'm just missing something and looking at the wrong approach but I would like to avoid manually setting/resetting cwd. It took me a while to figure out that setting cwd in the babel options wasn't doing anything since there are no error messages.
This is interesting, I was not aware of the cwd
option to babel. I'll have to investigate what it would take to read this babel configuration option instead of using process.cwd()
.
Please try:
const babelOpts = {
plugins: [
[
'istanbul',
Object.assign({cwd: __dirname}, require(path.join(__dirname, 'package.json')).nyc))
]
]
};
Note the require
of package.json
from __dirname
assumes this is what you want. Once you specify any option to babel-plugin-istanbul
this disables automatic loading of nyc config from package.json so you have to do it manually. If you don't need to load nyc configuration from package.json then you can replace the Object.assign(...)
with simply {cwd: __dirname}
to give the cwd
to test-exclude
and use the default include/exclude options. If you need to load package.json from a different directory you may need to adjust the path.join
.