闭包编译器是否支持 CommonJS 风格的 js 文件?

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

我正在尝试使用 google 闭包编译器。

我遵循了 google-closure-compiler-js 中带有 processCommonJsModules 标志的示例:

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'));
});

并且,index.js 包含 require('./app.js')

这是行不通的,错误消息也没有用:

[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');

就是这样。只说编译失败,没说原因

我阅读了闭包编译器存储库 (https://github.com/google/closure-compiler/wiki/JS-Modules) 中有关模块的文档,我发现了一个描述:Paths must be absolute or relative and omit the file extension.

我尝试了接下来的四种方法,但其中 none 行得通:

可能闭包编译器不支持需要js文件而是模块?

或者,我是不是遗漏了什么?

Closure-compiler 不会发现源文件 - 您必须传入所有可能的模块源。此外,您需要使用 dependency management options 以便编译器正确排序您的源文件。

我希望您的 gulp 脚本看起来类似于:

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'));
});