gpt4 book ai didi

javascript - JSPM - 使用导入与使用脚本标签包含客户端库文件有什么优点/缺点吗?

转载 作者:行者123 更新时间:2023-11-29 16:53:26 24 4
gpt4 key购买 nike

我开始在我的 Aurelia Web 项目中使用 JSPM,我想知道使用 import "<client side library>" 是否有任何后果或优势。 ?

我在 JS 类中看到过这样的客户端库代码:

import "jquery";
import "bootstrap/css/bootstrap.css!"
import "bootstrap";

export class App {
constructor {

}
}

问题:以这种方式导入它与传统的包含 <script> 相比有什么区别/优点/缺点?和 <link> .html 中的标签文件?

<html>
<head>
<link rel="stylesheet" src="<bootstrap path>/bootstrap.css">
</head>
<body>

<script type="text/javascript" src="<bootstrap path>/bootstrap.js"></script>
</body>
</html>

我的试验和错误告诉我,通过使用 import在特定的类/js 文件中,它将库限制为该特定的 View 文件,而不是全局可用。

最终,当您为生产构建这些项目时,这些库不需要存在于 index.html 中吗?

最佳答案

我的意见是:你应该使用 import关键字,原因如下:

  • 风格问题:它是 ES6 风格,Aurelia 有点强制你使用它
  • 可读性很重要:其他开发人员希望您使用 import , 所以这可能会让他们感到困惑
  • 再次,import负责加载它,所以如果你只是添加一个脚本标签,你可能会添加一些 import然后可能会重新声明一些功能。
  • :我觉得100个不是很漂亮<script>代码中的标签
  • 生产捆绑。你打算用 Grunt 或 Gulp 连接和丑化所有这些吗?如果是这样,那就是额外的工作,因为在默认配置中它是开箱即用的。如果不……好吧,不,你应该这样做
  • Rollup 。最甜蜜的部分来了。

JSPM is awesome! It's a little different to this project though – it combines a repository with a package manager and a client-side module loader, as opposed to simply bundling modules. JSPM allows you to use any module format and even develop without a build step, so it's a great choice for creating applications. Rollup generates smaller bundles that don't use the complex SystemJS format, and so is a better choice for creating libraries. A future version of JSPM may use Rollup internally, so you'll get the best of both worlds.

Rollup 是使用 import 的另一个pro .它的作用是提取您使用的库并获取您需要的最少代码量,因此如果您只需要一些东西,比方说,jQuery,您不需要让您的访问者下载 50KB(或多少)现在?)。

您可以阅读有关 Rollup 的更多信息 here .

关于javascript - JSPM - 使用导入与使用脚本标签包含客户端库文件有什么优点/缺点吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34327133/

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