Socket
Socket
Sign inDemoInstall

google-closure-compiler

Package Overview
Dependencies
58
Maintainers
1
Versions
1822
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    google-closure-compiler

Check, compile, optimize and compress Javascript with Closure-Compiler


Version published
Weekly downloads
118K
decreased by-1.6%
Maintainers
1
Created
Weekly downloads
 

Package description

What is google-closure-compiler?

The google-closure-compiler npm package is a powerful tool for optimizing JavaScript code. It can be used to minify, transpile, and optimize JavaScript, making it faster and more efficient. It also provides advanced features like dead code elimination, type checking, and more.

What are google-closure-compiler's main functionalities?

Minification

This feature allows you to minify JavaScript files, reducing their size and improving load times. The code sample demonstrates how to use the google-closure-compiler to minify a JavaScript file.

const closureCompiler = require('google-closure-compiler').compiler;
const compiler = new closureCompiler({
  js: 'input.js',
  compilation_level: 'SIMPLE',
  js_output_file: 'output.min.js'
});
compiler.run((exitCode, stdOut, stdErr) => {
  console.log(stdOut);
  console.error(stdErr);
});

Transpilation

This feature allows you to transpile JavaScript code from one version of ECMAScript to another. The code sample demonstrates how to transpile ECMAScript 6 code to ECMAScript 5 using google-closure-compiler.

const closureCompiler = require('google-closure-compiler').compiler;
const compiler = new closureCompiler({
  js: 'input.js',
  language_in: 'ECMASCRIPT6',
  language_out: 'ECMASCRIPT5',
  js_output_file: 'output.transpiled.js'
});
compiler.run((exitCode, stdOut, stdErr) => {
  console.log(stdOut);
  console.error(stdErr);
});

Dead Code Elimination

This feature allows you to eliminate dead code, which is code that is never executed, from your JavaScript files. The code sample demonstrates how to use the google-closure-compiler to perform dead code elimination.

const closureCompiler = require('google-closure-compiler').compiler;
const compiler = new closureCompiler({
  js: 'input.js',
  compilation_level: 'ADVANCED',
  js_output_file: 'output.optimized.js'
});
compiler.run((exitCode, stdOut, stdErr) => {
  console.log(stdOut);
  console.error(stdErr);
});

Type Checking

This feature allows you to perform type checking on your JavaScript code, helping to catch type-related errors. The code sample demonstrates how to enable type checking using google-closure-compiler.

const closureCompiler = require('google-closure-compiler').compiler;
const compiler = new closureCompiler({
  js: 'input.js',
  jscomp_warning: 'checkTypes',
  js_output_file: 'output.checked.js'
});
compiler.run((exitCode, stdOut, stdErr) => {
  console.log(stdOut);
  console.error(stdErr);
});

Other packages similar to google-closure-compiler

Readme

Source

google-closure-compiler

Check, compile, optimize and compress Javascript with Closure-Compiler

This repository tracks issues related to the node publication and associated plugins. Any bugs not related to the plugins themselves should be reported to the main repository.

Getting Started

Closure-compiler requires java to be installed and in the path.

If you are new to Closure-Compiler, make sure to read and understand the compilation levels as the compiler works very differently depending on the compilation level selected.

For help or questions with the compiler, the best resource is Stack Overflow. Posts there are monitored by multiple Closure Compiler team members.

You may also post in the Closure Compiler Discuss Google Group.

Please don't cross post to both Stackoverflow and Closure Compiler Discuss.

Usage

The compiler package now includes build tool plugins for Grunt and Gulp.

Installation

npm install --save google-closure-compiler

Configuration

The compiler has a large number of flags. The best documentation for the flags can be found by running the --help command of the compiler.jar found inside the node_modules/google-closure-compiler folder:

java -jar compiler.jar --help

Specifying Options

Both the grunt and gulp tasks take options objects. The option parameters map directly to the compiler flags without the leading '--' characters.

Values are either strings or booleans. Options which have multiple values can be arrays.

  {
    js: ['/file-one.js', '/file-two.js'],
    compilation_level: 'ADVANCED',
    js_output_file: 'out.js',
    debug: true
  }

For advanced usages, the options may be specified as an array of strings. These values include the "--" characters and are directly passed to the compiler in the order specified:

  [
    '--js', '/file-one.js',
    '--js', '/file-two.js',
    '--compilation_level', 'ADVANCED',
    '--js_output_file', 'out.js',
    '--debug'
  ]

When an array of flags is passed, the input files should not be specified via the build tools, but rather as compilation flags directly.

Using the Grunt Task

Include the plugin in your Gruntfile.js:

require('google-closure-compiler').grunt(grunt);
// The load-grunt-tasks plugin won't automatically load closure-compiler

Task targets, files and options may be specified according to the grunt Configuring tasks guide.

