为什么要在 wwwroot 中安装 bower 组件 [英] Why are bower components installed in wwwroot

查看:22
本文介绍了为什么要在 wwwroot 中安装 bower 组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我使用 ASP.NET 5 以来已经有一段时间了,所以让我感到惊讶的是 bower 组件现在默认放在 wwwrootlib 文件夹中.这是因为 .bowerrc 文件:

It has been a while since I played with ASP.NET 5 so it was a surprise to me that bower components are now by default put in wwwrootlib folder. This is the case because of the .bowerrc file:

{
    "directory": "wwwroot/lib"
}

在早期版本中,bower 组件存储在 ./bower_components 文件夹中,这对我来说仍然更有意义.

In earlier releases the bower components are stored in the ./bower_components folder, which still makes more sense to me.

我希望我需要一个 gulp/grunt(例如使用wiredep)任务来构建我的JavaScript 和CSS 文件并将其复制到wwwroot 文件夹中.

I would expect that I need a gulp/grunt (with wiredep for example) task to build and copy my JavaScript and CSS files into wwwroot folder.

显然我遗漏了一些东西,但我无法理解它或找到任何关于此问题的合适信息.

Clearly I'm missing something, but I can't get my head around it or find any suitable information on this matter.

为什么我要将所有 Bower 组件(包括 sources)放在 `wwwrootlib' 文件夹中,尤其是在部署时,以及部署我的 Asp.NET 5 Web 应用程序所需的工作流程是什么?

Why do I want all my bower components (including sources) in the `wwwrootlib' folder, especially when deploying, and what is the desired workflow on deploying my Asp.NET 5 web application?

推荐答案

我认为 bower_components 文件夹被废弃而现在使用 wwwroot/lib 的原因是因为无论是在开发还是生产中,静态文件都需要位于 wwwroot 下,否则之后每次编辑文件都需要再次运行 taskrunner 以将文件复制到 wwwroot 下.如果文件的开发版本和生产版本都位于 wwwroot 以下的某个位置,那么这是一个更有效的工作流程.这样您就可以编辑和刷新页面,而不是编辑运行 taskrunner 然后刷新页面.

I think the reason the bower_components folder was abandoned and now uses wwwroot/lib is because no matter whether in dev or production static files need to live below wwwroot otherwise after every edit of a file you need to run taskrunner again to copy the file below wwwroot. It is a more efficient workflow if both the dev and production versions of the files live somewhere below wwwroot. that way you can edit and refresh the page rather than edit run taskrunner and then refresh the page.

我的建议是在创建文件的缩小/处理生产版本时将 grunt 进程文件放入不同的文件夹中,例如 wwwroot/js.

What I suggest is make grunt process files into a different folder like wwwroot/js when creating minified/processed production versions of your files.

然后 wwwroot/lib 文件夹甚至可以从发布中排除,因为只有开发版本的库脚本会存在.

Then the wwwroot/lib folder could even be excluded from publish since only dev versions of library scripts would live there.

我在想我自己的不是 Bower 组件的自定义脚本可能不应该存在于 wwwroot/lib 下,所以也许我将未缩小的脚本放在 wwwroot/dev 下,并在 wwwroot/js 下处理所有生产内容,以便在生产中我只部署具有生产版本缩小/组合文件的 wwwroot/js 文件夹.所以基本上我们就是这样制作自己的捆绑包.

I'm thinking my own custom scripts which are not bower components should probably not live under wwwroot/lib so maybe I put the unminified ones under wwwroot/dev and process all production stuff under wwwroot/js so that in production I only deploy the wwwroot/js folder which has the production version minified/combined files. So basically we make our own bundles that way.

新的环境标签和脚本 taghelper 可以轻松指向开发和生产的不同文件位置,如本示例所示:

The new environment tags and and the script taghelper make it possible to easily point to different file locations for dev and production as seen in this example:

<environment names="Development">
    <script src="~/lib/jquery-validation/jquery.validate.js"></script>
    <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
</environment>
<environment names="Staging,Production">
    <script src="//ajax.aspnetcdn.com/ajax/jquery.validation/1.11.1/jquery.validate.min.js"
           asp-fallback-src="~/js/lib/jquery-validation/jquery.validate.js"
           asp-fallback-test="window.jquery && window.jquery.validator">
    </script>
    <script src="//ajax.aspnetcdn.com/ajax/mvc/5.2.3/jquery.validate.unobtrusive.min.js"
           asp-fallback-src="~/js/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"
           asp-fallback-test="window.jquery && window.jquery.validator && window.jquery.validator.unobtrusive">
    </script>
</environment>

因此,您可以轻松地在生产环境中使用 CDN.请注意,对于非 cdn 文件,除了 wwwroot 或下面的某个文件夹之外,您不能指向任何地方,因此在 wwwroot 之外的 bower_components 文件夹中包含文件不是您可以指向脚本的位置,因此将文件放在那里没有意义.

So you have easy ways to use a cdn in production. Note that for non cdn files you cannot point anywhere other than wwwroot or some folder below that so having files in a bower_components folder outside of wwwroot is not a location where you can point to scripts so there is no point in putting files there.

在将脚本链接到我的自定义脚本的开发版本时,我喜欢使用新的 taghelper 属性 asp-append-version="true" 将文件内容的哈希附加到 url 确保以前的浏览器缓存是文件被编辑或更改时绕过.而且这种情况不需要运行 taskrunner,我只需编辑并刷新页面.

When making script links to the dev version of my custom scripts I like to use the new taghelper attribute asp-append-version="true" which appends a hash of the file contents to the url ensuring that the previous browser cache is bypassed any time the file is edited or changed. And this happens without any need of running taskrunner, I just edit and refresh the page.

因此,总而言之,将所有脚本放在 wwwroot 之下是一个更好的工作流程,而不是将它们放在其他地方并且需要在每次编辑后运行 taskrunner 来移动它们.如果您不想从 wwwroot/lib 下方部署所有额外的杂物,则使用 taskrunner 将您想要的内容处理到不同的文件夹中,就像它们在 bower_components 文件夹中的 wwwroot 之外时一样处于早期测试阶段.并在您的网络应用的 project.json 中使用 publishExclude 排除 wwwroot/lib 的发布.

So in summary having all the scripts below wwwroot is a better workflow than having them elsewhere and needing to run taskrunner to move them after every edit. If you don't want to deploy all the extra cruft from below wwwroot/lib then process what you want into a different folder with taskrunner, the same as you would have to do if they were outside of wwwroot in a bower_components folder as they used to be in early betas. And exclude wwwroot/lib from publishing with publishExclude in the project.json of your web app.

这篇关于为什么要在 wwwroot 中安装 bower 组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