Babel plugin that rewrites __dirname
and __filename
to static values.
Source file t.js
:
console.log(__dirname);
console.log(__filename);
$ node t.js
/path/to
/path/to/t.js
$ babel --out-file build/t.js t.js
$ node build/t.js
/path/to/build
/path/to/build/t.js
Notice how the build
directory is part of the paths, which is not what we
want.
$ babel --out-file build/t.js --plugins transform-dirname-filename t.js
$ node build/t.js
/path/to
/path/to/t.js
So even though the generated file is a build/t.js
, the __dirname
and
__filename
values will still reference the source file!
$ npm install babel-plugin-transform-dirname-filename --save-dev
.babelrc
{
"plugins": ["transform-dirname-filename"]
}
$ babel --plugins transform-dirname-filename script.js
require("babel-core").transform("code", {
plugins: ["transform-dirname-filename"]
});