Skip to content

Commit

Permalink
[chore] migrate from webpack to esbuild to build demo-app locally (#2616
Browse files Browse the repository at this point in the history
)

- Use esbuild in demo-app
- Remove d3-request, start using fetch
- Upgrade react-markdown, react-virtualized because they use methods that only runs in node

Later ToDos:
Migrate from webpack to esbuild to build kepler.gl website

---------

Signed-off-by: Shan He <heshan0131@gmail.com>
  • Loading branch information
heshan0131 authored Sep 10, 2024
1 parent 7b512cf commit 9dbc80f
Show file tree
Hide file tree
Showing 20 changed files with 3,895 additions and 10,507 deletions.
2 changes: 1 addition & 1 deletion contributing/DEVELOPERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,4 +402,4 @@ For both scenarios, changes will be propagated from one system to the other and
[github-pr]: https://help.github.com/articles/creating-a-pull-request/
[jsDoc]: http://usejsdoc.org/
[tape]: https://github.com/substack/tape
[yarn-install]: https://yarnpkg.com/en/docs/install
[yarn-install]: https://yarnpkg.com/getting-started/install
14 changes: 0 additions & 14 deletions examples/demo-app/.babelrc

This file was deleted.

30 changes: 20 additions & 10 deletions examples/demo-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,38 @@

This is the src code of kepler.gl demo app. You can copy this folder out and run it locally.

