Closure Compiler 애플리케이션 시작하기
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
Closure Compiler 애플리케이션의 Hello World
Closure Compiler 애플리케이션은 JavaScript를 압축하고 최적화하며 오류를 찾는 Java 명령줄 유틸리티입니다. 간단한 JavaScript 프로그램으로 Closure Compiler 애플리케이션을 사용해 보려면 아래 단계를 따르세요.
이 실습을 진행하려면 Java 런타임 환경 버전 7이 필요합니다.
-
Closure Compiler 패키지 다운로드
closure-compiler
이라는 작업 디렉터리를 만듭니다.
Maven 저장소에서 가장 최근에 출시된 JAR 파일을 다운로드하고 closure-compiler
에 저장합니다.
-
JavaScript 파일 만들기
다음 JavaScript가 포함된 hello.js
이라는 파일을 만듭니다.
// A simple function.
function hello(longName) {
alert('Hello, ' + longName);
}
hello('New User');
이 파일을 closure-compiler
디렉터리에 저장합니다.
-
JavaScript 파일 컴파일
closure-compiler
디렉터리에서 다음 명령어를 실행합니다.
java -jar compiler.jar --js hello.js --js_output_file hello-compiled.js
이 명령어는 다음 JavaScript가 포함된 hello-compiled.js
라는 새 파일을 만듭니다.
function hello(a){alert("Hello, "+a)}hello("New User");
컴파일러가 주석, 공백, 불필요한 세미콜론을 삭제했습니다. 컴파일러는 또한 매개변수 이름 longName
를 더 짧은 이름 a
로 대체했습니다. 결과적으로 JavaScript 파일이 훨씬 작아집니다.
컴파일된 JavaScript 코드가 여전히 올바르게 작동하는지 확인하려면 다음과 같이 HTML 파일에 hello-compiled.js
를 포함하세요.
<html>
<head><title>Hello World</title></head>
<body>
<script src="hello-compiled.js"></script>
</body>
</html>
브라우저에서 HTML 파일을 로드하면 친근한 인사말이 표시됩니다.
다음 단계
이 예에서는 Closure Compiler에서 실행하는 가장 간단한 최적화만 보여줍니다. 컴파일러의 기능에 대해 자세히 알아보려면 고급 컴파일 및 Externs를 참고하세요.
Closure Compiler의 다른 플래그와 옵션에 대해 자세히 알아보려면 --help
플래그를 사용하여 jar를 실행하세요.
java -jar compiler.jar --help
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 Oracle 계열사의 등록 상표입니다.
최종 업데이트: 2025-07-27(UTC)
[null,null,["최종 업데이트: 2025-07-27(UTC)"],[[["\u003cp\u003eThe Closure Compiler is a Java command-line tool used to compress, optimize, and debug JavaScript code.\u003c/p\u003e\n"],["\u003cp\u003eThis guide provides a basic example of using the Closure Compiler to minify a simple "Hello World" JavaScript function.\u003c/p\u003e\n"],["\u003cp\u003eThe compiler removes unnecessary elements like comments and whitespace, shortens variable names, and produces a smaller, more efficient JavaScript file.\u003c/p\u003e\n"],["\u003cp\u003eYou can confirm the functionality of the compiled code by including it in an HTML file and loading it in a browser.\u003c/p\u003e\n"]]],[],null,["# Getting Started with the Closure Compiler Application\n\nThe Hello World of the Closure Compiler Application\n---------------------------------------------------\n\n\nThe Closure Compiler application is a Java command-line utility that\ncompresses, optimizes, and looks for mistakes in your JavaScript. To\ntry out the Closure Compiler application with a simple JavaScript\nprogram, follow the steps below.\n\n\nTo work through this exercise you need the Java Runtime Environment\nversion 7.\n\n1. **Download the Closure Compiler package**\n\n\n Create a working directory called `closure-compiler`.\n\n\n Download the most recently released JAR file from the\n [Maven repository](https://mvnrepository.com/artifact/com.google.javascript/closure-compiler), and\n save it in `closure-compiler`.\n2. **Create a JavaScript file**\n\n\n Create a file named `hello.js` containing the following\n JavaScript: \n\n ```carbon\n // A simple function.\n function hello(longName) {\n alert('Hello, ' + longName);\n }\n hello('New User');\n ```\n\n\n Save this file in the `closure-compiler` directory.\n3. **Compile the JavaScript file**\n\n\n Run the following command from\n the `closure-compiler` directory: \n\n ```\n java -jar compiler.jar --js hello.js --js_output_file hello-compiled.js\n ```\n\n\n This command creates a new file\n called `hello-compiled.js`, which contains the following\n JavaScript: \n\n ```text\n function hello(a){alert(\"Hello, \"+a)}hello(\"New User\");\n ```\n\n\n Note that the compiler has stripped comments, whitespace and an\n unnecessary semi-colon. The compiler has also replaced the parameter\n name `longName` with the shorter name `a`. The\n result is a much smaller JavaScript file.\n\n\n To confirm that the compiled JavaScript code still works correctly,\n include `hello-compiled.js` in an HTML file like this\n one: \n\n ```text\n \u003chtml\u003e\n \u003chead\u003e\u003ctitle\u003eHello World\u003c/title\u003e\u003c/head\u003e\n \u003cbody\u003e\n \u003cscript src=\"hello-compiled.js\"\u003e\u003c/script\u003e\n \u003c/body\u003e\n \u003c/html\u003e\n ```\n\n\n Load the HTML file in a browser, and you should see a friendly greeting!\n\n### Next Steps\n\n\nThis example illustrates only the most simple optimizations\nperformed by the Closure Compiler. To learn more about the\ncompiler's capabilities, read [Advanced\nCompilation and Externs](/closure/compiler/docs/api-tutorial3).\n\n\nTo learn more about other flags and options for the Closure\nCompiler, execute the jar with the `--help` flag: \n\n```\njava -jar compiler.jar --help\n```"]]