Babel plugin that rewrites __dirname and __filename to static values
npm install babel-plugin-transform-dirname-filenameBabel plugin that rewrites __dirname and __filename to static values.
Source file t.js:
``javascript`
console.log(__dirname);
console.log(__filename);
`sh`
$ node t.js
/path/to
/path/to/t.js
`sh
$ 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.
`sh
$ 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!
`sh`
$ npm install babel-plugin-transform-dirname-filename
.babelrc
`json`
{
"plugins": ["transform-dirname-filename"]
}
`sh`
$ babel --plugins transform-dirname-filename script.js
`javascript``
require("babel-core").transform("code", {
plugins: ["transform-dirname-filename"]
});