Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tootallnate/babel-plugin-transform-dirname-filename
Babel plugin that rewrites `__dirname` and `__filename` to static values
https://github.com/tootallnate/babel-plugin-transform-dirname-filename
Last synced: 4 days ago
JSON representation
Babel plugin that rewrites `__dirname` and `__filename` to static values
- Host: GitHub
- URL: https://github.com/tootallnate/babel-plugin-transform-dirname-filename
- Owner: TooTallNate
- Created: 2016-04-16T21:19:30.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-09-26T16:27:11.000Z (about 7 years ago)
- Last Synced: 2024-12-10T14:04:35.691Z (16 days ago)
- Language: JavaScript
- Homepage:
- Size: 4.88 KB
- Stars: 16
- Watchers: 3
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# babel-plugin-transform-dirname-filename
Babel plugin that rewrites `__dirname` and `__filename` to static values.
## Example
Source file `t.js`:
```javascript
console.log(__dirname);
console.log(__filename);
```### Execute normally
```sh
$ node t.js
/path/to
/path/to/t.js
```### Before
```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.### After
```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!## Installation
```sh
$ npm install babel-plugin-transform-dirname-filename --save-dev
```## Usage
### Via `.babelrc` (Recommended)
**.babelrc**
```json
{
"plugins": ["transform-dirname-filename"]
}
```### Via CLI
```sh
$ babel --plugins transform-dirname-filename script.js
```### Via Node API
```javascript
require("babel-core").transform("code", {
plugins: ["transform-dirname-filename"]
});
```