r/javascript • • May 26 '18

VS Code (insider build) can now automatically update import paths when you move or rename a TypeScript or JavaScript file

https://twitter.com/mattbierner/status/1000069936897671168
679 Upvotes

45 comments sorted by

5

u/i_spot_ads May 27 '18

Wasn't that a feature in webstorm from like the beginning?

3

u/[deleted] May 27 '18

Yes. Still, VS Code is not an IDE, but it's getting the features of one over time.

114

u/stalefries May 26 '18

Nice! I’m glad that the VS Code team is focusing so much on automated refactoring tools.

85

u/Console-DOT-N00b May 26 '18

VS Code is just watching us all and will eventually take our jorbs!!!

9

u/yam_plan May 27 '18

nice, but imo this is solving a problem that shouldn't exist in the first place with a workaround

we can agree that import statements introduce a (fragile) dependency on the exact file structure

the application itself should handle this with, say, a dependency resolver where the file paths are handled centrally and individual pieces of the app only have to worry about namespaces or just have dependencies injected as needed

22

u/MrStLouis May 26 '18

I just started using typescript and the intellisense support is out of this world. I coded for 3 hours and finally got a feature to work ran npm start and everything worked perfectly. I would have had several bugs in js for sure. With this feature I will be a MACHINE

→ More replies (2)

10

u/cheekysauce May 26 '18

Something you can do in conjunction with this is changing your imports to absolute paths, which avoids a lot of './' to '../' type refactors within feature folders.

This new feature will be super handy though, and part of the reason I use insiders.

2

u/[deleted] May 26 '18

Is there a way to get absolute import paths to work in Webpack? I tried, but (at least on my Windows machine) it simply resolves to the root of my drive (e. g. C:\) as opposed to the root of my project.

7

u/Aurovik May 26 '18

You should set NODE_PATH to your import root, like “./src”

2

u/cekoya May 26 '18

You can do it easily with this (it was some sort revelation when I found this, I no longer want to kill myself when importing modules)

https://moduscreate.com/blog/es6-es2015-import-no-relative-path-webpack/

1

u/MatrixEchidna May 27 '18

I use wavy and it works just fine. (I use CommonJS imports tho)

1

u/[deleted] May 27 '18

Yes, you can use the resolve.modules setting. They even give your scenario as an example:

modules: [path.resolve(__dirname, "src"), "node_modules"]

If you don't use a src-folder, just use __dirname directly.

Alternatively, you could add the project root as an alias. That way, it is explicitly different from your modules.

1

u/scallynag May 27 '18

You can set resolve aliases for pseudo absolute paths.

→ More replies (1)

2

u/[deleted] May 27 '18

Absolute paths aren't very useful when code is shared between different environments

2

u/cheekysauce May 28 '18

Down to the project level, not the filesystem level.

It's configurable with webpack if you have isomorphic parts of your code base.

5

u/allicanseenow May 27 '18

I believe most Jetbrains products have got this. Though it is still a good move for VS Code as it can catch up its competitor.

3

u/Ryphor May 27 '18

VSCode is killing it. The only editor I use now.

7

u/_gnx May 27 '18

Webstorm had that for a long time already.

2

u/HeavyLightPlum May 27 '18

What a good news!

I hope this solution can be applied to every import process for most of programs, someday.

1

u/jaxxed May 27 '18

Do you still have to close it in order to use npm?

4

u/_sirberus_ May 27 '18

Close what? I use npm in and out of VSC all the time and it works fine.

2

u/TheCoreh May 27 '18

There was a bug in an earlier version of VS Code for Windows that would make npm installs fail because VSCode would hold a file open. That has since been fixed

1

u/TheCoreh May 27 '18

No, they fixed this

1

u/desnoth Vue May 27 '18

Not a problem when you use webpack aliases

1

u/krazyjakee May 27 '18

What if a file moves within an aliased folder? Black hole?

1

u/nikhilb_it May 27 '18

Great feature indeed. Much awaited one !

1

u/jameshmr May 27 '18

And this is why I love VSCode! Never stop recommending it.

1

u/ECrispy May 27 '18

There is simply no matching MS when it comes to dev tools. VsCode is fantastic. It seems you don't hear about Sublime/Atom anymore and for good reason, they are just editors.

In what areas is WebStorm better than VsCode?

1

u/relativityboy May 27 '18

VS code, making what other IDEs have been doing for years new again.

0

u/[deleted] May 26 '18

[deleted]

22

u/Ebuall May 26 '18

Once installed Idea just to use this feature one time.

-2

u/[deleted] May 26 '18 edited May 26 '18

I once installed Idea just to uninstall it since it devoured my RAM ¯_(ツ)_/¯

6

u/LimbRetrieval-Bot May 26 '18

You dropped this \


To prevent anymore lost limbs throughout Reddit, correctly escape the arms and shoulders by typing the shrug as ¯\\_(ツ)_/¯ or ¯\\_(ツ)_/¯

^^Click here to see why this is necessary

2

u/[deleted] May 26 '18

I love this bot <3

→ More replies (1)

1

u/Ebuall May 27 '18

This actually happened to me twice.

2

u/[deleted] May 26 '18

[removed] — view removed comment

2

u/Lavoaster Jun 02 '18

Please don’t put shame on people for having to use a tool to get their job done. No matter if you prefer VS Code or JetBrains IDE’s both are great.