From 03680eb57489cf442f8c538141ea27c73d98d532 Mon Sep 17 00:00:00 2001 From: Anand Thakker Date: Mon, 12 Mar 2018 16:18:41 -0400 Subject: [PATCH] Switch to ES modules and Rollup (#6196) * [codemod] convert src/ from commonjs to es6 modules Using 5to6-codemod/transforms/{cjs,exports,named-export-generation}.js * [codemod] convert bench/ to use import instead of require() 5to6-codemod/transforms/cjs.js * [codemod] Convert util.* to use named imports https://gist.github.com/anandthakker/b636ad8cb7782c47998c4de9c0c5419d * Setup rollup * Manual es6 module fixes * Fix(ish) codegen * Apply fixed codegen * Add flow-remove-types require hook using pirates Upstream: https://github.com/flowtype/flow-remove-types/pull/62 * Update docs site to use es module import * Upgrade batfish * Update benchmarks build * Update style-spec build * [codemod] Convert test/unit to use import instead of require 5to6 cjs transform * Workaround for https://github.com/standard-things/esm/issues/301 * Add scripts to run node/tap with require hooks * [codemod] test/unit no-strict * Avoid @std/esm 'temporal dead zone' warning * Fixup unit tests * Add comments explaining bundling strategy * Update integration test suite implementation * Update yarn.lock Specifically for https://github.com/zaach/jsonlint/issues/103#issuecomment-367416531 * Polish build - Remove browserify - Update build tests * [lint] Use sourceType: script for test/integration/ * [lint] Add eslint-plugin-import, fix lint in src/ * [lint] Fix lint in test/ * [lint] Fix lint in bench/ * [lint] Fix lint for docs/ * Fix rollup config warning * Move rollup plugins to devDependencies * Use @mapbox-scoped rollup while awaiting upstream merge * Move comment in benchmarks.js * Restore comments stripped by codemods * Fix Style#queryRenderedFeature unit tests --- .eslintrc | 13 +- .gitignore | 1 + bench/benchmarks.js | 47 +- bench/benchmarks/expressions.js | 15 +- bench/benchmarks/filter_create.js | 11 +- bench/benchmarks/filter_evaluate.js | 16 +- bench/benchmarks/layers.js | 77 +- bench/benchmarks/layout.js | 34 +- bench/benchmarks/layout_dds.js | 9 +- bench/benchmarks/map_load.js | 8 +- bench/benchmarks/paint.js | 8 +- bench/benchmarks/query_box.js | 8 +- bench/benchmarks/query_point.js | 8 +- bench/benchmarks/style_layer_create.js | 14 +- bench/benchmarks/style_validate.js | 10 +- bench/benchmarks_view.js | 15 +- bench/lib/access_token.js | 2 +- bench/lib/axis.js | 4 +- bench/lib/benchmark.js | 2 +- bench/lib/create_map.js | 9 +- bench/lib/create_style.js | 9 +- bench/lib/statistics.js | 17 +- bench/rollup_config_benchmarks.js | 51 + bench/rollup_config_benchmarks_view.js | 25 + build/flow-remove-types.js | 68 + build/generate-access-token-script.js | 11 + build/generate-struct-arrays.js | 77 +- build/generate-style-code.js | 3 +- build/rollup_plugin_minify_style_spec.js | 20 + build/rollup_plugins.js | 52 + build/run-node | 3 + build/run-tap | 3 + docs/components/example.js | 2 +- docs/components/expression-metadata.js | 8 +- docs/pages/api.js | 2 +- docs/pages/example/.eslintrc | 54 +- .../mapbox-gl-geocoder-limit-region.html | 4 +- docs/pages/plugins.js | 2 +- docs/pages/style-spec.js | 3 +- flow-typed/mapbox-gl-js-test.js | 2 +- package.json | 76 +- rollup.config.js | 56 + rollup/benchmarks.js | 4 + rollup/mapboxgl.js | 47 + src/data/array_types.js | 56 +- src/data/bucket.js | 8 +- src/data/bucket/circle_attributes.js | 8 +- src/data/bucket/circle_bucket.js | 19 +- src/data/bucket/fill_attributes.js | 8 +- src/data/bucket/fill_bucket.js | 23 +- src/data/bucket/fill_extrusion_attributes.js | 8 +- src/data/bucket/fill_extrusion_bucket.js | 29 +- src/data/bucket/heatmap_bucket.js | 7 +- src/data/bucket/line_attributes.js | 8 +- src/data/bucket/line_bucket.js | 22 +- src/data/bucket/symbol_attributes.js | 132 +- src/data/bucket/symbol_bucket.js | 59 +- src/data/dem_data.js | 28 +- src/data/extent.js | 2 +- src/data/feature_index.js | 29 +- src/data/index_array_type.js | 10 +- src/data/load_geometry.js | 11 +- src/data/pos_attributes.js | 5 +- src/data/program_configuration.js | 28 +- src/data/raster_bounds_attributes.js | 5 +- src/data/segment.js | 33 +- src/geo/coordinate.js | 2 +- src/geo/lng_lat.js | 6 +- src/geo/lng_lat_bounds.js | 6 +- src/geo/transform.js | 45 +- src/gl/color_mode.js | 4 +- src/gl/context.js | 49 +- src/gl/depth_mode.js | 2 +- src/gl/framebuffer.js | 4 +- src/gl/index_buffer.js | 4 +- src/gl/stencil_mode.js | 2 +- src/gl/value.js | 15 +- src/gl/vertex_buffer.js | 5 +- src/index.js | 51 +- src/render/draw_background.js | 19 +- src/render/draw_circle.js | 9 +- src/render/draw_collision_debug.js | 8 +- src/render/draw_debug.js | 19 +- src/render/draw_fill.js | 19 +- src/render/draw_fill_extrusion.js | 25 +- src/render/draw_heatmap.js | 17 +- src/render/draw_hillshade.js | 15 +- src/render/draw_line.js | 11 +- src/render/draw_raster.js | 15 +- src/render/draw_symbol.js | 20 +- src/render/glyph_atlas.js | 10 +- src/render/glyph_manager.js | 24 +- src/render/image_atlas.js | 10 +- src/render/image_manager.js | 13 +- src/render/line_atlas.js | 6 +- src/render/painter.js | 80 +- src/render/pattern.js | 11 +- src/render/program.js | 19 +- src/render/texture.js | 5 +- src/render/tile_mask.js | 8 +- src/render/vertex_array_object.js | 4 +- src/shaders/encode_attribute.js | 8 +- src/shaders/index.js | 2 + src/source/canvas_source.js | 15 +- src/source/geojson_source.js | 23 +- src/source/geojson_worker_source.js | 22 +- src/source/geojson_wrapper.js | 10 +- src/source/image_source.js | 31 +- src/source/load_tilejson.js | 20 +- src/source/pixels_to_tile_units.js | 6 +- src/source/query_features.js | 8 +- src/source/raster_dem_tile_source.js | 22 +- src/source/raster_dem_tile_worker_source.js | 4 +- src/source/raster_tile_source.js | 27 +- src/source/rtl_text_plugin.js | 29 +- src/source/source.js | 32 +- src/source/source_cache.js | 30 +- src/source/tile.js | 47 +- src/source/tile_bounds.js | 7 +- src/source/tile_id.js | 14 +- src/source/vector_tile_source.js | 25 +- src/source/vector_tile_worker_source.js | 19 +- src/source/video_source.js | 18 +- src/source/worker.js | 27 +- src/source/worker_tile.js | 31 +- src/style-spec/build/strictify.js | 6 +- src/style-spec/composite.js | 4 +- src/style-spec/declass.js | 4 +- src/style-spec/deref.js | 4 +- src/style-spec/diff.js | 6 +- src/style-spec/error/parsing_error.js | 2 +- src/style-spec/error/validation_error.js | 2 +- .../expression/compound_expression.js | 21 +- .../expression/definitions/array.js | 18 +- .../expression/definitions/assertion.js | 19 +- src/style-spec/expression/definitions/at.js | 10 +- src/style-spec/expression/definitions/case.js | 7 +- .../expression/definitions/coalesce.js | 7 +- .../expression/definitions/coercion.js | 14 +- .../expression/definitions/equals.js | 12 +- .../expression/definitions/index.js | 53 +- .../expression/definitions/interpolate.js | 13 +- .../expression/definitions/length.js | 11 +- src/style-spec/expression/definitions/let.js | 2 +- .../expression/definitions/literal.js | 4 +- .../expression/definitions/match.js | 7 +- src/style-spec/expression/definitions/step.js | 7 +- src/style-spec/expression/definitions/var.js | 2 +- .../expression/evaluation_context.js | 9 +- src/style-spec/expression/index.js | 47 +- src/style-spec/expression/is_constant.js | 7 +- src/style-spec/expression/parsing_context.js | 29 +- src/style-spec/expression/parsing_error.js | 2 +- src/style-spec/expression/runtime_error.js | 2 +- src/style-spec/expression/scope.js | 2 +- src/style-spec/expression/stops.js | 9 +- src/style-spec/expression/types.js | 5 +- src/style-spec/expression/values.js | 20 +- src/style-spec/feature_filter/index.js | 8 +- src/style-spec/format.js | 6 +- src/style-spec/function/convert.js | 7 +- src/style-spec/function/index.js | 23 +- src/style-spec/group_by_layout.js | 4 +- src/style-spec/migrate.js | 8 +- src/style-spec/migrate/v8.js | 8 +- src/style-spec/migrate/v9.js | 6 +- src/style-spec/package.json | 23 +- src/style-spec/reference/latest.js | 3 +- src/style-spec/rollup.config.js | 22 + src/style-spec/style-spec.js | 52 +- src/style-spec/util/color.js | 4 +- src/style-spec/util/color_spaces.js | 26 +- src/style-spec/util/deep_equal.js | 2 +- src/style-spec/util/eval_support.js | 4 +- src/style-spec/util/extend.js | 4 +- src/style-spec/util/get_type.js | 4 +- src/style-spec/util/interpolate.js | 14 +- src/style-spec/util/ref_properties.js | 2 +- src/style-spec/util/result.js | 5 +- src/style-spec/util/unbundle_jsonlint.js | 6 +- src/style-spec/validate/latest.js | 4 +- src/style-spec/validate/validate.js | 72 +- src/style-spec/validate/validate_array.js | 10 +- src/style-spec/validate/validate_boolean.js | 8 +- src/style-spec/validate/validate_color.js | 10 +- src/style-spec/validate/validate_constants.js | 6 +- src/style-spec/validate/validate_enum.js | 8 +- .../validate/validate_expression.js | 13 +- src/style-spec/validate/validate_filter.js | 20 +- src/style-spec/validate/validate_function.js | 20 +- .../validate/validate_glyphs_url.js | 8 +- src/style-spec/validate/validate_layer.js | 20 +- .../validate/validate_layout_property.js | 6 +- src/style-spec/validate/validate_light.js | 10 +- src/style-spec/validate/validate_number.js | 8 +- src/style-spec/validate/validate_object.js | 10 +- .../validate/validate_paint_property.js | 6 +- src/style-spec/validate/validate_property.js | 16 +- src/style-spec/validate/validate_source.js | 12 +- src/style-spec/validate/validate_string.js | 8 +- src/style-spec/validate_style.js | 27 +- src/style-spec/validate_style.min.js | 29 +- src/style/create_style_layer.js | 28 + src/style/evaluation_parameters.js | 4 +- src/style/light.js | 42 +- src/style/load_glyph_range.js | 15 +- src/style/load_sprite.js | 17 +- src/style/parse_glyph_pbf.js | 11 +- src/style/pauseable_placement.js | 7 +- src/style/properties.js | 18 +- src/style/query_utils.js | 7 +- src/style/style.js | 135 +- src/style/style_layer.js | 57 +- .../style_layer/background_style_layer.js | 12 +- .../background_style_layer_properties.js | 8 +- src/style/style_layer/circle_style_layer.js | 25 +- .../circle_style_layer_properties.js | 8 +- .../style_layer/fill_extrusion_style_layer.js | 18 +- .../fill_extrusion_style_layer_properties.js | 8 +- src/style/style_layer/fill_style_layer.js | 20 +- .../fill_style_layer_properties.js | 8 +- src/style/style_layer/heatmap_style_layer.js | 16 +- .../heatmap_style_layer_properties.js | 8 +- .../style_layer/hillshade_style_layer.js | 12 +- .../hillshade_style_layer_properties.js | 8 +- src/style/style_layer/layer_properties.js.ejs | 8 +- src/style/style_layer/line_style_layer.js | 29 +- .../line_style_layer_properties.js | 8 +- src/style/style_layer/raster_style_layer.js | 12 +- .../raster_style_layer_properties.js | 8 +- src/style/style_layer/symbol_style_layer.js | 23 +- .../symbol_style_layer_properties.js | 8 +- src/style/style_layer_index.js | 16 +- src/style/validate_style.js | 23 +- src/style/zoom_history.js | 2 +- src/symbol/anchor.js | 7 +- src/symbol/check_max_angle.js | 2 +- src/symbol/clip_line.js | 4 +- src/symbol/collision_feature.js | 2 +- src/symbol/collision_index.js | 12 +- src/symbol/cross_tile_symbol_index.js | 7 +- src/symbol/get_anchors.js | 9 +- src/symbol/grid_index.js | 2 +- src/symbol/mergelines.js | 4 +- src/symbol/opacity_state.js | 4 +- src/symbol/placement.js | 18 +- src/symbol/projection.js | 25 +- src/symbol/quads.js | 10 +- src/symbol/shaping.js | 19 +- src/symbol/symbol_layout.js | 50 +- src/symbol/symbol_size.js | 27 +- src/symbol/transform_text.js | 6 +- src/ui/bind_handlers.js | 35 +- src/ui/camera.js | 54 +- src/ui/control/attribution_control.js | 11 +- src/ui/control/fullscreen_control.js | 13 +- src/ui/control/geolocate_control.js | 21 +- src/ui/control/logo_control.js | 9 +- src/ui/control/navigation_control.js | 13 +- src/ui/control/scale_control.js | 11 +- src/ui/events.js | 16 +- src/ui/handler/box_zoom.js | 15 +- src/ui/handler/dblclick_zoom.js | 6 +- src/ui/handler/drag_pan.js | 18 +- src/ui/handler/drag_rotate.js | 19 +- src/ui/handler/keyboard.js | 6 +- src/ui/handler/scroll_zoom.js | 23 +- src/ui/handler/touch_zoom_rotate.js | 17 +- src/ui/hash.js | 10 +- src/ui/map.js | 67 +- src/ui/marker.js | 13 +- src/ui/popup.js | 20 +- src/util/actor.js | 8 +- src/util/ajax.js | 14 +- src/util/browser.js | 24 +- src/util/browser/web_worker.js | 11 +- src/util/browser/window.js | 3 +- src/util/classify_rings.js | 9 +- src/util/config.js | 2 +- src/util/dictionary_coder.js | 4 +- src/util/dispatcher.js | 17 +- src/util/dom.js | 46 +- src/util/evented.js | 18 +- src/util/find_pole_of_inaccessibility.js | 11 +- src/util/global_worker_pool.js | 6 +- src/util/image.js | 10 +- src/util/intersection_tests.js | 11 +- src/util/is_char_in_unicode_block.js | 2 +- src/util/lru_cache.js | 2 +- src/util/mapbox.js | 17 +- src/util/performance.js | 4 +- src/util/script_detection.js | 58 +- src/util/smart_wrap.js | 6 +- src/util/struct_array.js | 7 +- src/util/struct_array.js.ejs | 2 +- src/util/throttle.js | 4 +- src/util/tile_cover.js | 7 +- src/util/token.js | 3 +- src/util/util.js | 111 +- src/util/vectortile_to_geojson.js | 2 +- src/util/verticalize_punctuation.js | 51 +- src/util/web_worker.js | 11 +- src/util/web_worker_transfer.js | 30 +- src/util/window.js | 29 +- src/util/worker_pool.js | 15 +- test/.eslintrc | 3 + test/ajax_stubs.js | 106 + test/build/browserify-test-fixture.js | 2 +- test/build/dev.test.js | 8 +- test/build/min.test.js | 23 +- test/build/style-spec.test.js | 33 +- test/expression.test.js | 7 +- test/integration/.eslintrc | 3 + test/node_modules/mapbox-gl-js-test.js | 9 +- test/query.test.js | 6 +- test/render.test.js | 6 +- test/stub_loader.js | 14 + test/suite_implementation.js | 101 +- test/unit/data/dem_data.test.js | 17 +- test/unit/data/fill_bucket.test.js | 20 +- test/unit/data/line_bucket.test.js | 24 +- test/unit/data/load_geometry.test.js | 14 +- test/unit/data/symbol_bucket.test.js | 39 +- test/unit/data/vertex_buffer.test.js | 10 +- test/unit/geo/coordinate.test.js | 6 +- test/unit/geo/lng_lat.test.js | 6 +- test/unit/geo/lng_lat_bounds.test.js | 8 +- test/unit/geo/transform.test.js | 15 +- test/unit/gl/state.test.js | 40 +- test/unit/mapbox-gl.js | 6 +- test/unit/render/glyph_manager.test.js | 40 +- test/unit/shaders/encode_attribute.test.js | 16 +- test/unit/source/canvas_source.test.js | 16 +- test/unit/source/geojson_source.test.js | 18 +- .../unit/source/geojson_worker_source.test.js | 12 +- test/unit/source/geojson_wrapper.test.js | 6 +- test/unit/source/image_source.test.js | 45 +- test/unit/source/query_features.test.js | 19 +- .../source/raster_dem_tile_source.test.js | 9 +- .../raster_dem_tile_worker_source.test.js | 10 +- test/unit/source/raster_tile_source.test.js | 9 +- test/unit/source/source_cache.test.js | 30 +- test/unit/source/tile.test.js | 28 +- test/unit/source/tile_id.test.js | 6 +- test/unit/source/tile_mask.test.js | 8 +- test/unit/source/vector_tile_source.test.js | 12 +- .../source/vector_tile_worker_source.test.js | 18 +- test/unit/source/worker.test.js | 8 +- test/unit/source/worker_tile.test.js | 17 +- test/unit/style-spec/color.js | 6 +- test/unit/style-spec/composite.test.js | 6 +- test/unit/style-spec/convert_function.test.js | 6 +- test/unit/style-spec/declass.test.js | 6 +- test/unit/style-spec/deref.test.js | 6 +- test/unit/style-spec/diff.test.js | 6 +- test/unit/style-spec/expression.test.js | 6 +- test/unit/style-spec/feature_filter.test.js | 17 +- test/unit/style-spec/format.test.js | 6 +- test/unit/style-spec/function.test.js | 8 +- test/unit/style-spec/group_by_layout.test.js | 6 +- test/unit/style-spec/interpolate.test.js | 8 +- test/unit/style-spec/migrate.test.js | 19 +- test/unit/style-spec/migrate/v8.js | 6 +- test/unit/style-spec/migrate/v9.js | 6 +- test/unit/style-spec/spec.test.js | 6 +- .../unit/style-spec/util/color_spaces.test.js | 8 +- test/unit/style-spec/validate.test.js | 15 +- test/unit/style/light.test.js | 12 +- test/unit/style/load_glyph_range.test.js | 53 +- test/unit/style/style.test.js | 233 +- test/unit/style/style_layer.test.js | 62 +- test/unit/style/style_layer_index.test.js | 12 +- test/unit/symbol/anchor.test.js | 6 +- test/unit/symbol/check_max_angle.test.js | 10 +- test/unit/symbol/collision_feature.js | 12 +- test/unit/symbol/cross_tile_symbol_index.js | 10 +- test/unit/symbol/get_anchors.test.js | 8 +- test/unit/symbol/grid_index.test.js | 7 +- test/unit/symbol/mergelines.test.js | 8 +- test/unit/symbol/quads.test.js | 11 +- test/unit/symbol/shaping.test.js | 10 +- test/unit/ui/camera.test.js | 13 +- test/unit/ui/control/attribution.test.js | 12 +- test/unit/ui/control/fullscreen.test.js | 10 +- test/unit/ui/control/geolocate.test.js | 10 +- test/unit/ui/control/logo.test.js | 9 +- test/unit/ui/control/scale.test.js | 9 +- test/unit/ui/handler/box_zoom.test.js | 12 +- test/unit/ui/handler/dblclick_zoom.test.js | 12 +- test/unit/ui/handler/drag_pan.test.js | 12 +- test/unit/ui/handler/drag_rotate.test.js | 16 +- test/unit/ui/handler/scroll_zoom.test.js | 18 +- .../unit/ui/handler/touch_zoom_rotate.test.js | 12 +- test/unit/ui/hash.test.js | 10 +- test/unit/ui/map.test.js | 35 +- test/unit/ui/map/isMoving.test.js | 14 +- test/unit/ui/map/isRotating.test.js | 12 +- test/unit/ui/map/isZooming.test.js | 14 +- test/unit/ui/map_events.test.js | 10 +- test/unit/ui/marker.test.js | 16 +- test/unit/ui/popup.test.js | 16 +- test/unit/util/actor.test.js | 31 +- test/unit/util/ajax.test.js | 25 +- test/unit/util/browser.test.js | 6 +- test/unit/util/classify_rings.test.js | 14 +- test/unit/util/dispatcher.test.js | 24 +- test/unit/util/evented.test.js | 6 +- .../util/find_pole_of_inaccessibility.test.js | 8 +- test/unit/util/lru_cache.test.js | 6 +- test/unit/util/mapbox.test.js | 12 +- test/unit/util/struct_array.test.js | 5 +- test/unit/util/throttle.test.js | 6 +- test/unit/util/tile_cover.test.js | 8 +- test/unit/util/token.test.js | 6 +- test/unit/util/util.test.js | 116 +- test/unit/util/web_worker_transfer.test.js | 30 +- test/unit/util/worker_pool.test.js | 15 +- yarn.lock | 3921 ++++++++--------- 418 files changed, 5747 insertions(+), 5433 deletions(-) create mode 100644 bench/rollup_config_benchmarks.js create mode 100644 bench/rollup_config_benchmarks_view.js create mode 100644 build/flow-remove-types.js create mode 100644 build/generate-access-token-script.js create mode 100644 build/rollup_plugin_minify_style_spec.js create mode 100644 build/rollup_plugins.js create mode 100755 build/run-node create mode 100755 build/run-tap create mode 100644 rollup.config.js create mode 100644 rollup/benchmarks.js create mode 100644 rollup/mapboxgl.js create mode 100644 src/style-spec/rollup.config.js create mode 100644 src/style/create_style_layer.js create mode 100644 test/ajax_stubs.js create mode 100644 test/stub_loader.js diff --git a/.eslintrc b/.eslintrc index 91ec6bdb95b..23beb60f33f 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,15 +1,24 @@ { "extends": [ "mourner", - "plugin:flowtype/recommended" + "plugin:flowtype/recommended", + "plugin:import/recommended" ], "parser": "babel-eslint", "parserOptions": { "sourceType": "script" }, "plugins": [ - "flowtype" + "flowtype", + "import" ], + "settings": { + "import/ignore": [ + "@mapbox/gl-matrix", + "@mapbox/shelf-pack", + "@mapbox/whoots-js" + ] + }, "rules": { "array-bracket-spacing": "off", "block-scoped-var": "error", diff --git a/.gitignore b/.gitignore index 5e99fabcee8..d59787feb0f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +/rollup/build/ /docs/components/api.json /dist/mapbox-gl-dev.js /dist/mapbox-gl.js diff --git a/bench/benchmarks.js b/bench/benchmarks.js index 4d9aa73e65c..c699ef286e8 100644 --- a/bench/benchmarks.js +++ b/bench/benchmarks.js @@ -1,6 +1,8 @@ // @flow -require('../src').accessToken = require('./lib/access_token'); +import mapboxgl from '../src'; +import accessToken from './lib/access_token'; +mapboxgl.accessToken = accessToken; window.mapboxglVersions = window.mapboxglVersions || []; window.mapboxglBenchmarks = window.mapboxglBenchmarks || {}; @@ -13,19 +15,36 @@ function register(Benchmark) { window.mapboxglBenchmarks[Benchmark.name][version] = new Benchmark(); } -register(require('./benchmarks/layout')); -register(require('./benchmarks/layout_dds')); -register(require('./benchmarks/paint')); -require('./benchmarks/layers').forEach(register); -register(require('./benchmarks/map_load')); -register(require('./benchmarks/style_validate')); -register(require('./benchmarks/style_layer_create')); -register(require('./benchmarks/query_point')); -register(require('./benchmarks/query_box')); -require('./benchmarks/expressions').forEach(register); -register(require('./benchmarks/filter_create')); -register(require('./benchmarks/filter_evaluate')); +import Layout from './benchmarks/layout'; +import LayoutDDS from './benchmarks/layout_dds'; +import Paint from './benchmarks/paint'; +import LayerBenchmarks from './benchmarks/layers'; +import Load from './benchmarks/map_load'; +import Validate from './benchmarks/style_validate'; +import StyleLayerCreate from './benchmarks/style_layer_create'; +import QueryPoint from './benchmarks/query_point'; +import QueryBox from './benchmarks/query_box'; +import ExpressionBenchmarks from './benchmarks/expressions'; +import FilterCreate from './benchmarks/filter_create'; +import FilterEvaluate from './benchmarks/filter_evaluate'; +register(Layout); +register(LayoutDDS); +register(Paint); +LayerBenchmarks.forEach(register); +register(Load); +register(Validate); +register(StyleLayerCreate); +register(QueryPoint); +register(QueryBox); +ExpressionBenchmarks.forEach(register); +register(FilterCreate); +register(FilterEvaluate); + +import getWorkerPool from '../src/util/global_worker_pool'; +// Set up the worker blob URL--written to window.mapboxGlWorkerUrl before this +// module is executed--before acquiring workers. +mapboxgl.workerUrl = window.mapboxGlWorkerUrl; // Ensure the global worker pool is never drained. Browsers have resource limits // on the max number of workers that can be created per page. -require('../src/util/global_worker_pool')().acquire(-1); +getWorkerPool().acquire(-1); diff --git a/bench/benchmarks/expressions.js b/bench/benchmarks/expressions.js index f77322b990c..dfa65767dbe 100644 --- a/bench/benchmarks/expressions.js +++ b/bench/benchmarks/expressions.js @@ -1,11 +1,12 @@ // @flow -const Benchmark = require('../lib/benchmark'); -const accessToken = require('../lib/access_token'); -const spec = require('../../src/style-spec/reference/latest'); -const convertFunction = require('../../src/style-spec/function/convert'); -const {isFunction, createFunction} = require('../../src/style-spec/function'); -const {createPropertyExpression} = require('../../src/style-spec/expression'); +import Benchmark from '../lib/benchmark'; + +import accessToken from '../lib/access_token'; +import spec from '../../src/style-spec/reference/latest'; +import convertFunction from '../../src/style-spec/function/convert'; +import { isFunction, createFunction } from '../../src/style-spec/function'; +import { createPropertyExpression } from '../../src/style-spec/expression'; import type {StylePropertySpecification} from '../../src/style-spec/style-spec'; import type {StylePropertyExpression} from '../../src/style-spec/expression'; @@ -102,7 +103,7 @@ class ExpressionEvaluate extends ExpressionBenchmark { } } -module.exports = [ +export default [ FunctionCreate, FunctionConvert, FunctionEvaluate, diff --git a/bench/benchmarks/filter_create.js b/bench/benchmarks/filter_create.js index d5bec17847f..a8d45c903c7 100644 --- a/bench/benchmarks/filter_create.js +++ b/bench/benchmarks/filter_create.js @@ -1,13 +1,14 @@ // @flow -const Benchmark = require('../lib/benchmark'); -const createFilter = require('../../src/style-spec/feature_filter'); -const filters = require('../data/filters.json'); +import Benchmark from '../lib/benchmark'; -module.exports = class FilterCreate extends Benchmark { +import createFilter from '../../src/style-spec/feature_filter'; +import filters from '../data/filters.json'; + +export default class FilterCreate extends Benchmark { bench() { for (const filter of filters) { createFilter(filter.filter); } } -}; +} diff --git a/bench/benchmarks/filter_evaluate.js b/bench/benchmarks/filter_evaluate.js index e07c270c7e0..041470c0fa7 100644 --- a/bench/benchmarks/filter_evaluate.js +++ b/bench/benchmarks/filter_evaluate.js @@ -1,12 +1,12 @@ -const Benchmark = require('../lib/benchmark'); -const VectorTile = require('@mapbox/vector-tile').VectorTile; -const Pbf = require('pbf'); -const createFilter = require('../../src/style-spec/feature_filter'); -const filters = require('../data/filters.json'); -const assert = require('assert'); +import Benchmark from '../lib/benchmark'; +import { VectorTile } from '@mapbox/vector-tile'; +import Pbf from 'pbf'; +import createFilter from '../../src/style-spec/feature_filter'; +import filters from '../data/filters.json'; +import assert from 'assert'; -module.exports = class FilterEvaluate extends Benchmark { +export default class FilterEvaluate extends Benchmark { setup() { return fetch('/bench/data/785.vector.pbf') .then(response => response.arrayBuffer()) @@ -46,4 +46,4 @@ module.exports = class FilterEvaluate extends Benchmark { } } } -}; +} diff --git a/bench/benchmarks/layers.js b/bench/benchmarks/layers.js index 29d71f1e1ee..470c5a139b7 100644 --- a/bench/benchmarks/layers.js +++ b/bench/benchmarks/layers.js @@ -1,7 +1,7 @@ -const Benchmark = require('../lib/benchmark'); -const createMap = require('../lib/create_map'); -const style = require('../data/empty.json'); +import Benchmark from '../lib/benchmark'; +import createMap from '../lib/create_map'; +import style from '../data/empty.json'; function generateLayers(layer) { const generated = []; @@ -100,41 +100,44 @@ class LayerFillExtrusion extends LayerBenchmark { } class LayerHeatmap extends LayerBenchmark { - constructor() { - super(); - - this.layerStyle = Object.assign({}, style, { - sources: { - 'heatmap': { - 'type': 'geojson', - 'data': require('../data/naturalearth-land.json'), - 'maxzoom': 23 - } - }, - layers: generateLayers({ - 'id': 'layer', - 'type': 'heatmap', - 'source': 'heatmap', - 'paint': { - "heatmap-radius": 50, - "heatmap-weight": { - "stops": [[0, 0.5], [4, 2]] + setup() { + return fetch('/bench/data/naturalearth-land.json') + .then(response => response.json()) + .then(data => { + this.layerStyle = Object.assign({}, style, { + sources: { + 'heatmap': { + 'type': 'geojson', + 'data': data, + 'maxzoom': 23 + } }, - "heatmap-intensity": 0.9, - "heatmap-color": [ - "interpolate", - ["linear"], - ["heatmap-density"], - 0, "rgba(0, 0, 255, 0)", - 0.1, "royalblue", - 0.3, "cyan", - 0.5, "lime", - 0.7, "yellow", - 1, "red" - ] - } + layers: generateLayers({ + 'id': 'layer', + 'type': 'heatmap', + 'source': 'heatmap', + 'paint': { + "heatmap-radius": 50, + "heatmap-weight": { + "stops": [[0, 0.5], [4, 2]] + }, + "heatmap-intensity": 0.9, + "heatmap-color": [ + "interpolate", + ["linear"], + ["heatmap-density"], + 0, "rgba(0, 0, 255, 0)", + 0.1, "royalblue", + 0.3, "cyan", + 0.5, "lime", + 0.7, "yellow", + 1, "red" + ] + } + }) + }); }) - }); + .then(() => super.setup()); } } @@ -214,7 +217,7 @@ class LayerSymbol extends LayerBenchmark { } -module.exports = [ +export default [ LayerBackground, LayerCircle, LayerFill, diff --git a/bench/benchmarks/layout.js b/bench/benchmarks/layout.js index 1b76258a7a6..b8ac7bbdaf3 100644 --- a/bench/benchmarks/layout.js +++ b/bench/benchmarks/layout.js @@ -1,28 +1,22 @@ // @flow -const Benchmark = require('../lib/benchmark'); -const createStyle = require('../lib/create_style'); - -const VT = require('@mapbox/vector-tile'); -const Protobuf = require('pbf'); -const assert = require('assert'); -const promisify = require('pify'); - -const WorkerTile = require('../../src/source/worker_tile'); -const StyleLayerIndex = require('../../src/style/style_layer_index'); -const deref = require('../../src/style-spec/deref'); -const {OverscaledTileID} = require('../../src/source/tile_id'); - -const { - normalizeStyleURL, - normalizeSourceURL, - normalizeTileURL -} = require('../../src/util/mapbox'); +import Benchmark from '../lib/benchmark'; + +import createStyle from '../lib/create_style'; +import VT from '@mapbox/vector-tile'; +import Protobuf from 'pbf'; +import assert from 'assert'; +import promisify from 'pify'; +import WorkerTile from '../../src/source/worker_tile'; +import StyleLayerIndex from '../../src/style/style_layer_index'; +import deref from '../../src/style-spec/deref'; +import { OverscaledTileID } from '../../src/source/tile_id'; +import { normalizeStyleURL, normalizeSourceURL, normalizeTileURL } from '../../src/util/mapbox'; import type {TileJSON} from '../../src/types/tilejson'; // Note: this class is extended in turn by the LayoutDDS benchmark. -module.exports = class Layout extends Benchmark { +export default class Layout extends Benchmark { glyphs: Object; icons: Object; workerTile: WorkerTile; @@ -136,4 +130,4 @@ module.exports = class Layout extends Benchmark { return promise; } -}; +} diff --git a/bench/benchmarks/layout_dds.js b/bench/benchmarks/layout_dds.js index 5610c670a13..af3812c50ad 100644 --- a/bench/benchmarks/layout_dds.js +++ b/bench/benchmarks/layout_dds.js @@ -1,11 +1,12 @@ // @flow -const Layout = require('./layout'); -const {OverscaledTileID} = require('../../src/source/tile_id'); +import Layout from './layout'; + +import { OverscaledTileID } from '../../src/source/tile_id'; const LAYER_COUNT = 2; -module.exports = class LayoutDDS extends Layout { +export default class LayoutDDS extends Layout { tileIDs(): Array { return [ new OverscaledTileID(15, 0, 15, 9373, 12535) @@ -92,4 +93,4 @@ module.exports = class LayoutDDS extends Layout { return Promise.resolve(style); } -}; +} diff --git a/bench/benchmarks/map_load.js b/bench/benchmarks/map_load.js index 7852aefa704..8d54de97346 100644 --- a/bench/benchmarks/map_load.js +++ b/bench/benchmarks/map_load.js @@ -1,8 +1,8 @@ -const Benchmark = require('../lib/benchmark'); -const createMap = require('../lib/create_map'); +import Benchmark from '../lib/benchmark'; +import createMap from '../lib/create_map'; -module.exports = class MapLoad extends Benchmark { +export default class MapLoad extends Benchmark { bench() { return createMap({ style: { @@ -12,4 +12,4 @@ module.exports = class MapLoad extends Benchmark { } }).then(map => map.remove()); } -}; +} diff --git a/bench/benchmarks/paint.js b/bench/benchmarks/paint.js index bcab11907e0..c996ac9e02e 100644 --- a/bench/benchmarks/paint.js +++ b/bench/benchmarks/paint.js @@ -1,12 +1,12 @@ -const Benchmark = require('../lib/benchmark'); -const createMap = require('../lib/create_map'); +import Benchmark from '../lib/benchmark'; +import createMap from '../lib/create_map'; const width = 1024; const height = 768; const zooms = [4, 8, 11, 13, 15, 17]; -module.exports = class Paint extends Benchmark { +export default class Paint extends Benchmark { setup() { return Promise.all(zooms.map(zoom => { return createMap({ @@ -34,4 +34,4 @@ module.exports = class Paint extends Benchmark { map.remove(); } } -}; +} diff --git a/bench/benchmarks/query_box.js b/bench/benchmarks/query_box.js index bb51e361c1d..53f2b3a10be 100644 --- a/bench/benchmarks/query_box.js +++ b/bench/benchmarks/query_box.js @@ -1,12 +1,12 @@ -const Benchmark = require('../lib/benchmark'); -const createMap = require('../lib/create_map'); +import Benchmark from '../lib/benchmark'; +import createMap from '../lib/create_map'; const width = 1024; const height = 768; const zooms = [4, 8, 11, 13, 15, 17]; -module.exports = class QueryBox extends Benchmark { +export default class QueryBox extends Benchmark { setup() { return Promise.all(zooms.map(zoom => { return createMap({ @@ -30,4 +30,4 @@ module.exports = class QueryBox extends Benchmark { map.remove(); } } -}; +} diff --git a/bench/benchmarks/query_point.js b/bench/benchmarks/query_point.js index 3bb37a8be47..3d5150b1067 100644 --- a/bench/benchmarks/query_point.js +++ b/bench/benchmarks/query_point.js @@ -1,6 +1,6 @@ -const Benchmark = require('../lib/benchmark'); -const createMap = require('../lib/create_map'); +import Benchmark from '../lib/benchmark'; +import createMap from '../lib/create_map'; const width = 1024; const height = 768; @@ -17,7 +17,7 @@ for (let x = 0; x < d; x++) { } } -module.exports = class QueryPoint extends Benchmark { +export default class QueryPoint extends Benchmark { setup() { return Promise.all(zooms.map(zoom => { return createMap({ @@ -43,4 +43,4 @@ module.exports = class QueryPoint extends Benchmark { map.remove(); } } -}; +} diff --git a/bench/benchmarks/style_layer_create.js b/bench/benchmarks/style_layer_create.js index 7ba3a163632..83e2293cf8e 100644 --- a/bench/benchmarks/style_layer_create.js +++ b/bench/benchmarks/style_layer_create.js @@ -1,10 +1,10 @@ -const Benchmark = require('../lib/benchmark'); -const accessToken = require('../lib/access_token'); -const StyleLayer = require('../../src/style/style_layer'); -const deref = require('../../src/style-spec/deref'); +import Benchmark from '../lib/benchmark'; +import accessToken from '../lib/access_token'; +import createStyleLayer from '../../src/style/create_style_layer'; +import deref from '../../src/style-spec/deref'; -module.exports = class StyleLayerCreate extends Benchmark { +export default class StyleLayerCreate extends Benchmark { setup() { return fetch(`https://api.mapbox.com/styles/v1/mapbox/streets-v9?access_token=${accessToken}`) .then(response => response.json()) @@ -13,7 +13,7 @@ module.exports = class StyleLayerCreate extends Benchmark { bench() { for (const layer of this.layers) { - StyleLayer.create(layer); + createStyleLayer(layer); } } -}; +} diff --git a/bench/benchmarks/style_validate.js b/bench/benchmarks/style_validate.js index b1d13a0e8de..363520614c9 100644 --- a/bench/benchmarks/style_validate.js +++ b/bench/benchmarks/style_validate.js @@ -1,9 +1,9 @@ -const Benchmark = require('../lib/benchmark'); -const accessToken = require('../lib/access_token'); -const validateStyle = require('../../src/style-spec/validate_style.min'); +import Benchmark from '../lib/benchmark'; +import accessToken from '../lib/access_token'; +import validateStyle from '../../src/style-spec/validate_style.min'; -module.exports = class StyleValidate extends Benchmark { +export default class StyleValidate extends Benchmark { setup() { return fetch(`https://api.mapbox.com/styles/v1/mapbox/streets-v9?access_token=${accessToken}`) .then(response => response.json()) @@ -13,4 +13,4 @@ module.exports = class StyleValidate extends Benchmark { bench() { validateStyle(this.json); } -}; +} diff --git a/bench/benchmarks_view.js b/bench/benchmarks_view.js index 09a596d09c1..101e8d041a7 100644 --- a/bench/benchmarks_view.js +++ b/bench/benchmarks_view.js @@ -1,13 +1,8 @@ -const React = require('react'); -const ReactDOM = require('react-dom'); -const d3 = require('d3'); -const Axis = require('./lib/axis'); -const { - summaryStatistics, - regression, - kde, - probabilitiesOfSuperiority -} = require('./lib/statistics'); +import React from 'react'; +import ReactDOM from 'react-dom'; +import * as d3 from 'd3'; +import Axis from './lib/axis'; +import { summaryStatistics, regression, kde, probabilitiesOfSuperiority } from './lib/statistics'; const versionColor = d3.scaleOrdinal(['#1b9e77', '#7570b3', '#d95f02']); const formatSample = d3.format(".3r"); diff --git a/bench/lib/access_token.js b/bench/lib/access_token.js index f817a73d6cb..905ad26ba9f 100644 --- a/bench/lib/access_token.js +++ b/bench/lib/access_token.js @@ -8,7 +8,7 @@ const accessToken = ( localStorage.setItem('accessToken', accessToken); -module.exports = accessToken; +export default accessToken; function getURLParameter(name) { const regexp = new RegExp(`[?&]${name}=([^&#]*)`, 'i'); diff --git a/bench/lib/axis.js b/bench/lib/axis.js index 909cc710c07..567d59330b3 100644 --- a/bench/lib/axis.js +++ b/bench/lib/axis.js @@ -1,4 +1,4 @@ -const React = require('react'); +import React from 'react'; function identity(x) { return x; @@ -82,4 +82,4 @@ class Axis extends React.Component { } } -module.exports = Axis; +export default Axis; diff --git a/bench/lib/benchmark.js b/bench/lib/benchmark.js index fa68b4fcffd..ee44cdb2deb 100644 --- a/bench/lib/benchmark.js +++ b/bench/lib/benchmark.js @@ -125,4 +125,4 @@ class Benchmark { } } -module.exports = Benchmark; +export default Benchmark; diff --git a/bench/lib/create_map.js b/bench/lib/create_map.js index d44daeea5e0..cde19b4e5d7 100644 --- a/bench/lib/create_map.js +++ b/bench/lib/create_map.js @@ -1,9 +1,10 @@ // @flow -const Map = require('../../src/ui/map'); -const browser = require('../../src/util/browser'); +import Map from '../../src/ui/map'; -module.exports = function (options: any): Promise { +import browser from '../../src/util/browser'; + +export default function (options: any): Promise { return new Promise((resolve, reject) => { const container = document.createElement('div'); container.style.width = `${options.width || 512}px`; @@ -33,4 +34,4 @@ module.exports = function (options: any): Promise { .on('error', (e) => reject(e.error)) .on('remove', () => container.remove()); }); -}; +} diff --git a/bench/lib/create_style.js b/bench/lib/create_style.js index 36a8ba7f074..95c0ea85a79 100644 --- a/bench/lib/create_style.js +++ b/bench/lib/create_style.js @@ -1,7 +1,8 @@ // @flow -const Style = require('../../src/style/style'); -const {Evented} = require('../../src/util/evented'); +import Style from '../../src/style/style'; + +import { Evented } from '../../src/util/evented'; class StubMap extends Evented { _transformRequest(url) { @@ -9,7 +10,7 @@ class StubMap extends Evented { } } -module.exports = function (styleJSON: StyleSpecification): Promise