我正在学习npm模块,并且我试图将此模块打包为npm包。
我已阅读有关如何创建npm的文档
https://docs.npmjs.com/getting-started/creating-node-modules
我理解导出函数是必需的,我的问题是,我应该将以下函数包装为导出语句!? : - /
/*!
* fullcalendar-rightclick v1.8
* Docs & License: https://github.com/mherrmann/fullcalendar-rightclick
* (c) 2015 Michael Herrmann
*/
(function($) {
function monkeyPatchViewClass(View) {
View = View.class || View;
var renderFn = 'render' in View.prototype ? 'render' : 'renderDates';
var originalRender = View.prototype[renderFn];
View.prototype[renderFn] = function() {
originalRender.call(this);
if (! this.el.data('fullcalendar-rightclick')) {
this.registerRightclickListener();
this.el.data('fullcalendar-rightclick', true)
}
};
View.prototype.registerRightclickListener = function() {
var that = this;
this.el.on('contextmenu', function(ev) {
var eventElt = $(ev.target).closest('.fc-event');
if (eventElt.length) {
var seg = eventElt.data('fc-seg');
return that.trigger('eventRightclick', this, seg.event, ev);
} else {
// Users of this library may add custom content inside
// FullCalendar's DOM structure, eg. popovers. We don't want
// to catch rightclicks on these custom elements, so we
// check that the clicked element actually lies inside one
// of FullCalendars default containers:
var fcContainer = $(ev.target).closest(
'.fc-bg, .fc-slats, .fc-content-skeleton, ' +
'.fc-bgevent-skeleton, .fc-highlight-skeleton'
);
if (fcContainer.length) {
var cell;
if (that.coordMap) {
// FullCalendar < 2.5.0:
that.coordMap.build();
cell = that.coordMap.getCell(ev.pageX, ev.pageY);
} else {
// FullCalendar >= 2.5.0:
that.prepareHits();
var hit = that.queryHit(ev.pageX, ev.pageY);
cell = that.getHitSpan(hit);
}
if (cell)
return that.trigger(
'dayRightclick', null, cell.start, ev
);
}
}
});
}
}
var fc = $.fullCalendar;
monkeyPatchViewClass(fc.views.agenda);
monkeyPatchViewClass(fc.views.basic);
})(jQuery);
答案 0 :(得分:0)
不,你不需要将模块代码包装到函数范围内 nodejs模块的内容在它自己的私有作用域中执行,你使用module.exports将其内容暴露给其他模块。
您可能有兴趣阅读nodejs modules以了解详情。
编辑:
刚刚意识到您正在尝试使用针对浏览器环境的第三方库。如果你想使用npm构建浏览器库,通常会将它与browserify或webpack一起用作构建过程的一部分,这会将nodejs模块转换为浏览器可加载脚本并插入所有必要的行李。