小编ray*_*ray的帖子

在webpack中创建全局功能

我正在使用webpack制作一个打字稿插件.一切都运作良好,但我有一个问题,从外面看到它.例如,我有文件:

/* ./src/a.ts */
class A {
    constructor(args) {...}
}
export default A;

/* ./src/app.ts */
import A from "./a.ts";
function init(args) {
   new A(args);
}
export { init };

/* ./index.html */
<html>
<head>...</head>
<body>
...

<!-- webpack bundle ts files into ./dist/app.js -->
<script src="./dist/app.js"></script>
<script>init({...});</script>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

有了这个,我得到了Uncaught ReferenceError: init is not defined.在捆绑文件中我可以看到这个函数不是全局的,但在其他函数中是这样的:

/* 1 */
/***/ function(module, exports) { ... }
Run Code Online (Sandbox Code Playgroud)

如何公开这个功能?

javascript typescript webpack

10
推荐指数
1
解决办法
7578
查看次数

标签 统计

javascript ×1

typescript ×1

webpack ×1