如何将多个外部 Javascript 脚本合并为一个?
Posted
技术标签:
【中文标题】如何将多个外部 Javascript 脚本合并为一个?【英文标题】:How can I combining many external Javascript scripts into one? 【发布时间】:2020-01-14 21:18:21 【问题描述】:我使用 gtmetrix 检查我的网站,建议我的网站减少 HTTP 请求并减少 DNS 查找。所以我必须将许多外部javascript脚本组合成一个
在我的默认布局中是这样的
对于 css :
<link rel="shortcut icon" href="/content/assets/img/favicon.ico">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="/content/libs/materialize/css/materialize.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.1.6/css/swiper.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css">
<link rel="stylesheet" href="/content/assets/style/style.min.css?v=@currentVersion">
<link rel="stylesheet" href="/content/assets/style/style-responsive.min.css?v=@currentVersion">
<link rel="stylesheet" href="/content/assets/style/custom-form-materialize.css">
<link href="/Content/dist/social-share/sharetastic.css" rel="stylesheet" />
<link rel="stylesheet" href="/content/assets/style/happy.min.css?v=@currentVersion">
对于js:
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="/Content/dist/social-share/sharetastic.min.js"></script>
<script src="/Content/assets/script/jquery.twbsPagination.min.js"></script>
<script src="/Content/libs/materialize/js/materialize.min.js?v=@currentVersion"></script>
<script src="/Content/assets/script/select2.full.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.1.6/js/swiper.js"></script>
<script src="https://www.youtube.com/iframe_api"></script>
<script src="/Content/assets/script/rendercomponent.js"></script>
<script type="text/javascript" src="/Content/assets/script/script.min.js?v=@currentVersion"></script>
<script src="/Content/assets/script/share-content.js?v=@currentVersion"></script>
<script src="/Content/assets/script/jcanvas.min.js" type="text/javascript"></script>
<script src="/Content/assets/script/happy.min.js?v=@currentVersion" type="text/javascript"></script>
如果将它们合二为一不会影响网站? JS和CSS网站没问题吧?
【问题讨论】:
您正在寻找一个打包工具,也许是 Webpack? @CertainPerformance No. 我的网站使用 sitecore cms 这有什么关系? @Jared Smith 这是asp.net。似乎它无法在c#中实现 @SuccessMan 我可能不是 C#/ASP.NET 开发人员,但我是 know how to use google search... 【参考方案1】:成功人士的评论是正确答案。 Webpack 或其他捆绑器将允许您组合 js 文件并减少请求数量。
【讨论】:
可以在sitecore cms中实现吗? 你应该可以。你可以看看asmagin.com/2016/10/10/…以上是关于如何将多个外部 Javascript 脚本合并为一个?的主要内容,如果未能解决你的问题,请参考以下文章