如何为多个环境管理 ASP.NET Core bundleconfig.json?

编程入门 行业动态 更新时间:2024-10-23 15:24:20
本文介绍了如何为多个环境管理 ASP.NET Core bundleconfig.json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在开发环境和生产环境中使用 ASP.NET Core bundleconfig.json 的最佳实践是什么?先前的捆绑程序 (BundleCollection) 会注意 DEBUG 编译器指令,并且在调试时不会缩小脚本列表.

What's the best practice for using the ASP.NET Core bundleconfig.json with development versus production environments? The prior bundler (BundleCollection) would pay attention to the DEBUG compiler directive and not minify the list of scripts when you're debugging.

新范例似乎是在测试 ASPNETCORE_ENVIRONMENT 值的 HTML 模板中包含 标记.虽然我没有看到将该环境变量合并到 bundleconfig.json 工作流程中的方法.

It appears as though the new paradigm is to have <environment> tags in the HTML templates that test for the ASPNETCORE_ENVIRONMENT value. Though I don't see a way of incorporating that environment variable into the bundleconfig.json workflow.

我看到的一种方法是为 bundleconfig.json 中的每个捆绑输出维护 2 个列表,缩小和非缩小版本,以便可以调试 JavaScript.或者,我可以在开发 <environment> 标记中放置指向未捆绑 JavaScript 的直接链接,然后在生产/暂存 <environment> 标记中引用捆绑和缩小的版本.

One way I see is to maintain 2 lists for each bundle output in bundleconfig.json, minified and non-minified versions, so that the JavaScript can be debugged. Alternatively I could put direct links to the unbundled JavaScript in the development <environment> tag and then reference the bundled and minified version in the production/staging <environment> tag.

无论哪种方式,都需要维护 2 个 JavaScript 文件列表(所有这些都适用于 CSS 文件).这对我来说似乎是倒退了一步,以前您只需要维护一个源文件列表,而 BundleCollection 只会在适当的时候缩小.

Either way there's a need to maintain 2 lists of JavaScript files (and all this goes for CSS files as well). That seems like a step backwards to me, where before you only needed to maintain a single list of source files and the the BundleCollection would only minify when appropriate.

我在这里遗漏了什么,还是我需要更进一步研究 Gulp 以便能够处理不同的环境?

Am I missing something here or do I need to go a step further and investigate Gulp to be able to handle the different environments?

推荐答案

我想我找到了答案.我正要创建一个 HTML 助手来读取开发环境的 bundleconfig.json,但似乎我不是第一个认为这是个好主意的人.请注意,.NET Core 实现链接到页面底部

I think I found my answer. I was about to create an HTML helper to read the bundleconfig.json for the development environment, but it appears I'm not the first one to think this was a good idea. Note that the .NET Core implementation is linked to at the bottom of the page

github/madskristensen/BundlerMinifier/wiki/解绑脚本进行调试

编辑

对于 .NET Core 实现,对 bundleconfig.json 的引用期望它位于/Configs 文件夹中,在您的项目中可能是这样,也可能不是.对我来说,我只是将它放在项目的根目录中.

For the .NET Core implementation, the reference to the bundleconfig.json was expecting it to be in a /Configs folder, which may or may not be the case in your project. For me, I just had it in the root of the project.

编辑

因此,如果源文件位于 wwwroot 文件夹之外,这将不起作用.将文件放在 wwwroot 文件夹之外是完全合理的,因此我正在研究让 html 帮助程序指向将在调试模式下流式传输文件的路径

So this doesn't work if the source files are outside of the wwwroot folder. Having files outside of the wwwroot folder is completely reasonable, so I'm investigating having the html helper point to a path that will stream the files in debug mode

可能的解决方案

这是我对解决方案的改进:

Here's my evolution of the solution:

gist.github/rupe120/512a9eb837383963f80fd9ef4984eb15

更新

我修改了我的解决方案以在路由定义中使用 {*filePath},因此现在无需对路径进行编码

I modified my solution to use {*filePath} in the route definition, so there's now no need to encode the path

更新

我认为这是我要做的最后一次重大更新.我用 bundleconfig.json 中的 outputFileName 值替换了静态基本路由字符串.因此,现在有尽可能多的调试路径,因为将有缩小文件,并且不用担心名称冲突.此外,您可以在调试时看到哪些文件包含在哪个包中,我认为这很酷.

I think this is the last major update I'll do. I replaced the static base route string with the outputFileName values from the bundleconfig.json. So now there's as many debug routes as there will be minified files and no fear what so ever of name collisions. Additionally you can see what files are included in which bundle when you're debugging, which I think is pretty cool.

更多推荐

如何为多个环境管理 ASP.NET Core bundleconfig.json?

本文发布于:2023-11-15 16:59:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1596297.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   环境管理   何为   ASP   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!