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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With