For production, use google-closure-compiler to create a single-file, optimized bundle. This process performs type checking, dead code removal, and identifier shortening.
- Install the compiler:
npm install --save-dev google-closure-compiler. - Run the compiler with the following flags:
--js: Your source file.--js: The Closure library source files (e.g., node_modules/google-closure-library/**/*.js).--dependency_mode=PRUNE: Tells the compiler to only include dependencies required by the entry point.--entry_point=goog:<module_name>: The namespace of your entry point.--js_output_file: The name of the resulting bundle.
- In your production HTML, replace the
base.js and deps.js scripts with a single <script> tag pointing to your compiled file.
# 1. Install
npm install --save-dev google-closure-compiler
# 2. Compile
$(npm bin)/google-closure-compiler \
--js hello.js \
--js node_modules/google-closure-library/**/*.js \
--dependency_mode=PRUNE \
--entry_point=goog:hello \
--js_output_file hello_compiled.js