如何将JavaScript对象的所有函数导出为模块,而不必对文件进行大量更改



我有这个原始的非模块化JavaScript文件(luxon.js(。

然后我有一个JavaScript文件(实际上是TypeScript(,我在其中导入它,如下所示:

import * as luxon from './luxon.js';

所以不,我需要把luxon.js变成一个模块。我通过删除来做到这一点

var luxon = (function (exports) { 

}({}));

然后,我在末尾添加以下行。

export { DateTime, Duration, Interval, Info, Zone, FixedOffsetZone, IANAZone, InvalidZone, LocalZone, Settings };

这很好(在谷歌chrome中(
但假设这是更多的功能
我如何做类似的事情

exports exports;

export * from exports;

所以我不必手动枚举对象"中已经存在的所有函数;出口"?

注意:
对象导出存在,并且所有要导出的函数都已分配给其自身。

var exports = {};
exports.DateTime = DateTime;
exports.Duration = Duration;
exports.Interval = Interval;
exports.Info = Info;
exports.Zone = Zone;
exports.FixedOffsetZone = FixedOffsetZone;
exports.IANAZone = IANAZone;
exports.InvalidZone = InvalidZone;
exports.LocalZone = LocalZone;
exports.Settings = Settings;

另外请注意,请不要向我提供指向npm存储库的链接,在那里luxon已经被模块化了
我对luxon不感兴趣,我感兴趣的是如何快速模块化还不是模块的东西(只需最少的努力(。

我设法做到了。我建议您使用ES6版本的库。如果您仍然希望使用浏览器全局版本作为模块,请执行以下步骤。

luxon.js的变化:

更改此

var luxon = (function (exports) {
'use strict';

通过这个

export default (function (exports) {
'use strict'

index.html

<script type="module" src="index.js"></script>

index.js

import luxon from "./luxon.js";
console.log(luxon.DateTime.local().setZone('America/New_York').minus({ weeks: 1 }).endOf('day').toISO());

相关内容

最新更新