简体   繁体   中英

How can I tell Closure compiler that something exists in a separate file?

I have a project which includes library.js and mycode.js . My code includes

let x = new LibraryObject ();

I tried running mycode.js through Google Closure which complains

ERROR - variable LibraryObject is undeclared

when the -W VERBOSE flag is given.

I could concatenate the two files into libraryandmycode.js but I want to keep them separate because reasons. I tried declaring LibraryObject in mycode.js but then I get an "illegal redeclared variable" error.

Without modifying library.js , can I add something to mycode.js which will establish that LibraryObject exists, but without redeclaring it?

This causes no problems in the browser but I want validation to pass for each file separately as well as when they're taken together.

The criteria is that no errors are produced under strict interpretation whether either of the two scripts are parsed individually, or when both are taken together.

Is this possible?

Extern files are used to provide type information to the compiler for symbols that are not included in the compilation. Extern files are valid JavaScript, but only contain type definitions - don't try to use the library itself.

Use the --externs flag to pass an extern file to the compiler.

The compiler projects hosts several extern files . It's also possible to write your own .

If you are looking for an example, the jQuery externs are well maintained.

This is how it works for me.

java -jar closure-compiler-v20160713.jar
The compiler is waiting for input via stdin.
let a= new Test ();
^Z
var a=new Test;

I definitely dont have Test() declared. Are you using a different version?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM