Getting working directory and file name in react & webpack

删除回忆录丶 提交于 2020-01-17 05:21:04

问题


I am working with a react js project. i am using webpack and redux with that. Here is the folder structure of my project.

-assets
-src  
 -component   
  -index.jsx  
 -container   
  -index.jsx

For now i want to use dynamic className for the index.jsx files according to their working directory name. Example:

for index.jsx in the folder component, the className will be

src-component

for index.jsx in the folder container, the className will be

src-component

I have tried to use npm module path for that. But the __dirname gives the output of the url of the browser '/'. How can i get that pwd from the jsx file.


回答1:


By default, webpack mocks Node's __dirname to "/". You can enable the real dirname by adding the following to your webpack configuration:

node: {
    __dirname: true
},

After that, __dirname will be set, relative to the resolve context in your webpack configuration.



来源:https://stackoverflow.com/questions/38914250/getting-working-directory-and-file-name-in-react-webpack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!