slyx
slyx

Reputation: 2264

Does closure compiler support CommonJS-style require with js file?

I'm trying to use google closure compiler.

I followed the example in google-closure-compiler-js with processCommonJsModules flag:

gulp.task('script', function () {
  return gulp.src('./src/app/index.js')
    .pipe(compiler({
      compilationLevel: 'SIMPLE',
      warningLevel: 'VERBOSE',
      jsOutputFile: 'output.min.js',  // outputs single file
      createSourceMap: true,
      processCommonJsModules: true
    }));
    .pipe(gulp.dest('./dist'));
});

and, index.js contains require('./app.js').

This doesn't work and the error message is very unhelpful:

[17:59:04] src\app\index.js:2 (JSC_COMMONJS_MODULE_LOAD_ERROR)
[17:59:04] Failed to load module "./app.js"
[17:59:04] require('./app.js');

That's all. It just says that compilation failed without mentioning the reason.

I've read the documentation about modules in closure compiler repository (https://github.com/google/closure-compiler/wiki/JS-Modules), and I found that a description: Paths must be absolute or relative and omit the file extension.

I've tried next four ways but none of them works:

Maybe closure compiler does not support requiring js file but module?

Or, am I missing something?

Upvotes: 2

Views: 426

Answers (1)

Chad Killingsworth
Chad Killingsworth

Reputation: 14411

Closure-compiler won't discover source files - you have to pass in all possible module sources. Also, you need to use the dependency management options so that the compiler properly orders your source files.

I would expect your gulp script to look something similar to:

gulp.task('script', function () {
  // modify this to get all of your source files
  return gulp.src('./src/app/**') 
    .pipe(compiler({
      compilationLevel: 'SIMPLE',
      warningLevel: 'VERBOSE',
      jsOutputFile: 'output.min.js',  // outputs single file
      createSourceMap: true,
      processCommonJsModules: true,
      dependency_mode: 'LOOSE',
      entry_point: './src/app/index'
    }));
    .pipe(gulp.dest('./dist'));
});

Upvotes: 2

Related Questions