Reprint Basic introduction module of TypeScript (2)
Generate module code
According to the module target specified during compilation, the compiler will generate appropriate code for Node.js (CommonJS), require.js (AMD), UMD, SystemJS or ECMAScript 2015 native module (ES6) module loading system.
For more information about the define, require, and register calls in the generated code, see the documentation for each module loader.
The following simple example shows how names used during import and export are translated into module load code.
SimpleModule.ts
import m = require("mod"); export let t = m.something + 1;
AMD/RequireJS SimpleModule.js
define(["require", "exports", "./mod"], function (require, exports, mod_1) { exports.t = mod_1.something + 1; });
CommonJS/Node SimpleModule.js
var mod_1 = require("./mod"); exports.t = mod_1.something + 1;
UMD SimpleModule.js
(function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports", "./mod"], factory); } })(function (require, exports) { var mod_1 = require("./mod"); exports.t = mod_1.something + 1; });
System SimpleModule.js
System.register(["./mod"], function(exports_1) { var mod_1; var t; return { setters:[ function (mod_1_1) { mod_1 = mod_1_1; }], execute: function() { exports_1("t", t = mod_1.something + 1); } } });
Native ECMAScript 2015 modules SimpleModule.js
import { something } from "./mod"; export var t = something + 1;
Simple example
Next, we integrate the Validator implementation used in the previous [basic modules of TypeScript (I)] article to export only a single named export of each module.
To compile, we must specify the module target on the command line.
For Node.js, use -- module commonjs;
For require.js, use -- module amd. as follows
tsc --module commonjs Test.ts
At compile time, each module becomes a separate. js file.
As with reference tags, the compiler will follow the import statement to compile dependent files. Validation.ts
export interface StringValidator { isAcceptable(s: string): boolean; }
ZipCodeValidator.ts
import { StringValidator } from './Validation'; export const numberRegexp = /^[0-9]+$/; export class ZipCodeValidator implements StringValidator { isAcceptable(s: string): boolean { return s.length === 5 && numberRegexp.test(s); } }
LettersOnlyValidator.ts
import { StringValidator } from './Validation'; const letterRegexp = /^[A-Za-z]+/; export class LettersOnlyValidator implements StringValidator { isAcceptable(s: string): boolean { return letterRegexp.test(s); } }
Test.ts
import { StringValidator } from './Validation'; import { ZipCodeValidator } from './ZipCodeValidator'; import { LettersOnlyValidator } from './LettersOnlyValidator'; // test data let strings = ["Hello", "98052", "101"]; // let validators:{ [s: string]: StringValidator } = {}; validators["zip code validator"] = new ZipCodeValidator(); validators["letter validator"] = new LettersOnlyValidator(); strings.forEach((e) => { for (let name in validators) { console.log(`"${e}" - ${ validators[name].isAcceptable(e) ? "matches" : 'does not match'} ${name}`) } });
After compiling, run it to get the following results
"Hello" - does not match zip code validator "Hello" - matches letter validator "98052" - matches zip code validator "98052" - does not match letter validator "101" - does not match zip code validator "101" - does not match letter validator