How to prevent usage of *.min.js in BundleTransfor

2020-07-17 16:15发布

问题:

I use BundleTransformer.Core 1.9.25. I have included angular-animate.js in bundle. But in generated bundle file I saw the error:

/* Minification failed. Returning unminified contents.
(402,118-125): run-time error JS1019: Can't have 'break' outside of loop: break a

The reason is that the bundle uses angular-animate.min.js instead of angular-animate.js. When I delete angular-animate.min.js file, it uses angular-animate.js and there is not errors.

Web.config contains:

<bundleTransformer xmlns="http://tempuri.org/BundleTransformer.Configuration.xsd">
  <core>
    <js usePreMinifiedFiles="false">
      <translators>
        <add name="NullTranslator" type="BundleTransformer.Core.Translators.NullTranslator, BundleTransformer.Core" enabled="false" />
      </translators>
      <minifiers>
        <add name="NullMinifier" type="BundleTransformer.Core.Minifiers.NullMinifier, BundleTransformer.Core" />
      </minifiers>
      <fileExtensions>
        <add fileExtension=".js" assetTypeCode="JavaScript" />
      </fileExtensions>
    </js>
  </core>
</bundleTransformer>

As you see usePreMinifiedFiles=false attribute doesn't prevent usage of existing *.min.js files.

回答1:

I just forgot to add ScriptTransformer in App_Start/BundleConfig.cs:

var scriptTransformer = new ScriptTransformer();
bundle.Transforms.Add(scriptTransformer);

It fixed the problem.

But I chose the solution without using ScriptTransformer (BundleTransformer.Core). It is cleaning of FileExtensionReplacementList list:

bundles.FileExtensionReplacementList.Clear();

By default FileExtensionReplacementList has two values:

.Add("min", OptimizationMode.WhenEnabled);
.Add("debug", OptimizationMode.WhenDisabled);

And it was a cause of my problem.



回答2:

In the BundleConfig.cs file find

BundleTable.EnableOptimizations = true;

and change the value to false.