我想在打字稿中編寫我的前端代碼並希望導出代碼,即我的瀏覽器可以在 <script src="..."></script>
中加載代碼。在瀏覽器中使用的轉發打字稿
我通過browserify和tsify的方式做到了這一點。我的麻煩是,我的代碼不能在全局名稱空間中訪問。在<script>
標籤中加載腳本會執行它,當然,但是如果我打算將它加載爲可用於內聯<script>
或類似的函數庫?
更新
一個例子是
index.ts
function foo(): void {
console.log("bar");
}
具有以下CONF編譯產生以下的javascript
tsconfig.json
個{
"compilerOptions": {
"module": "UMD",
"target": "es5",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true
}
}
index.js
function foo() {
console.log("bar");
}
這是好的。但是,如果index.js輸入一些內容,例如我notification
功能,然後我得到這個
(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", "./notifications"], factory);
}
})(function (require, exports) {
"use strict";
var notifications_1 = require("./notifications");
notifications_1.notification("blerp");
function foo() {
console.log("bar");
}
});
現在foo
被包裹在某些範圍內,當我加載它在我的網站,不提供:在瀏覽器中爲<script src="require.js" data-main="index"></script>
foo is undefined
要清楚,你有多個打字稿文件,並且你正在使用browserfy來編譯它們。然後,你希望這些功能在全球範圍內可用? – vintem