11

एमवीसी 4 के नवीनतम संस्करण का उपयोग करते हुए मैं जावास्क्रिप्ट को छोटा नहीं कर सकता जब इसमें आरक्षित शब्द मुख्य नाम के रूप में होते हैं!एमवीसी 4 बंडल minification जावास्क्रिप्ट आरक्षित शब्दों के साथ काम नहीं करता

वैध जावास्क्रिप्ट के साथ नीचे दी गई त्रुटि देखें जो कम से कम होनी चाहिए।

क्या कोई यह जानता है कि जावास्क्रिप्ट को ["]] नोटेशन का उपयोग करने के लिए पुनः लिखने के इस छोटे से कैसे ठीक करें?

पीएस प्रश्न में कोड कुछ हज़ार लाइन लंबी है, इसलिए यह एक विकल्प नहीं है!

/* Minification failed. Returning unminified contents. 
(3,9-15): run-time warning JS1010: Expected identifier: delete 
(4,9-13): run-time warning JS1010: Expected identifier: case 
(5,9-11): run-time warning JS1010: Expected identifier: if 
(3,9-15): run-time error JS1137: 'delete' is a new reserved word and should not be used as an identifier: delete 
(4,9-13): run-time error JS1137: 'case' is a new reserved word and should not be used as an identifier: case 
(5,9-11): run-time error JS1137: 'if' is a new reserved word and should not be used as an identifier: if 
*/ 
var context = {}; 

context.delete = {}; 
context.case = {}; 
context.if = {}; 

सवाल नोड, कैसेट, Combres, की तरह एक और विकल्प के साथ जा रहा बिना है servicestack आदि

हम कैसे आरक्षित शब्द के साथ गेंद खेलने के लिए MVC4 मिलता है।

मुझे विश्वास करना मुश्किल लगता है कि 6 महीने के बाद और इसके लिए कोई समर्थन नहीं है!

+3

बस इसे अपने स्वयं और पहले सोचा। अरे यह काम किया, क्योंकि कोई स्पष्ट अपवाद प्रदर्शित नहीं किया गया था। लेकिन इसके बजाय, बकवास को कम नहीं किया गया था और जेएस में एक छोटी सी छोटी टिप्पणी इनपुट थी। यह वास्तव में बदसूरत है। – Daniel

+1

परीक्षण नहीं किया है, लेकिन बिल्डर प्रति बंडल स्विच करने योग्य लगता है। क्या आप वैल्यू को स्विच नहीं कर सकते हैं? शायद वेबग्रेज़ में जेएसपेसर का उपयोग करके और सेटिंग्स का उपयोग कर सकते हैं। AddRenamePair? – Daniel

+0

हे जेम्स। एक अच्छा सवाल है, लेकिन ईसीएमएएसस्क्रिप्ट मानक का उपयोग करने के खिलाफ सलाह देते समय बंडलिंग आरक्षित शब्दों का समर्थन क्यों करेगा? https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Reserved_Words – garfbradaz

उत्तर

8

बस कोशिश की और यह काम करता है। क्षमा करें लेकिन बदसूरत कोड। यह आपके सदस्यों को delete नाम और उनके उपयोगों को प्रतिस्थापित करेगा।

public class CustomBundle : ScriptBundle 
{ 
    public CustomBundle(string virtualPath) : base(virtualPath) 
    { 
     this.Builder = new CustomBuilder(); 
    } 
    public CustomBundle(string virtualPath, string cdnPath) : base(virtualPath, cdnPath) {} 
} 

public class CustomBuilder : IBundleBuilder { 
    public string BuildBundleContent(Bundle bundle, BundleContext context, IEnumerable<FileInfo> files) 
    { 
     var content = new StringBuilder(); 
     foreach (var fileInfo in files) 
     { 
      var parser = new Microsoft.Ajax.Utilities.JSParser(Read(fileInfo)); 
      parser.Settings.AddRenamePair("delete", "fooDelete"); 
      content.Append(parser.Parse(parser.Settings).ToCode()); 
      content.Append(";"); 
     } 

     return content.ToString(); 
    } 

    private string Read(FileInfo file) 
    { 
     using(var r = file.OpenText()) 
     { 
      return r.ReadToEnd(); 
     } 
    } 
} 
+0

यह शानदार डैनियल है, मैं निश्चित रूप से इसके साथ एक खेल खेल रहा हूं। – garfbradaz

+1

कोड में बहुत प्यार नहीं है, लेकिन यह उन्हें स्विच करेगा, लेकिन केवल तभी जब इसे मिनीफ़ीइंग किया जाए। इसके बारे में यहां ब्लॉग किया गया: http://daniel.wertheim.se/2012/11/16/customizing-the-minification-of-javascript- इन-एएसपी-नेट-एमवीसी 4-स्वीकार्य-आरक्षित शब्द/ – Daniel

+0

क्या आप सहमत होंगे कि इस तरह के तर्क को कुछ MSBuild स्क्रिप्ट में कहने के लिए और अधिक कुशल होगा? – garfbradaz

2

आशा है कि यह बहुत देर हो चुकी है। आप अपने minification परिवर्तन को लागू कर सकते हैं और इन त्रुटियों को अनदेखा कर सकते हैं।

var bundle = new ScriptBundle("~/bundles/xxxbundle", null, new JsMinifyIgnoreReservedWordError()). 
    Include("~/Scripts/xxx.js"); 

private class JsMinifyIgnoreReservedWordError : IBundleTransform 
{ 
    private const string JsContentType = "text/javascript"; 

    public void Process(BundleContext context, BundleResponse response) 
    { 
     if (context == null) 
     { 
      throw new ArgumentNullException("context"); 
     } 
     if (response == null) 
     { 
      throw new ArgumentNullException("response"); 
     } 
     if (!context.EnableInstrumentation) 
     { 
      Minifier minifier = new Minifier(); 

      string result = minifier.MinifyJavaScript(response.Content, new CodeSettings 
      { 
       EvalTreatment = EvalTreatment.MakeImmediateSafe, 
       PreserveImportantComments = false, 
         IgnoreErrorList = "JS1137" // ignore 'is a new reserved word and should not be used as an identifier' error 
        }); 

      if (minifier.ErrorList.Count > 0) 
      { 
       GenerateErrorResponse(response, minifier.ErrorList); 
      } 
      else 
      { 
       response.Content = result; 
      } 
     } 
     response.ContentType = JsContentType; 
    } 

    private static void GenerateErrorResponse(BundleResponse bundle, IEnumerable<object> errors) 
    { 
     StringBuilder stringBuilder = new StringBuilder(); 

     stringBuilder.Append("/* "); 
     stringBuilder.Append("Minification failed. Returning unminified contents.").AppendLine(); 

     foreach (object error in errors) 
     { 
      stringBuilder.Append(error).AppendLine(); 
     } 

     stringBuilder.Append(" */").AppendLine(); 
     stringBuilder.Append(bundle.Content); 

     bundle.Content = stringBuilder.ToString(); 
    } 
} 
+1

धन्यवाद @bigbangbyproduct मैं इसे पूरा करने में सक्षम हूं लेकिन मुझे 'स्क्रिप्टबंडल' को' बंडल 'में बदलना है क्योंकि इसमें सही कन्स्ट्रक्टर पैरामीटर हैं। –

+1

भी, अनदेखी सूची में और त्रुटि संख्या जोड़ने के लिए, आप अल्पविराम से अलग मूल्य का उपयोग कर सकते हैं। –

संबंधित मुद्दे