简体   繁体   English

如何通过移动 JavaScript 项目中的文件进行重构并自动更新引用?

[英]How can I refactor by moving files in a JavaScript project and have references update automatically?

General tips are welcome.欢迎使用一般提示。 My specific situation is a React app, compiled with WebPack, with lots of files.我的具体情况是一个 React 应用程序,用 WebPack 编译,有很多文件。 If I want to move a file or folder within the project, is there a good way to do this such that references, such as import & require statements, update automatically?如果我想移动项目中的文件或文件夹,是否有一种很好的方法可以使引用(例如 import 和 require 语句)自动更新?

Bonus points for solutions using Atom or VSCode.使用 Atom 或 VSCode 的解决方案的奖励积分。

NPM scripts will also work. NPM 脚本也将起作用。 Thanks.谢谢。

I used a few text editors with plugins and IDE's during the years, this includes Atom, VSCode, SublimeText, etc. but keep coming back to Jetbrains products and one of the reason is the refactoring capabilities.这些年来,我使用了一些带有插件和 IDE 的文本编辑器,其中包括 Atom、VSCode、SublimeText 等,但不断回到 Jetbrains 产品,原因之一是重构功能。

Jetbrains dedicated JavaScript IDE, Webstorm does it very well without any additional plugins or hacks. Jetbrains 专用的 JavaScript IDE, Webstorm在没有任何额外插件或黑客的情况下做得很好。

All you need to do is drag and drop your file or folder to another location and all the relevant imports will be updated (make sure search for references is ticked in the confirmation pop up).您需要做的就是将您的文件或文件夹拖放到另一个位置,所有相关的导入都将更新(确保在确认弹出窗口中勾选搜索参考)。 This applies to both es6 import and/or common js require() .这适用于es6 import和/或common js require()

This is not all, you can rename variables, class, function names, whatever you like, just make sure you doing it by selecting the text , then right click then refactor and rename (you'll see in this menu you can do much more if you want)这还不是全部,您可以重命名变量、类、函数名称,无论您喜欢什么,只需确保通过选择文本来执行此操作,然后右键单击然后重构并重命名(您将在此菜单中看到您可以执行更多操作)如果你想)

Whenever you about to confirm your changes you can select the option in the pop-up that is "search in comments and strings" if you want which is really cool as you can keep your documentation up to date as well.每当您要确认更改时,您都可以在弹出窗口中选择“在注释和字符串中搜索”选项(如果您愿意),这真的很酷,因为您还可以使文档保持最新。

This official documentation goes more in deep, but generally if you do the above, it will be enough. 这个官方文档讲的比较深入,但是一般你做上面的就够了。

I guess if you don't feel confident enough, start up the server, with (create-react-app it will reload every time you make a change as hot reloading is built in) and if something goes wrong with your refactoring you will know it straight away.我想如果您觉得不够自信,请启动服务器,使用(create-react-app 它会在您每次进行更改时重新加载,因为内置了热重新加载)并且如果您的重构出现问题,您就会知道它马上。

Just fyi I am not associated with Jetbrains, I just like the product.仅供参考,我与 Jetbrains 无关,我只是喜欢这个产品。 Webstorm is not free (however it's very cheap) - go for the 30 day trial version if you don't wish to pay. Webstorm 不是免费的(但是它非常便宜) - 如果您不想付费,请选择 30 天试用版。

UPDATE:更新:

Also note, this feature support both relative and absolute paths as well as any file extensions, so including .*scss etc.另请注意,此功能支持相对路径和绝对路径以及任何文件扩展名,因此包括.*scss等。

Found a couple of plugins for VSCode.找到了几个 VSCode 插件。 Personally, I haven't used them but they might work for you就个人而言,我没有使用过它们,但它们可能对你有用

VS Code has an option to update imports on file move or rename. VS Code 可以选择在文件移动或重命名时更新导入。 This feature was enabled with the May 2018 Release.此功能已在 2018 年 5 月版本中启用。 Whenever a file is moved, VS code detects and asks for confirmation to update the import references also.每当移动文件时,VS 代码都会检测并要求确认更新导入引用。

Visual Studio Code - May 2018 Visual Studio Code - 2018 年 5 月

You can control the prompting behavior with the javascript.updateImportsOnFileMove.enabled and typescript.updateImportsOnFileMove.enabled settings.您可以使用 javascript.updateImportsOnFileMove.enabled 和 typescript.updateImportsOnFileMove.enabled 设置来控制提示行为。

This usually updates the import statements, but it might not update any strings variables/comments references to the moved files.这通常会更新导入语句,但它可能不会更新对移动文件的任何字符串变量/注释引用。

NOTE: It worked only for js/ts files.注意:它仅适用于 js/ts 文件。 For other types, like scss file, it does not update the @imports.对于其他类型,如 scss 文件,它不会更新 @imports。

Most editors/plugins do not support full fledged tracking and refactoring of move/rename actions for all file types, as it becomes more complex and ambiguous when relative paths, absolute paths are used in the same project.大多数编辑器/插件不支持对所有文件类型的移动/重命名操作进行完全成熟的跟踪和重构,因为在同一项目中使用相对路径、绝对路径时会变得更加复杂和模糊。

To summarize, for moving a file or folder, there are two instances the references needs to be updated.总而言之,要移动文件或文件夹,有两个实例需要更新引用。

  1. In the moved file itself, if any import is using a relative path在移动的文件本身中,如果任何导入使用相对路径
  2. Other files which import the moved module/files导入移动模块/文件的其他文件

I do not think the first is supported in VS Code (and probably in other editors too).我认为 VS Code 不支持第一个(也可能在其他编辑器中)。 The second one is supported in VS Code, but only for js/ts file imports. VS Code 支持第二个,但仅适用于 js/ts 文件导入。

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

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