Bundler and Minifier Documentation

repository·master·Indexed 20 days ago

https://github.com/madskristensen/bundlerminifier

A Visual Studio extension and CLI tool for automating the bundling and minification of CSS, JavaScript, and HTML files. It uses a bundleconfig.json file for configuration, supports real-time editing, MSBuild integration for CI/CD, and provides functionality to convert configurations to Gulp workflows. Features include globbing patterns, JavaScript source maps, and a dedicated CLI for processing assets and watching for file changes.

Tokens
3.5K
Snippets
6
Records
21
Agent score
71%

What's inside Bundler and Minifier

  1. Create a bundle in Visual Studio

    master

    To create a bundle, select two or more files of the same type (CSS, JS, or HTML) in the Solution Explorer, right-click, and select the bundling option. The configuration is automatically saved to a bundleconfig.json file at the project root. Any edits to the source files will instantly trigger an update to the bundle output.

    // Configuration is stored in bundleconfig.json at the project root
  2. Convert bundleconfig.json to Gulp

    master

    You can automatically generate a Gulp workflow based on your existing bundleconfig.json. This feature will:

    1. Create a gulpfile.js (if it doesn't exist).
    2. Create a package.json (if it doesn't exist).
    3. Install the necessary Node modules via npm.

    The resulting gulpfile.js uses the paths from bundleconfig.json but executes the bundling/minification using standard Gulp plugins.

  3. Configure source maps for JavaScript

    master

    Source maps are currently supported for JavaScript minification only. A .map file is automatically generated next to the .min.js file.

    Warning: If you manually delete a .map file, it will not be recreated on subsequent minifications unless explicitly enabled in the configuration.

    To ensure source maps are enabled, add the "sourceMap": true property to your bundle configuration in bundleconfig.json.

    {
      "outputFileName": "output/all.js",
      "inputFiles": [ "js/*.js" ],
      "sourceMap": true
    }
  4. Minify individual files

    master
    To minify a single JS, CSS, or HTML file, right-click the file in Solution Explorer and select the minify option. This creates a new file with the pattern _[filename].min.[ext]_ nested under the original file. The minified file is automatically updated whenever the source file is modified.
  5. Suppress output file generation

    master

    If you want to use another tool (like Gulp or server-side code) to handle the actual bundling/minification while still using the bundleconfig.json for configuration, you can prevent the extension from generating output files.

    To do this, right-click bundleconfig.json and uncheck the option to enable output generation.

  6. Enable bundling and minification for CI/Build steps

    master

    In ASP.NET MVC and WebForms projects, you can integrate bundling and minification into your MSBuild process for CI/CD scenarios.

    1. Right-click the bundleconfig.json file in Solution Explorer.
    2. Select the option to enable bundling/minification on build.

    This installs a NuGet package into your packages folder containing an MSBuild task that runs the same compilers used by the extension.

    Note: For ASP.NET Core projects, refer to the project wiki.

  7. How Task Runner Explorer manages bindings for bundleconfig.json

    master

    When using the Visual Studio Task Runner Explorer with BundlerMinifier, the tool manages configuration bindings using a sidecar file named <configPath>.bindings (e.g., bundleconfig.json.bindings).

    • Loading: The tool looks for a line starting with ///<binding within the .bindings file to retrieve the XML binding configuration. If no such line exists or the file is missing, it defaults to <binding />.
    • Saving: When bindings are updated, the tool writes the XML to the .bindings file, prefixing it with ///. This file is treated as a nested file under the primary bundleconfig.json in the Visual Studio Solution Explorer.
    • Persistence: The tool attempts to perform a silent save of the primary configuration file if it is not already marked as dirty to ensure the project state remains consistent.
  8. Enable Bundle on Build in Visual Studio

    master

    The Bundler and Minifier Visual Studio extension provides a command to automatically trigger bundling and minification during the build process.

    To use this feature:

    1. Select your bundleconfig.json file in the Solution Explorer.
    2. Use the command (typically found in the context menu or via the extension's menu) to toggle the 'Bundle on Build' setting.

    Requirements & Constraints:

    • The command is only visible and enabled when a bundleconfig.json file is selected.
    • It is not supported for WEBSITE_PROJECT or ASPNET_5 project types, as these project types cannot execute the necessary build tasks.
    • If the required NuGet package is not already installed in your project, the extension will prompt you to install it via NuGet.
  9. Clear generated output files in Visual Studio

    master

    The ClearOutputFiles command allows you to delete all generated bundle and minification output files associated with a bundleconfig.json file.

    Usage Requirements

    To enable the command in the Visual Studio menu, one of the following must be true:

    1. Project Level: You have an active project that contains a valid bundleconfig.json file.
    2. File Level: You have a single file selected in the Solution Explorer, and that file is a bundleconfig.json file.

    If these conditions are not met, the command will be hidden or disabled in the Visual Studio UI.

  10. Suppress or produce output files in Visual Studio

    master

    Within the Visual Studio environment, you can toggle whether BundlerMinifier generates output files based on your bundleconfig.json settings. This is controlled via a command in the Visual Studio menu.

    Usage Behavior

    • When enabled (Checked): The tool will generate the minified/bundled output files as defined in your configuration.
    • When disabled (Unchecked): The tool will suppress the generation of output files.

    Command Availability

    • The command is only visible and enabled if a valid bundleconfig.json file is detected in the active project or if a single bundleconfig.json file is currently selected in the Solution Explorer.
    • If no project is active or no configuration file is found, the command is hidden.