简体   繁体   English

Typescript 用于具有一个全局文件和单个页面文件的网站

[英]Typescript for website with one global file and individual page files

I'll preface this by saying that I've never used Typescript before, I'm tentatively experimenting with it because I really like the idea of "stricter JS".我先说我以前从未使用过 Typescript,我正在尝试使用它,因为我真的很喜欢“更严格的 JS”的想法。 However, my knowledge of it is extremely limited at this time.但是,目前我对它的了解非常有限。

I've basically built my own library of functions for my website.我基本上已经为我的网站构建了自己的函数库。 A "core.js" file that is built by concatenating the files in "core/*.js".通过连接“core/*.js”中的文件构建的“core.js”文件。 Each of those files would define properties on a global window.myLib object.这些文件中的每一个都将在全局window.myLib object 上定义属性。

Then, each page has its own JS file that handles events for that particular page.然后,每个页面都有自己的 JS 文件来处理该特定页面的事件。 They can call things from the global window.myLib .他们可以从全局window.myLib调用事物。

Each page then has two script files:然后每个页面都有两个脚本文件:

<script src="/core.js"></script>
<script src="/js/page_name_here.js"></script>

And this works quite nicely.这很好用。

I'm using VSCode for development and have set up a "globals.d.ts" file that lists what properties and functions are defined in the library, which allows Intellisense to provide very useful tooltips and hints on what is where.我正在使用 VSCode 进行开发,并设置了一个“globals.d.ts”文件,其中列出了库中定义的属性和函数,这允许 Intellisense 提供非常有用的工具提示和提示。

So I'm wanting to "level up" by actually going full Typescript with this, and having tsc compile my JS files.所以我想通过实际使用 Typescript 并让tsc编译我的 JS 文件来“升级”。

Step 1 would be compiling the library.第 1 步是编译库。 At the moment this is done PHP-side(,) by checking file modification times and, if necessary.目前这是通过检查文件修改时间并在必要时通过 PHP-side(,) 完成的。 concatenating the files and sending the result off to closure-compiler?连接文件并将结果发送到闭包编译器? I believe this is how I should do it in TS -- is this correct?我相信这就是我在 TS 中应该这样做的方式——这是正确的吗?

window.myLib = {}
window.myLib.eventHandlers = require("./core/event-handlers.ts")
window.myLib.AJAX = require("./core/ajax.ts")
// ...

If not, what is correct for building a file like this?如果不是,那么构建这样的文件是正确的?

Step 2 then is the individual page files.第 2 步是单独的页面文件。 I think since I've already done the work of defining "globals.d.ts" with the library functions, I don't have to do anything in particular to have those files be able to use library functions.既然我已经完成了使用库函数定义“globals.d.ts”的工作,我不需要做任何特别的事情来让这些文件能够使用库函数。 tsc should be able to read the definitions and use them whenever I refer to window.myLib . tsc应该能够阅读定义并在我提及window.myLib时使用它们。 Am I correct on that, or do I actually need to reference the core file in some way?我对此是否正确,还是我实际上需要以某种方式引用核心文件?

Any further pointers aside from these two questions would be much appreciated as well.除了这两个问题之外的任何进一步的指示也将不胜感激。 I'm very eager to try something new (to me) but there's a bit of a massive leap between "just write the JS and the browser will figure it out" and "you're writing for a compiler now!"我非常渴望尝试一些新的东西(对我来说),但是在“只需编写 JS,浏览器就会解决它”和“你现在正在为编译器编写!”之间有一点巨大的飞跃。

As for the 'TS ==> JS' compile step, I strongly suggest you take a look at what babel has to offer.至于 'TS ==> JS' 编译步骤,我强烈建议你看看babel提供了什么。 It plays quite nicely with the TS compiler through its plugin architecture, and can be integrated in most (if not all) modern build tools like webpack .它通过其插件架构与 TS 编译器配合得非常好,并且可以集成到大多数(如果不是全部)现代构建工具中,例如webpack

Warning : setting up a full webpack-with-babel-wih-typescript project can be very time-consuming and feel tedious.警告:设置一个完整的 webpack-with-babel-wih-typescript 项目可能非常耗时且乏味。 You might hit the point where you feel that things are getting overly complex, for you 'just' wanted to do a website in TypeScript.您可能会觉得事情变得过于复杂,因为您“只是”想在 TypeScript 中创建一个网站。

Not sure if you are doing this more as a learning project (where the quality of the final output is not the main concern), or if you want to use TypeScript and get real .不确定您是否更多地将其作为一个学习项目(最终 output 的质量不是主要问题),或者您是否想使用 TypeScript并获得真正的. In the latter case, I suggest taking a look at one of the popular frontend frameworks.在后一种情况下,我建议看一下流行的前端框架之一。 Even if you want to have a fully static site, they offer solutions to modularity, and offer a (more or less) out-of-the-box TypeScript experience.即使您想拥有完整的 static 站点,它们也提供模块化解决方案,并提供(或多或少)开箱即用的 TypeScript 体验。

You might want to take a look at:你可能想看看:

  • NuxtJs , which is based on the (somewhat easier-to-lean-than-react) Vue.js and also supports TypeScript NuxtJs ,它基于(比反应更容易精简) Vue.js并且还支持 TypeScript
  • Gatsbyjs , the react -equivalent to nuxtjs. Gatsbyjsreact -相当于 nuxtjs。 I have created a starter with TypeScript, eslint+prettier, and a decent vscode config.我用 TypeScript、eslint+prettier 和一个不错的 vscode 配置创建了一个启动器
  • Svelte , which claims to love TypeScript and has gained enough hype to be taken seriously. Svelte声称喜欢 TypeScript并获得了足够的炒作,值得认真对待。 Probably more fun than learning react, but not as mature.可能比学习反应更有趣,但没有那么成熟。
  • any of the other popular web frameworks that support TS.任何其他支持 TS 的流行 web 框架。 There are plenty.有很多。 Just probably don't code something yourself, if you care about your hours-to-output ratio.如果您关心您的工作时间与产出比,您可能不会自己编写代码。

Each of those frameworks handles modularity slightly different, but for me it all boils down to:这些框架中的每一个处理模块化略有不同,但对我来说,这一切都归结为:

  • write small, modular functions with no implicit dependencies (eg don't depend on functions living on the window object.) and a single responsibility.编写没有隐式依赖的小型模块化函数(例如,不依赖于window object 上的函数。)和单一职责。
  • import the functions (using TypeScripts import syntax ) wherever you need them in your components在组件中任何需要它们的地方导入函数(使用TypeScript 导入语法
  • let the framework do the rest, eg compiling, bundling, code splitting … the hard stuff让框架完成 rest,例如编译、捆绑、代码拆分……这些困难的事情

TL;DR: if you want to work on a (potentially) real project, use a framework. TL;DR:如果您想从事(可能)真正的项目,请使用框架。

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

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