Basic Configuration Example:
// Project configuration.
grunt.initConfig({
  'closure-compiler': {
    my_target: {
      files: {
        'dest/output.min.js': ['src/js/**/*.js']
      },
      options: {
        compilation_level: 'SIMPLE',
        language_in: 'ECMASCRIPT5_STRICT',
        create_source_map: 'dest/output.min.js.map',
        output_wrapper: '(function(){\n%output%\n}).call(this)\n//# sourceMappingURL=output.min.js.map'
      }
    }
  }
});
Closure Library Example:
// Project configuration.
grunt.initConfig({
  'closure-compiler': {
    my_target: {
      files: {
        'dest/output.min.js': ['src/js/**/*.js']
      },
      options: {
        js: '/node_modules/google-closure-library/**.js'
        compilation_level: 'SIMPLE',
        manage_closure_dependencies: true,
        language_in: 'ECMASCRIPT5_STRICT',
        create_source_map: 'dest/output.min.js.map',
        output_wrapper: '(function(){\n%output%\n}).call(this)\n//# sourceMappingURL=output.min.js.map'
      }
    }
  }
});
Advanced Usage with Arguments Array:
// Project configuration.
grunt.initConfig({
  'closure-compiler': {
    my_target: {
      options: {
        // When args is present, all other options are ignored
        args: [
          '--js', '/file-one.js',
          '--js', '/file-two.js',
          '--compilation_level', 'ADVANCED',
          '--js_output_file', 'out.js',
          '--debug'
        ]
      }
    }
  }
});

Using the Gulp Plugin

The gulp plugin supports piping multiple files through the compiler.

Options are a direct match to the compiler flags without the leading "--".

Basic Configuration Example:
var closureCompiler = require('google-closure-compiler').gulp();

gulp.task('js-compile', function () {
  return gulp.src('./src/js/**/*.js', {base: './'})
      .pipe(closureCompiler({
          compilation_level: 'SIMPLE',
          warning_level: 'VERBOSE',
          language_in: 'ECMASCRIPT6_STRICT',
          language_out: 'ECMASCRIPT5_STRICT',
          output_wrapper: '(function(){\n%output%\n}).call(this)\n//# sourceMappingURL=output.min.js.map'
          js_output_file: 'output.min.js'
        }))
      .pipe(gulp.dest('./dist/js'));
});

Use without gulp.src

Gulp files are all read into memory, transformed into a JSON stream, and piped through the compiler. With large source sets this can lead to performance issues.

Closure-compiler can natively expand file globs which will greatly alleviate this issue.

var closureCompiler = require('google-closure-compiler').gulp();

gulp.task('js-compile', function () {
  return closureCompiler({
        js: './src/js/**.js',
        compilation_level: 'SIMPLE',
        warning_level: 'VERBOSE',
        language_in: 'ECMASCRIPT6_STRICT',
        language_out: 'ECMASCRIPT5_STRICT',
        output_wrapper: '(function(){\n%output%\n}).call(this)\n//# sourceMappingURL=output.min.js.map'
        js_output_file: 'output.min.js'
      })
      .pipe(gulp.dest('./dist/js'));
});
gulp.src base option

Gulp attempts to set the base of a glob from the point of the first wildcard. This isn't always what is desired. Users can specify the { base: 'path' } option to gulp.src calls to override this behavior.

Advanced Usage with Arguments Array:
var closureCompiler = require('google-closure-compiler').gulp();

gulp.task('js-compile', function () {
  return closureCompiler([
        '--js', '/file-one.js',
        '--js', '/file-two.js',
        '--compilation_level', 'ADVANCED',
        '--js_output_file', 'out.js',
        '--debug'
      ])
      .pipe(gulp.dest('./dist/js'));
});

Gulp Sourcemaps

The gulp plugin supports gulp sourcemaps.

var closureCompiler = require('google-closure-compiler').gulp();
var sourcemaps = require('gulp-sourcemaps');

gulp.task('js-compile', function () {
  return gulp.src('./src/js/**/*.js', {base: './'})
      .pipe(sourcemaps.init())
      .pipe(closureCompiler({
          compilation_level: 'SIMPLE',
          warning_level: 'VERBOSE',
          language_in: 'ECMASCRIPT6_STRICT',
          language_out: 'ECMASCRIPT5_STRICT',
          output_wrapper: '(function(){\n%output%\n}).call(this)\n//# sourceMappingURL=output.min.js.map'
          js_output_file: 'output.min.js'
        }))
      .pipe(souremaps.write('/'))
      .pipe(gulp.dest('./dist/js'));
});

Windows Path Length Limitations

Windows command shells have a maximum length for a command. This is surprisingly easy to hit when you allow the build tools to expand globs for large sets of source files for the compiler.

This can be avoided by specifying the input globs as compiler arguments via the --js flag and let it expand the files. You can even mix these techniques. Files specified via js options will specified first.

A flagfile can also be used to workaround this issue.

Grunt Example

  'closure-compiler': {
    my_target: {
      files: {
        'dest/out.min.js': ['src/**/*.js']
      },
      options: {
        js: 'node_modules/google-closure-library/**.js'
        // other options here
      }
    }
  }

Gulp Example

var closureCompiler = require('google-closure-compiler').gulp();
gulp.src('src/**/*.js')
    .pipe(closureCompiler({
      js: 'node_modules/google-closure-library/**.js'
      // other options here
    })
    .pipe(gulp.dest('dist/out.min.js'));

Plugin Authors and Native Node Usage

A low-level node class is included to facilitate spawning the compiler jar as a process from Node. In addition, it exposes a static property with the path to the compiler jar file.

var ClosureCompiler = require('google-closure-compiler').compiler;

console.log(ClosureCompiler.jar_path); // absolute path the compiler jar

var closureCompiler = new ClosureCompiler({
  js: 'file-one.js',
  compilation_level: 'ADVANCED'
});

var compilerProcess = closureCompiler.run(function(exitCode, stdOut, stdErr) {
  //compilation complete
});

License

Copyright 2015 The Closure Compiler Authors

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Version History

Closure Compiler release notes can be found on the main repository wiki.

Keywords

FAQs

Last updated on 19 Nov 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc