Taritsyn / BundleTransformer

Bundle Transformer - a modular extension for System.Web.Optimization (also known as the Microsoft ASP.NET Web Optimization Framework).
Apache License 2.0
130 stars 19 forks source link

NUglifyJsMinifier appears to be incompatible with TypeScript enums that are transpiled to ES2015 #79

Closed RudeySH closed 10 months ago

RudeySH commented 10 months ago

The NUglifyJsMinifier throws an error when it encounters a transpiled TypeScript enum.

Example TypeScript enum:

enum AchievementsDisplayMode {
    All = 0,
    Unrestricted = 1,
}

This enum gets transpiled into: (at least when targeting ES2015)

"use strict";
var AchievementsDisplayMode;
(function (AchievementsDisplayMode) {
    AchievementsDisplayMode[AchievementsDisplayMode["All"] = 0] = "All";
    AchievementsDisplayMode[AchievementsDisplayMode["Unrestricted"] = 1] = "Unrestricted";
})(AchievementsDisplayMode || (AchievementsDisplayMode = {}));

When I run my application, I get the following exception:

BundleTransformer.NUglify.NUglifyParsingException: Message: Strict-mode does not allow assignment to undefined variables: AchievementsDisplayMode
Error code: JS1300
Severity: 0
Subcategory: run-time
File: /scripts/Generated/AchievementsDisplayMode.js
Start line: 6
Start column: 32
End line: 6
End column: 55

NUglify itself seems to work fine:

var result = Uglify.Js(@"""use strict"";
var AchievementsDisplayMode;
(function (AchievementsDisplayMode) {
    AchievementsDisplayMode[AchievementsDisplayMode[""All""] = 0] = ""All"";
    AchievementsDisplayMode[AchievementsDisplayMode[""Unrestricted""] = 1] = ""Unrestricted"";
})(AchievementsDisplayMode || (AchievementsDisplayMode = {}));");

Console.WriteLine(result.Errors.Count); // -> 0

How come NUglify is able to parse the JS without errors, while BundleTransformer throws an NUglifyParsingException?

As a workaround you can disable strict mode, but that is not an acceptable solution.

RudeySH commented 10 months ago

I did some more testing. Even with slightly more advanced usage of NUglify, by itself it is able to parse the JS just fine. Still can't get it to work with BundleTransfomer though.

var parser = new JSParser
{
    Settings = new CodeSettings
    {
        StrictMode = true,
    },
};

var result = parser.Parse(@"""use strict"";
var AchievementsDisplayMode;
(function (AchievementsDisplayMode) {
    AchievementsDisplayMode[AchievementsDisplayMode[""All""] = 0] = ""All"";
    AchievementsDisplayMode[AchievementsDisplayMode[""Unrestricted""] = 1] = ""Unrestricted"";
})(AchievementsDisplayMode || (AchievementsDisplayMode = {}));");

parser.CompilerError += (x, y) =>
{
    // Success! This event never fires.
};
Taritsyn commented 10 months ago

Hello, Rudey!

I can't reproduce this error using the default settings of the BundleTransformer.NUglify module. It is possible that you are using some other settings or old version of the BundleTransformer.NUglify module.

Try to reproduce this error in the form of demo project and send me a link to it.

RudeySH commented 10 months ago

While creating the demo project, I figured out what was wrong.

My bundle included a JS file that uses the enum before the enum JS file itself was included. Apparently this was never a problem while using the built-in ScriptBundle. I began using CustomScriptBundle and NullOrderer today, which is probably why this issue popped up. I solved the issue by including all enums before any other includes.