Add `ClosureCompiler` filter. #170

Merged
merged 3 commits into from Mar 22, 2014

Projects

None yet

2 participants

@jadb

A new filter for those of us who want to use the Google Closure compiler API. Similar to the ClosureJS filter but without having to install JAVA nor the closure/compiler.jar.

The compilation level can be set using the level key in settings like so:

[filter_ClosureCompiler]
level = SIMPLE_OPTIMIZATIONS

One can also check statistics and/or warnings (only in debug mode):

[filter_ClosureCompiler]
statistics = true
warnings = true

Furthermore, extra parameters can be passed to the API like so:

[filter_ClosureCompiler]
level = ADVANCED_OPTIMIZATIONS
formatting = pretty_print
externs_url = http://www.myserver.com/myexterns.js

That's about it!

Hoping it is judged useful to others and gets added to the plugin's core.

Best,

Jad

@markstory markstory and 1 other commented on an outdated diff Jul 14, 2013
Lib/Filter/ClosureCompiler.php
+
+/**
+ * Settings.
+ *
+ * NOTE: statistics and warnings are only used when in debug mode.
+ *
+ * - level (string) Defaults to WHITESPACE_ONLY. Values: SIMPLE_OPTIMIZATIONS, ADVANCED_OPTIMIZATIONS.
+ * - print (string) How to output the errors, statistics and/or warnings.
+ * - statistics (boolean) Defaults to FALSE.
+ * - warnings (mixed) Defaults to FALSE. Values: TRUE or QUIET, DEFAULT, VERBOSE.
+ *
+ * @var array
+ */
+ protected $_settings = array(
+ 'level' => null,
+ 'print' => "%s:\n%s\n",
@markstory
markstory Jul 14, 2013

Does this need to be configurable?

@markstory markstory and 1 other commented on an outdated diff Jul 14, 2013
Lib/Filter/ClosureCompiler.php
+ 'exclude_default_externs',
+ 'formatting',
+ 'use_closure_library',
+ 'language'
+ );
+
+/**
+ * {@inheritdoc}
+ */
+ public function output($filename, $content) {
+ $defaults = array('compilation_level' => $this->_settings['level']);
+
+ $errors = $this->_query($content, array('output_info' => 'errors'));
+ if (!empty($errors)) {
+ printf($this->_settings['print'], 'Errors', $errors);
+ exit;
@markstory
markstory Jul 14, 2013

It is probably better to throw an exception instead of calling exit here.

@markstory markstory and 1 other commented on an outdated diff Jul 14, 2013
Lib/Filter/ClosureCompiler.php
+
+ foreach ($this->_settings as $setting => $value) {
+ if (!in_array($setting, array('warnings', 'statistics')) || true != $value) {
+ continue;
+ }
+
+ $args = array('output_info' => $setting);
+ if ('warnings' == $setting && in_array($value, array('QUIET', 'DEFAULT', 'VERBOSE'))) {
+ $args['warning_level'] = $value;
+ }
+
+ $$setting = $this->_query($content, $args);
+ printf($this->_settings['print'], ucfirst($setting), $$setting);
+ }
+
+ return $output;
@markstory
markstory Jul 14, 2013

Is it helpful to have the warnings dumped?

@jadb
jadb Jul 14, 2013

I believe so, depending on the compression level, different warnings are thrown.

@markstory markstory commented on the diff Jul 14, 2013
Lib/Filter/ClosureCompiler.php
+ * @throws Exception If curl triggers an error.
+ * @return string
+ */
+ protected function _query($content, $args = array()) {
+ $args = array_merge($this->_defaults, $args);
+ if (!empty($this->_settings['level'])) {
+ $args['compilation_level'] = $this->_settings['level'];
+ }
+
+ foreach ($this->_settings as $key => $val) {
+ if (in_array($key, $this->__params)) {
+ $args[$key] = $val;
+ }
+ }
+
+ $ch = curl_init();
@markstory
markstory Jul 14, 2013

Probably should throw an exception when the environment is lacking curl.

@markstory
markstory Feb 27, 2014

Using HttpSocket would be better as some environments may lack curl support.

@jadb

Bumping this to see if there are any chances this will get merged soon so I can use the original repo again?

@markstory markstory commented on the diff Feb 27, 2014
Lib/Filter/ClosureCompiler.php
+ return $output;
+ }
+
+ foreach ($this->_settings as $setting => $value) {
+ if (!in_array($setting, array('warnings', 'statistics')) || true != $value) {
+ continue;
+ }
+
+ $args = array('output_info' => $setting);
+ if ('warnings' == $setting && in_array($value, array('QUIET', 'DEFAULT', 'VERBOSE'))) {
+ $args['warning_level'] = $value;
+ }
+
+ $$setting = $this->_query($content, $args);
+ printf("%s:\n%s\n", ucfirst($setting), $$setting);
+ }
@markstory
markstory Feb 27, 2014

Why is this printing here?

@jadb
jadb Feb 27, 2014

For the shell command's output. Should I remove it?

@markstory
markstory Feb 27, 2014

I'm not overly familiar with the closure webservice, what purpose does printing this out serve?

@jadb
jadb Feb 27, 2014

Closure can return an output:

output_info
The value of this parameter indicates the kind of information that you want from the compiler.
There are four possible kinds of output: compiled_code, warnings, errors, and statistics. This
example uses the value compiled_code, which tells the Closure Compiler service to output the
compressed version of the JavaScript it receives in the request.

In here it's used to output 'warnings' and/or 'statistics'.

@markstory markstory added this to the 0.11 milestone Mar 4, 2014
@markstory markstory modified the milestone: 0.13, 0.11 Mar 22, 2014
@markstory markstory merged commit b9d4812 into markstory:master Mar 22, 2014

1 check passed

Details default The Travis CI build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment