Skip to content

Commit 9598d7c

Browse files
committed
[fix] Importing only used components from echarts
1 parent 02e68ba commit 9598d7c

14 files changed

+97
-229
lines changed

.prettierrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
22
"trailingComma": "all",
33
"bracketSpacing": false,
4-
"tabWidth": 2
4+
"tabWidth": 2,
5+
"printWidth": 88,
6+
"insertFinalNewLine": true
57
}

README.md

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,47 +23,24 @@ yarn start
2323

2424
## Building the library
2525

26-
NetJSONGraph.js offers **3 build variants** to match different needs.
27-
28-
**Note**: using `yarn build` will generate the all the variants.
29-
30-
### Complete Bundle
26+
To build the production-ready library:
3127

3228
```bash
33-
yarn build:complete
29+
yarn build
3430
```
3531

36-
**Output**: single file with all the dependencies (echarts and leaflet).
37-
38-
- `netjsongraph-complete.[hash].min.js`
39-
40-
**Best for**: apps that need to support both network graphs and geographic maps.
41-
42-
### ECharts Bundle
43-
44-
```bash
45-
yarn build:echarts
46-
```
47-
48-
**Output**: single javascript file optimized for rendering network graphs.
49-
50-
- `netjsongraph-echarts.[hash].min.js`
51-
52-
**Best for**: apps focused exclusively on network topology graphs.
53-
54-
### Chunked Build
55-
56-
```bash
57-
yarn build:chunks
58-
```
32+
**Output**: Single optimized bundle with all dependencies included.
5933

60-
**Output**: separate files for optimal loading.
34+
- `netjsongraph.[hash].min.js` - Complete library with ECharts and Leaflet
35+
- `netjsongraph.[hash].min.js.map` - Source map for debugging
36+
- Compressed versions (`.gz` and `.br`) for optimized delivery
6137

62-
- `netjsongraph.[hash].min.js` - Core library
63-
- `echarts.[hash].min.js` - Network graph rendering
64-
- `leaflet.[hash].min.js` - Geographic map rendering
38+
The build includes:
39+
- **ECharts** for network graph rendering
40+
- **Leaflet** for geographic map rendering
41+
- **Core NetJSONGraph.js** functionality
6542

66-
**Best for**: large applications with conditional loading.
43+
This unified bundle approach ensures compatibility and simplifies deployment while maintaining optimal performance through advanced webpack optimizations.
6744

6845
### Run Tests
6946

jest.global-setup.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
module.exports = async () => {
22
// timezone returned in tests must be always UTC
33
process.env.TZ = "UTC";
4-
process.env.NODE_ENV = "test";
54
};

