gpt4 book ai didi

npm - 使用 es6 样式导入导入 npm 模块而无需转译?

转载 作者:行者123 更新时间:2023-12-04 17:44:52 24 4
gpt4 key购买 nike

我想从我的代码中调用一个 npm 模块,它是一个 es6 模块。有没有办法在不转译或捆绑我的代码的情况下做到这一点?我不想转译的原因是为了简单起见,所以我可以在调试时立即在浏览器中看到我的代码更改。

最佳答案

您可以使用 script type="module" 在浏览器中使用 native ESM 模块。 .仅适用 for browsers that support it .
index.html

<html>
<head></head>
<body>
<script type="module" src="my-script.js"></script>
</body>
</html>
my-script.js
import {stuff} from './module1.js';
import Stuff from './module2.js';

console.log(Stuff);
console.log(stuff);
module1.js
export const stuff = {b: 1};
module2.js
export default {a: 1};

然后设置一个快速的 Web 服务器来查看页面的工作情况:
python -m SimpleHTTPServer 7654
也就是说,如果您的问题是在每次更改时刷新代码并在 ES6 开发模式下调试它,我推荐 sourceMaps作为解决方案。与 sourceMaps在 ES6 中调试开发版本时,您可以看到您的代码在生产中已编译(或根据需要“转译”)。 Webpack(或替代品)现在非常优化,可以进行部分编译 very quickly每次保存时重新加载浏览器。

关于npm - 使用 es6 样式导入导入 npm 模块而无需转译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52565097/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com