From a Symfony 5 project, given a simple "foo.js" file (added to the Encore variable in the webpack.config.js file)
For some reasons, I want to programmatically get the filename "foo" within the file.
But, from the "foo.js" file, when I want to get the name of the current file with __filemane, I get "index.js" instead of, I'm looking for, "foo".
Is there a way for get what I'm looking for ?
Thanks for any help :)
Hi,
That's not related to Webpack Encore, but I guess you can do something like this:
const config = Encore.getWebpackConfig();
config.target = 'node';
config.node = config.node || {};
config.node.__filename = true;
config.node.__dirname = true; // if you want to get `__dirname`
return config;
See https://webpack.js.org/configuration/node/ and https://github.com/webpack/webpack/issues/1599#issuecomment-186841345
works perfectly !
Thank you for your speedily answer !
the other solution is to use import.meta instead, which is the way to deal with that in ES modules (and works in webpack without any additional config)
Most helpful comment
the other solution is to use
import.metainstead, which is the way to deal with that in ES modules (and works in webpack without any additional config)