babel-plugin-transform-dirname-filename icon indicating copy to clipboard operation
babel-plugin-transform-dirname-filename copied to clipboard

Possibility to make __dirname relative

Open olegstepura opened this issue 8 years ago • 1 comments

Hi!

Is it possible to configure this plugin to make __dirname relative to some folder? If for example I have project root and I want to inject relative file path to the resulting code it would be good to be able to write

console.log('[' + __dirname + '/' + __filename + ']', 'my stuff');

which would then expand to

console.log('[' + 'component/Main' + '/' + Main.js + ']', 'my stuff');

olegstepura avatar Jan 26 '17 11:01 olegstepura

You may be able to get away with using module.filename (global variable -- doesn't come from this library)

Example input:

console.log(module.id);

Example output:

./src/index.js

Additionally, if you're using Webpack, you can set

node: {
  __filename: true,
  __dirname: true,
},

and it will make it relative from the root directory

SebastienGllmt avatar Mar 07 '20 22:03 SebastienGllmt