捆绑JavaScript课程Uncaught SyntaxError:意外令牌< [英] Bundling JavaScript courses Uncaught SyntaxError: Unexpected token <

查看:123
本文介绍了捆绑JavaScript课程Uncaught SyntaxError:意外令牌<的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用mvc4课程的捆绑包功能

Using the bundle feature of mvc4 courses

未捕获到的SyntaxError:意外令牌<

Uncaught SyntaxError: Unexpected token <

在加载时.使用debug="true",一切都像例外.

on loading. With debug="true" everything is works like excepted.

我该如何解决错误或仅针对脚本禁用捆绑功能?

How can i solve the error or can i disable the bundle feature just for scripts?

已解决
重命名了捆绑包名称,使其与任何目录都不匹配

Solved
Renamed the bundle name to not match up with any directory

推荐答案

在回答引起此错误的原因的问题之前,必须首先弄清楚该错误发生在哪里.捆绑在一起时,代码语法的唯一区别是将代码最小化.一种非常简单的方法是使用Bundle代替ScriptBundle:

Before you can answer the question of what caused this error, you must first figure out where the error occurred. The only difference in the syntax of your code when bundled is that it is minified. A very simple way to do this is to use a Bundle instead of a ScriptBundle:

        var thirdParty = new Bundle("~/bundles/thirdParty").Include(
            "~/Scripts/jquery-{version}.js",
            "~/Scripts/bootstrap.js",
            "~/Scripts/jquery-ui-{version}.js",
            "~/Scripts/jquery.mockjson.js",
            "~/Scripts/jQuery.XDomainRequest.js",
            "~/Scripts/knockout-{version}.js"
            );

        thirdParty.Transforms.Clear();

        bundles.Add(thirdParty);

现在,如果您有多个JavaScript捆绑包,请一个接一个地对其进行处理,直到有了罪魁祸首捆绑包为止.

Now, if you have multiple JavaScript bundles, do this for them one by one until you have the culprit bundle.

我发现调试这些问题的唯一方法是将您的捆绑包分成两半以进一步分解:

The only way that I've found to debug these issues is to take your bundle and split it in half to break it down further:

        var thirdParty1 = new Bundle("~/bundles/thirdParty1").Include(
            "~/Scripts/jquery-{version}.js",
            "~/Scripts/bootstrap.js",
            "~/Scripts/jquery-ui-{version}.js"
            );

        bundles.Add(thirdParty1);

        var thirdParty2 = new ScriptBundle("~/bundles/thirdParty2").Include(
            "~/Scripts/jquery.mockjson.js",
            "~/Scripts/jQuery.XDomainRequest.js",
            "~/Scripts/knockout-{version}.js"
            );

        bundles.Add(thirdParty2);

请注意,我们仅禁用了两个捆绑包之一的缩小功能-thirdParty1.确保并更新您的@Scripts.Render以指向您的新捆绑包.当您生成并重新加载时,您将继续得到错误消息,或者您不会,但随后将知道哪一部分包含了麻烦的代码.但是请确保同时进行两种方法的测试,在我的示例中将thirdParty1最小化和将thirdParty2最小化,反之亦然,以确保没有其他事情发生.您可能还需要保留DevTools或打开的任何浏览器调试器,并查看包的来源以确保它们按预期运行.

Notice that we've only disabled minification for one of the two bundles - thirdParty1. Be sure and update your @Scripts.Render to point to your new bundles. When you build and reload, you will either continue to get the error, or you won't, and will then know which half contains the troublesome code. But be sure and test it both ways, minifying thirdParty1 and unminifying thirdParty2 in my example and vice-versa to be certain something else isn't going on. You also might want to keep DevTools or whatever browser debugger you have open and look at the source of your bundles to ensure they are acting as expected.

如果您有很多脚本,请一次从一个最小捆绑包(在本例中为thirdParty1)从一个非最小捆绑包(thirdParty2)中移走脚本,也可以逐块移动.记住要在两者之间进行重建,并注意不要更改脚本的包含顺序.

Continue by moving the scripts from the minified bundle (thirdParty1 in my case) from the unminified bundle (thirdParty2) either one at a time or in chunks, if you have a lot of scripts. Remember to rebuild in-between, and be careful not to change the inclusion order of your scripts.

这至少应该使您了解出现问题的文件-并希望搜索<"会得到您的答案.

That should at least get you down to the file that has the issue - and hopefully searching for "<" will get you your answer.

希望有帮助.

这篇关于捆绑JavaScript课程Uncaught SyntaxError:意外令牌&lt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