我如何有效地将 google-closure javascript 转换为现代 ES6?
How could I effectively convert google-closure javascript to modern ES6?
我有一个使用 google-闭包样式模块 goog.provide
和 classes 等的代码库
我想使这个代码库现代化,但它大约有 15,000 行代码,我希望有一个工具可以帮助我。
这些工具坏了,我尝试了很多方法让它们工作,但它们就是不工作。这是一个失败的原因。
https://github.com/angular/clutz
https://github.com/DreierF/closure-es6-converter
我想如果我有一个工具可以将闭包 classes 和模块更新为 ES6 变体,我可以手动完成其他所有事情。
此工具可用于更新到现代 ES6 class 语法,但值得注意的是不会更新闭包模块。
https://github.com/lebab/lebab
这个工具可以让我将闭包模块转换为 es6 模块吗?或者它只产生缩小的输出。
https://webpack.js.org/plugins/closure-webpack-plugin/#usage-example
关于此您应该阅读 2 个文档:
ES6 modules and Closure interop
和
Migrating from goog.modules to ES6 modules
每个人的一些亮点:
ES6 modules can use goog.require like goog.modules to reference Closure files:
const math = goog.require('goog.math');
export const MY_CONSTANT = math.sum(1, 1, 2, 3, 5, 8, 13, 21);
The Closure Library has the function goog.declareModuleId(/** string */ id)
, which, when called in an ES6 module, associates the ES6 module with a goog.module-like id (e.g. something that can be goog.require'd
, goog.module.getd
, goog.forwardDeclared
, and goog.requireTyped
, and does not create any global symbols). goog.requires for these symbols will return the entire module object, as if bound by import * as.
goog.declareModuleId('example.es6');
export const MY_CONSTANT = 'Hello!';
和
Closure module default exports
Closure modules can have a "default" export via exports = value;. When using this syntax the module's exports become the default export. This is frequently used with modules that export a single class.
goog.module('my.Class');
class Class {}
// Default export. goog.require for this module returns this class.
exports = Class;
What about migrating Closure modules that call declareLegacyNamespace?
goog.module.declareLegacyNamespace is not supported in ES6 modules. It breaks a fundamental principle of modules: that modules do not create global values. Its purpose was to allow migration from goog.provide to Closure modules. We do not support it when moving to ES6 modules.
It is recommended you migrate all goog.provided files that are relying on the declareLegacyNamespace call to Closure or ES6 modules first, or at the very least have them call goog.module.get in a goog.scope to get references to modules. If this is not possible and migration of the Closure module is still desired, you'll need to follow similar steps to migrating a Closure module with default exports, except your *_shim.js file will call declareLegacyNamespace. You will only be able to delete this file once all the goog.provide files have migrated.
我有一个使用 google-闭包样式模块 goog.provide
和 classes 等的代码库
我想使这个代码库现代化,但它大约有 15,000 行代码,我希望有一个工具可以帮助我。
这些工具坏了,我尝试了很多方法让它们工作,但它们就是不工作。这是一个失败的原因。 https://github.com/angular/clutz https://github.com/DreierF/closure-es6-converter
我想如果我有一个工具可以将闭包 classes 和模块更新为 ES6 变体,我可以手动完成其他所有事情。
此工具可用于更新到现代 ES6 class 语法,但值得注意的是不会更新闭包模块。 https://github.com/lebab/lebab
这个工具可以让我将闭包模块转换为 es6 模块吗?或者它只产生缩小的输出。 https://webpack.js.org/plugins/closure-webpack-plugin/#usage-example
关于此您应该阅读 2 个文档:
ES6 modules and Closure interop
和
Migrating from goog.modules to ES6 modules
每个人的一些亮点:
ES6 modules can use goog.require like goog.modules to reference Closure files:
const math = goog.require('goog.math'); export const MY_CONSTANT = math.sum(1, 1, 2, 3, 5, 8, 13, 21);
The Closure Library has the function
goog.declareModuleId(/** string */ id)
, which, when called in an ES6 module, associates the ES6 module with a goog.module-like id (e.g. something that can begoog.require'd
,goog.module.getd
,goog.forwardDeclared
, andgoog.requireTyped
, and does not create any global symbols). goog.requires for these symbols will return the entire module object, as if bound by import * as.goog.declareModuleId('example.es6'); export const MY_CONSTANT = 'Hello!';
和
Closure module default exports Closure modules can have a "default" export via exports = value;. When using this syntax the module's exports become the default export. This is frequently used with modules that export a single class.
goog.module('my.Class'); class Class {} // Default export. goog.require for this module returns this class. exports = Class;
What about migrating Closure modules that call declareLegacyNamespace? goog.module.declareLegacyNamespace is not supported in ES6 modules. It breaks a fundamental principle of modules: that modules do not create global values. Its purpose was to allow migration from goog.provide to Closure modules. We do not support it when moving to ES6 modules.
It is recommended you migrate all goog.provided files that are relying on the declareLegacyNamespace call to Closure or ES6 modules first, or at the very least have them call goog.module.get in a goog.scope to get references to modules. If this is not possible and migration of the Closure module is still desired, you'll need to follow similar steps to migrating a Closure module with default exports, except your *_shim.js file will call declareLegacyNamespace. You will only be able to delete this file once all the goog.provide files have migrated.