Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google closure compiler process node_modules

I'm trying the Google Closure Compiler as an alternative to webpack. Currently I'm using the following to compile all files inside my frontend folder:

java -jar closure-compiler.jar --process_common_js_modules --js_output_file=static/out.js 'lib/js/src/frontend/*.js'"

The problem is one of those files requires React & ReactDOM. I get the following error:

lib/js/src/frontend/app.js:7: ERROR - Failed to load module "react"
var React    = require("react");
           ^^^^^^^^^^^^^^^^

lib/js/src/frontend/app.js:8: ERROR - Failed to load module "react-dom"
var ReactDom = require("react-dom");
           ^^^^^^^^^^^^^^^^^^^^

How do I make sure that the Google Closure Compiler looks inside the node_modules folder to find the relevant third-party modules?

like image 780
Seneca Avatar asked Jan 24 '17 16:01

Seneca


1 Answers

Recent versions of Closure-Compiler support node modules.

The compiler will not discover source files - you have to pass the source for each module to the compiler. In addition, you need to pass any package.json files from the packages to the compiler as well using --js node_modules/react/package.json.

If you are using modules throughout your project, you probably want to use the dependency management flags so that the compiler automatically sorts your source files for you.

Based on your example above, your compilation command might look like this:

java -jar closure-compiler.jar --process_common_js_modules
    --dependency_mode=STRICT
    --module_resolution=NODE
    --entry_point="lib/js/src/frontend/app"
    --js_output_file=static/out.js
    --js='node_modules/react/package.json'
    --js='node_modules/react/**/*.js'
    --js='node_modules/react-dom/package.json'
    --js='node_modules/react-dom/**/*.js'
    --js='lib/js/src/frontend/*.js'

Full documentation and some additional tooling to utilize these features will be available shortly after the next release.

like image 145
Chad Killingsworth Avatar answered Oct 28 '22 20:10

Chad Killingsworth