简体   繁体   English

避免在ES6 JavaScript Web项目中导入深层嵌套的模块

[英]avoiding deep nested module imports in an ES6 JavaScript web project

I am trying to tackle that I am currently have with my JavaScript project structure. 我正在尝试解决我目前在JavaScript项目结构中遇到的问题。 I am writing ES6 syntax with webpack. 我正在用webpack编写ES6语法。 My current directory structure looks like this 我当前的目录结构如下所示

project-dir
  |_ packages.json
  |_ webpack.config.js
  |_ html
  |  |_ ***
  |  |_ ***
  |_ js
     |_ app.js
     |_ routes
        |_ abc-component
        |  |_ components
        |  |  |_ abc1.js
        |  |  |_ abc2.js
        |  |_ index.js
        |
        |_ xyz-component
        |  |_ components
        |  |  |_ xyz1.js
        |  |  |_ xyz2.js
        |  |_ index.js
        |_ reducers.js

This is simpler structure. 这是更简单的结构。 But the problem is that the "reducers.js" and the "abc1.js/xyz1.js" need access to the same functionality. 但是问题是“ reducers.js”和“ abc1.js / xyz1.js”需要访问相同的功能。 So, if that functionality lives in "reducers.js" then the "abc1.js/xyz1.js" would have to import it as "../../reducers.js". 因此,如果该功能存在于“ reducers.js”中,则“ abc1.js / xyz1.js”必须将其导入为“ ../../reducers.js”。 If that functionality lives split up in "abc1.js" and "xyz1.js" etc, then "reducers.js" will have to import each one of them as ".\\abc-component\\components\\abc1.js" and ".\\xyz-component\\component\\xyz1.js". 如果该功能分散在“ abc1.js”和“ xyz1.js”等中,则“ reducers.js”将必须将每个导入为“。\\ abc-component \\ components \\ abc1.js”和“ \\ XYZ分量\\分量\\ xyz1.js”。 The first way, it feels like i am reaching way up and the second way, it feels like i am reaching way up. 第一种方式,感觉就像是我正在上升;第二种方式,感觉就像是我正在上升。 The code is also constrained to the structure of the file layout. 该代码还受文件布局结构的限制。 This directory structure could go deeper down and we will end up with very ugly imports. 此目录结构可能会更深入,最终我们将得到非常丑陋的导入。

So, it makes sense for me to pull out this functionality that is shared by reducers.js and "abc1/js/xyz1.js" into a different module. 因此,对我来说,将由reducers.js和“ abc1 / js / xyz1.js”共享的此功能引入另一个模块是有意义的。 In doing that i thought about putting that in a another git repo, but this functionality is very project specific and didn't want the hassle of putting it in another repo. 为此,我考虑将其放入另一个git仓库中,但是此功能是非常特定于项目的,并且不希望将其放入另一个仓库中的麻烦。

I tried creating a "lib" folder under the "js" folder and put the common functionality it in that folder along with a packages.json file. 我尝试在“ js”文件夹下创建一个“ lib”文件夹,并将其常用功能以及packages.json文件放入该文件夹中。 That way, i thought i would just add it as a "dependencies" in the project's package.json file with the "file:\\lib\\common" value. 这样,我想我只是将其作为“依赖项”添加到项目的package.json文件中,并带有“ file:\\ lib \\ common”值。 This way, it will reference that module locally. 这样,它将在本地引用该模块。 But this landed me in a load of trouble. 但这使我陷入了麻烦。 The problems i am encountering are 1) I couldn't write ES6 in this common module since webpack is not processing it 2) Everytime I change something in the common.js, i have to run "npm install" 3) npm seems to be caching an old version of the common module ever after i change it and run "npm install". 我遇到的问题是1)由于webpack无法处理,所以我无法在此通用模块中编写ES6 2)每次更改common.js中的内容时,我都必须运行“ npm install” 3)npm似乎是在我更改公共模块并运行“ npm install”之后,就缓存该公共模块的旧版本。 Not sure where it comes from, i did clean the npm cache by running "npm cache clean" 4) Even when i just write old javascript in this common module and not ES6, webpack watch does not pick up on changes to this file and re-render the app. 不知道它来自哪里,我确实通过运行“ npm cache clean”来清理npm缓存4)即使我只是在此通用模块中而不是ES6中编写旧的javascript,webpack watch也不接受对该文件的更改并重新-渲染应用程序。

I was wondering if i could get some thoughts on how best to go about solving this problem. 我想知道我是否可以对如何最好地解决这个问题有一些想法。 Also, thoughts about how i tried and exceptionally failed in solving the problem with a local module would be very helpful. 另外,对我如何尝试并在解决本地模块问题方面异常失败的想法也会很有帮助。

One way to deal with this is to put your common code into a node module and just import it like you would any other library. 一种解决方法是将您的通用代码放入节点模块,然后像导入其他任何库一样将其导入。

This would work similarly to the "lib" idea you suggested but without needing relative imports. 这将与您建议的“ lib”想法类似,但是不需要相对导入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM