WebOptimizer-ASP.NET Core的捆绑器和压缩器

ASP.NET Core didn't have a runtime bundler like previous versions of ASP.NET. This was a bummer as I was a fan. Fortunately Mads Kristensen created one and put it on GitHub, called WebOptimizer.

ASP.NET Core没有像ASP.NET以前的版本那样的运行时捆绑程序。 我真是个迷,真是太可惜了。 幸运的是, Mads Kristensen创建了一个并将其放置在名为WebOptimizer的GitHub上

WebOptimizer - ASP.NET Core middleware for bundling and minification of CSS and JavaScript files at runtime. With full server-side and client-side caching to ensure high performance.

WebOptimizer-ASP.NET Core中间件,用于在运行时捆绑和缩小CSS和JavaScript文件。 具有完整的服务器端和客户端缓存,以确保高性能。

I'll try it out on a default ASP.NET Core 2.0 app.

我将在默认的ASP.NET Core 2.0应用程序上尝试一下。

First, assuming I've installed http://dot.net I'll run

首先,假设我已经安装了http://dot.net,我将运行


C:\Users\scott\Desktop> cd squishyweb

C:\Users\scott\Desktop\squishyweb> dotnet new mvc
The template "ASP.NET Core Web App (Model-View-Controller)" was created successfully.
This template contains technologies from parties other than Microsoft, see https://aka.ms/template-3pn for details.

SNIP

Restore succeeded.

Then I'll add a reference to the WebOptimizer package. Be sure to check the versioning and pick the one you want, or use the latest.

然后,我将添加对WebOptimizer包引用。 确保检查版本并选择所需的版本,或使用最新版本。

C:\Users\scott\Desktop\squishyweb> dotnet add package LigerShark.WebOptimizer.Core --version 1.0.178-beta 

Add the service in ConfigureServices and add it (I'll do it conditionally, only when in Production) in Configure. Notice I had to put it before UseStaticFiles() because I want it to get the first chance at those requests.

在ConfigureServices中添加服务,然后在Configure中添加它(我将有条件地进行此操作,仅在生产环境中)。 请注意,我必须将其放在UseStaticFiles()之前,因为我希望它能在这些请求中获得第一次机会。

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddWebOptimizer();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseWebOptimizer();
app.UseExceptionHandler("/Home/Error");
}

app.UseStaticFiles();

app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}

After running "dotnet run" I'll request site.css as an example and see it's automatically minimized:

运行“ dotnet run”后,我将以site.css为例,并看到它自动最小化:

CSS minification automatically

You can control the pipeline with globbing like this:

您可以通过如下方式控制管道:

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddWebOptimizer(pipeline =>
{
pipeline.MinifyJsFiles("js/a.js", "js/b.js", "js/c.js");
});
}

If I wanted to combine some files into an output "file" that'll be held/cached only in memory, I can do that also. To be clear, it'll never touch the disk, it's just a URL. Then I can just refer to it with a <link> within my Razor Page or main Layout.

如果我想将一些文件组合成仅在内存中保留/缓存的输出“文件”,我也可以这样做。 需要明确的是,它永远不会碰到磁盘,它只是一个URL。 然后,我可以在Razor页面或主布局中使用<link>引用它。

services.AddWebOptimizer(pipeline =>
{
pipeline.AddCssBundle("/css/mybundle.css", "css/*.css");
});

WebOptimizer also supports automatic "cache busting" with a ?v= query string created by a TagHelper. It can even compile Scss (Sass) files into CSS. There's plugins for TypeScript, Less, and Markdown too!

WebOptimizer还通过TagHelper创建的?v =查询字符串支持自动“缓存清除”。 它甚至可以将Scss(Sass)文件编译为CSS。 还有用于TypeScript,Less和Markdown的插件!

WebOptimizer is open source and lives at https://github.com/ligershark/WebOptimizer. Go check it out, kick the tires, and see if it meets your needs! Maybe get involved and make a fix or help with docs! There are already some open issues you could start helping with.

WebOptimizer是开源的,位于https://github.com/ligershark/WebOptimizer 。 去检查一下,踢一下轮胎,看看它是否满足您的需求! 也许参与其中,并提供有关文档的修复或帮助! 您已经可以解决一些悬而未决的问题

翻译自: https://www.hanselman.com/blog/weboptimizer-a-bundler-and-minifier-for-aspnet-core

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值