如何在纯JavaScript环境中使用UMD打字稿模块

时间:2016-11-24 08:21:19

标签: typescript

我希望typescript将我的* .ts文件转换为umd * .js文件。问题是我无法在纯JavaScript环境中使用我的模块,因为模块赢得了t be set into the window`对象。

tsconfig.json

{
  "compilerOptions": {
    "module": "umd",
    "removeComments": true,
    "preserveConstEnums": true,
    "sourceMap": true
  },
  "include": [
    "src/**/*.ts"
  ]
}

的src / Car.js

export default class Car {
    drive() {
        console.log('Driving');
    }
}

生成的js-File:

(function (dependencies, 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(dependencies, factory);
    }
})(["require", "exports"], function (require, exports) {
    "use strict";
    var Car = (function () {
        function Car() {
        }
        Car.prototype.drive = function () {
            console.log('Driving');
        };
        return Car;
    }());
    exports.__esModule = true;
    exports["default"] = Car;
});
//# sourceMappingURL=Car.js.map

在我的index.html中,我希望能够像这样调用驱动器:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Title</title>

    <script src="src/Car.js"></script>
</head>
<body>

    <script>
        // On document ready
        var car = new Car();
        car.drive();
    </script>

</body>
</html>

这可以用打字稿吗?你能提供一个例子吗?

编辑:在这篇文章中,我可以看到UMD TypeScript并不涵盖标准的js案例:https://github.com/Microsoft/TypeScript/issues/3322 这仍然是正确的吗?

0 个答案:

没有答案