如何将 <script type "module" >= 的 umd 导入浏览器,包括node_modules



我可以以某种方式转译打字稿代码并将其导入浏览器而无需 Webpack 吗?我在代码中使用node_modules,所以这让它有点复杂。

我尝试将其作为模块导入:

<script type="module">
   import * as foo from '../../dist/index.js';
   console.log(foo);
</script>

但它只导入了这个奇怪的东西:

Module {Symbol(Symbol.toStringTag): "Module"}

对我来说,最好的选择是将其转译为 umd,以便它可以普遍导入浏览器/作为node_module导入。

实际上这取决于导入文件的内容。

例如,如果 index.js如下所示:

export const x = "Hello World"
export function y() { console.log( "y()" ) }

然后,使用代码时,应使用 foo.x 访问 x 值,或使用foo.y()调用 y() 函数。

您还可以使用其他语法仅获取一个导出的对象:

<script type="module">
   import {x} from '../../dist/index.js';
   console.log(x);
</script>

最新更新