TypeScript 包管理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12715020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:12:03  来源:igfitidea点击:

TypeScript package management

typescript

提问by Abdullah Jibaly

Looks like TypeScript has a nice module system, however does this replace the need for something like requirejs? In other words, when you "compile" a typescript package does it handle all the dependency resolution for you? Examples would be appreciated!

看起来 TypeScript 有一个不错的模块系统,但是这是否取代了对 requirejs 之类的需求?换句话说,当您“编译”一个打字稿包时,它是否会为您处理所有依赖项解析?示例将不胜感激!

回答by mohamed hegazy

TypeScript does not have a runtime module loader. You will still need to provide a module loader to use at runtime, e.g. require js. TypeScript supports generating JavaScript code compatable with either commonJS (for node.js scripts) and AMD loaders (e.g. requireJS). To specify which one to use pass in the "--module " switch to the compiler with either "amd" or "commonjs".

TypeScript 没有运行时模块加载器。您仍然需要提供一个在运行时使用的模块加载器,例如 require js。TypeScript 支持生成与 commonJS(用于 node.js 脚本)和 AMD 加载器(例如 requireJS)兼容的 JavaScript 代码。要指定使用哪个,请使用“amd”或“commonjs”将“--module”切换到编译器。

Here is how you export a module in TypeScript:

以下是在 TypeScript 中导出模块的方法:

export module depModule { 
    export class A { 
    }
}

and here is the generated JavaScript code with --module amd switch:

这是使用 --module amd 开关生成的 JavaScript 代码:

define(["require", "exports"], function(require, exports) {
    (function (depModule) {
        var A = (function () {
            function A() { }
            return A;
        })();
        depModule.A = A;
    })(exports.depModule || (exports.depModule = {}));
})

回答by Brian Terlson

TypeScript can emit code that will work with both AMD-compatible dependency loaders like requirejs or commonjs environments like Node. You can control this with the --module option to the compiler. The compiler itself doesn't handle dependency resolution at runtime, that's up to you to work out, but it does attempt resolve dependencies at compile time so it can give you type information.

TypeScript 可以发出可以与 AMD 兼容的依赖加载器(如 requirejs)或 commonjs 环境(如 Node.js)一起使用的代码。您可以使用编译器的 --module 选项来控制它。编译器本身不会在运行时处理依赖项解析,这取决于您自己解决,但它确实会在编译时尝试解析依赖项,因此它可以为您提供类型信息。

You can see examples of projects that use modules in the test directory at CodePlex, this onefor example. The output of compiling these test projects are .js files that will be loadable either with, for example, requirejs (if you pass --module amd) or Node (if you pass --module commonjs).

您可以在 CodePlex 的 test 目录中看到使用模块的项目示例,例如这个。编译这些测试项目的输出是 .js 文件,这些文件可以使用 requirejs(如果您通过 --module amd)或 Node(如果您通过 --module commonjs)加载。

回答by Peter StJ

If your application will be composed only by typescript modules / files it is possible to not use module loaders or other dependency management. You will need to include all dependencies with comment references and use the --out option of the compiler. Eg:

如果您的应用程序仅由打字稿模块/文件组成,则可能不使用模块加载器或其他依赖项管理。您需要使用注释引用包含所有依赖项,并使用编译器的 --out 选项。例如:

tsc --out compiled.js app.ts

This will put in compiled.js the generated js for app.ts and all its dependencies, including dependencies of dependencies. The resulting file can then be included directly in the html file with the script tag. It can also be minified. Module loading on runtime is not supported

这将在compiled.js 中放入为app.ts 生成的js 及其所有依赖项,包括依赖项的依赖项。然后可以将生成的文件直接包含在带有脚本标记的 html 文件中。它也可以缩小。不支持在运行时加载模块