gcc

1.0.2 • Public • Published

gcc

This module is being deprecated in favor of closurecompiler, and will be eventually unpublished. Please migrate.

Node wrapper for Google Closure Compiler.

Changelog

GCC upholds the Semantic Versioning Specification.

Installation

npm install gcc

Usage

var compiler = require('gcc');
var source = ['file.js', 'file2.js'];
var destination = 'result.js';
var options = { compilation_level: 'WHITESPACE_ONLY' };
var callback = function (error, stdout, stderr) {
    if (error) {
        console.error(error);
    } else {
        console.log('Compiled size: ' + stdout.length + 'bytes');
    }
};
 
compiler.compile(source, destination, options, callback);

API

Assuming:

var compiler = require('gcc');

compiler.java

String specifying java command. Default: java

If java is not exposed globally in system PATH, set the destination to it here.

compiler.defaults

Options for closure compiler that will be used in every compile() call unless overridden by options argument. Contains:

{
    compilation_level: 'SIMPLE_OPTIMIZATIONS'
}

compiler.compile()

Function that does the compiling. Arguments:

compiler.compile(source [, destination ] [, options ] [, callback ]);
  • source Mixed : Path, or an array of paths to JavaScript files that should be concatenated and minified.
  • destination String : When specified, output will be saved to this path.
  • options Object : Object with Closure Compiler options. Extends compiler.defaults.
  • callback Function : When specified, will be executed at the end of the compiling process. Accepts 3 arguments: error which is null when no errors occurred, stdout containing the minified code, and stderr containing the error output.

Return

Returns the compiling process with stdout and stderr streams.

Examples

Compile example.js into example.min.js with WHITESPACE_ONLY compilation level.

compiler.compile('example.js', 'example.min.js', { compilation_level: 'WHITESPACE_ONLY' });

Compile example1.js and example2.js with default options, and handle the output in callback.

compiler.compile(['example1.js', 'example2.js'], function (error, stdout, stderr) {
    if (error) {
        console.log(error);
    } else {
        console.log('Minified size: ' + stdout.length + 'bytes');
    }
});

Compile example1.js and example2.js with custom options, and handle the compiling process manually.

var compiling = compiler.compile(['example1.js', 'example2.js'], { compilation_level: 'WHITESPACE_ONLY' });
 
compiling.stdout.on('data', function (data) {
    console.log('stdout: ' + data);
});
 
compiling.stderr.on('data', function (data) {
    console.log('stderr: ' + data);
});
 
compiling.on('close', function (code) {
    console.log('Compiling process has finished with exit code: ' + code);
});

Closure Compiler options

All options are passed as keys in an options object.

If an option is a flag with no input value, pass true as an option value:

compiler.compile('example.js', 'result.js', {
    debug: true
});

If an option accepts multiple values, pass them as an array to option value:

compiler.compile('example.js', 'result.js', {
    compilation_level: 'ADVANCED_OPTIMIZATIONS',
    externs: [
        'jquery.js',
        'underscore.js'
    ]
});

compilation_level:

  • WHITESPACE_ONLY : Removes comments, line breaks, unnecessary spaces, and other whitespace.
  • SIMPLE_OPTIMIZATIONS : ^ plus shortens local variables, function names, and function parameters.
  • ADVANCED_OPTIMIZATIONS : ^ but also for global variables, function names, and function parameters.

For more specific explanation, visit the Closure Compiler Compilation Levels documentation.


Rest of the Closure Compiler options as defined in --help

