简体   繁体   English

如何从Javascript中按名称导入Typescript生成的模块?

[英]How can I import a typescript-generated module by name from within Javascript?

I want to import a function from a typescript-generated module into javascript's global namespace. 我想将一个由打字稿生成的模块中的函数导入到javascript的全局命名空间中。 Here is my typescript module foo.ts : 这是我的打字稿模块foo.ts

export const fooFn = (): string => {
  return "hello";
};

Here is my HTML file: 这是我的HTML文件:

<html>
<body>
    <script src="foo.ts"></script>
    <script type="application/javascript">
        window.fooFn = require("./foo.ts").fooFn;
        alert(fooFn());
    </script>
</body>
</html>

This is giving me the error Cannot find module './foo.ts' . 这给了我Cannot find module './foo.ts'的错误。 After messing around a bit, I've found I can import it using a parcel-generated index (eg, window.fooFn = require(7).fooFn ) but obviously this is not a workable solution, and the index changes every time I restart the parcel dev server. 经过一番混乱之后,我发现我可以使用包裹生成的索引(例如, window.fooFn = require(7).fooFn )将其导入,但是显然这不是可行的解决方案,并且每次我更改索引重新启动包裹开发服务器。

So, my question is, how can I import this module by name? 因此,我的问题是,如何按名称导入此模块?

Here is my tsconfig.json for reference: 这是我的tsconfig.json供参考:

{
  "compilerOptions": {
    "strict": true,
    "target": "es2015",
    "jsx": "react",
  }
}

If you're wondering why I want to expose this function on the global namespace, it's so that I can easily access it from within a WebAssembly application. 如果您想知道为什么要在全局名称空间中公开此函数,那么我可以从WebAssembly应用程序中轻松访问它。

This is still an unsolved issue . 这仍然是一个未解决的问题

Until TypeScript supports a dedicated compilation target you're only options to use a module bundler. 在TypeScript支持专用的编译目标之前,您只能选择使用模块捆绑程序。

The most popular ones these days are webpack , rollup or parcel . 这些天最流行的是webpack汇总包裹

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UglifyJS不会破坏TypeScript生成的JavaScript中的函数名称 - UglifyJS does not mangle function names in TypeScript-generated JavaScript 如何将javascript AMD模块导入外部TypeScript模块? - How can I import a javascript AMD module into an external TypeScript module? 如何在我的Typescript生成的js中调用函数 - How to call a function in my typescript-generated js 回复:如何将javascript AMD模块导入外部TypeScript模块? (使用module.exports =…时) - Re: How can I import a javascript AMD module into an external TypeScript module? (When using module.exports=…) 如何在Typescript中导入Javascript模块 - How to import Javascript module in Typescript 我可以通过编程方式从Javascript / Typescript中的一个函数中以相同的名称调用另一个函数吗? - Can I programmatically call another function with the same name from within a function in Javascript/Typescript? 如何在使用Google protobuf时调用陷入“System.register()”模块中的javascript函数(由typescript生成)? - How to invoke a javascript function (generated from typescript) trapped within “System.register()” module while using Google protobuf? 如何从JavaScript模块中访问“ this”? - How can I access “this” from within a JavaScript module? 如何在打字稿中导入javascript节点模块? - How to import javascript node module in typescript? 如何在WebStorm中查看从打字稿生成的JavaScript? - how do i view generated javascript from typescript in WebStorm?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM