Typescript AMD 目标解析为 CommonJS

Typescript AMD Target Resolving to CommonJS

我的项目中有一个 tsconfig,它指定 'amd' 的模块目标,但是当我的文件编译时,我得到的输出看起来更像 CommonJS。示例:

tsconfig:

{
    "compilerOptions": {
        "module": "amd",
        "target": "es5",
        "moduleResolution": "node",
        "sourceMap": false,
        "newLine": "LF",
        "baseUrl": ".",
        "lib": ["es5", "es2015.promise", "dom"]
    }
}

打字稿文件:

export function test() {
    console.log('Starting Up', '<--------------');
}

编译文件:

define(["require", "exports"], function (require, exports) {
    Object.defineProperty(exports, "__esModule", { value: true });
    function test() {
        console.log('Starting Up', '<--------------');
    }
    exports.test = test;
});

预期的编译文件:

define([], function () {
    function test() {
        console.log('Starting Up', '<--------------');
    }
    return { test: test };
});

让我失望的是 'export' 对象。这对于 AMD 模块来说应该不是必需的,只有 return 语句。有办法纠正这个问题吗?

不幸的是没有。那是 TypeScript 的 AMD 输出的形状,它是 AMD 兼容的。 AMD 提供此功能,TypeScript 使用它。