الأسئلة الشائعة
تنظيم صفحاتك في مجموعات يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
- ما هو Closure Compiler؟ لماذا يجب استخدامها؟
- Closure Compiler هي أداة لتسريع عملية تنزيل JavaScript وتشغيلها. يمكنك استخدام Closure Compiler لتقليل حجم ملفات JavaScript وزيادة فعاليتها.
- ما هي أوجه الاختلاف بين Closure Compiler وبرامج ضغط JavaScript الأخرى المتاحة؟
-
بشكل عام، يجب أن يفي برنامج Closure Compiler بمتطلبات الضغط أو يتجاوزها مقارنةً بأدوات تصغير الرموز الأخرى، وأن يحسّن أوقات التنزيل لتطبيق الويب. بالإضافة إلى ذلك، يمكن أن يساعدك Closure Compiler في العثور على أخطاء في بناء الجملة أثناء التطوير (بدلاً من أثناء الاختبار) وتحديد أنماط الرموز البرمجية التي قد تحتوي على أخطاء.
في "الوضع البسيط"، من المفترض أن يكون أداء Closure Compiler أفضل من أداء الأدوات الأخرى لأنّه يستخدم تحليلًا مشابهًا للمترجم البرمجي للعثور على طرق إضافية لتقليل حجم الرمز البرمجي. على سبيل المثال، يمكن لبرنامج Closure Compiler تضمين الدوال التي يتم استخدامها في عدد قليل من المواضع، وإعادة استخدام أسماء المتغيرات، وإجراء العمليات الحسابية مسبقًا للتعبيرات الثابتة.
في "الوضع المتقدّم"، يمكن أن تستخدم أداة Closure Compiler أيضًا التعليقات التوضيحية الخاصة بالأنواع التي أضفتها للعثور على الأخطاء التي يصعب رصدها.
- هل يمكن لبرنامج Closure Compiler تجميع رمز JavaScript المضمّن في HTML؟
- لا، لا يعمل Closure Compiler إلا على الملفات التي تحتوي على JavaScript فقط.
- هل يمكنني استخدام Closure Compiler مع أدوات تصغير أخرى لملفات JavaScript؟
-
نعم. يقرأ Closure Compiler أي JavaScript صالح وينشئ JavaScript صالحًا، لذا يمكنك تطبيق Closure Compiler على ملف JavaScript إما قبل أو بعد تشغيل الملف من خلال أداة تصغير مختلفة.
تذكَّر أنّ Closure Compiler وبرامج التصغير الأخرى قد تتضمّن توقعات بشأن الرمز البرمجي الوارد. قد يؤدي برنامج التصغير الذي يزيل التعليقات إلى إزالة التراخيص أو معلومات التعليقات التوضيحية التي تحتاج إليها أداة أخرى، على سبيل المثال.
- كيف يمكنني تصحيح أخطاء JavaScript التي ينتجها Closure Compiler؟
- إذا كان الرمز البرمجي الذي تم تجميعه يعرض أخطاء أو سلوكًا غير متوقع، يمكنك استخدام "خرائط المصدر" لتصحيح الخطأ. توفّر خريطة المصدر عملية ربط بين الرمز البرمجي المجمَّع ورمز المصدر الأصلي، ما يتيح لأدوات المطوّرين في المتصفّح عرض رمز المصدر الأصلي بدلاً من الرمز البرمجي المجمَّع. لجعل Closure Compiler ينتج خريطة مصدر، مرِّر العلامة
--create_source_map
على سطر الأوامر. على سبيل المثال: $ java -jar compiler.jar --js example.js --create_source_map ./example-map --js_output_file example-compiled.js
بعد ذلك، إذا كنت تستخدم متصفّحًا يتوافق مع Source Maps (مثل Chrome أو Firefox)، يمكنك ضبط نقاط توقّف كما تفعل مع الرمز غير المجمَّع، وستعرض أدوات المطوّرين في المتصفّح سطر الرمز المقابل في المصدر الأصلي. لمزيد من المعلومات حول أدوات المطوّرين في Chrome، بما في ذلك معلومات حول خرائط المصدر، يُرجى الاطّلاع على تصحيح أخطاء JavaScript. - هل يقدّم المترجم أي تسوية بين سرعة تنفيذ التطبيق وحجم رمز التنزيل؟
- نعم. يُجري أي برنامج تجميع محسِّن مفاضلات. تتسبّب بعض تحسينات الحجم في حدوث بعض النفقات العامة الصغيرة المتعلقة بالسرعة. ومع ذلك، حرص مطوّرو Closure Compiler على عدم إضافة وقت تشغيل إضافي كبير. بل إنّ بعض تحسينات المترجم البرمجي تؤدي إلى تقليل وقت التشغيل (راجِع السؤال التالي).
- هل يحسّن المترجم سرعة التنفيذ؟
- في معظم الحالات، تكون الشفرة الأصغر حجمًا أسرع، لأنّ وقت التنزيل هو عادةً أهم عامل في سرعة تطبيقات الويب. تساهم التحسينات التي تقلّل من التكرار في تسريع وقت تشغيل الرمز البرمجي أيضًا.
- هل هناك أي قيود على حجم الملفات التي يمكن تجميعها؟
- تتضمّن خدمة الويب الخاصة بالتجميع حدًا أقصى لحجم الملف، ولكن لا يتضمّن تطبيق المجمّع المستقل هذا الحد.
- هل يتوفّر Closure Compiler لجميع المنصات؟
- تمت كتابة برنامج الترجمة البرمجية بلغة Java، لذا يمكن تشغيله في أي مكان يتم فيه تشغيل Java.
- هل يمكن للمحوّل البرمجي معالجة أي JavaScript قانوني؟
- في معظم الأحيان. يمكن أن تؤدي بعض بنى JavaScript، بما في ذلك
eval()
وwith()
، إلى إبطال الافتراضات التي تستند إليها عمليات التحويل التي يجريها المحوّل البرمجي. - ما مقدار المعلومات التي أحتاج إلى معرفتها حول تطوير الويب لاستخدام Closure Compiler؟
- Closure Compiler هي أداة لتطوير JavaScript، لذا يجب أن تعرف كيفية البرمجة بلغة JavaScript لاستخدام المحوّل البرمجي. ومع ذلك، يمكن لأي شخص يستخدم JavaScript الاستفادة من استخدام أداة Closure Compiler.
- كيف يعمل Closure Compiler مع Closure Library؟
- يوفّر Closure Compiler عمليات تحقّق وتحسين خاصة للرموز التي تستخدم Closure Library. بالإضافة إلى ذلك، يمكن لخدمة Closure Compiler تضمين ملفات Closure Library تلقائيًا. توضّح طريقة العثور على ما تحتاج إليه في Closure بنية الإعلان عن أجزاء Closure التي تحتاج إليها. راجِع مرجع واجهة برمجة التطبيقات للحصول على معلومات حول استخدام Closure Library مع واجهة برمجة التطبيقات. لاستخدام Closure Library مع تطبيق Closure Compiler، عليك أولاً تنزيل Closure Library. يتم تفعيل التوافق مع Closure Library في تطبيق المحول البرمجي تلقائيًا.
- يتوقّف الرمز البرمجي عن العمل أو يعرض "المترجم" أخطاءً عند الترجمة باستخدام
ADVANCED_OPTIMIZATIONS
. لماذا؟ - يتطلّب استخدام الوضع المتقدّم عادةً بعض التحضير وإجراء تغييرات على الرمز. توضّح مقالة الترجمة البرمجية المتقدّمة والتعريفات الخارجية كيفية التأكّد من أنّ الرمز البرمجي يعمل مع
ADVANCED_OPTIMIZATIONS
. - لماذا تظهر فواصل أسطر عشوائية في البرامج النصية المجمَّعة؟
- يضيف Closure Compiler فواصل أسطر عمدًا كل 500 حرف تقريبًا، لأنّ برامج جدار الحماية والخوادم الوكيلة تتلف أحيانًا ملفات JavaScript الكبيرة التي تحتوي على أسطر طويلة جدًا أو تتجاهلها. يمكن تجنُّب هذه المشكلة من خلال إضافة فواصل أسطر كل 500 حرف. لا تؤثر إزالة فواصل الأسطر في دلالات النص البرمجي. يكون التأثير على حجم الرمز البرمجي صغيرًا، ويحسِّن المحوّل البرمجي موضع فواصل الأسطر ليكون تأثير حجم الرمز البرمجي أصغر عند ضغط الملفات باستخدام gzip.
- لديّ إشعارات حقوق طبع ونشر أو نص ترخيص برامج مفتوحة المصدر يجب أن يظهر في رمز المصدر. كيف يمكنني منع Closure Compiler من إزالة هذا النص؟
- يتوافق Closure Compiler مع علامة
@license
JSDoc. أضِف العلامة @license
إلى أي تعليق JSDoc للاحتفاظ بالتعليق في ناتج المترجم. يمكنك الاطّلاع على إضافة تعليقات توضيحية إلى JavaScript من أجل Closure Compiler للحصول على مزيد من المعلومات.
إنّ محتوى هذه الصفحة مرخّص بموجب ترخيص Creative Commons Attribution 4.0 ما لم يُنصّ على خلاف ذلك، ونماذج الرموز مرخّصة بموجب ترخيص Apache 2.0. للاطّلاع على التفاصيل، يُرجى مراجعة سياسات موقع Google Developers. إنّ Java هي علامة تجارية مسجَّلة لشركة Oracle و/أو شركائها التابعين.
تاريخ التعديل الأخير: 2025-07-26 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","easyToUnderstand","thumb-up"],["ساعَدني المحتوى في حلّ مشكلتي.","solvedMyProblem","thumb-up"],["غير ذلك","otherUp","thumb-up"]],[["لا يحتوي على المعلومات التي أحتاج إليها.","missingTheInformationINeed","thumb-down"],["الخطوات معقدة للغاية / كثيرة جدًا.","tooComplicatedTooManySteps","thumb-down"],["المحتوى قديم.","outOfDate","thumb-down"],["ثمة مشكلة في الترجمة.","translationIssue","thumb-down"],["مشكلة في العيّنات / التعليمات البرمجية","samplesCodeIssue","thumb-down"],["غير ذلك","otherDown","thumb-down"]],["تاريخ التعديل الأخير: 2025-07-26 (حسب التوقيت العالمي المتفَّق عليه)"],[[["\u003cp\u003eThe Closure Compiler is a tool for making JavaScript download and run faster by reducing file size and improving efficiency.\u003c/p\u003e\n"],["\u003cp\u003eIt offers greater compression than standard minifiers and helps identify syntax errors and potential bugs during development.\u003c/p\u003e\n"],["\u003cp\u003eThe Closure Compiler works with any valid JavaScript and can be used alongside other minifiers for enhanced optimization.\u003c/p\u003e\n"],["\u003cp\u003eSource maps enable debugging of compiled code by providing a mapping back to the original source.\u003c/p\u003e\n"],["\u003cp\u003eWhile prioritizing download speed, the compiler also performs optimizations to enhance execution speed without significant runtime overhead.\u003c/p\u003e\n"]]],[],null,["- [What is the Closure Compiler? Why should I use it?](#what)\n- [How is the Closure Compiler different\n from the other JavaScript compressors that are out there?](#how-different)\n- [Can the Closure Compiler compile JavaScript that's embedded in HTML?](#tags)\n- [Can I use the Closure Compiler together with other JavaScript\n minifiers?](#other-minifiers)\n- [How can I debug the JavaScript that the Closure Compiler produces?](#sourcemaps)\n- [Does the compiler make any trade-off between my application's\n execution speed and download code size?](#tradeoffs)\n- [Does the compiler optimize for speed?](#speed)\n- [Are there any restrictions on the size of the files that can be compiled?](#size-restrictions)\n- [Is the Closure Compiler available for all platforms?](#platforms)\n- [Can the Compiler process any legal JavaScript?](#restrictions)\n- [How does the Closure Compiler work with\n the Closure Library?](#how-closure)\n- [My code stops working or the Compiler produces\n errors when I compile\n with `ADVANCED_OPTIMIZATIONS`. Why?](#advanced)\n- [Why are there random line feeds in\n compiled scripts?](#linefeeds)\n- [I have copyright notices or open source license\n text that must appear in my source code. How do I keep the Closure\n Compiler from stripping this text out?](#license)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\nWhat is the Closure Compiler? Why should I use it?\n:\n The Closure Compiler is a tool for making JavaScript download and\n run faster. You can use the Closure Compiler to reduce the size of\n your JavaScript files and to make them more efficient.\n\n\nHow is the Closure Compiler different from the other JavaScript\ncompressors that are out there?\n\n: Generally, the Closure Compiler should meet or exceed the compression\n of other code minification tools, and improve download times for your\n web application. In addition, the Closure Compiler can help you\n find syntax errors during development (rather than during testing)\n and identify potentially buggy code patterns.\n\n In Simple mode, Closure Compiler should be able to do better than\n other tools because it uses compiler-like analysis to find additional\n ways to minimize code size. For example, the Closure Compiler can\n inline functions that are used in only a few occurrences, reuse\n variable names, and pre-compute constant expressions.\n\n In Advanced mode, Closure Compiler can also use your added type\n annotations to find hard-to-spot bugs.\n\n\nCan the Closure Compiler compile JavaScript that's embedded in HTML?\n:\n No. The Closure Compiler works only on files that contain only JavaScript.\n\n\nCan I use the Closure Compiler together with other JavaScript\nminifiers?\n\n: Yes. Closure Compiler reads in any valid JavaScript and generates\n valid JavaScript, so you can apply the Closure Compiler to a\n JavaScript file either before or after you run the file through a\n different minifier.\n\n Remember that Closure Compiler and other minifiers might have\n expectations about the incoming code. A minifier that strips\n comments may remove licenses or annotation information needed by\n another tool, for example.\n\n\nHow can I debug the JavaScript that the Closure Compiler produces?\n:\n If your compiled code is throwing errors or exhibiting unexpected behavior,\n you can use Source Maps to debug the issue. A source map provides a mapping\n between the compiled code and the original source code so that the browser's\n developer tools can show you your original source code instead of the compiled code.\n To make the Closure Compiler produce a source map, pass the\n `--create_source_map` flag on the command line. For example: \n\n ```\n $ java -jar compiler.jar --js example.js --create_source_map ./example-map --js_output_file example-compiled.js\n ```\n\n Then, if you're using a browser that supports Source Maps (such as Chrome\n or Firefox), you can set breakpoints just as you would for uncompiled code,\n and the browser's developer tools will show the corresponding line of code\n in the original source. For more information about Chrome's developer tools, including\n information about source maps, see\n [Debugging JavaScript](https://developers.google.com/chrome-developer-tools/docs/javascript-debugging).\n\n\nDoes the compiler make any trade-off between my application's\nexecution speed and download code size?\n:\n Yes. Any optimizing compiler makes trade-offs. Some size\n optimizations do introduce small speed overheads. However, the\n Closure Compiler's developers have been careful not to introduce\n significant additional runtime. Some of the compiler's\n optimizations even decrease runtime (see next question).\n\n\nDoes the compiler optimize for speed?\n:\n In most cases smaller code is faster code, since download time is usually the most important speed factor in web applications. Optimizations that\n reduce redundancies speed up the run time of code as well.\n\n\nAre there any restrictions on the size of the files that can be compiled?\n:\n The compilation web service has a maximum file size, but the\n standalone compiler application does not.\n\n\nIs the Closure Compiler avalable for all platforms?\n:\n The compiler is written in Java, so it can run anywhere Java runs.\n\n\nCan the Compiler process any legal JavaScript?\n:\n Mostly. Some JavaScript constructs, including `eval()`\n and `with()`, can invalidate assumptions on which the\n compiler's transformations are based.\n\n\nHow much do I need to know about web development to use the\nClosure Compiler?\n:\n The Closure Compiler is a tool for JavaScript development, so you\n do need to know how to program in JavaScript to use the\n compiler. But anyone who uses JavaScript can benefit from using\n the Closure Compiler.\n\n\nHow does the Closure Compiler work with the Closure Library?\n:\n The Closure Compiler provides special checks and optimizations for\n code that uses the Closure Library. In addition, the Closure\n Compiler service can automatically include Closure Library\n files. [Finding\n Your Way around Closure](/closure/library/docs/introduction#deps) describes the syntax for declaring the\n parts of Closure that you need. See\n the [API\n reference](/closure/compiler/docs/api-ref#closure) for information on using the Closure Library with the\n API. To use the Closure Library with the Closure Compiler\n application you must\n first [download\n the Closure Library](https://github.com/google/closure-library). Support for the Closure Library is\n enabled in the compiler application by default.\n\n\nMy code stops working or the Compiler produces errors when I\ncompile with `ADVANCED_OPTIMIZATIONS`. Why?\n:\n Using Advanced mode usually requires some preparation and code\n changes. [Advanced Compilation\n and Externs](/closure/compiler/docs/api-tutorial3) explains how to make sure your code works\n with `ADVANCED_OPTIMIZATIONS`.\n\n\nWhy are there random line feeds in compiled scripts?\n:\n The Closure Compiler intentionally adds line breaks every 500\n characters or so. Firewalls and proxies sometimes corrupt or\n ignore large JavaScript files with very long lines. Adding line\n breaks every 500 characters prevents this problem. Removing the\n line breaks has no effect on a script's semantics. The impact on code size is small, and the Compiler\n optimizes line break placement so that the code size penalty is even smaller when files are gzipped.\n\n\nI have copyright notices or open source\nlicense text that must appear in my source code. How do I keep the\nClosure Compiler from stripping this text out?\n:\n Closure Compiler supports the JSDoc `@license` tag. Add the\n `@license` tag to any JSDoc comment to preserve the\n comment in the compiler\n output. See [Annotating\n JavaScript for the Closure Compiler](/closure/compiler/docs/js-for-compiler#tag-license) for more information."]]