简体   繁体   English

JavaScript / TypeScript Transpiler工作流程

[英]JavaScript/TypeScript transpiler workflow

My workflow with JS/HTML has always been 我的JS / HTML工作流程一直

  1. write code 写代码
  2. reload website 重新加载网站
  3. fix bugs / tweak code 修复错误/调整代码
  4. goto 2 转到2

All my projects are still on AngularJS 1.x, and I'm afraid I have to move on to the TypeScript-cli-transpilation workflow. 我所有的项目仍在AngularJS 1.x上,恐怕我必须继续进行TypeScript-cli-transpilation工作流程。

I don't really understand it. 我不太了解 I always saw an inmense benefit of just being able to hit F5 and then see the result of the JavaScript code. 我总是看到能够击中F5然后查看JavaScript代码结果的巨大好处。

I don't understand how I can continue using that F5-based workflow when there is a compiler which needs to be invoked before every page reload. 我不明白当需要在每次重新加载页面之前调用一个编译器时,如何继续使用基于F5的工作流。

Am I missing something? 我想念什么吗? What is the workflow supposed to look like? 工作流程应该是什么样的?

This is not necessarily focused on Angular 2+, as other frameworks seem to want to be compiled as well. 由于其他框架似乎也希望进行编译,因此不一定要专注于Angular 2+。


About 90% of my JavaScript coding occurs inside an editor which has excellent SFTP-support, so I SFTP into the development machine and edit the HTML/JS live on this development server. 我约有90%的JavaScript编码发生在具有出色SFTP支持的编辑器中,因此我将SFTP插入开发机器并在此开发服务器上实时编辑HTML / JS。 I have at least three dozen software servers on about three machines, all with a very clean per-project/server directory structure, so I just have to select the directory in the editor and start editing the files, and reload them in the browser. 我在大约三台计算机上至少有三打软件服务器,每个服务器都有一个非常整洁的按项目/服务器目录结构,因此我只需要在编辑器中选择目录并开始编辑文件,然后将它们重新加载到浏览器中即可。

Switching to CLI would need to have the CLI-server adapt to the existing directory structure, have the CLI-server run alongside the server which actually hosts the HTML/JS-files. 切换到CLI将需要使CLI服务器适应现有的目录结构,并使CLI服务器与实际承载HTML / JS文件的服务器一起运行。 It then somehow needs to compile the modified files and put them into the directory where I usually just go ahead editing the file. 然后,它需要以某种方式编译修改后的文件,并将它们放到我通常只是继续编辑文件的目录中。

My structure usually is as follows 我的结构通常如下

/project1/data/log.txt
               uploaded.jpg
         /python/server.py
                /server_handler_1.py (auto-reloads)
                /server_handler_2.py (auto-reloads)
                /html/page1.html
                      page2.html
                /scripts/script1.js
                         script2.js
                /static/image1.png
                        image2.png

Then, when all is done, I push it via rsync to the destination server. 然后,完成所有操作后,我通过rsync将其推送到目标服务器。

I very seldomely code web projects on the local machine. 我非常有效率地在本地计算机上编写Web项目代码。 I use no IDE's or "modern" text-editors, mainly because of their lack of good SFTP support, but also because of their performance. 我不使用IDE或“现代”文本编辑器,主要是因为它们缺乏良好的SFTP支持,还因为它们的性能。

