คำถามที่พบบ่อย
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
-
Closure Compiler คืออะไร เหตุใดฉันจึงควรใช้
-
Closure Compiler เป็นเครื่องมือที่ช่วยให้ดาวน์โหลดและ
เรียกใช้ JavaScript ได้เร็วขึ้น คุณใช้ Closure Compiler เพื่อลดขนาด
ไฟล์ JavaScript และเพิ่มประสิทธิภาพได้
-
Closure Compiler แตกต่างจากตัวบีบอัด JavaScript
อื่นๆ ที่มีอยู่ทั่วไปอย่างไร
-
โดยทั่วไปแล้ว Closure Compiler ควรมีประสิทธิภาพในการบีบอัด
มากกว่าหรือเท่ากับเครื่องมือลดขนาดโค้ดอื่นๆ และช่วยปรับปรุงเวลาในการดาวน์โหลด
เว็บแอปพลิเคชัน นอกจากนี้ Closure Compiler ยังช่วยคุณ
ค้นหาข้อผิดพลาดทางไวยากรณ์ระหว่างการพัฒนา (แทนที่จะเป็นการทดสอบ)
และระบุรูปแบบโค้ดที่อาจมีข้อบกพร่องได้ด้วย
ในโหมด Simple นั้น 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 สร้างขึ้นได้อย่างไร
-
หากโค้ดที่คอมไพล์แล้วแสดงข้อผิดพลาดหรือมีลักษณะการทำงานที่ไม่คาดคิด
คุณสามารถใช้ Source Map เพื่อแก้ไขข้อบกพร่องของปัญหาได้ แผนที่แหล่งที่มาจะแสดงการเชื่อมโยง
ระหว่างโค้ดที่คอมไพล์แล้วกับซอร์สโค้ดต้นฉบับ เพื่อให้เครื่องมือสำหรับนักพัฒนาซอฟต์แวร์
ของเบราว์เซอร์แสดงซอร์สโค้ดต้นฉบับแทนโค้ดที่คอมไพล์แล้ว
หากต้องการให้ Closure Compiler สร้าง Source Map ให้ส่งแฟล็ก
--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 รวมถึง
ข้อมูลเกี่ยวกับ Source Map ได้ที่
การแก้ไขข้อบกพร่องของ 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 กับ API ได้ในเอกสารอ้างอิง
API หากต้องการใช้ Closure Library กับแอปพลิเคชัน Closure Compiler
คุณต้องดาวน์โหลด
Closure Library ก่อน การรองรับ Closure Library จะ
เปิดใช้ในแอปพลิเคชันคอมไพเลอร์โดยค่าเริ่มต้น
-
โค้ดของฉันหยุดทำงานหรือคอมไพเลอร์แสดงข้อผิดพลาดเมื่อฉัน
คอมไพล์ด้วย
ADVANCED_OPTIMIZATIONS
เหตุผล
-
การใช้โหมดขั้นสูงมักจะต้องมีการเตรียมการและการเปลี่ยนแปลงโค้ด
บางอย่าง การคอมไพล์ขั้นสูง
และไฟล์ภายนอกอธิบายวิธีตรวจสอบว่าโค้ดของคุณทำงาน
ร่วมกับ
ADVANCED_OPTIMIZATIONS
ได้
-
เหตุใดจึงมีฟีดบรรทัดแบบสุ่มในสคริปต์ที่คอมไพล์แล้ว
-
Closure Compiler จะเพิ่มการขึ้นบรรทัดใหม่ทุกๆ 500
อักขระโดยประมาณ ไฟร์วอลล์และพร็อกซีบางครั้งอาจทำให้ไฟล์
JavaScript ขนาดใหญ่ที่มีบรรทัดยาวมากเสียหายหรือละเว้นไฟล์ดังกล่าว การเพิ่มการขึ้นบรรทัดใหม่
ทุกๆ 500 อักขระจะช่วยป้องกันปัญหานี้ได้ การนำตัวแบ่งบรรทัด
ออกจะไม่มีผลต่อความหมายของสคริปต์ ผลกระทบต่อขนาดโค้ดมีน้อย และคอมไพเลอร์
จะเพิ่มประสิทธิภาพการวางบรรทัดเพื่อให้ค่าปรับขนาดโค้ดน้อยลงไปอีกเมื่อไฟล์ถูกบีบอัดด้วย Gzip
-
ฉันมีประกาศเกี่ยวกับลิขสิทธิ์หรือข้อความใบอนุญาตโอเพนซอร์ส
ที่ต้องปรากฏในซอร์สโค้ด ฉันจะป้องกันไม่ให้
Closure Compiler ลบข้อความนี้ออกได้อย่างไร
-
Closure Compiler รองรับแท็ก JSDoc
@license
เพิ่มแท็ก
@license
ลงในความคิดเห็น JSDoc เพื่อเก็บ
ความคิดเห็นไว้ในเอาต์พุตของคอมไพเลอร์
ดูข้อมูลเพิ่มเติมได้ที่การใส่คำอธิบายประกอบ
JavaScript สำหรับ Closure Compiler
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[null,null,["อัปเดตล่าสุด 2025-07-26 UTC"],[[["\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,["# Frequently Asked Questions\n\n- [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."]]