locked
Bundle javascript, lambda and default parameter value RRS feed

  • Question

  • User220660661 posted

    Hi,

    Bundles seems to fail if a private lambda function with default parameter value is in a javascript file. The problem appears both in vs2017 and 2019 (latest versions).

    Step to reproduce:

     - Create a simple javascript file with this content:

    var test={};
    test.subNameSpace={
        test:() => {
            var sub=(info=false) => { console.log(`info: ${info}`) };
            sub(true);
        }
    }
    window.onload=function() {
        test.subNameSpace.test();
    }

    Add this file in the page with Bundles.

    Publish the site.

    The generated javascript file is wrong; the error is "Uncaught SyntaxError: Malformed arrow function parameter list" and the generated file contains this text:

    [...]  test.subNameSpace={test:()=>{var n=n=false=>{ [...]

    That is correctly marked as an error from the browser (var n=n=false=>{ is not a correct javascript syntax).

    Obviously I could resolve removing lambda expression, but I use almost lambda for methods and this give me many troubles because I can test the error only publishing (in visual studio the bundle is not created and so the error doesn't appear).

    Can Microsoft please resolve this issue?

    B.R.

    Cristiano Larghi 

    Saturday, November 21, 2020 7:53 PM

All replies

  • User-474980206 posted

    The bundler uses NUglify under the covers. You can open an issuer on the site, or better yet fix and do a pull request.

        https://github.com/trullock/NUglify

    you might want to switch to npm tool chain which typically is kept more current.

    Saturday, November 21, 2020 9:19 PM
  • User220660661 posted

    bruce (sqlwork.com)

    The bundler uses NUglify under the covers. You can open an issuer on the site, or better yet fix and do a pull request.

        https://github.com/trullock/NUglify

    Thank for your answer. I'm not sure we are speaking about the same because seems that NUglify is client-side, I'm spoken about the server-side bundle. As this:

    // In BundleConfig.cs.
    bundles.Add(new ScriptBundle("~/bundles/test").Include("~/Scripts/test.js"));
    // In a Razor Page.
    @Scripts.Render("~/bundles/test")

    I don't know if this is the correct forum, but is the only which speaks about javascript. I use this in an Standard (not Core) Asp.Net MVC project.


    bruce (sqlwork.com)

    you might want to switch to npm tool chain which typically is kept more current.

    Sorry, but I have not understood this sentence, can you please explain me? Thanks!

    Sunday, November 22, 2020 11:17 AM
  • User-474980206 posted

    NUglify is the tools the server bundler calls to make the min files. Check that you have a current version. The github site says it’s up es2021, but I don’t know what version of the bundler the nuget packages uses. Check if there is a newer version of the bundler package.

    note. I don’t use the server bundler but rather webpack which is a node package.

    Sunday, November 22, 2020 4:55 PM
  • User220660661 posted

    I think that NUglify is used in .Net Core (where "standard" bundling has been removed), not in a standard Asp.Net MVC5 project.

    I don't find information about what the Bundles logic in BundleConfig.cs is, but Nuglify is not installed (I have also installed it now, but no difference).

    Am I wrong?

    Perhaps I could remove the bundles logic and using NUglify (I already use Grunt for some never modified files without problems). But is a very big solution and is not so simple, I prefer to continue to use BundleConfig.cs logic. I think that is nonsensical that doesn't works with some valid js syntaxes.

    N.b. the test project is updated to all nuget latest releases.

    Sunday, November 22, 2020 5:46 PM
  • User-474980206 posted

    NUglify is a nuget library named after the old javascript uglify plugin and a port of the old Microsoft Ajax Minifier. 

    I checked and the current  bundler in 4.* template projects is webgrease 1.6.0.  this is an old package that has not been updated since 2014. it only exists on the codeplex archive. you might be able to download the archive and create a more current version that uses a current version of NUglify 

      https://archive.codeplex.com/?p=webgrease

    note: .net 4.8 is a mature product and only receiving security updates. 

    if you are using grunt (really old school, not even gulp), then you probably should use grunt-babel to minify, especially if you want to use current javascript code syntax.

     

    Monday, November 23, 2020 4:34 PM
  • User220660661 posted

    I'm speaking about bundling in a asp.net project:
    https://docs.microsoft.com/it-it/aspnet/mvc/overview/performance/bundling-and-minification

    Yes, in the future I will change the way I create bundling, but Microsoft said to use it for a decade, and I have actually no time to refactoring this part of code.

    My project is .net 4.7.2; 4.8 has been releases in May, 2019 but I have actually no time to convert.

    Specially I love that bundling is implemented automatically only when publish, so I can debug javascript while development, I'll study for this solution.

    But Microsoft speaks about this for a MVC "standard" project, but doesn't work. Is embarrassing.

    Tuesday, November 24, 2020 7:10 AM
  • User220660661 posted

    Any idea of how to replace the BundleConfig maintaining the same features?

    • In Razor a unique assignment, regardless of debug/relese/publish (e.g. actually is @Scripts.Render("~/Scripts/WeavingPlanning"));
    • When work in visual studio (debug, could be different in release, actually is the same) in the page must be sended all singol files uncompressed (so I can debug e.g. with chrome console);
    • When publish and open the site instead must be present a singol file compressed and minified.

    Thank's

    b.r.

    Cristiano Larghi

    Tuesday, November 24, 2020 2:32 PM
  • User-474980206 posted

    c.larghi@gmail.com

    I'm speaking about bundling in a asp.net project:
    https://docs.microsoft.com/it-it/aspnet/mvc/overview/performance/bundling-and-minification

    as webgrease which is no longer maintained. A new bundler was used for .net core, and will not work with 4.7. I know of no direct replacement. You only have a couple options

    • fix webgrease to support modern JavaScript 
    • use old JavaScript syntax supported by webgrease
    • use Babel to transpile your JavaScript to old syntax
    • switch to a new bundler
    Tuesday, November 24, 2020 3:57 PM
  • User220660661 posted

    Hi, I have resolved using BundleTransformer (and using NUglify).

    https://github.com/Taritsyn/BundleTransformer

    Friday, December 11, 2020 6:12 PM