Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

[chore] Get started minimal example with esbuild (#3028)

Igor Dykhta7c3365fcf877
diff --git a/docs/api-reference/get-started.md b/docs/api-reference/get-started.md
index e86cbc135..7eee698fc 100644
--- a/docs/api-reference/get-started.md
+++ b/docs/api-reference/get-started.md
@@ -18,6 +18,8 @@ Kepler.gl is built on top of [Mapbox GL](https://www.mapbox.com). A mapbox accou
#### 0. Working Template
+Check full example on [Github](https://github.com/keplergl/kepler.gl/tree/master/examples/get-started).
+
```js
import * as React from "react";
import ReactDOM from "react-dom/client";
@@ -49,7 +51,7 @@ const enhancers = applyMiddleware(...middleWares);
const initialState = {};
const store = createStore(reducers, initialState, compose(enhancers));
-const App2 = () => (
+const App = () => (
<div
style={{
position: "absolute",
@@ -74,10 +76,10 @@ const App2 = () => (
const mapStateToProps = (state) => state;
const dispatchToProps = (dispatch) => ({ dispatch });
-const ConnectedApp = connect(mapStateToProps, dispatchToProps)(App2);
+const ConnectedApp = connect(mapStateToProps, dispatchToProps)(App);
const Root = () => (
<Provider store={store}>
- <App2 />
+ <ConnectedApp />
</Provider>
);
diff --git a/examples/get-started/.yarnrc.yml b/examples/get-started/.yarnrc.yml
new file mode 100644
index 000000000..19e3db2fd
--- /dev/null
+++ b/examples/get-started/.yarnrc.yml
@@ -0,0 +1,4 @@
+# https://yarnpkg.com/configuration/yarnrc
+nodeLinker: node-modules
+# Define the registry to use when fetching packages.
+npmRegistryServer: 'https://registry.yarnpkg.com'
diff --git a/examples/get-started/esbuild.config.mjs b/examples/get-started/esbuild.config.mjs
new file mode 100644
index 000000000..372deb837
--- /dev/null
+++ b/examples/get-started/esbuild.config.mjs
@@ -0,0 +1,105 @@
+// SPDX-License-Identifier: MIT
+// Copyright contributors to the kepler.gl project
+
+import esbuild from 'esbuild';
+import copyPlugin from 'esbuild-plugin-copy';
+import process from 'node:process';
+import fs from 'node:fs';
+import {spawn} from 'node:child_process';
+
+const args = process.argv;
+
+const port = 8080;
+
+const config = {
+ platform: 'browser',
+ format: 'iife',
+ logLevel: 'info',
+ loader: {'.js': 'jsx', '.css': 'css'},
+ entryPoints: ['src/app.tsx'],
+ outfile: 'dist/bundle.js',
+ bundle: true,
+ define: {
+ NODE_ENV: JSON.stringify(process.env.NODE_ENV || 'production'),
+ 'process.env.MapboxAccessToken': JSON.stringify(process.env.MapboxAccessToken || ''),
+ 'process.env.DropboxClientId': JSON.stringify(process.env.DropboxClientId || ''),
+ 'process.env.MapboxExportToken': JSON.stringify(process.env.MapboxExportToken || ''),
+ 'process.env.CartoClientId': JSON.stringify(process.env.CartoClientId || ''),
+ 'process.env.FoursquareClientId': JSON.stringify(process.env.FoursquareClientId || ''),
+ 'process.env.FoursquareDomain': JSON.stringify(process.env.FoursquareDomain || ''),
+ 'process.env.FoursquareAPIURL': JSON.stringify(process.env.FoursquareAPIURL || ''),
+ 'process.env.FoursquareUserMapsURL': JSON.stringify(process.env.FoursquareUserMapsURL || ''),
+ 'process.env.OpenAIToken': JSON.stringify(process.env.OpenAIToken || ''),
+ 'process.env.NODE_DEBUG': JSON.stringify(false)
+ },
+ plugins: [
+ copyPlugin({
+ resolveFrom: 'cwd',
+ assets: {
+ from: ['src/index.html'],
+ to: ['dist']
+ }
+ })
+ ]
+};
+
+function openURL(url) {
+ const cmd = {
+ darwin: ['open'],
+ linux: ['xdg-open'],
+ win32: ['cmd', '/c', 'start']
+ };
+ const command = cmd[process.platform];
+ if (command) {
+ spawn(command[0], [...command.slice(1), url]);
+ }
+}
+
+(async () => {
+ if (args.includes('--build')) {
+ const result = await esbuild
+ .build({
+ ...config,
+
+ minify: true,
+ sourcemap: false,
+ metafile: true
+ })
+ .catch(e => {
+ console.error(e);
+ process.exit(1);
+ });
+ fs.writeFileSync('dist/esbuild-metadata.json', JSON.stringify(result.metafile));
+ }
+
+ if (args.includes('--start')) {
+ await esbuild
+ .context({
+ ...config,
+ minify: false,
+ sourcemap: true,
+ banner: {
+ js: `new EventSource('/esbuild').addEventListener('change', () => location.reload());`
+ }
+ })
+ .then(async ctx => {
+ await ctx.watch();
+ await ctx.serve({
+ servedir: 'dist',
+ port,
+ fallback: 'dist/index.html',
+ onRequest: ({remoteAddress, method, path, status, timeInMS}) => {
+ console.info(remoteAddress, status, `"${method} ${path}" [${timeInMS}ms]`);
+ }
+ });
+ console.info(
+ `kepler.gl demo app running at ${`http://localhost:${port}`}, press Ctrl+C to stop`
+ );
+ openURL(`http://localhost:${port}`);
+ })
+ .catch(e => {
+ console.error(e);
+ process.exit(1);
+ });
+ }
+})();
diff --git a/examples/get-started/package.json b/examples/get-started/package.json
new file mode 100644
index 000000000..f159bbcc8
--- /dev/null
+++ b/examples/get-started/package.json
@@ -0,0 +1,32 @@
+{
+ "name": "kepler-app-esbuild",
+ "version": "0.0.1",
+ "license": "MIT",
+ "scripts": {
+ "bootstrap": "yarn install && yarn",
+ "build": "node esbuild.config.mjs --build",
+ "start": "node esbuild.config.mjs --start"
+ },
+ "dependencies": {
+ "@kepler.gl/components": "3.1.1",
+ "@kepler.gl/reducers": "3.1.1",
+ "react": "^18.2.0",
+ "react-dom": "^18.2.0",
+ "react-redux": "^8.0.5",
+ "react-scripts": "^5.0.1"
+ },
+ "devDependencies": {
+ "@types/mapbox-gl": "^2.7.10",
+ "@types/node": "^18.15.3",
+ "@types/react": "^18.0.28",
+ "@types/react-dom": "^18.0.11",
+ "assert": "^2.1.0",
+ "axios": "^1.3.4",
+ "esbuild": "^0.25.0",
+ "esbuild-plugin-copy": "^2.1.1",
+ "html-loader": "^4.2.0",
+ "ts-loader": "^9.4.2",
+ "ts-node": "^10.9.1",
+ "typescript": "^4.9.5"
+ }
+}
diff --git a/examples/get-started/src/app.tsx b/examples/get-started/src/app.tsx
new file mode 100644
index 000000000..e847eb121
--- /dev/null
+++ b/examples/get-started/src/app.tsx
@@ -0,0 +1,63 @@
+import * as React from 'react';
+import ReactDOM from 'react-dom/client';
+import {connect, Provider} from 'react-redux';
+import AutoSizer from 'react-virtualized/dist/commonjs/AutoSizer';
+import {applyMiddleware, combineReducers, compose, createStore} from 'redux';
+import document from 'global/document';
+
+import keplerGlReducer, {enhanceReduxMiddleware} from '@kepler.gl/reducers';
+import KeplerGl from '@kepler.gl/components';
+
+const reducers = combineReducers({
+ keplerGl: keplerGlReducer.initialState({
+ uiState: {
+ readOnly: false,
+ currentModal: null
+ }
+ })
+});
+
+const middleWares = enhanceReduxMiddleware([
+ // Add other middlewares here
+]);
+
+const enhancers = applyMiddleware(...middleWares);
+
+const initialState = {};
+const store = createStore(reducers, initialState, compose(enhancers));
+
+const App = () => (
+ <div
+ style={{
+ position: 'absolute',
+ top: '0px',
+ left: '0px',
+ width: '100%',
+ height: '100%'
+ }}
+ >
+ <AutoSizer>
+ {({height, width}) => (
+ <KeplerGl
+ mapboxApiAccessToken="pk.xxx.yyy" // Replace with your mapbox token
+ id="map"
+ width={width}
+ height={height}
+ />
+ )}
+ </AutoSizer>
+ </div>
+);
+
+const mapStateToProps = state => state;
+const dispatchToProps = dispatch => ({dispatch});
+const ConnectedApp = connect(mapStateToProps, dispatchToProps)(App);
+const Root = () => (
+ <Provider store={store}>
+ <ConnectedApp />
+ </Provider>
+);
+
+const container = document.getElementById('root');
+const root = ReactDOM.createRoot(container);
+root.render(<Root />);
diff --git a/examples/get-started/src/index.html b/examples/get-started/src/index.html
new file mode 100644
index 000000000..62368da91
--- /dev/null
+++ b/examples/get-started/src/index.html
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Get Started</title>
+ </head>
+ <body>
+ <div id="root" />
+ <script src="./bundle.js"></script>
+ </body>
+</html>
diff --git a/examples/get-started/tsconfig.json b/examples/get-started/tsconfig.json
new file mode 100644
index 000000000..1035fe6da
--- /dev/null
+++ b/examples/get-started/tsconfig.json
@@ -0,0 +1,11 @@
+{
+ "compilerOptions": {
+ "sourceMap": true,
+ "module": "commonjs",
+ "target": "es5",
+ "jsx": "react",
+ "esModuleInterop": true
+ },
+ "include": ["src"],
+ "compileOnSave": false
+}