从多个TypeScript类创建单个CommonJS模块

从多个TypeScript类创建单个CommonJS模块

本文介绍了从多个TypeScript类创建单个CommonJS模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出将应用程序拆分为几个CommonJS模块的最佳方法,这些模块可以被其他应用程序使用。

I'm trying to work out the best way to split my application into a few CommonJS modules that can be consumed by other applications.

我有5个TS类,我想将它们捆绑为一个CommonJS模块。然后,我打算将该模块发布到私有NPM存储库,以便其他应用程序可以使用它。理想情况下,我想将相关的* .d.ts定义文件打包在一起。

I have 5 TS classes, and I'd like to bundle them as a single CommonJS module. I then intend to publish this module to a private NPM repo, so it can be consumed by other applications. Ideally I'd like to package the relevant *.d.ts definition files with it.

执行此操作的最佳方法是什么?我正在使用外部TS模块,但是每个TS类都会产生一个单独的CommonJS模块。

What's the best way to do this? I'm using external TS modules, but these produce a separate CommonJS module per TS class.

推荐答案

据我所知打字稿还不支持组合外部模块。从他们在Codeplex上的Wiki中获取:

As far as i know typescript doesn't support combining external modules yet. From their wiki on codeplex:

但是,您可以通过在打字稿中使用内部模块来达到目的,因为tsc编译器可以将它们编译为单个文件,然后可以使用 module.exports 指令,使整个命名空间成为一个CommonJS模块。

However, you can do a trick by using internal modules in typescript, since the tsc compiler has the ability to compile them into a single file, and then you can just add one more file with a module.exports directive for the whole namespace to make it a CommonJS module.

这里是一个分步示例。假设您将以下内部模块分为三个文件:

Here is a step by step example. Let's say you have the following internal modules split into three files:

Validation.ts

module Validation {
    export interface StringValidator {
        isAcceptable(s: string): boolean;
    }
}

ZipCodeValidator.ts

/// <reference path="Validation.ts" />
module Validation {
    var numberRegexp = /^[0-9]+$/;
    export class ZipCodeValidator implements StringValidator {
        isAcceptable(s: string) {
            return s.length === 5 && numberRegexp.test(s);
        }
    }
}

LettersOnlyValidator.ts

/// <reference path="Validation.ts" />
module Validation {
    var lettersRegexp = /^[A-Za-z]+$/;
    export class LettersOnlyValidator implements StringValidator {
        isAcceptable(s: string) {
            return lettersRegexp.test(s);
        }
    }
}

如果使用with编译它们 tsc编译器中的--out参数,您可以将它们组合成一个文件。但是,这并不能使它们成为CommonJS模块。要导出它们,您可以使用技巧来添加一个名为 ValidationExport.ts 的ts文件,其中包含名称空间的导出指令:

If you compile these with with the --out parameter in the tsc compiler you can combine them into a single file. However, that doesn't make them a CommonJS module. To export them you use a trick to add one more ts file called ValidationExport.ts containing the export directive for the namespace:

var module: any = <any>module;
module.exports = Validation;

然后您可以运行tsc命令将所有内容编译为一个名为 validationmodule.js的文件:

And then you can run the tsc command to compile everything to a single file called "validationmodule.js":

tsc --out validationmodule.js Validation.ts ZipCodeValidator.ts LettersOnlyValidator.ts ValidationExport.ts

输出是可以在Node.js中使用的CommonJS模块:

The output is a CommonJS module you can use in Node.js:

var Validation = require("./validationmodule");

var zipCodeValidator = new Validation.ZipCodeValidator();
var lettersOnylValidator = new Validation.LettersOnlyValidator();

console.log(zipCodeValidator.isAcceptable("16211"));
console.log(lettersOnylValidator.isAcceptable("5555"));

这篇关于从多个TypeScript类创建单个CommonJS模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:06