Détail du package

babel-plugin-transform-dirname-filename

TooTallNate4.7kMIT1.1.0

Babel plugin that rewrites dirname and filename to static values

babel, plugin, dirname, filename

readme

babel-plugin-transform-dirname-filename

Babel plugin that rewrites __dirname and __filename to static values.

Example

Source file t.js:

console.log(__dirname);
console.log(__filename);

Execute normally

$ node t.js
/path/to
/path/to/t.js

Before

$ 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.

After

$ 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!

Installation

$ npm install babel-plugin-transform-dirname-filename

Usage

Via .babelrc (Recommended)

.babelrc

{
  "plugins": ["transform-dirname-filename"]
}

Via CLI

$ babel --plugins transform-dirname-filename script.js

Via Node API

require("babel-core").transform("code", {
  plugins: ["transform-dirname-filename"]
});

changelog

1.1.0 / 2016-05-02

  • [494606239a] - add .gitignore file (Nathan Rajlich)
  • [9f77d808bf] - only include __dirname and __filename when the script actually uses those variables (Nathan Rajlich)

1.0.0 / 2016-04-16