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

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

问题描述

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

<代码>{目录":wwwroot/lib"}

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

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

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

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

解决方案

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

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

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

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

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

<script src="~/lib/jquery-validation/jquery.validate.js"></script><script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script></环境><环境名称="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 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"></环境>

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

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

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

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"
}

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

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.

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?

解决方案

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.

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

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

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.

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>

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.

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.

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天全站免登陆