lib/js/echarts-leaflet/LeafletCoordSys.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
import { util, graphic, matrix } from "echarts/core";
2+
13
/* eslint-disable no-underscore-dangle */
24
// Underscore dangling allowed to identify internal methods and variable
35
/**
46
* generate leaflet coord system
5-
* @param {object} echarts api object
67
* @param {object} L leaflet
78
*
89
* @return {function} LeafletCoordSys
910
*/
10-
function createLeafletCoordSystem(echarts, L) {
11-
const {util, graphic, matrix} = echarts;
11+
function createLeafletCoordSystem(L) {
1212
const CustomOverlay = L.Layer.extend({
1313
initialize(container) {
1414
this._container = container;

lib/js/echarts-leaflet/LeafletModel.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import { extendComponentModel } from "echarts/core";
2+
13
/**
24
* extend echarts model
3-
* @param {object} echarts
45
*/
5-
export default function extendLeafletModel(echarts) {
6+
export default function extendLeafletModel() {
67
/**
78
* compare if two arrays of length 2 are equal
89
* @param {Array} a array of length 2
@@ -13,7 +14,7 @@ export default function extendLeafletModel(echarts) {
1314
return a && b && a[0] === b[0] && a[1] === b[1];
1415
}
1516

16-
echarts.extendComponentModel({
17+
extendComponentModel({
1718
type: "leaflet",
1819

1920
getLeaflet() {

lib/js/echarts-leaflet/LeafletView.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import {extendComponentView, getInstanceByDom} from "echarts/core";
12
/* eslint-disable no-underscore-dangle */
23
/**
34
* extend echarts view
4-
* @param {object} echarts
55
* @param {object} L
66
*/
7-
export default function extendLeafletView(echarts, L) {
8-
echarts.extendComponentView({
7+
export default function extendLeafletView(L) {
8+
extendComponentView({
99
type: "leaflet",
1010

1111
render(leafletModel, ecModel, api) {
@@ -76,7 +76,7 @@ export default function extendLeafletView(echarts, L) {
7676
if (rendering) {
7777
return;
7878
}
79-
79+
8080
api.dispatchAction({
8181
type: "leafletRoam",
8282
});
@@ -90,7 +90,7 @@ export default function extendLeafletView(echarts, L) {
9090
* handler for map resize event
9191
*/
9292
function resizeHandler() {
93-
echarts.getInstanceByDom(api.getDom()).resize();
93+
getInstanceByDom(api.getDom()).resize();
9494
}
9595

9696
if (this._oldMoveHandler) {

lib/js/echarts-leaflet/index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
import createLeafletCoordSystem from "./LeafletCoordSys";
22
import extendLeafletModel from "./LeafletModel";
33
import extendLeafletView from "./LeafletView";
4+
import {registerCoordinateSystem, registerAction} from "echarts/core";
45

56
/**
67
* echarts register leaflet coord system
7-
* @param {object} echarts
88
* @param {object} L
99
* @param {object} API {
1010
* colorTool: "zrender/lib/tool/color",
1111
* { each }: "zrender/lib/core/util",
1212
* env: "zrender/lib/core/env",
1313
* }
1414
*/
15-
function registerLeafletSystem(echarts, L, API) {
16-
extendLeafletModel(echarts);
17-
extendLeafletView(echarts, L);
15+
function registerLeafletSystem(L, API) {
16+
extendLeafletModel();
17+
extendLeafletView(L);
1818

19-
echarts.registerCoordinateSystem(
19+
registerCoordinateSystem(
2020
"leaflet",
21-
createLeafletCoordSystem(echarts, L),
21+
createLeafletCoordSystem(L),
2222
);
2323

24-
echarts.registerAction(
24+
registerAction(
2525
{
2626
type: "leafletRoam",
2727
event: "leafletRoam",

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
"test": "jest --silent",
99
"dev": "webpack serve --open --mode development",
1010
"start": "yarn dev",
11-
"build": "yarn build:chunks && yarn build:echarts && yarn build:complete",
12-
"build:chunks": "webpack --progress --mode production --env BUILD_TYPE=chunks",
13-
"build:echarts": "webpack --progress --mode production --env BUILD_TYPE=echarts-bundle",
14-
"build:complete": "webpack --progress --mode production --env BUILD_TYPE=complete-bundle",
11+
"build": "webpack --progress --mode production",
1512
"coverage": "jest --silent --coverage",
1613
"coveralls": "jest --silent -f ./coverage/lcov.info && cat ./coverage/lcov.info | coveralls || echo 'no coverage file found'",
1714
"precommit": "lint-staged",

src/js/leaflet-loader.js

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,23 @@
44
*/
55

66
let leafletInstance = null;
7-
let isLoaded = false;
87

98
/**
109
* Get the Leaflet instance, loading it conditionally based on build type
1110
* @returns {Object|null} Leaflet instance or null if not available in current build
1211
*/
1312
export function getLeaflet() {
14-
if (isLoaded) {
15-
return leafletInstance;
16-
}
17-
18-
// In test environment, always try to load Leaflet for testing
19-
const shouldLoadLeaflet =
20-
(typeof process !== "undefined" &&
21-
process.env &&
22-
process.env.NODE_ENV === "test") ||
23-
// eslint-disable-next-line no-undef
24-
(typeof __INCLUDE_LEAFLET__ !== "undefined" && __INCLUDE_LEAFLET__);
13+
if (leafletInstance !== null) return leafletInstance;
2514

26-
if (shouldLoadLeaflet) {
27-
try {
28-
// eslint-disable-next-line import/no-dynamic-require,global-require
29-
leafletInstance = require("leaflet/dist/leaflet");
30-
isLoaded = true;
31-
return leafletInstance;
32-
} catch (error) {
33-
console.warn("Failed to load Leaflet:", error);
34-
leafletInstance = null;
35-
}
36-
} else {
37-
const buildType =
38-
// eslint-disable-next-line no-undef
39-
typeof __BUILD_TYPE__ !== "undefined" ? __BUILD_TYPE__ : "unknown";
40-
console.info(`Leaflet not available in ${buildType} bundle`);
15+
try {
16+
// eslint-disable-next-line import/no-dynamic-require,global-require
17+
leafletInstance = require("leaflet/dist/leaflet");
18+
} catch (error) {
19+
console.warn("Failed to load Leaflet:", error);
4120
leafletInstance = null;
4221
}
4322

44-
isLoaded = true;
4523
return leafletInstance;
4624
}
4725

48-
const L = getLeaflet();
49-
50-
export default L;
26+
export default getLeaflet();

src/js/netjsongraph.clients.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import {graphic} from "echarts/core";
2+
13
/* eslint-disable no-underscore-dangle */
2-
/* global echarts */
34
/**
45
* Attaches a visual overlay to display WiFi clients as colored dots around nodes.
56
*
@@ -10,7 +11,6 @@
1011
*/
1112
function attachClientsOverlay(graph, options = {}) {
1213
const chart = graph.echarts;
13-
const g = echarts.graphic;
1414
const colors = {
1515
wifi: (options.colors && options.colors.wifi) || "#d35454",
1616
};
@@ -48,7 +48,7 @@ function attachClientsOverlay(graph, options = {}) {
4848
// Create the overlay group and attach it to the series view
4949
const parent = getSeriesViewGroup();
5050
if (!parent) return {destroy() {}};
51-
const overlay = new g.Group({silent: true, z: 100, zlevel: 1});
51+
const overlay = new graphic.Group({silent: true, z: 100, zlevel: 1});
5252
parent.add(overlay);
5353

5454
// Extract node radius from graph configuration for positioning calculations
@@ -118,7 +118,7 @@ function attachClientsOverlay(graph, options = {}) {
118118
const y = centerY + distance * Math.sin(angle);
119119

120120
overlay.add(
121-
new g.Circle({
121+
new graphic.Circle({
122122
shape: {cx: x, cy: y, r: radius},
123123
style: {fill: color},
124124
silent: true,

0 commit comments

Comments
 (0)