Closed gnestor closed 7 years ago
@gnestor Thank you for investigating this. Unfortunately, this isn't working in my machine.
I've also tried:
var transformOptions = {
babelrc: true,
};
but it doesn't work either.
A solution to #3 would be that jp-babel
looks for .babelrc
and sets transformOptions
accordingly if it finds one.
@n-riesco I just updated the PR to use jp-babel's babel-preset-es2015
by providing an absolute path to it. I've confirmed that I no longer get Error: Couldn't find preset "es2015"
.
Regarding https://github.com/n-riesco/jp-babel/issues/3, I started working on finding and importing config from .babelrc
. One question: How do we get the CWD of the jupyter notebook? Given that (say cwd
), we could:
function findFile(file, dir) {
var result = fs.readdirSync(dir).find(result => result === file);
if (result) return path.resolve(result);
return findFile(file, path.join(dir, '..'));
}
function getBabelConfig(path) {
if (!path) return null;
try {
return JSON.parse(fs.readFileSync(path, 'utf-8'));
} catch(err) {
return null;
}
}
var babelrc = getBabelConfig(findFile('.babelrc', cwd));
var transformOptions = babelrc || {
presets: [path.resolve(path.join(__dirname, "..", "node_modules", "babel-preset-es2015"))],
};
This traverses up to the directory tree (starting from the notebook CWD) looking for a .babelrc
and uses its config if it exists. Otherwise, it defaults to jp-babel's babel-preset-es2015
.
The problem with the notebook CWD is that it only makes sense for local kernels (remote kernels, in principle, cannot access notebook paths).
I understand the need for making the use case of local kernels as convenient as possible. But this requires help from the frontend.
jp-babel
, ijavascript
and jp-coffeescript
provide a flag to set the session working directory:
jp-babel --jp-working-dir=path
When this flag isn't provided, the session working directory is set to the current working directory. This means that the following example will launch the jupyter notebook with jp-babel kernels that use /path/to/my/project
as the session working directory.
$ cd /path/to/my/project
$ jp-babel
Sorry I'm digressing. The point I wanted to make is that I think we should use:
var babelrc = getBabelConfig(findFile('.babelrc', process.cwd()));
and let the frontend set the kernel working directory. If the frontend doesn't help, the user has the workaround of setting this manually with jp-babel --jp-working-dir=path
.
@n-riesco Thanks, very helpful. I just updated the PR again. Now, if I run notebook in a directory with a .babelrc
file (anywhere up its directory tree), jp-babel will use that config and if not, it will default to using babel-preset-es2015
. I think this fixes https://github.com/n-riesco/jp-babel/issues/2 and https://github.com/n-riesco/jp-babel/issues/3. Give it a try!
@n-riesco Updated! Here's a test notebook (should've done that from the start!). https://gist.github.com/gnestor/5ee40cf0009e5aa037948f7c4ad6044c
@gnestor Hi! I've just tested the PR and it works for me.
Before I merge it, please, could you update the AUTHORS file? I ask contributors to do so, to confirm their consent to distributing their contributions under the LICENSE terms.
Thanks for fixing issues #2 and #3.
@n-riesco Thank you for starting this project and helping me with these issues.
Partially fixes https://github.com/n-riesco/jp-babel/issues/2
Theoretically, if options are not passed to
babel.transform
then babel should use the.babelrc
options. I haven't fully tested to determine if this in fact is happening. I know that moduleimport
is not working in the notebook within a project that has a qualified.babelrc
...