Skip to content
Babel plugin that returns an object containing paths like __dirname and __filename as static values
JavaScript
Branch: master
Clone or download
ndelangen Merge pull request #3 from dzienisz/patch-1
update Config/Install guide
Latest commit 6382fd9 Feb 1, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
__snapshots__ Initial commit Jan 23, 2018
src Initial commit Jan 23, 2018
.editorconfig Add .editorconfig Jan 25, 2018
.gitignore
README.md update Config/Install guide Jan 28, 2019
macro.js Find root on OS dependent paths - otherwise it doesn't find a real ro… Jan 28, 2018
package.json
test.js
yarn.lock

README.md

Babel paths.macro

A babel macro 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
extention .js
filename input
baseAbsolute /Users/you/project/src/
base /src/

The default import is equal to base.

Example

Source file input.js:

import base, { filename } from 'babel-plugin-module-paths/paths.macro';

console.log(base, filename);

alert(filename, base);

function usePathsForSomething() {
  return [filename, base];
};

Output:

console.log("/src/", "input");

alert("input", "/src/");

function usePathsForSomething() {
  return ["input", "/src/"];
};

Config

Install:

yarn add paths.macro

If you don't have babel-macros already you must also install that:

yarn add babel-plugin-macros

Ensure you have babel-plugin-macros in your babel config (.babelrc).

{
  "plugins": ["macros"]
}
You can’t perform that action at this time.