You can continue with the same workflow. 您可以继续相同的工作流程。 The typescript compiler (or, if you're using Angular, the Angular compiler -- which uses typescript compiler under the hood) has a 'watch' mode, so it compiles TypeScript files as you save them, meaning that you can hit F5 an see your changes. Typescript编译器(或者,如果您使用的是Angular,则Angular编译器-在后台使用Typescript编译器)具有“监视”模式,因此在保存它们时会编译TypeScript文件,这意味着您可以按F5查看您的更改。

In fact, if you're using the Angular CLI, it automatically updates the web page when you save a file and it compiles the result, so you don't even have to hit F5 at all. 实际上,如果您使用的是Angular CLI,则它会在保存文件时自动更新网页,并编译结果,因此您根本不需要按F5键。

And if you don't use Angular, you can start the TypeScript compiler in watch mode, and it'll compile each typescript file as you save it: 如果您不使用Angular,则可以在watch模式下启动TypeScript编译器,并在保存时编译每个Typescript文件:

tsc -w

So if that are your worries, you can forget them, and instead think of all the benefits of a typed language. 因此,如果您担心的是这些,则可以将其忘记,而不必考虑打字语言的所有好处。

If you are like me you get no joy in learning how to setup a project these days, but the good news is the workflow, once all configured correctly, has never been better. 如果您像我一样,这几天就不会学习如何设置项目,这是个好消息,但好消息是,工作流程一旦正确配置,再好不过了。

Compared to your original workflow you can layer up like so: 与原始工作流程相比,您可以像这样分层:

Compile in watch mode 在监视模式下编译

Use TypeScript's --watch compiler option : 使用TypeScript的--watch编译器选项

tsc --watch

Every time a source *.ts file is changed it will incrementally recompile the output *.js files. 每次更改源*.ts文件时,它将递增地重新编译输出*.js文件。 Now you can edit code and refresh your browser window and see the changes, just like before. 现在,您可以像以前一样编辑代码并刷新浏览器窗口并查看更改。

Use an IDE that compiles on save 使用可在保存时编译的IDE

Many IDEs with support for TypeScript, such as Visual Studio Code and Atom , can compile TypeScript to JavaScript every time you edit a file. 许多支持TypeScript的IDE,例如Visual Studio CodeAtom ,每次您编辑文件时都可以将TypeScript编译为JavaScript。 Again, now you can simply edit code and refresh browser like you did before, without even needing to use the command line. 同样,现在您可以像以前一样简单地编辑代码并刷新浏览器,甚至不需要使用命令行。

Use Webpack 使用Webpack

Webpack is a JavaScript bundler. Webpack是一个JavaScript捆绑器。 It will be quite intimidating at first, but it's powerful, flexible, and currently an industry standard. 刚开始时,它会令人生畏,但它功能强大,灵活,并且目前是行业标准。 You can think of it like a highly configurable build tool for browser development. 您可以将其视为用于浏览器开发的高度可配置的构建工具。 They have a guide for setting up TypeScript . 他们有设置TypeScript指南 Once you have configured webpack to handle TypeScript (using ts-loader ) you can run the webpack-dev-server . 一旦将webpack配置为处理TypeScript(使用ts-loader ),就可以运行webpack-dev-server Now code changes will not only incrementally recompile TypeScript on save (in memory, not on the file system), but it will also live-reload the browser window for you. 现在,代码更改不仅将在保存时(在内存中,而不是在文件系统上)以增量方式重新编译TypeScript,而且还将为您实时重新加载浏览器窗口。 Webpack has a lot of tricks, so the initial setup is well worth it. Webpack有很多技巧,因此初始设置非常值得。

(Angular) Use angular-cli (角度)使用angular-cli

For Angular projects they provide the angular-cli , a tool built on top of Webpack which makes the initial setup and development workflow even simpler. 对于Angular项目,他们提供了angular-cli (一种基于Webpack构建的工具),它使初始设置和开发工作流程更加简单。

(React) Use react-create-app (反应)使用react-create-app

Similarly for React projects they've built create-react-app , a tool to get up and running without much configuration. 类似地,对于React项目,他们构建了create-react-app ,该工具无需大量配置即可启动和运行。 It has a lot of options that simplify the setup, for example you can easily add Flow , a static type-system similar to TypeScript that works great with React, or you can use create-react-app-typescript which is a fork of the project that enables TypeScript (which also works great with React). 它有很多选项可以简化设置,例如,您可以轻松地添加Flow ,类似于TypeScript的静态类型系统,该类型系统可与React很好地配合使用,或者您可以使用create-react-app-typescript ,它是启用TypeScript的项目(也可以与React一起使用)。


Conclusion 结论

I know where you're coming from, I reminisce about the simple days of web development myself, but hopefully I've convinced you that there are some great tools to enable really good workflows today, even better than before! 我知道您来自哪里,我想起了Web开发的简单日子,但是希望我已经使您相信,有一些很棒的工具可以使今天真正好的工作流程变得比以前更好! This post actually only scratches the surface. 这篇文章实际上只是刮擦表面。

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

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