Note: Option js_output_file is ignored because when specified, compiler binary doesn't return stdout/err, but only writes to the file which breaks callback arguments. Use the compiler() destination argument instead please. The destination file is saved with fs writing stream consuming the stdout.

 --accept_const_keyword                 : Allows usage of const keyword.
 --angular_pass                         : Generate $inject properties for
                                          AngularJS for functions annotated
                                          with @ngInject
 --charset VAL                          : Input and output charset for all
                                          files. By default, we accept UTF-8 as
                                          input and output US_ASCII
 --closure_entry_point VAL              : Entry points to the program. Must be
                                          goog.provide'd symbols. Any goog.provi
                                          de'd symbols that are not a transitive
                                          dependency of the entry points will
                                          be removed. Files without goog.provide
                                          s, and their dependencies, will
                                          always be left in. If any entry
                                          points are specified, then the
                                          manage_closure_dependencies option
                                          will be set to true and all files
                                          will be sorted in dependency order.
 --common_js_entry_module VAL           : Root of your common JS dependency
                                          hierarchy. Your main script.
 --common_js_module_path_prefix VAL     : Path prefix to be removed from
                                          CommonJS module names.
 --compilation_level [WHITESPACE_ONLY   : Specifies the compilation level to
 | SIMPLE_OPTIMIZATIONS | ADVANCED_OPTI : use. Options: WHITESPACE_ONLY,
 MIZATIONS]                             : SIMPLE_OPTIMIZATIONS, ADVANCED_OPTIMIZ
                                          ATIONS
 --create_name_map_files                : If true, variable renaming and
                                          property renaming map files will be
                                          produced as {binary name}_vars_map.out
                                          and {binary name}_props_map.out. Note
                                          that this flag cannot be used in
                                          conjunction with either variableMapOut
                                          putFile or property_map_output_file
 --create_source_map VAL                : If specified, a source map file
                                          mapping the generated source files
                                          back to the original source file will
                                          be output to the specified path. The
                                          %outname% placeholder will expand to
                                          the name of the output file that the
                                          source map corresponds to.
 --debug                                : Enable debugging options
 --define (--D, -D) VAL                 : Override the value of a variable
                                          annotated @define. The format is
                                          <name>[=<val>], where <name> is the
                                          name of a @define variable and <val>
                                          is a boolean, number, or a single-quot
                                          ed string that contains no single
                                          quotes. If [=<val>] is omitted, the
                                          variable is marked true
 --externs VAL                          : The file containing JavaScript
                                          externs. You may specify multiple
 --extra_annotation_name VAL            : A whitelist of tag names in JSDoc.
                                          You may specify multiple
 --flagfile VAL                         : A file containing additional command-l
                                          ine options.
 --formatting [PRETTY_PRINT | PRINT_INP : Specifies which formatting options,
 UT_DELIMITER | SINGLE_QUOTES]          : if any, should be applied to the
                                          output JS. Options: PRETTY_PRINT,
                                          PRINT_INPUT_DELIMITER, SINGLE_QUOTES
 --generate_exports                     : Generates export code for those
                                          marked with @export
 --help                                 : Displays this message
 --js VAL                               : The JavaScript filename. You may
                                          specify multiple
 --js_output_file VAL                   : Primary output filename. If not
                                          specified, output is written to stdout
 --jscomp_error VAL                     : Make the named class of warnings an
                                          error. Options:accessControls,
                                          ambiguousFunctionDecl, checkRegExp,
                                          checkStructDictInheritance, checkTypes
                                          , checkVars, const, constantProperty,
                                          deprecated, duplicateMessage,
                                          es5Strict, externsValidation,
                                          fileoverviewTags, globalThis,
                                          internetExplorerChecks, invalidCasts,
                                          misplacedTypeAnnotation, missingProper
                                          ties, missingReturn,nonStandardJsDocs,
                                          reportUnknownTypes, suspiciousCode,
                                          strictModuleDepCheck, typeInvalidation
                                          , undefinedNames, undefinedVars,
                                          unknownDefines, uselessCode, visibilit
                                          y
 --jscomp_off VAL                       : Turn off the named class of warnings.
                                          Options:accessControls, ambiguousFunct
                                          ionDecl, checkRegExp, checkStructDictI
                                          nheritance, checkTypes, checkVars,
                                          const, constantProperty, deprecated,
                                          duplicateMessage, es5Strict, externsVa
                                          lidation, fileoverviewTags, globalThis
                                          , internetExplorerChecks, invalidCasts
                                          , misplacedTypeAnnotation, missingProp
                                          erties, missingReturn,nonStandardJsDoc
                                          s, reportUnknownTypes, suspiciousCode,
                                          strictModuleDepCheck, typeInvalidation
                                          , undefinedNames, undefinedVars,
                                          unknownDefines, uselessCode, visibilit
                                          y
 --jscomp_warning VAL                   : Make the named class of warnings a
                                          normal warning. Options:accessControls
                                          , ambiguousFunctionDecl, checkRegExp,
                                          checkStructDictInheritance, checkTypes
                                          , checkVars, const, constantProperty,
                                          deprecated, duplicateMessage,
                                          es5Strict, externsValidation,
                                          fileoverviewTags, globalThis,
                                          internetExplorerChecks, invalidCasts,
                                          misplacedTypeAnnotation, missingProper
                                          ties, missingReturn,nonStandardJsDocs,
                                          reportUnknownTypes, suspiciousCode,
                                          strictModuleDepCheck, typeInvalidation
                                          , undefinedNames, undefinedVars,
                                          unknownDefines, uselessCode, visibilit
                                          y
 --language_in VAL                      : Sets what language spec that input
                                          sources conform. Options: ECMASCRIPT3
                                          (default), ECMASCRIPT5, ECMASCRIPT5_ST
                                          RICT
 --logging_level VAL                    : The logging level (standard java.util.
                                          logging.Level values) for Compiler
                                          progress. Does not control errors or
                                          warnings for the JavaScript code
                                          under compilation
 --manage_closure_dependencies          : Automatically sort dependencies so
                                          that a file that goog.provides symbol
                                          X will always come before a file that
                                          goog.requires symbol X. If an input
                                          provides symbols, and those symbols
                                          are never required, then that input
                                          will not be included in the compilatio
                                          n.
 --module VAL                           : A JavaScript module specification.
                                          The format is <name>:<num-js-files>[:[
                                          <dep>,...][:]]]. Module names must be
                                          unique. Each dep is the name of a
                                          module that this module depends on.
                                          Modules must be listed in dependency
                                          order, and JS source files must be
                                          listed in the corresponding order.
                                          Where --module flags occur in
                                          relation to --js flags is unimportant.
                                          Provide the value 'auto' to trigger
                                          module creation from CommonJSmodules.
 --module_output_path_prefix VAL        : Prefix for filenames of compiled JS
                                          modules. <module-name>.js will be
                                          appended to this prefix. Directories
                                          will be created as needed. Use with
                                          --module
 --module_wrapper VAL                   : An output wrapper for a JavaScript
                                          module (optional). The format is
                                          <name>:<wrapper>. The module name
                                          must correspond with a module
                                          specified using --module. The wrapper
                                          must contain %s as the code placeholde
                                          r. The %basename% placeholder can
                                          also be used to substitute the base
                                          name of the module output file.
 --only_closure_dependencies            : Only include files in the transitive
                                          dependency of the entry points
                                          (specified by closure_entry_point).
                                          Files that do not provide dependencies
                                          will be removed. This supersedesmanage
                                          _closure_dependencies
 --output_manifest VAL                  : Prints out a list of all the files in
                                          the compilation. If --manage_closure_d
                                          ependencies is on, this will not
                                          include files that got dropped
                                          because they were not required. The
                                          %outname% placeholder expands to the
                                          JS output file. If you're using
                                          modularization, using %outname% will
                                          create a manifest for each module.
 --output_module_dependencies VAL       : Prints out a JSON file of dependencies
                                          between modules.
 --output_wrapper VAL                   : Interpolate output into this string
                                          at the place denoted by the marker
                                          token %output%. Use marker token
                                          %output|jsstring% to do js string
                                          escaping on the output.
 --print_ast                            : Prints a dot file describing the
                                          internal abstract syntax tree and
                                          exits
 --print_pass_graph                     : Prints a dot file describing the
                                          passes that will get run and exits
 --print_tree                           : Prints out the parse tree and exits
 --process_closure_primitives           : Processes built-ins from the Closure
                                          library, such as goog.require(),
                                          goog.provide(), and goog.exportSymbol(
                                          )
 --process_common_js_modules            : Process CommonJS modules to a
                                          concatenable form.
 --process_jquery_primitives            : Processes built-ins from the Jquery
                                          library, such as jQuery.fn and
                                          jQuery.extend()
 --property_map_input_file VAL          : File containing the serialized
                                          version of the property renaming map
                                          produced by a previous compilation
 --property_map_output_file VAL         : File where the serialized version of
                                          the property renaming map produced
                                          should be saved
 --source_map_format [V1 | DEFAULT |    : The source map format to produce.
 V2 | V3]                               : Options: V1, V2, V3, DEFAULT. DEFAULT
                                          produces V2.
 --summary_detail_level N               : Controls how detailed the compilation
                                          summary is. Values: 0 (never print
                                          summary), 1 (print summary only if
                                          there are errors or warnings), 2
                                          (print summary if the 'checkTypes'
                                          diagnostic  group is enabled, see
                                          --jscomp_warning), 3 (always print
                                          summary). The default level is 1
 --third_party                          : Check source validity but do not
                                          enforce Closure style rules and
                                          conventions
 --transform_amd_modules                : Transform AMD to CommonJS modules.
 --translations_file VAL                : Source of translated messages.
                                          Currently only supports XTB.
 --translations_project VAL             : Scopes all translations to the
                                          specified project.When specified, we
                                          will use different message ids so
                                          that messages in different projects
                                          can have different translations.
 --use_only_custom_externs              : Specifies whether the default externs
                                          should be excluded
 --use_types_for_optimization           : Experimental: perform additional
                                          optimizations based on available
                                          information.  Inaccurate type
                                          annotations may result in incorrect
                                          results.
 --variable_map_input_file VAL          : File containing the serialized
                                          version of the variable renaming map
                                          produced by a previous compilation
 --variable_map_output_file VAL         : File where the serialized version of
                                          the variable renaming map produced
                                          should be saved
 --version                              : Prints the compiler version to stderr.
 --warning_level [QUIET | DEFAULT |     : Specifies the warning level to use.
 VERBOSE]                               : Options: QUIET, DEFAULT, VERBOSE
 --warnings_whitelist_file VAL          : A file containing warnings to
                                          suppress. Each line should be of the
                                          form
                                          <file-name>:<line-number>?  <warning-d
                                          escription>

Readme

Keywords

none

Package Sidebar

Install

npm i gcc

Weekly Downloads

336

Version

1.0.2

License

none

Last publish

Collaborators

  • darsain