单文件库使用 TypeScript 和 ES6 Modules 的可行性

Feasibility of using TypeScript and ES6 Modules for a single-file library

目前,我们的组织正在开发一个 JavaScript 库以用于我们的一个产品。目前,这是通过以下步骤开发的:

我们一直在考虑更新我们的代码库,我们正在关注 TypeScript 功能的最新更新,具体来说,转向使用单个 tsconfig.json 和 ES6 模块的使用。我承认我自己没有最全面的知识,但我遇到过以下问题:

我是不是漏掉了一些基础知识?使用 ES6 classes 来实现我们想要完成的事情是否可行,如果可行,建议的工作流程是什么(在工具方面?)或者我们是否需要继续使用命名空间 class 是这种要求的输出吗?

ES6 modules seem to be dependent on preserving a directory structure so this might mean we lose our namespaces

一般情况下不正确,但这就是您在使用 typescript 时使用它们的方式 "within a project"。

I'm not sure how we will later be able to access classes on an ES5 platform

使用像 --module commonjs 这样的模块模式编译成 ES5。使用 commonjs 模块(又名 NPM 模块)是一种成熟的模式,主要工作流工具都支持它,例如 webpackjspmbrowserify.

which our library does not really have as it is generally just a series of class declarations with functionality to interact with our organization's products.

从中创建一个 index.tsexportpublic api。这个文件就是你的入口点。