在react&中获取工作目录和文件名网页包 [英] Getting working directory and file name in react & webpack

查看:507
本文介绍了在react&中获取工作目录和文件名网页包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与React js项目合作.我正在使用webpack和redux.这是我的项目的文件夹结构.

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

现在,我想根据其工作目录名称为index.jsx文件使用动态className.示例:

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

对于文件夹组件中的index.jsx,className将为

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

src组件

src-component

对于文件夹容器中的index.jsx,className将为

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

src组件

src-component

我试图为此使用npm模块路径.但是__dirname给出了浏览器'/'的URL的输出.我如何从jsx文件中获取该密码.

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.

推荐答案

默认情况下,webpack将Node的__dirname模仿为"/".您可以通过在Webpack配置中添加以下内容来启用真实目录名:

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
},

然后,将相对于Webpack配置中的resolve上下文设置__dirname.

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

这篇关于在react&中获取工作目录和文件名网页包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