编译器+库:如何设置库文件夹,但不编译?

Compiler+Library: how to set library folder, but don't compile it?

我有一个使用 Openlayers 库的应用程序。 Openlayers 使用闭包库。

我想将所有内容(应用程序、Openlayers 和 Closure 库)编译到一个 whole_application.js 文件中。

问题是 Closure Compiler 在所有源上运行,将未使用的函数放入我的 whole_application.js 文件中,例如 goog.ui.

我的应用程序是用 ES5 编写的,围绕一个简单的 goog.require/goog.provide。 (我也想移植到ES6)

我想设置类似 root.js 的东西 - 依赖关系树的根 - 一个包含所有 goog.require 的文件,我可以在其中写入

goog.provide('myApplication');

goog.require('ol.source.Vector'); // openlayers module
goog.require('myApplication.someModule');

并且要开心。 有什么办法吗?

或者,我可以设置包含我的源代码的文件夹以编译其中的所有内容,并设置一个具有依赖项的文件夹(例如 openlayers/closure 库)以仅在那里搜索所需的依赖项吗?

或者,也许还有其他解决方案?

Compilation/Deployment 与 gulp / google-closure-compiler 一起执行。 这是脚本:

dirs.deploy = 'folder/with/my/sources'

var closureLibraryPath = './node_modules/google-closure-library/'

var closureLibraryExcludes = [
  "!**/*_test.js",
  "!**/*_perf.js",
  "!**/*tester.js",
  "!**/*promise/testsuiteadapter.js",
  "!**/*osapi/osapi.js",
  "!**/*svgpan/svgpan.js",
  "!**/*alltests.js",
  "!**/*node_modules**.js",
  "!**/*protractor_spec.js",
  "!**/*protractor.conf.js",
  "!**/*browser_capabilities.js",
  "!./doc/**.js"
];

gulp.task('deps-deploy', ['copy'], function (cb) {
  var exec = require('child_process').exec;
  exec(`
    cd ` + dirs.deploy + `/js &&
    chmod 644 deps.js &&
    "./` + closureLibraryPath + 'closure/bin/build/depswriter.py" ui.js --root_with_prefix="assets assets" > deps.js');
});


gulp.task('process-broadcast-assets', ['deps-deploy', 'copy', 'clean'], function() {
      var compilerOptions = {
    "compilation_level": "SIMPLE",
    "language_in": "ECMASCRIPT6",
    "language_out": "ECMASCRIPT5",
    "source_map_format": "V3",
    "warning_level": "QUIET",
    "formatting": "PRETTY_PRINT",
    "js_output_file": "whole_application.js",
    "output_wrapper": "%output%"
  };
  var paths = [].concat(dirs.deploy + '/js/assets/**/*.js', closureLibraryPath + 'closure/goog/**/*.js', closureLibraryPath + 'third_party/**/*.js' );
      paths = paths.concat(closureLibraryExcludes);
      return gulp.src(paths)
      .pipe(chmod(644))
      .pipe(closureCompiler(compilerOptions))
      .on('warning', onWarning)
      .on('error', onError)
      .pipe(gulp.dest(dirs.deploy + '/js'));
});

谢谢。

The problem is that Closure Compiler runs on all sources, putting the unused functions into my whole_application.js file, such as goog.ui.

您需要使用 dependency management 标志。根据您的示例,您可能需要 STRICT 模式和 goog:myApplication 的入口点。编译器将删除您的应用程序不需要的任何文件。