-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ES6 modules & Rollup #4989
ES6 modules & Rollup #4989
Conversation
import {LatLngBounds, toLatLngBounds} from '../geo/LatLngBounds'; | ||
|
||
import { | ||
any3d as isAny3D, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
at least within iD and Studio, we've tried to avoid as
statements so that it's easier to track down code across the whole codebase. Minor nitpick, and not necessarily something you want to adopt, but I think it's worked well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'd like to avoid as
too, but the problem is that without namespaces, some of the exported names become awkward, and I wanted each file to export names that will end up in the namespace. See a397b63 for how the Browser
module looked, for example. Maybe we'll want to migrate to new names, but in a breaking way, in future.
@@ -15,7 +15,7 @@ import {wrapNum} from '../../core/Util'; | |||
* [Proj4Leaflet](https://github.com/kartena/Proj4Leaflet) plugin. | |||
*/ | |||
|
|||
export var Base = { | |||
export var Base = extend({}, { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why extend an empty object here here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind that, I was confused by eslint
throwing an error like A function with a name starting with an uppercase letter should only be used as a constructor new-cap
, but did not notice it was due to misuse of the Bounds
constructor
@@ -29,7 +29,8 @@ | |||
"test": "jake test", | |||
"build": "jake build", | |||
"release": "./build/publish.sh", | |||
"rollup": "rollup src/Leaflet.js -f umd -n L > bundle.js" | |||
"lint": "node_modules/.bin/eslint --fix src/", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
node_modules/.bin
is not necessary in package.json
scripts — it's in path automatically.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Somehow my Linux setup doesn't work like that. 😩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@IvanSanchez so eslint
doesn't work but rollup
and jake
do? That's weird.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my case, I put rollup
and jake
manually in my $PATH
because I was tired of running them locally (but they have to be updated/upgraded separately). Not so with eslint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well this is weird, it shouldn't be like that... But I'm sure others had a problem like this, should be googleable...
I have the same experience. ./node_modules/.bin has to be specified to take priority over globally installed executables. |
|
||
import gitRev from 'git-rev-sync' | ||
|
||
// TODO: There should be a way to skip the git branch+rev when doing a production build |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
process.env.NODE_ENV
or a rollup production configuration would do the trick.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, but I'll wait for @mourner's input on the issue. This will affect the build/publish.sh
script.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will it? As far as I see, gitRev
is only needed for the L.version
property and copyright comment, and publish.sh
takes the clean semver version from package.json
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I mean is that publish.sh
will probably need a third rollup config file, in order to skip the git rev.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can either set an env var (read from the config), or just leave git rev in the copyright and L.version
— no big deal.
@@ -30,7 +31,7 @@ export {Class} from './core/Class'; | |||
|
|||
import {Evented} from './core/Events'; | |||
export {Evented}; | |||
export var Mixin = {Events: Evented.prototype}; | |||
// export var Mixin = {Events: Evented.prototype}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accidental? I did this for backwards compat.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mourner I was not aware this was a backwards-compat issue, sorry 'bout that :-(
I tried to make some clever use of |
Let's rebase to fix the Canvas conflict, and also maybe ditch all uses of |
@mourner Rebased. I'll take a break from this branch, and will let someone else rewrite the named |
@IvanSanchez BTW what's up with the unrunnable Path tests? |
@mourner Maybe we should export |
792e3ea
to
1b124f0
Compare
1a686a6
to
addaa88
Compare
After some rebasing hell and some fiddling with the VML code, the IE8 builds work again. |
@@ -5,7 +5,7 @@ | |||
<div class="announcement">Jan 23, 2017 — <a href="http://leafletjs.com/2017/01/23/leaflet-1.0.3.html">Leaflet 1.0.3</a>, a bugfix release, is out.</div> | |||
|
|||
<p>Leaflet is the leading open-source JavaScript library for mobile-friendly interactive maps. | |||
Weighing just about <abbr title="33 KB gzipped — that's 123 KB minified and 218 KB in the source form, with 10 KB of CSS (2 KB gzipped) and 11 KB of images.">33 KB of JS</abbr>, | |||
Weighing just about <abbr title="38 KB gzipped — that's 133 KB minified and 378 KB in the source form, with 10 KB of CSS (2 KB gzipped) and 11 KB of images.">38 KB of JS</abbr>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoa, is leaflet-src.js
really 378kb?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, because we added docstrings to everything, and that kinda doubles the size of the file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I forgot about that :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
per@linfro:~/Documents/Projects/Leaflet$ npm run build
> leaflet@1.0.3 build /home/per/Documents/Projects/Leaflet
> jake build
Bundling and compressing 67 files...
Uncompressed: 372.64 KB (new)
Saved to dist/leaflet-src.js
Compressed: 141.92 KB (new)
Saved to dist/leaflet.js
Gzipped: 37.97 KB
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've reviewed this by actually trying to use this branch in a pretty large Leaflet based application. The application uses quite a few plugins as well.
From what I can tell, this branch now works with this application, I can't notice any differences between this an Leaflet 1.0.2, which we used before.
I think we need a more proper code review as well, but this at least indicates there are no obvious gotchas if we merge this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding index.js
files to subdirectories/namespace allows to streamline the import/export in the main file. I commented on a few lines with specific suggestions.
} else if (typeof define === 'function' && define.amd) { | ||
define(L); | ||
} | ||
export {Control, control}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a file ./control/index.js
and doing the preparation of Control
/control
there would streamline the code in Leaflet.js
down to
export {Control, control} from './control';
|
||
console.log('Bundling and compressing ' + files.length + ' files...'); | ||
|
||
var copy = fs.readFileSync('src/copyright.js', 'utf8').replace('{VERSION}', version), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The file copyright.js
can be removed.
import {TouchZoom} from './map/handler/Map.TouchZoom'; | ||
Map.TouchZoom = TouchZoom; | ||
|
||
export {Map, createMap as map} from './map/Map'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a file ./map/index.js
and doing the preparation of Map
there would streamline the code in Leaflet.js
down to
export {Map, map} from './map';
import {TileLayerWMS, tileLayerWMS} from './layer/tile/TileLayer.WMS'; | ||
TileLayer.WMS = TileLayerWMS; | ||
tileLayer.wms = tileLayerWMS; | ||
export {TileLayer, tileLayer}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a file ./layer/tile/index.js
and doing the preparation of TileLayer
/tileLayer
there would streamline the code in Leaflet.js
down to
export {GridLayer, gridLayer, TileLayer, tileLayer} from './layer/tile';
GeoJSON.latLngsToCoords = latLngsToCoords; | ||
GeoJSON.getFeature = getFeature; | ||
GeoJSON.asFeature = asFeature; | ||
export {GeoJSON, geoJSON, geoJson}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add the static functions to the GeoJSON class in ./layer/GeoJSON.js
? Or prepare and export a GeoJSON_with_static_functions
object in ./layer/GeoJSON.js
?
CRS.EPSG4326 = EPSG4326; | ||
CRS.Simple = Simple; | ||
|
||
export {CRS}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a file ./geo/crs/index.js
and doing the preparation of CRS
there would streamline the code in Leaflet.js
down to
export {CRS} from './geo/crs';
|
||
// geo/projection | ||
|
||
import * as Projection from './geo/projection/Projection'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename Projection.js
to index.js
to comply with other suggestions and the node.js module import.
export {Icon}; | ||
|
||
export {DivIcon, divIcon} from './layer/marker/DivIcon'; | ||
export {Marker, marker} from './layer/marker/Marker'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Combine all markers in ./layer/marker/index.js
and export them here all at once:
export{Icon, icon, DivIcon, divIcon, Marker, marker} from './layer/marker';
export {Circle, circle} from './layer/vector/Circle'; | ||
export {Polyline, polyline} from './layer/vector/Polyline'; | ||
export {Polygon, polygon} from './layer/vector/Polygon'; | ||
export {Rectangle, rectangle} from './layer/vector/Rectangle'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Combine all vector layer classes in ./layer/vector/index.js
and export them here all at once:
export{Renderer, Canvas, canvas, SVG, svg, Path, CircleMarker, circleMarker, Circle, circle, Polyline, polyline, Polygon polygon, Rectange rectangle} from './layer/vector';
@simon04 You make a few good points! As far as I'm aware, Rollup (et al) can tree-shake better if the modules are not assigned, but just imported/exported. However, right now I think it'd be better to just merge this as it is, and worry about cleaner ES6 module exports later. As long as the ES5 bundle works exactly the same, all that can be postponed. |
This an in-progress PR that switches Leaflet to use ES6 modules system and Rollup for bundling. Closes #3229. It's A LOT of tedious work going through all the code, but it looks like it's going to be a major improvement.
L.Util.bind
will turn into justbind
, which means pretty much all references to Leaflet functions/classes will be properly minified, reducing the bundle size significantly (in theory).document.write
hack.cc @IvanSanchez @patrickarlt @olanod @tmcw @perliedman @yohanboniface @hyperknot
Status:
publish.sh
scriptpointer
event handling hackstouch
event handling hacksL.Path
tests fromsuites/layer/vector/PathSpec.js
to somewhere elseleaflet-rollup-src.js
toleaflet-src.js
L.version
to a string including revision