Webpack-encore: Symfony 5 with webpack encore : __filename return always "index.js" instead of the name of the executed script file

Created on 5 Dec 2020  路  3Comments  路  Source: symfony/webpack-encore

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 :)

Most helpful comment

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)

All 3 comments

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)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fanchyfanch picture fanchyfanch  路  3Comments

heitorvrb picture heitorvrb  路  4Comments

wenmingtang picture wenmingtang  路  4Comments

powerlimit picture powerlimit  路  4Comments

MatthD picture MatthD  路  4Comments