leaflet-plugins icon indicating copy to clipboard operation
leaflet-plugins copied to clipboard

Be module-friendly

Open efc-awebb opened this issue 10 years ago • 11 comments

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.

efc-awebb avatar Mar 24 '15 15:03 efc-awebb

:+1: nice idea!

dmitry avatar Jul 21 '15 15:07 dmitry

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

johnd0e avatar Jul 10 '19 10:07 johnd0e

I'm on it...

brunob avatar Jul 24 '19 14:07 brunob

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

johnd0e avatar Jul 24 '19 15:07 johnd0e

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

brunob avatar Jul 24 '19 15:07 brunob

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

johnd0e avatar Jul 24 '19 16:07 johnd0e

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

brunob avatar Jul 24 '19 16:07 brunob

I like KISS, but not WET.

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

johnd0e avatar Jul 24 '19 20:07 johnd0e

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.

brunob avatar Jul 25 '19 05:07 brunob

related to #267

gelinger777 avatar Jun 16 '21 17:06 gelinger777

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

GhadaAJIMI avatar Jun 15 '22 15:06 GhadaAJIMI