Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/storybookjs/paths.macro
👣 Babel plugin that returns an object containing paths like __dirname and __filename as static values
https://github.com/storybookjs/paths.macro
Last synced: 3 months ago
JSON representation
👣 Babel plugin that returns an object containing paths like __dirname and __filename as static values
- Host: GitHub
- URL: https://github.com/storybookjs/paths.macro
- Owner: storybookjs
- Created: 2018-01-23T13:25:01.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2021-08-30T07:10:22.000Z (about 3 years ago)
- Last Synced: 2024-04-14T12:07:37.836Z (7 months ago)
- Language: JavaScript
- Homepage:
- Size: 95.7 KB
- Stars: 32
- Watchers: 4
- Forks: 7
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Babel paths.macro
A [babel macro](https://github.com/kentcdodds/babel-plugin-macros) that allows you to "import" the location of the source-file.
The supported paths you can import:
name | example
------------ | ---
npmRoot | `/Users/you/project`
gitRoot | `/Users/you/project`
wd | `/Users/you/project`
fileAbsolute | `/Users/you/project/src/input.js`
file | `input.js`
extension | `.js`
filename | `input`
baseAbsolute | `/Users/you/project/src/`
base | `/src/`The default import is equal to `base`.
## Example
Source file `input.js`:
```js
import base, { filename } from 'paths.macro';console.log(base, filename);
alert(filename, base);
function usePathsForSomething() {
return [filename, base];
}
```Output:
```js
console.log("/src/", "input");alert("input", "/src/");
function usePathsForSomething() {
return ["input", "/src/"];
}
```## Config
Install:
```sh
yarn add paths.macro
```If you don't have babel-macros already you must also install that:
```sh
yarn add babel-plugin-macros
```Ensure you have `babel-plugin-macros` in your babel config (`.babelrc`).
```json
{
"plugins": ["macros"]
}
```