AspNetCoreWebBundler 1.0.0-preview2
dotnet add package AspNetCoreWebBundler --version 1.0.0-preview2
NuGet\Install-Package AspNetCoreWebBundler -Version 1.0.0-preview2
<PackageReference Include="AspNetCoreWebBundler" Version="1.0.0-preview2" />
paket add AspNetCoreWebBundler --version 1.0.0-preview2
#r "nuget: AspNetCoreWebBundler, 1.0.0-preview2"
// Install AspNetCoreWebBundler as a Cake Addin #addin nuget:?package=AspNetCoreWebBundler&version=1.0.0-preview2&prerelease // Install AspNetCoreWebBundler as a Cake Tool #tool nuget:?package=AspNetCoreWebBundler&version=1.0.0-preview2&prerelease
ASP.NET Core Web Bundler (Preview)
A NuGet Package for CSS, JavaScript, and HTML bundling and minification at build time and runtime during development.
Features
- Combine CSS, JavaScript, and HTML files into one or more output files.
- Minify one or multiple files into one or more output files.
- Bundle files at build time using MSBuild Tasks.
- Automatically bundle files during
Debug
mode. Check here.
Setup
Add the NuGet package to your project.
Since this package is primarily required at compilation time, set PrivateAssets="All" in the package reference. This ensures that the package does not get included in the output during the publishing process, keeping the bin folder clean.
<ItemGroup>
<PackageReference Include="AspNetCoreWebBundler" Version="x.y.z" PrivateAssets="All" />
</ItemGroup>
Add the configuration AspNetCoreWebBundler.json
file in project root directory. To separate paths in segments /
should be used.
Example of a configuration file:
[
// creates 2 javascript files, one bundled and one minified
{
"dest": "wwwroot/js/dist/all.js",
"src": [
"wwwroot/js/src/site.js",
"wwwroot/js/src/**/*.js"
],
"minify": {
"enabled": true // this is by default
},
"sourceMap": false
},
// creates 2 css files, one bundled and one minified
{
"dest": "wwwroot/css/dist/all.css",
"src": [
"wwwroot/css/src/site.css",
"wwwroot/css/src/**/*.css",
]
},
// creates multiple files (bundled and minified)
{
"dest": "wwwroot/js/dist2/",
"src": [
"wwwroot/js/src2/site.js",
"wwwroot/js/src2/**/*.js"
]
},
// creates multiple files (minified only) preserving the source directory tree
{
"dest": "wwwroot/js/dist2/**/*.min.js",
"src": [
"wwwroot/js/src2/**/*.js"
]
}
]
Configuration specs
In the AspNetCoreWebBundler.json configuration file, all paths specified for source (src) and destination (dest) files are relative to the location of the configuration file itself. This means that the paths should be defined based on the directory structure starting from where the AspNetCoreWebBundler.json file resides.
dest
(string
)
The output to generate. Can be a file, a directory or a glob pattern.
src
(string[]
)
An array of files to bundle together. Can contain a file, a directory or a glob pattern.
minify
(object
)
Minification options for the output type.
enabled
- Enable or disable the minification process. Default istrue
.gZip
- Compress bundle as gzip. If the minification is enabled, only the minified file will be compressed. Default isfalse
.
Other options differ depending on the type of input files (JavaScript, CSS or HTML).
JavaScript
termSemicolons
(bool
) - Add a semicolon at the end of the parsed code. Default isfalse
.alwaysEscapeNonAscii
(bool
) - Always escape non-ASCII characters as \uXXXX or to let the output encoding object handle that via the JsEncoderFallback object for the specified output encoding format. Default isfalse
(let the Encoding object handle it).preserveImportantComments
(bool
) - Remove all comments except those marked as important. Default istrue
.evalTreatment
(string
) - Settings for how to treat eval statements. Default isignore
.ignore
- Ignore all eval statements (default). This assumes that code that is eval'd will not attempt to access any local variables or functions, as those variables and function may be renamed.makeImmediateSafe
- Assume any code that is eval'd will attempt to access local variables and functions declared in the same scope as the eval statement. This will turn off local variable and function renaming in any scope that contains an eval statement.makeAllSafe
- Assume that any local variable or function in any accessible scope chain may be referenced by code that is eval'd. This will turn off local variable and function renaming for all scopes that contain an eval statement, and all their parent scopes up the chain to the global scope.
CSS
adjustRelativePaths
(bool
) - Adjusts the paths based on the output file position. Default istrue
.termSemicolons
(bool
) - Add a semicolon at the end of the parsed code. Default isfalse
.decodeEscapes
(bool
) - Unicode escape strings (eg. '\ff0e') should be replaced by it's actual character or not. Default is true. Default istrue
.colorNames
(string
) - How to treat known color names. Default isstrict
.strict
- Convert strict names to hex values if shorter; hex values to strict names if shorter. Leave all other color names or hex values as-specified.hex
- Always use hex values; do not convert any hex values to color names.major
- Convert known hex values to major-browser color names if shorter; and known major-browser color names to hex if shorter.noSwap
- Don't swap names for hex or hex for names, whether or not one is shorter.
preserveImportantComments
(bool
) - Remove all comments except those marked as important. Default istrue
.
HTML
attributesCaseSensitive
(bool
) - Treat attributes as case sensitive. Default isfalse
.tagsCaseSensitive
(bool
) - Treat tag names as case sensitive. Default isfalse
.collapseWhitespaces
(bool
) - Collapse whitespaces. Default istrue
.removeComments
(bool
) - Remove all comments (except those marked as important). Default istrue
.removeAttributeQuotes
(bool
) - Remove the quotes around attributes when possible. Default istrue
.removeOptionalTags
(bool
) - Remove optional tags (e.g:</p>
or</li>
). Default isfalse
.decodeEntityCharacters
(bool
) - Decode entity characters to their shorter character equivalents. Default istrue
.shortBooleanAttribute
(bool
) - use the short version of a boolean attribute if value is true. Default istrue
.isFragmentOnly
(bool
) - The parsing is occuring on an HTML fragment to avoid creating missing tags (like html, body, head). Default istrue
.minifyJs
(bool
) - Minify js inside <script> tags. Default istrue
.minifyJsAttributes
(bool
) - Minify js inside JS event attributes (e.g. onclick, onfocus). Default istrue
.minifyCss
(bool
) - Minify css inside <style> tags. Default istrue
.minifyCssAttributes
(bool
) - Minify css inside style attribute. Default istrue
.keepOneSpaceWhenCollapsing
(bool
) - Keep one space when collapsing multiple adjacent whitespace characters. Default isfalse
.indent
(string
) - The string used for one level of indent. Default is two spaces.
sourceMap
(bool
)
If true
, will generate a source map for the bundled file. Default is false
.
sourceMapRootPath
(string
)
Source root URI that will be added to the map object as the sourceRoot property.
Development
To enable runtime bundling support during development, you need to modify the ConfigureServices method.
using AspNetCoreWebBundler;
...
public void ConfigureServices(IServiceCollection services)
{
#if DEBUG
// Add runtime bundling services only during Debug mode
services.AddRuntimeWebBundler();
#endif
// Other service configurations...
}
AddRuntimeWebBundler
: This method sets up an IHostedService
that watches for changes in the source files (CSS, JavaScript and HTML) within the solution directories.
It specifically monitors projects directories within the solution containing configuration files.
To disable the bundling/minification process, you can include the following property in your .csproj file:
<PropertyGroup>
<EnableAspNetCoreWebBundler>false</EnableAspNetCoreWebBundler>
</PropertyGroup>
DesignTimeBuild
By default the Bundler is disabled on DesignTimeBuild. To enable it include the following property in your .csproj file:
<PropertyGroup>
<EnableAspNetCoreWebBundlerDesignTimeBuild>true</EnableAspNetCoreWebBundlerDesignTimeBuild>
</PropertyGroup>
TODO
- More tests
License
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- Newtonsoft.Json (>= 13.0.3)
- NUglify (>= 1.21.9)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0-preview2 | 95 | 8/12/2024 |
1.0.0-preview1 | 60 | 8/3/2024 |