Creating a single CommonJS module from several TypeScript classes

后端 未结 2 480
暖寄归人
暖寄归人 2021-02-08 03:21

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.

I have 5 TS classes, and I\'d like t

2条回答
  •  一生所求
    2021-02-08 04:00

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

    TypeScript has a one-to-one correspondence between external module source files and their emitted JS files. One effect of this is that it's not possible to use the --out compiler switch to concatenate multiple external module source files into a single JavaScript file.

    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

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

    LettersOnlyValidator.ts

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

    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 = module;
    module.exports = Validation;
    

    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
    

    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"));
    

提交回复
热议问题