shramov / leaflet-plugins

Plugins for Leaflet library
http://psha.org.ru/b/leaflet-plugins.html
MIT License
722 stars 289 forks source link

Be module-friendly #158

Open efc-awebb opened 9 years ago

efc-awebb commented 9 years ago

These classes can be made module-friendly by using a wrapper like this (credit to @sheppard).

(function (factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD
        define(['leaflet'], factory);
    } else if (typeof module !== 'undefined') {
        // Node/CommonJS
        module.exports = factory(require('leaflet'));
    } else {
        // Browser globals
        if (typeof this.L === 'undefined')
            throw 'Leaflet must be loaded first!';
        factory(this.L);
    }
}(function (L) {

L.BingLayer = /* ... */

return L.BingLayer;

}));

This would enable, for example:

var L = require('leaflet');

// Add BingLayer support
var BingLayer = require('./node_modules/leaflet-plugins/layer/tile/Bing');
BingLayer(L);

As it stands, developers writing modular code would have to either monkey-patch that wrapper into place or set up a shim to convert the code into a modular form - neither of which is particularly desirable.

dmitry commented 9 years ago

:+1: nice idea!

johnd0e commented 5 years ago

This is mentioned in Leaflet's official PLUGIN-GUIDE.md

brunob commented 5 years ago

I'm on it...

johnd0e commented 5 years ago

But I do not think that mentioned pattern should be put directly into source files. Better if it'd done in some build process.

brunob commented 5 years ago

We don't have build script and i think this is oversized to use one for that.

johnd0e commented 5 years ago

Well, if you prefer to see same ugly pattern in ~15 source files, instead of simple build script..

brunob commented 5 years ago

Yes, KISS https://en.wikipedia.org/wiki/KISS_principle

johnd0e commented 5 years ago

I like KISS, but not WET.

How about DRY https://en.m.wikipedia.org/wiki/Don't_repeat_yourself?

brunob commented 5 years ago

huhu, sorry but i prefer to keep the release process simple (push a tag & publish to npm).

Anyway, feel free to provide a PR or an issue to describe the build process you would like to use.

gelinger777 commented 3 years ago

related to #267

GhadaAJIMI commented 2 years ago

As a user of this pluggin do I have to do this to inclure the rotate function into leaflet ?