#### 1. Install
#### Pre requirement
- [Node.js ^18.x](http://nodejs.org): We use Node to generate the documentation, run a
development web server, run tests, and generate distributable files. Depending on your system,
you can install Node either from source or as a pre-packaged bundle.
- [Yarn 4.4.0](https://yarnpkg.com): We use Yarn to install our Node.js module dependencies
(rather than using npm). See the detailed [installation instructions][yarn-install].

```sh
npm install
```

or
#### 1. Install Dependencies

```sh
yarn
yarn install
```


#### 2. Mapbox Token
add mapbox access token to node env
#### 2. Mapbox Token and Cloud storage client id
A collection of enviroment variables to pass mapbox tokens, and client ids for the cloud storages

```sh
export MapboxAccessToken=<your_mapbox_token>
export DropboxClientId=<your_dropbox_client_id>
export MapboxExportToken=<your_mapbox_export_token>
export CartoClientId=<your_carto_client_id>
export FoursquareClientId=<your_foursquare_client_id>
export FoursquareDomain=<your_foursquare_domain>
export FoursquareUserMapsURL=<your_foursquare_user_map_url>

```

#### 3. Start the app

```sh
npm start
yarn start
```

[yarn-install]: https://yarnpkg.com/getting-started/install
File renamed without changes.
220 changes: 220 additions & 0 deletions examples/demo-app/esbuild.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
import esbuild from 'esbuild';
import {replace} from 'esbuild-plugin-replace';

import process from 'node:process';
import fs from 'node:fs';
import {spawn} from 'node:child_process';
import {join} from 'node:path';
import KeplerPackage from '../../package.json' assert {type: 'json'};

const args = process.argv;
const LIB_DIR = '../../';
const NODE_MODULES_DIR = join(LIB_DIR, 'node_modules');
const SRC_DIR = join(LIB_DIR, 'src');

// For debugging deck.gl, load deck.gl from external deck.gl directory
const EXTERNAL_DECK_SRC = join(LIB_DIR, 'deck.gl');

// For debugging loaders.gl, load loaders.gl from external loaders.gl directory
const EXTERNAL_LOADERS_SRC = join(LIB_DIR, 'loaders.gl');

// For debugging hubble.gl, load hubble.gl from external hubble.gl directory
const EXTERNAL_HUBBLE_SRC = join(LIB_DIR, '../../hubble.gl');

const port = 8080;

// add alias to serve from kepler src, resolve libraries so there is only one copy of them
const RESOLVE_LOCAL_ALIASES = {
react: `${NODE_MODULES_DIR}/react`,
'react-dom': `${NODE_MODULES_DIR}/react-dom`,
'react-redux': `${NODE_MODULES_DIR}/react-redux/lib`,
'styled-components': `${NODE_MODULES_DIR}/styled-components`,
'react-intl': `${NODE_MODULES_DIR}/react-intl`,
// Suppress useless warnings from react-date-picker's dep
'tiny-warning': `${SRC_DIR}/utils/src/noop.ts`,
// kepler.gl and loaders.gl need to use same apache-arrow
'apache-arrow': `${NODE_MODULES_DIR}/apache-arrow`
};

const config = {
platform: 'browser',
format: 'iife',
logLevel: 'info',
loader: {'.js': 'jsx'},
entryPoints: ['src/main.js'],
outfile: 'dist/bundle.js',
bundle: true,
define: {
NODE_ENV: JSON.stringify(process.env.NODE_ENV || 'production'),
'process.env.MapboxAccessToken': JSON.stringify(process.env.MapboxAccessToken), // eslint-disable-line
'process.env.DropboxClientId': JSON.stringify(process.env.DropboxClientId), // eslint-disable-line
'process.env.MapboxExportToken': JSON.stringify(process.env.MapboxExportToken), // eslint-disable-line
'process.env.CartoClientId': JSON.stringify(process.env.CartoClientId), // eslint-disable-line
'process.env.FoursquareClientId': JSON.stringify(process.env.FoursquareClientId), // eslint-disable-line
'process.env.FoursquareDomain': JSON.stringify(process.env.FoursquareDomain), // eslint-disable-line
'process.env.FoursquareAPIURL': JSON.stringify(process.env.FoursquareAPIURL), // eslint-disable-line
'process.env.FoursquareUserMapsURL': JSON.stringify(process.env.FoursquareUserMapsURL) // eslint-disable-line
},
plugins: [
// automatically injected kepler.gl package version into the bundle
replace({
__PACKAGE_VERSION__: KeplerPackage.version,
exclude: /node_modules/
})
]
};

function addAliases(externals, args) {
const resolveAlias = RESOLVE_LOCAL_ALIASES;

// Combine flags
const useLocalDeck = args.includes('--env.deck') || args.includes('--env.hubble_src');
const useRepoDeck = args.includes('--env.deck_src');

// resolve deck.gl from local dir
if (useLocalDeck || useRepoDeck) {
// Load deck.gl from root node_modules
// if env.deck_src Load deck.gl from deck.gl/modules/main/src folder parallel to kepler.gl
resolveAlias['deck.gl'] = useLocalDeck
? `${NODE_MODULES_DIR}/deck.gl/src`
: `${EXTERNAL_DECK_SRC}/modules/main/src`;

// if env.deck Load @deck.gl modules from root node_modules/@deck.gl
// if env.deck_src Load @deck.gl modules from deck.gl/modules folder parallel to kepler.gl
externals['deck.gl'].forEach(mdl => {
resolveAlias[`@deck.gl/${mdl}`] = useLocalDeck
? `${NODE_MODULES_DIR}/@deck.gl/${mdl}/src`
: `${EXTERNAL_DECK_SRC}/modules/${mdl}/src`;
// types are stored in different directory
resolveAlias[`@deck.gl/${mdl}/typed`] = useLocalDeck
? `${NODE_MODULES_DIR}/@deck.gl/${mdl}/typed`
: `${EXTERNAL_DECK_SRC}/modules/${mdl}/src/types`;
});

['luma.gl', 'probe.gl', 'loaders.gl'].forEach(name => {
// if env.deck Load ${name} from root node_modules
// if env.deck_src Load ${name} from deck.gl/node_modules folder parallel to kepler.gl
resolveAlias[name] = useLocalDeck
? `${NODE_MODULES_DIR}/${name}/src`
: name === 'probe.gl'
? `${EXTERNAL_DECK_SRC}/node_modules/${name}/src`
: `${EXTERNAL_DECK_SRC}/node_modules/@${name}/core/src`;

// if env.deck Load @${name} modules from root node_modules/@${name}
// if env.deck_src Load @${name} modules from deck.gl/node_modules/@${name} folder parallel to kepler.gl`
externals[name].forEach(mdl => {
resolveAlias[`@${name}/${mdl}`] = useLocalDeck
? `${NODE_MODULES_DIR}/@${name}/${mdl}/src`
: `${EXTERNAL_DECK_SRC}/node_modules/@${name}/${mdl}/src`;
});
});
}

if (args.includes('--env.loaders_src')) {
externals['loaders.gl'].forEach(mdl => {
resolveAlias[`@loaders.gl/${mdl}`] = `${EXTERNAL_LOADERS_SRC}/modules/${mdl}/src`;
});
}

if (args.includes('--env.hubble_src')) {
externals['hubble.gl'].forEach(mdl => {
resolveAlias[`@hubble.gl/${mdl}`] = `${EXTERNAL_HUBBLE_SRC}/modules/${mdl}/src`;
});
}

return resolveAlias;
}

function openURL(url) {
// Could potentially be replaced by https://www.npmjs.com/package/open, it was throwing an error when tried last
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 () => {
// local dev

const modules = ['@deck.gl', '@loaders.gl', '@luma.gl', '@probe.gl', '@hubble.gl'];
const loadAllDirs = modules.map(
dir =>
new Promise(success => {
fs.readdir(join(NODE_MODULES_DIR, dir), (err, items) => {
if (err) {
const colorRed = '\x1b[31m';
const colorReset = '\x1b[0m';
console.log(
`${colorRed}%s${colorReset}`,
`Cannot find ${dir} in node_modules, make sure it is installed. ${err}`
);

success(null);
}
success(items);
});
})
);

const externals = await Promise.all(loadAllDirs).then(results => ({
'deck.gl': results[0],
'loaders.gl': results[1],
'luma.gl': results[2],
'probe.gl': results[3],
'hubble.gl': results[4]
}));

const localAliases = addAliases(externals, args);

if (args.includes('--build')) {
await esbuild
.build({
...config,

minify: true,
sourcemap: false
})
.catch(e => {
console.error(e);
process.exit(1);
});
}

if (args.includes('--start')) {
await esbuild
.context({
...config,
minify: false,
sourcemap: true,
// add alias to resolve libraries so there is only one copy of them
...(process.env.NODE_ENV === 'local' ? {alias: localAliases} : {}),
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);
});
}
})();
58 changes: 23 additions & 35 deletions examples/demo-app/package.json
Original file line number Diff line number Diff line change
@@ -1,65 +1,50 @@
{
"scripts": {
"start": "webpack-dev-server --mode development --progress --hot --open",
"start-prod": "webpack-dev-server -p --progress --hot --open",
"start-local": "webpack-dev-server --mode development --env.es6 --progress --hot --open",
"start-local-deck": "webpack-dev-server --mode development --env.es6 --env.deck --progress --hot --open",
"start-local-deck-src": "webpack-dev-server --mode development --env.es6 --env.deck_src --progress --hot --open",
"start-local-loaders-src": "webpack-dev-server --mode development --env.es6 --env.loaders_src --progress --hot --open",
"start-local-hubble-src": "webpack-dev-server --mode development --env.es6 --env.hubble_src --progress --hot --open",
"start-local-https": "webpack-dev-server --mode development --https --env.es6 --progress --hot --open"
"start": "node esbuild.config.mjs --start",
"start:local": "NODE_ENV=local node esbuild.config.mjs --start --env.deck",
"start:local-deck": "NODE_ENV=local node esbuild.config.mjs --start --env.deck",
"start:local-deck-src": "NODE_ENV=local node esbuild.config.mjs --start --env.deck_src",
"start:local-loaders-src": "NODE_ENV=local node esbuild.config.mjs --start --env.loaders_src",
"start:local-hubble-src": "NODE_ENV=local node esbuild.config.mjs --start --env.hubble_src"
},
"dependencies": {
"@auth0/auth0-spa-js": "^2.1.2",
"@carto/toolkit": "0.0.1-rc.18",
"@kepler.gl/actions": "^3.0.0-alpha.1",
"@kepler.gl/cloud-providers": "^3.0.0-alpha.1",
"@kepler.gl/components": "^3.0.0-alpha.1",
"@kepler.gl/constants": "^3.0.0-alpha.1",
"@kepler.gl/processors": "^3.0.0-alpha.1",
"@kepler.gl/reducers": "^3.0.0-alpha.1",
"@kepler.gl/schemas": "^3.0.0-alpha.1",
"@kepler.gl/styles": "^3.0.0-alpha.1",
"@kepler.gl/utils": "^3.0.0-alpha.1",
"@loaders.gl/arrow": "^4.1.0-alpha.4",
"@loaders.gl/core": "^4.1.0-alpha.4",
"@loaders.gl/csv": "^4.1.0-alpha.4",
"@loaders.gl/json": "^4.1.0-alpha.4",
"d3-request": "^1.0.6",
"@types/classnames": "^2.3.1",
"@types/keymirror": "^0.1.1",
"classnames": "^2.2.1",
"d3-format": "^2.0.0",
"dropbox": "^4.0.12",
"esbuild": "^0.23.1",
"global": "^4.3.0",
"lodash.debounce": "^4.0.8",
"lodash.get": "^4.4.2",
"keymirror": "^0.1.1",
"prop-types": "^15.6.0",
"react": "^18.2.0",
"react-copy-to-clipboard": "^5.0.1",
"react-dom": "^18.2.0",
"react-markdown": "^4.2.2",
"react-palm": "^3.3.6",
"react-intl": "^6.3.0",
"react-markdown": "^6.0.3",
"react-redux": "^8.0.5",
"react-router": "3.2.5",
"react-router-redux": "^4.0.8",
"react-virtualized": "^9.21.0",
"redux": "^4.2.1",
"redux-actions": "^2.2.1",
"redux-thunk": "^1.0.0",
"styled-components": "^4.1.3"
},
"devDependencies": {
"@babel/core": "^7.12.1",
"@babel/plugin-transform-class-properties": "^7.12.1",
"@babel/plugin-transform-export-namespace-from": "^7.12.1",
"@babel/plugin-transform-logical-assignment-operators": "^7.12.1",
"@babel/plugin-transform-modules-commonjs": "^7.12.1",
"@babel/plugin-transform-nullish-coalescing-operator": "^7.12.1",
"@babel/plugin-transform-optional-chaining": "^7.12.1",
"@babel/plugin-transform-runtime": "^7.12.1",
"@babel/plugin-transform-typescript": "^7.16.8",
"@babel/preset-env": "^7.12.1",
"@babel/preset-react": "^7.12.1",
"@babel/preset-typescript": "^7.16.7",
"babel-loader": "^8.0.0",
"webpack": "^4.29.0",
"webpack-cli": "^3.2.1",
"webpack-dev-middleware": "^3.5.1",
"webpack-dev-server": "^3.1.14",
"webpack-hot-middleware": "^2.24.3"
},
"resolutions": {
"@luma.gl/core": "8.5.21",
"@luma.gl/webgl": "8.5.21",
Expand All @@ -74,5 +59,8 @@
"node": "18.18.2",
"yarn": "4.4.0"
},
"packageManager": "yarn@4.4.0"
"packageManager": "yarn@4.4.0",
"devDependencies": {
"esbuild-plugin-replace": "^1.4.0"
}
}
Loading

0 comments on commit 9dbc80f

Please sign in to comment.