From faa42219faecb3d7ff162439131ea07039226aec Mon Sep 17 00:00:00 2001 From: BOUILLAGUET Quentin Date: Fri, 15 Mar 2024 13:07:59 +0100 Subject: [PATCH] feat: ESM distribution BREAKING CHANGE: The itowns library drops the CommonJS distribution in favor of a standard ECMAScript module (ESM) distribution. --- .babelrc | 3 ++- package-lock.json | 23 +++++++++++++++++++ package.json | 5 +++- test/functional/.eslintrc.cjs | 2 +- test/functional/3dtiles_25d.js | 2 +- test/functional/3dtiles_basic.js | 2 +- test/functional/3dtiles_batch_table.js | 2 +- test/functional/3dtiles_ion.js | 2 +- test/functional/3dtiles_pointcloud.js | 2 +- test/functional/CameraUtils.js | 2 +- test/functional/GlobeControls.js | 2 +- test/functional/effects_postprocessing.js | 2 +- test/functional/effects_split.js | 2 +- test/functional/effects_stereo.js | 2 +- test/functional/misc_clamp_ground.js | 2 +- test/functional/misc_collada.js | 2 +- test/functional/misc_colorlayer_visibility.js | 2 +- test/functional/misc_instancing.js | 2 +- test/functional/oriented_images.js | 2 +- test/functional/potree_25d_map.js | 2 +- test/functional/potree_3d_map.js | 2 +- test/functional/source_file_geojson_raster.js | 2 +- test/functional/source_file_gpx_3d.js | 2 +- test/functional/source_file_kml_raster.js | 2 +- .../functional/source_file_kml_raster_usgs.js | 2 +- test/functional/source_stream_wfs_25d.js | 2 +- test/functional/source_stream_wfs_3d.js | 2 +- test/functional/source_stream_wfs_raster.js | 2 +- test/functional/vector_tile_3d_mesh_mapbox.js | 2 +- test/functional/vector_tile_raster_2d.js | 2 +- test/functional/vector_tile_raster_3d.js | 2 +- test/functional/view_25d_map.js | 2 +- test/functional/view_2d_map.js | 2 +- test/functional/view_3d_map.js | 2 +- test/functional/view_multi_25d.js | 2 +- test/functional/view_multiglobe.js | 2 +- test/functional/widgets_3dtiles_style.js | 2 +- test/hooks_functional.js | 15 +++++++----- 38 files changed, 72 insertions(+), 42 deletions(-) diff --git a/.babelrc b/.babelrc index 728dc8aae5..ded830aa4d 100644 --- a/.babelrc +++ b/.babelrc @@ -4,7 +4,7 @@ "targets": { "browsers": "defaults and supports webgl2" }, - "modules": "auto" + "modules": false }] ], "plugins": [ @@ -17,6 +17,7 @@ ".css" ] }], + ["module-extension-resolver"], ["@babel/plugin-transform-runtime", { "regenerator": false }], diff --git a/package-lock.json b/package-lock.json index 52bbc7a710..60970aa34d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -31,7 +31,9 @@ "babel-plugin-inline-import": "^3.0.0", "babel-plugin-minify-dead-code-elimination": "^0.5.2", "babel-plugin-minify-replace": "^0.5.0", + "babel-plugin-module-extension-resolver": "^1.0.0", "babel-plugin-module-resolver": "^5.0.0", + "babel-register-esm": "^1.2.5", "c8": "^9.1.0", "chalk": "^5.3.0", "chart.js": "^4.4.1", @@ -3449,6 +3451,15 @@ "dev": true, "license": "MIT" }, + "node_modules/babel-plugin-module-extension-resolver": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/babel-plugin-module-extension-resolver/-/babel-plugin-module-extension-resolver-1.0.0.tgz", + "integrity": "sha512-TIwNuS8jfdmoyn+tKMM9T6fczI5y6hHbicbCTIF+RyPSNSnQMlZNnCs/pQceOEZ/0qFNbaj5p0LCLG2VS1EF7g==", + "dev": true, + "engines": { + "node": ">=6.9.0" + } + }, "node_modules/babel-plugin-module-resolver": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/babel-plugin-module-resolver/-/babel-plugin-module-resolver-5.0.0.tgz", @@ -3541,6 +3552,18 @@ "@babel/core": "^7.0.0-0" } }, + "node_modules/babel-register-esm": { + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/babel-register-esm/-/babel-register-esm-1.2.5.tgz", + "integrity": "sha512-WaVd3Rm42kndYnufn8u1SbUUwuCxL2GAQX/7QXUL3w/7PffB+HcXrzmAqk1x01TjhFh/npSZ9Z3MNBc6dDb6Uw==", + "dev": true, + "engines": { + "node": ">=12.17.0" + }, + "peerDependencies": { + "@babel/core": "*" + } + }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", diff --git a/package.json b/package.json index 9cab897816..1b509c0ce0 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "name": "itowns", "version": "2.42.0", "description": "A JS/WebGL framework for 3D geospatial data visualization", + "type": "module", "main": "lib/Main.js", "exports": { ".": "./lib/Main.js", @@ -17,7 +18,7 @@ "test-functional": "mocha -t 100000 --require test/hooks_functional.js --recursive test/functional", "test-with-coverage": "c8 -n src -r html cross-env npm run test-unit", "test-with-coverage_lcov": "c8 -n src --reporter=lcov cross-env npm run test-unit", - "base-test-unit": "cross-env BABEL_DISABLE_CACHE=1 mocha --require @babel/register --file test/unit/bootstrap.js", + "base-test-unit": "cross-env BABEL_DISABLE_CACHE=1 mocha --file test/unit/bootstrap.js --loader=babel-register-esm", "build": "cross-env NODE_ENV=production webpack", "build-dev": "cross-env NODE_ENV=development webpack", "transpile": "cross-env BABEL_DISABLE_CACHE=1 babel src --out-dir lib", @@ -82,7 +83,9 @@ "babel-plugin-inline-import": "^3.0.0", "babel-plugin-minify-dead-code-elimination": "^0.5.2", "babel-plugin-minify-replace": "^0.5.0", + "babel-plugin-module-extension-resolver": "^1.0.0", "babel-plugin-module-resolver": "^5.0.0", + "babel-register-esm": "^1.2.5", "c8": "^9.1.0", "chalk": "^5.3.0", "chart.js": "^4.4.1", diff --git a/test/functional/.eslintrc.cjs b/test/functional/.eslintrc.cjs index abb7f61076..6807fb6b0c 100644 --- a/test/functional/.eslintrc.cjs +++ b/test/functional/.eslintrc.cjs @@ -5,7 +5,7 @@ module.exports = { ], parserOptions: { ecmaVersion: 2017, - sourceType: 'script', + sourceType: 'module', ecmaFeatures: { impliedStrict: true, }, diff --git a/test/functional/3dtiles_25d.js b/test/functional/3dtiles_25d.js index 28b1415bb0..533b3c6ca3 100644 --- a/test/functional/3dtiles_25d.js +++ b/test/functional/3dtiles_25d.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('3dtiles_25d', function _() { let result; diff --git a/test/functional/3dtiles_basic.js b/test/functional/3dtiles_basic.js index e4fd1765e7..6ffa74bc58 100644 --- a/test/functional/3dtiles_basic.js +++ b/test/functional/3dtiles_basic.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('3dtiles_basic', function _() { let result; diff --git a/test/functional/3dtiles_batch_table.js b/test/functional/3dtiles_batch_table.js index 6852bea081..662a082de4 100644 --- a/test/functional/3dtiles_batch_table.js +++ b/test/functional/3dtiles_batch_table.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('3dtiles_batch_table', function _() { let result; diff --git a/test/functional/3dtiles_ion.js b/test/functional/3dtiles_ion.js index b45186dac3..d402d22442 100644 --- a/test/functional/3dtiles_ion.js +++ b/test/functional/3dtiles_ion.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('3dtiles_ion', function _() { let result; diff --git a/test/functional/3dtiles_pointcloud.js b/test/functional/3dtiles_pointcloud.js index 167341d351..4c88639e8d 100644 --- a/test/functional/3dtiles_pointcloud.js +++ b/test/functional/3dtiles_pointcloud.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('3dtiles_pointcloud', function _() { let result; diff --git a/test/functional/CameraUtils.js b/test/functional/CameraUtils.js index 3914db0603..f9b32e7c8d 100644 --- a/test/functional/CameraUtils.js +++ b/test/functional/CameraUtils.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('Camera utils with globe example', function _() { before(async () => { diff --git a/test/functional/GlobeControls.js b/test/functional/GlobeControls.js index bccf81257c..6dbef35b97 100644 --- a/test/functional/GlobeControls.js +++ b/test/functional/GlobeControls.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; // global variables let middleWidth; diff --git a/test/functional/effects_postprocessing.js b/test/functional/effects_postprocessing.js index 99e88ca954..0b67ec41df 100644 --- a/test/functional/effects_postprocessing.js +++ b/test/functional/effects_postprocessing.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('effects_postprocessing', function _() { let result; diff --git a/test/functional/effects_split.js b/test/functional/effects_split.js index e615e05276..05b66f5b80 100644 --- a/test/functional/effects_split.js +++ b/test/functional/effects_split.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('effects_split', function _() { let result; diff --git a/test/functional/effects_stereo.js b/test/functional/effects_stereo.js index da84bd8682..40277afeed 100644 --- a/test/functional/effects_stereo.js +++ b/test/functional/effects_stereo.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('effects_stereo', function _() { let result; diff --git a/test/functional/misc_clamp_ground.js b/test/functional/misc_clamp_ground.js index f673259431..0ec85739ca 100644 --- a/test/functional/misc_clamp_ground.js +++ b/test/functional/misc_clamp_ground.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('misc_clamp_ground', function _() { let result; diff --git a/test/functional/misc_collada.js b/test/functional/misc_collada.js index e0f6e237e8..f559e82d9b 100644 --- a/test/functional/misc_collada.js +++ b/test/functional/misc_collada.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('misc_collada', function _() { let result; diff --git a/test/functional/misc_colorlayer_visibility.js b/test/functional/misc_colorlayer_visibility.js index d5a8e09fe9..7c1d6be2f3 100644 --- a/test/functional/misc_colorlayer_visibility.js +++ b/test/functional/misc_colorlayer_visibility.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('misc_colorlayer_visibility', function _() { let result; diff --git a/test/functional/misc_instancing.js b/test/functional/misc_instancing.js index add160eefa..fa4f9f122b 100644 --- a/test/functional/misc_instancing.js +++ b/test/functional/misc_instancing.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('misc_instancing', function _() { let result; diff --git a/test/functional/oriented_images.js b/test/functional/oriented_images.js index 519a01e0c6..3863b119ac 100644 --- a/test/functional/oriented_images.js +++ b/test/functional/oriented_images.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('misc_georeferenced_images', function _() { let result; diff --git a/test/functional/potree_25d_map.js b/test/functional/potree_25d_map.js index d822232455..fec05ae4db 100644 --- a/test/functional/potree_25d_map.js +++ b/test/functional/potree_25d_map.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('potree_25d_map', function _() { let result; diff --git a/test/functional/potree_3d_map.js b/test/functional/potree_3d_map.js index 67a467c6b3..10597b0a68 100644 --- a/test/functional/potree_3d_map.js +++ b/test/functional/potree_3d_map.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('potree_3d_map', function _() { let result; diff --git a/test/functional/source_file_geojson_raster.js b/test/functional/source_file_geojson_raster.js index 0223aeded4..c45bee1392 100644 --- a/test/functional/source_file_geojson_raster.js +++ b/test/functional/source_file_geojson_raster.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('source_file_geojson_raster', function _() { let result; diff --git a/test/functional/source_file_gpx_3d.js b/test/functional/source_file_gpx_3d.js index 7e38c5227d..6a5fb97ec8 100644 --- a/test/functional/source_file_gpx_3d.js +++ b/test/functional/source_file_gpx_3d.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('source_file_gpx_3d', function _() { let result; diff --git a/test/functional/source_file_kml_raster.js b/test/functional/source_file_kml_raster.js index 68e107f8c7..e081b59720 100644 --- a/test/functional/source_file_kml_raster.js +++ b/test/functional/source_file_kml_raster.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('source_file_kml_raster', function _() { let result; diff --git a/test/functional/source_file_kml_raster_usgs.js b/test/functional/source_file_kml_raster_usgs.js index ec63524e63..2647faa2b8 100644 --- a/test/functional/source_file_kml_raster_usgs.js +++ b/test/functional/source_file_kml_raster_usgs.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('source_file_kml_raster_usgs', function _() { let result; diff --git a/test/functional/source_stream_wfs_25d.js b/test/functional/source_stream_wfs_25d.js index fc4db4af36..7e542e702a 100644 --- a/test/functional/source_stream_wfs_25d.js +++ b/test/functional/source_stream_wfs_25d.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('source_stream_wfs_25d', function _() { let result; diff --git a/test/functional/source_stream_wfs_3d.js b/test/functional/source_stream_wfs_3d.js index 64a5b47b7b..3434f45b6a 100644 --- a/test/functional/source_stream_wfs_3d.js +++ b/test/functional/source_stream_wfs_3d.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('source_stream_wfs_3d', function _() { let result; diff --git a/test/functional/source_stream_wfs_raster.js b/test/functional/source_stream_wfs_raster.js index cc62a5f245..51aacd6287 100644 --- a/test/functional/source_stream_wfs_raster.js +++ b/test/functional/source_stream_wfs_raster.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('source_stream_wfs_raster', function _() { let result; diff --git a/test/functional/vector_tile_3d_mesh_mapbox.js b/test/functional/vector_tile_3d_mesh_mapbox.js index 903514c45b..98093a678b 100644 --- a/test/functional/vector_tile_3d_mesh_mapbox.js +++ b/test/functional/vector_tile_3d_mesh_mapbox.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('vector_tile_3d_mesh_mapbox', function _describe() { let result; diff --git a/test/functional/vector_tile_raster_2d.js b/test/functional/vector_tile_raster_2d.js index a45c731f1e..f02c9eb998 100644 --- a/test/functional/vector_tile_raster_2d.js +++ b/test/functional/vector_tile_raster_2d.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('vector_tile_raster_2d', function _() { let result; diff --git a/test/functional/vector_tile_raster_3d.js b/test/functional/vector_tile_raster_3d.js index 122bf49ee1..9d636267a5 100644 --- a/test/functional/vector_tile_raster_3d.js +++ b/test/functional/vector_tile_raster_3d.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('vector_tile_raster_3d', function _() { let result; diff --git a/test/functional/view_25d_map.js b/test/functional/view_25d_map.js index 8493b17d46..be3f034a33 100644 --- a/test/functional/view_25d_map.js +++ b/test/functional/view_25d_map.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('view_25d_map', function _() { let result; diff --git a/test/functional/view_2d_map.js b/test/functional/view_2d_map.js index ff1d99cf69..1c65b1d543 100644 --- a/test/functional/view_2d_map.js +++ b/test/functional/view_2d_map.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('view_2d_map', function _() { let result; diff --git a/test/functional/view_3d_map.js b/test/functional/view_3d_map.js index c3722f5ee2..79b0165ec8 100644 --- a/test/functional/view_3d_map.js +++ b/test/functional/view_3d_map.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('view_3d_map', function _() { let result; diff --git a/test/functional/view_multi_25d.js b/test/functional/view_multi_25d.js index 233b43f167..b4ea295a51 100644 --- a/test/functional/view_multi_25d.js +++ b/test/functional/view_multi_25d.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('view_multi_25d', function _() { let result; diff --git a/test/functional/view_multiglobe.js b/test/functional/view_multiglobe.js index a2866887a6..23a78835bf 100644 --- a/test/functional/view_multiglobe.js +++ b/test/functional/view_multiglobe.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('view_multiglobe', function _() { let result; diff --git a/test/functional/widgets_3dtiles_style.js b/test/functional/widgets_3dtiles_style.js index 147f3ee836..9e7ad730e5 100644 --- a/test/functional/widgets_3dtiles_style.js +++ b/test/functional/widgets_3dtiles_style.js @@ -1,4 +1,4 @@ -const assert = require('assert'); +import assert from 'assert'; describe('Widget C3dTilesStyle', function _() { let result; diff --git a/test/hooks_functional.js b/test/hooks_functional.js index 257bef4ae7..3d4340dd92 100644 --- a/test/hooks_functional.js +++ b/test/hooks_functional.js @@ -1,9 +1,9 @@ /* global page, itowns, view, initialPosition */ // eslint-disable-next-line import/no-extraneous-dependencies -const puppeteer = require('puppeteer'); -const net = require('net'); -const fs = require('fs'); -const http = require('http'); +import puppeteer from 'puppeteer'; +import net from 'net'; +import fs from 'fs'; +import http from 'http'; // this line allows to disable a warning generated by node when more than 10 // listeners are added for a particular event (this default warning helps // finding memory leaks). In our case, the listener to blame is @@ -12,7 +12,9 @@ const http = require('http'); // listener is added for every example test but this is intentional and it // is not related to a memory leak so we disable this warning. // More info: https://nodejs.org/docs/latest/api/events.html#events_emitter_setmaxlisteners_n -require('events').EventEmitter.prototype._maxListeners = 100; +import events from 'events'; + +events.EventEmitter.prototype._maxListeners = 100; let itownsServer; let itownsPort; @@ -159,7 +161,8 @@ const waitUntilItownsIsIdle = async (screenshotName) => { return result; }; -exports.mochaHooks = { +// eslint-disable-next-line import/prefer-default-export +export const mochaHooks = { beforeAll: async () => { let server; if (!process.env.USE_DEV_SERVER) {