[英]Export object from typescript through webpack and use it in JS
我想要做的是將我在Typescript中編寫的庫與Webpack 2捆綁在一起,甚至可以在香草JS中使用該庫。 建築物運行良好且沒有錯誤,但是在使用導出的對象時,它看起來像一個空對象{}
。
因此,我嘗試執行以下操作:
bundle.js
和my-js-script.js
文件 import {SomeStuff} from 'some-stuff';
export class ClassA {
constructor(){
// Does other stuff with SomeStuff
}
}
// What I want to achieve is something like this
export const myModule = new ClassA();
我認為創建一個導入捆綁包和my-js-script文件的html文件足以訪問myModule
常量。 但是不幸的是,這還不夠。
<html>
<head>
<script src="./bundle.js"></script>
<script src="./my-js-script.js"></script>
</head>
<body>
</body>
</html>
myModule.doSomething();
我想念什么? 還是根本沒有機會這樣做? Webpack配置非常簡單
var path = require("path");
module.exports = {
entry: "./src/web-onion.ts",
output: {
path: path.resolve(__dirname, 'dist/'),
filename: "bundle.js"
},
resolve: {
extensions: [".webpack.js", ".web.js", ".ts", ".tsx", ".js"]
},
module: {
loaders: [
{ test: /\.tsx?$/, loader: "ts-loader" }
]
}
}
你不能這樣子。 myModule
不會作為全局導出。 如果要全局導出某些內容,請在TypeScript文件中使用:
window.something = myModule
注意:您將不得不擴展Window界面或使用(window as any)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.