You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jun 5, 2020. It is now read-only.
Repository navigation
This repository was archived by the owner on Jun 5, 2020. It is now read-only.
In Class2.js there is a reference to Class1.js with require(''./Class1.js').
I could use absolute paths by setting an environment-variable in index.js and reading this in every require-statement. But I would like to know which is the best-practice solution for this problem.
Unfortunately this does not work. I would expect the require statement to get removed, because the required-file is in the compiler's source directory. If I would use absolute paths uncompiled sources would be loaded, which compiled version might be in the compilation result.
I am sorry, I do not seem to understand this fully. Of course you can change the require statement to whatever you like, for example: require(path.join(__dirname, "..", "Class1.js")); with __dirname being just a reference to the current directory of the file. If you are using ADVANCED_OPTIMIZATIONS, it might also be required to add externs so that the __dirname variable does not become renamed by CC. Or what am I getting wrong?
The closure compiler concatenates the three source files lib/Class1.js, lib/Class2.js and index.js and compiles them into a file, called compiled.js. The compiled.js should not reference to any of the uncompiled files with a require statement.
You are right, but some preprocessing could be added like replacing require-statements to source-files with assignments.
I only want the compiler to make static type check to my sources. I do not care about the result right now. How would you handle the following situtation?
// index.js
var TestClass = require('lib/TestClass.js');
// compiler should throw an error (does concatenation)
console.log(TestClass.add("hello", "world"));
// lib/TestClass.js
var TestClass = function() {};
/**
* @type {number}
*/
TestClass.prototype.attribute = "some initial value";
/**
* Adds two numbers.
*
* @param {number} a Operand a.
* @param {number} b Operand b.
* @return {number} Sum of a and b.
*/
TestClass.prototype.add = function(a, b) {
return a+b;
}
module.exports = TestClass;
The compilation for these files should fail due to the wrong invocation of "add" with strings instead of numbers AND the wrong type of the attribute. But I do get the following compilation result:
function a() {
}
a.prototype.add = function(b, c) {
return b + c;
};
module.a = a;
a = require("lib/TestClass.js");
console.log(a.add("hello", "world"));
^ This will require the file if run without Preprocessor as the inline function will return {} and not include anything. When run with Preprocessor with SINGLEFILE=true, it will include the file instead, making it safe to be compiled through CC afterwards. ProtoBuf.js uses Preprocessor.js for example for building different versions of the library prior to CC compilation.
So, I guess CC's CommonJS support is far from being usable.
To pass options (any that closure compiler supports) to ClosureCompiler.js:
new ClosureCompiler(options)
ClosureCompiler.compile(files, options, callback)
Or on the command line to ccjs
Remains browserify and Preprocessor.js. I am sorry that I cannot assist you any further, it's just that I am not a maintainer of closure compiler itself, only of this package.
Closure Compiler itself works fine for me with the following command: closure-compiler --process_common_js_modules --common_js_entry_module=./src/index.js --externs=./externs.js --compilation_level=ADVANCED_OPTIMIZATIONS src
However when I try to use ccjs I get the error Illegal source file after options: src.
Then I tried using ccjs like this: ccjs ./src/index.js --process_common_js_modules --common_js_entry_module=./src/index.js --externs=./externs.js --compilation_level=ADVANCED_OPTIMIZATIONS
Now it tells me that it can't resolve the module.
Activity
dcodeIO commentedon Dec 17, 2013
In Class2.js, try:
codinkai commentedon Dec 19, 2013
Unfortunately this does not work. I would expect the require statement to get removed, because the required-file is in the compiler's source directory. If I would use absolute paths uncompiled sources would be loaded, which compiled version might be in the compilation result.
dcodeIO commentedon Dec 19, 2013
I am sorry, I do not seem to understand this fully. Of course you can change the require statement to whatever you like, for example:
require(path.join(__dirname, "..", "Class1.js"));with __dirname being just a reference to the current directory of the file. If you are using ADVANCED_OPTIMIZATIONS, it might also be required to add externs so that the __dirname variable does not become renamed by CC. Or what am I getting wrong?codinkai commentedon Dec 19, 2013
The closure compiler concatenates the three source files lib/Class1.js, lib/Class2.js and index.js and compiles them into a file, called compiled.js. The compiled.js should not reference to any of the uncompiled files with a require statement.
dcodeIO commentedon Dec 19, 2013
I see. In that case, you might want to give Preprocessor a try to bundle your files, possibly leaving stuff out, prior to compilation.
There are also some CC switches for commonjs but I haven't used these, yet.
Just to make this clear: I cannot change the way Closure Compiler works, as ClosureCompiler.js is just a wrapper around it.
codinkai commentedon Dec 19, 2013
You are right, but some preprocessing could be added like replacing require-statements to source-files with assignments.
I only want the compiler to make static type check to my sources. I do not care about the result right now. How would you handle the following situtation?
The compilation for these files should fail due to the wrong invocation of "add" with strings instead of numbers AND the wrong type of the attribute. But I do get the following compilation result:
dcodeIO commentedon Dec 19, 2013
What I found on this: http://www.nonblocking.io/2011/12/experimental-support-for-common-js-and.html (CC internal)
Maybe this could be useful, too: http://browserify.org/ (bundles require statements)
Or, like mentioned above, using Preprocessor.js prior to compilation, e.g. by adding conditional blocks
^ This will require the file if run without Preprocessor as the inline function will return {} and not include anything. When run with Preprocessor with SINGLEFILE=true, it will include the file instead, making it safe to be compiled through CC afterwards. ProtoBuf.js uses Preprocessor.js for example for building different versions of the library prior to CC compilation.
codinkai commentedon Dec 27, 2013
TypeChecking works, when I invoke the compiler as follows. But I have the same problems as in https://groups.google.com/forum/#!topic/closure-compiler-discuss/1hPmS7EOhj8. Unfortunately I haven't found a solution for it yet.
By the way, how do I pass flag-options to the ClosureCompiler class?
codinkai commentedon Dec 27, 2013
https://groups.google.com/forum/#!topic/closure-compiler-discuss/acD1bSh3FAQ
dcodeIO commentedon Dec 27, 2013
So, I guess CC's CommonJS support is far from being usable.
To pass options (any that closure compiler supports) to ClosureCompiler.js:
new ClosureCompiler(options)ClosureCompiler.compile(files, options, callback)ccjsRemains browserify and Preprocessor.js. I am sorry that I cannot assist you any further, it's just that I am not a maintainer of closure compiler itself, only of this package.
nerdbeere commentedon Feb 23, 2016
Closure Compiler itself works fine for me with the following command:
closure-compiler --process_common_js_modules --common_js_entry_module=./src/index.js --externs=./externs.js --compilation_level=ADVANCED_OPTIMIZATIONS srcHowever when I try to use
ccjsI get the errorIllegal source file after options: src.Then I tried using ccjs like this:
ccjs ./src/index.js --process_common_js_modules --common_js_entry_module=./src/index.js --externs=./externs.js --compilation_level=ADVANCED_OPTIMIZATIONSNow it tells me that it can't resolve the module.