Câu hỏi thường gặp
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
-
Closure Compiler là gì? Tại sao tôi nên sử dụng tính năng này?
-
Trình biên dịch Closure là một công cụ giúp tải xuống và chạy JavaScript nhanh hơn. Bạn có thể sử dụng Closure Compiler để giảm kích thước tệp JavaScript và giúp các tệp này hoạt động hiệu quả hơn.
-
Closure Compiler có gì khác biệt so với các trình nén JavaScript khác hiện có?
-
Nhìn chung, Closure Compiler sẽ đáp ứng hoặc vượt quá mức nén của các công cụ giảm thiểu mã khác và cải thiện thời gian tải xuống cho ứng dụng web của bạn. Ngoài ra, Closure Compiler có thể giúp bạn tìm lỗi cú pháp trong quá trình phát triển (thay vì trong quá trình kiểm thử) và xác định các mẫu mã có thể có lỗi.
Ở chế độ Đơn giản, Closure Compiler có thể hoạt động hiệu quả hơn các công cụ khác vì công cụ này sử dụng phương thức phân tích giống như trình biên dịch để tìm thêm các cách giảm thiểu kích thước mã. Ví dụ: Trình biên dịch Closure có thể nội tuyến các hàm chỉ được dùng trong một vài trường hợp, sử dụng lại tên biến và tính toán trước các biểu thức hằng số.
Ở chế độ Nâng cao, Closure Compiler cũng có thể sử dụng chú thích kiểu mà bạn đã thêm để tìm các lỗi khó phát hiện.
-
Trình biên dịch Closure có thể biên dịch JavaScript được nhúng trong HTML không?
-
Không. Closure Compiler chỉ hoạt động trên những tệp chỉ chứa JavaScript.
-
Tôi có thể sử dụng Closure Compiler cùng với các trình rút gọn JavaScript khác không?
-
Có. Trình biên dịch Closure đọc mọi JavaScript hợp lệ và tạo ra JavaScript hợp lệ, vì vậy, bạn có thể áp dụng Trình biên dịch Closure cho một tệp JavaScript trước hoặc sau khi chạy tệp đó thông qua một trình rút gọn khác.
Hãy nhớ rằng Closure Compiler và các trình thu nhỏ khác có thể có những kỳ vọng về mã đến. Một trình rút gọn loại bỏ các nhận xét có thể xoá giấy phép hoặc thông tin chú thích mà một công cụ khác cần, chẳng hạn như.
-
Làm cách nào để gỡ lỗi JavaScript mà Trình biên dịch Closure tạo ra?
-
Nếu mã đã biên dịch của bạn gặp lỗi hoặc có hành vi không mong muốn, bạn có thể sử dụng Source Maps để gỡ lỗi. Bản đồ nguồn cung cấp mối liên kết giữa mã đã biên dịch và mã nguồn ban đầu để công cụ dành cho nhà phát triển của trình duyệt có thể cho bạn thấy mã nguồn ban đầu thay vì mã đã biên dịch.
Để Trình biên dịch Closure tạo một bản đồ nguồn, hãy truyền cờ
--create_source_map
trên dòng lệnh. Ví dụ:
$ java -jar compiler.jar --js example.js --create_source_map ./example-map --js_output_file example-compiled.js
Sau đó, nếu đang sử dụng một trình duyệt hỗ trợ Source Maps (chẳng hạn như Chrome hoặc Firefox), bạn có thể đặt điểm ngắt giống như đối với mã chưa biên dịch và công cụ dành cho nhà phát triển của trình duyệt sẽ cho thấy dòng mã tương ứng trong nguồn ban đầu. Để biết thêm thông tin về các công cụ cho nhà phát triển của Chrome, bao gồm cả thông tin về bản đồ nguồn, hãy xem phần Gỡ lỗi JavaScript.
-
Trình biên dịch có đánh đổi giữa tốc độ thực thi và kích thước mã tải xuống của ứng dụng của tôi không?
-
Có. Mọi trình biên dịch tối ưu hoá đều có những điểm đánh đổi. Một số hoạt động tối ưu hoá kích thước sẽ làm tăng tốc độ một chút. Tuy nhiên, các nhà phát triển Closure Compiler đã cẩn thận để không đưa thêm thời gian chạy đáng kể. Một số hoạt động tối ưu hoá của trình biên dịch thậm chí còn làm giảm thời gian chạy (xem câu hỏi tiếp theo).
-
Trình biên dịch có tối ưu hoá về tốc độ không?
-
Trong hầu hết các trường hợp, mã nhỏ hơn sẽ nhanh hơn vì thời gian tải xuống thường là yếu tố tốc độ quan trọng nhất trong các ứng dụng web. Các hoạt động tối ưu hoá giúp giảm sự dư thừa cũng đẩy nhanh thời gian chạy của mã.
-
Có hạn chế nào về kích thước của các tệp có thể được biên dịch không?
-
Dịch vụ web biên dịch có kích thước tệp tối đa, nhưng ứng dụng trình biên dịch độc lập thì không.
-
Trình biên dịch Closure có dùng được cho tất cả các nền tảng không?
-
Trình biên dịch được viết bằng Java, nên có thể chạy ở bất cứ nơi nào Java chạy.
-
Trình biên dịch có thể xử lý mọi JavaScript hợp lệ không?
-
Hầu hết. Một số cấu trúc JavaScript, bao gồm
eval()
và with()
, có thể làm mất hiệu lực các giả định mà các phép biến đổi của trình biên dịch dựa trên đó.
-
Tôi cần biết bao nhiêu về phát triển web để sử dụng Trình biên dịch Closure?
-
Closure Compiler là một công cụ để phát triển JavaScript, vì vậy, bạn cần biết cách lập trình bằng JavaScript để sử dụng trình biên dịch này. Nhưng bất kỳ ai sử dụng JavaScript đều có thể hưởng lợi từ việc sử dụng Trình biên dịch Closure.
-
Closure Compiler hoạt động như thế nào với Closure Library?
-
Trình biên dịch Closure cung cấp các chế độ kiểm tra và tối ưu hoá đặc biệt cho mã sử dụng Thư viện Closure. Ngoài ra, dịch vụ Trình biên dịch Closure có thể tự động bao gồm các tệp Thư viện Closure. Tìm hiểu về Closure mô tả cú pháp để khai báo các phần của Closure mà bạn cần. Hãy xem tài liệu tham khảo về API để biết thông tin về cách sử dụng Thư viện Closure với API. Để sử dụng Thư viện Closure với ứng dụng Trình biên dịch Closure, trước tiên, bạn phải tải Thư viện Closure xuống. Theo mặc định, ứng dụng trình biên dịch sẽ hỗ trợ Thư viện Closure.
-
Mã của tôi ngừng hoạt động hoặc Trình biên dịch tạo ra lỗi khi tôi biên dịch bằng
ADVANCED_OPTIMIZATIONS
. Tại sao?
-
Việc sử dụng Chế độ nâng cao thường yêu cầu bạn chuẩn bị và thay đổi mã. Advanced Compilation and Externs (Biên dịch nâng cao và Extern) giải thích cách đảm bảo mã của bạn hoạt động với
ADVANCED_OPTIMIZATIONS
.
-
Tại sao lại có các ký tự xuống dòng ngẫu nhiên trong tập lệnh đã biên dịch?
-
Closure Compiler cố tình thêm dấu ngắt dòng sau mỗi 500 ký tự. Đôi khi, tường lửa và proxy sẽ làm hỏng hoặc bỏ qua các tệp JavaScript lớn có dòng rất dài. Việc thêm dấu ngắt dòng sau mỗi 500 ký tự sẽ ngăn chặn vấn đề này. Việc xoá dấu ngắt dòng không ảnh hưởng đến ngữ nghĩa của tập lệnh. Tác động đến kích thước mã là nhỏ và Trình biên dịch tối ưu hoá vị trí ngắt dòng để mức phạt kích thước mã thậm chí còn nhỏ hơn khi các tệp được nén bằng gzip.
-
Tôi có thông báo bản quyền hoặc văn bản giấy phép nguồn mở phải xuất hiện trong mã nguồn của mình. Làm cách nào để ngăn Closure Compiler loại bỏ văn bản này?
-
Trình biên dịch Closure hỗ trợ thẻ JSDoc
@license
. Thêm thẻ @license
vào bất kỳ nhận xét JSDoc nào để giữ lại nhận xét trong đầu ra của trình biên dịch. Hãy xem bài viết Chú thích JavaScript cho Trình biên dịch Closure để biết thêm thông tin.
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-07-26 UTC.
[null,null,["Cập nhật lần gần đây nhất: 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."]]