-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathStartup.cs
211 lines (177 loc) · 8.48 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Smidge.Cache;
using Smidge.Options;
using Smidge.Models;
using Smidge.FileProcessors;
using Smidge.Nuglify;
using Microsoft.Extensions.Hosting;
using Smidge.InMemory;
using Microsoft.Extensions.FileProviders;
using System.IO;
namespace Smidge.Web
{
//public class DotlessPreProcessor : IPreProcessor
//{
// private readonly IHostingEnvironment _hostingEnvironment;
// public DotlessPreProcessor(IHostingEnvironment hostingEnvironment)
// {
// _hostingEnvironment = hostingEnvironment;
// }
// public async Task ProcessAsync(FileProcessContext fileProcessContext, PreProcessorDelegate next)
// {
// if (Path.GetExtension(fileProcessContext.WebFile.FilePath) == ".less")
// {
// var result = dotless.Core.Less.Parse(fileProcessContext.FileContent);
// fileProcessContext.Update(result);
// }
// await next(fileProcessContext);
// }
//}
public class Startup
{
// Entry point for the application.
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
public IConfigurationRoot Configuration { get; }
public IWebHostEnvironment CurrentEnvironment { get; }
/// <summary>
/// Constructor sets up the configuration - for our example we'll load in the config from appsettings.json with
/// a sub configuration value of 'smidge'
/// </summary>
/// <param name="env"></param>
public Startup(IWebHostEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
Configuration = builder.Build();
CurrentEnvironment = env;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSingleton<ISmidgeFileProvider>(f =>
{
var hostEnv = f.GetRequiredService<IWebHostEnvironment>();
return new SmidgeFileProvider(
hostEnv.WebRootFileProvider,
new PhysicalFileProvider(Path.Combine(hostEnv.ContentRootPath, "Smidge", "Static")));
});
// Or use services.AddSmidge() to test from smidge.json config.
services.AddSmidge(Configuration.GetSection("smidge"));
// We could replace a processor in the default pipeline like this
//services.Configure<SmidgeOptions>(opt =>
//{
// opt.PipelineFactory.OnCreateDefault = (type, pipeline) => pipeline.Replace<JsMinifier, NuglifyJs>(opt.PipelineFactory);
//});
// We could change a lot of defaults like this
services.Configure<SmidgeOptions>(options =>
{
//options.PipelineFactory.OnCreateDefault = (type, processors) =>
//options.FileWatchOptions.Enabled = true;
//options.PipelineFactory.OnCreateDefault = GetDefaultPipelineFactory;
options.DefaultBundleOptions.DebugOptions.SetCacheBusterType<AppDomainLifetimeCacheBuster>();
options.DefaultBundleOptions.ProductionOptions.SetCacheBusterType<AppDomainLifetimeCacheBuster>();
});
services.AddSmidgeNuglify();
services.AddSmidgeInMemory();
//services.AddSingleton<IPreProcessor, DotlessPreProcessor>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Add the following to the request pipeline only in development environment.
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// Add Error handling middleware which catches all application specific errors and
// sends the request to the following path or controller action.
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(CurrentEnvironment.ContentRootPath, "Smidge", "Static")),
RequestPath = "/smidge-static"
});
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "Default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
app.UseSmidge(bundles =>
{
//Create pre-defined bundles
//var lessPipeline = bundles.PipelineFactory.DefaultCss();
//lessPipeline.Processors.Insert(0, bundles.PipelineFactory.Resolve<DotlessPreProcessor>());
//bundles.CreateCss(
// "less-test",
// lessPipeline,
// "~/Css/test.less")
// .WithEnvironmentOptions(BundleEnvironmentOptions.Create()
// .ForDebug(builder => builder.EnableCompositeProcessing().SetCacheBusterType<AppDomainLifetimeCacheBuster>())
// .Build());
bundles.Create("test-bundle-1",
new JavaScriptFile("~/Js/Bundle1/a1.js"),
new JavaScriptFile("~/Js/Bundle1/a2.js"),
//NOTE: This is already min'd based on it's file name, therefore
// by convention JsMin should be removed
new JavaScriptFile("~/Js/Bundle1/a3.min.js"))
.WithEnvironmentOptions(bundles.DefaultBundleOptions)
.OnOrdering(collection =>
{
//return some custom ordering
return collection.OrderBy(x => x.FilePath);
});
bundles.CreateJs("test-bundle-2", "~/Js/Bundle2")
.WithEnvironmentOptions(BundleEnvironmentOptions.Create()
.ForDebug(builder => builder
.EnableCompositeProcessing()
.EnableFileWatcher()
.SetCacheBusterType<AppDomainLifetimeCacheBuster>()
.CacheControlOptions(enableEtag: false, cacheControlMaxAge: 0))
.Build()
);
bundles.Create("test-bundle-3", WebFileType.Js, "~/Js/Bundle2");
bundles.Create("test-bundle-4",
new CssFile("~/Css/Bundle1/a1.css"),
new CssFile("~/Css/Bundle1/a2.css"));
bundles.CreateJs("libs-js",
//Here we can change the default pipeline to use Nuglify for this single bundle
bundles.PipelineFactory.Create<NuglifyJs>(),
"~/Js/Libs/jquery-1.12.2.js", "~/Js/Libs/knockout-es5.js");
bundles.CreateCss("libs-css",
//Here we can change the default pipeline to use Nuglify for this single bundle (we'll replace the default)
bundles.PipelineFactory.DefaultCss().Replace<CssMinifier, NuglifyCss>(bundles.PipelineFactory),
"~/Css/Libs/font-awesome.css");
bundles.Create("test-bundle-10", new JavaScriptFile("~/test10.js")
{
RequestPath = "/smidge-static"
});
bundles
.CreateCss("notfound-map-css-bundle",
"~/Css/notFoundMap.min.css"
);
});
app.UseSmidgeNuglify();
}
}
}