EternallyCurious
EternallyCurious

Reputation: 2415

Running Google Closure compiler with grunt

I am trying to create a grunt task to build a javascript project using requirejs with closure compiler as the optimizer. I am using the grunt-contrib-requirejs plugin.

https://www.npmjs.org/package/grunt-contrib-requirejs

Although I haven't explicitly setup the closure compiler, here is the config and the error:

Config:

requirejs:
      compile:
        options:
          sourceMap: true
          baseUrl: "client"
          mainConfigFile: "main.js"
          name: "main.js"
          out: "build/main.js"
          optimize: "closure"
          #generateSourceMaps: true
          closure:
            CompilerOptions: {},
            CompilationLevel: 'SIMPLE_OPTIMIZATIONS'
            loggingLevel: 'WARNING'

Error:

C:\Users\Project>grunt requirejs
Running "requirejs:compile" (requirejs) task
{ [Error: Error: optimizer with name of "closure" not found for this environment

    at Object.optimize.js (C:\Users\Project\node_modules\requirejs\bin\r.js:24771:27)
]
  originalError: [Error: optimizer with name of "closure" not found for this env
ironment] }

Closure is written in java and my project is in nodejs and I'm not sure how to set it up right.

Upvotes: 2

Views: 1908

Answers (1)

Blaise
Blaise

Reputation: 13479

The documentation states that Closure as an optimizer can only be used when running requirejs in Java. You're running requirejs in Node which is JavaScript, not Java.

You can work around this issue by doing it in two separate steps:

  1. Run the RequireJs task, output to a temporary file
  2. Run another task that Closure-compiles the temporary file to the final file

You can use this Grunt package if you want to run the Closure Java binary on your own machine. Alternatively, you could use my Grunt Closure script that uses Closure REST API, which doesn't require Java or the Closure binary to be installed on your system.

Upvotes: 4

Related Questions