0

I am very new to Bundling and minification, I try to implement it for the first time in my MVC project.

I have added a BundleConfig.cs file:

public class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        //libs scripts
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
        "~/Scripts/libs/jquery/jquery-{version}.js",
        "~/Scripts/libs/jquery/jquery-ui-{version}.js",
        "~/Scripts/libs/jquery/jquery.mask*",
        "~/Scripts/libs/jquery/jquery.validate*"));

        bundles.Add(new ScriptBundle("~/bundles/ko").Include(
        "~/Scripts/libs/ko/knockout-{version}.js"));

        //site scripts
        bundles.Add(new ScriptBundle("~/bundles/site").Include(
                    "~/Scripts/site/*.js"));

        bundles.Add(new StyleBundle("~/Content/site/").Include("~/Content/site/*.css"));
    }
}

And added in Global.asax:

BundleConfig.RegisterBundles(BundleTable.Bundles);

Then I rendered the scripts/css in my Layout page:

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title</title>
    @Styles.Render("~/Content/site/Fonts.css")
    @Styles.Render("~/Content/site/Site.css")
    @RenderSection("styles", required: false)

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/ko")
    @Scripts.Render("~/bundles/site")
    @RenderSection("scripts", required: false)
</head>

But this is not working, I keep getting all kind of errors that indicate that the scripts and css are not being recognized.

For example:

Uncaught ReferenceError: jQuery is not defined

What am I doing wrong?

user3378165
  • 6,546
  • 17
  • 62
  • 101

2 Answers2

1

As per @BasantaMatia comment that gave me the idea, setting:

BundleTable.EnableOptimizations = true

In the Global.asax file, after:

BundleConfig.RegisterBundles(BundleTable.Bundles);

Solved the issue.

user3378165
  • 6,546
  • 17
  • 62
  • 101
0

See here you are adding 2 times all the scripts files,

You already added reference your bundle jquery, ko and site.

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/ko")
@Scripts.Render("~/bundles/site")

Then there is no need to add again these files.

<script src="~/Scripts/Libs/jquery/jquery-3.1.1.min.js"></script>
<script src="~/scripts/libs/jquery/jquery-ui-1.12.1.min.js"></script>
<script src="~/Scripts/Libs/jquery-mask/jquery.mask.min.js"></script>
<script src="~/Scripts/Libs/knockout/knockout-3.4.1.js"></script>
<script src="~/Scripts/Site/Site.js"></script>
Basanta Matia
  • 1,504
  • 3
  • 15
  • 27