|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// Copyright contributors to the kepler.gl project |
| 3 | + |
| 4 | +import esbuild from 'esbuild'; |
| 5 | +import {replace} from 'esbuild-plugin-replace'; |
| 6 | +import {dotenvRun} from '@dotenv-run/esbuild'; |
| 7 | +import copyPlugin from 'esbuild-plugin-copy'; |
| 8 | + |
| 9 | +import process from 'node:process'; |
| 10 | +import fs from 'node:fs'; |
| 11 | +import {spawn} from 'node:child_process'; |
| 12 | +import {join} from 'node:path'; |
| 13 | + |
| 14 | +const args = process.argv; |
| 15 | + |
| 16 | +const port = 8080; |
| 17 | + |
| 18 | +const NODE_ENV = JSON.stringify(process.env.NODE_ENV || 'production'); |
| 19 | + |
| 20 | +// Ensure a single instance of React and friends to avoid invalid hook calls |
| 21 | +const ROOT_NODE_MODULES = join('..', '..', 'node_modules'); |
| 22 | +const thirdPartyAliases = { |
| 23 | + react: join(ROOT_NODE_MODULES, 'react'), |
| 24 | + 'react-dom': join(ROOT_NODE_MODULES, 'react-dom'), |
| 25 | + 'react-redux': join(ROOT_NODE_MODULES, 'react-redux', 'lib'), |
| 26 | + 'styled-components': join(ROOT_NODE_MODULES, 'styled-components'), |
| 27 | + 'apache-arrow': join(ROOT_NODE_MODULES, 'apache-arrow') |
| 28 | +}; |
| 29 | + |
| 30 | +const config = { |
| 31 | + platform: 'browser', |
| 32 | + format: 'iife', |
| 33 | + logLevel: 'info', |
| 34 | + loader: { |
| 35 | + '.js': 'jsx', |
| 36 | + '.ts': 'ts', |
| 37 | + '.tsx': 'tsx', |
| 38 | + '.css': 'css', |
| 39 | + '.ttf': 'file', |
| 40 | + '.woff': 'file', |
| 41 | + '.woff2': 'file' |
| 42 | + }, |
| 43 | + entryPoints: ['src/main.tsx'], |
| 44 | + outfile: 'dist/bundle.js', |
| 45 | + bundle: true, |
| 46 | + define: { |
| 47 | + NODE_ENV, |
| 48 | + 'process.env.MapboxAccessToken': JSON.stringify(process.env.MapboxAccessToken || '') |
| 49 | + }, |
| 50 | + plugins: [ |
| 51 | + dotenvRun({ |
| 52 | + verbose: true, |
| 53 | + environment: NODE_ENV, |
| 54 | + root: '../../.env' |
| 55 | + }), |
| 56 | + replace({ |
| 57 | + __PACKAGE_VERSION__: '3.1.10', |
| 58 | + include: /constants\/src\/default-settings\.ts/ |
| 59 | + }), |
| 60 | + copyPlugin({ |
| 61 | + resolveFrom: 'cwd', |
| 62 | + assets: { |
| 63 | + from: ['index.html'], |
| 64 | + to: ['dist/index.html'] |
| 65 | + } |
| 66 | + }) |
| 67 | + ] |
| 68 | +}; |
| 69 | + |
| 70 | +function openURL(url) { |
| 71 | + const cmd = { |
| 72 | + darwin: ['open'], |
| 73 | + linux: ['xdg-open'], |
| 74 | + win32: ['cmd', '/c', 'start'] |
| 75 | + }; |
| 76 | + const command = cmd[process.platform]; |
| 77 | + if (command) { |
| 78 | + spawn(command[0], [...command.slice(1), url]); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +(async () => { |
| 83 | + if (args.includes('--build')) { |
| 84 | + const result = await esbuild |
| 85 | + .build({ |
| 86 | + ...config, |
| 87 | + alias: thirdPartyAliases, |
| 88 | + minify: true, |
| 89 | + sourcemap: false, |
| 90 | + metafile: true, |
| 91 | + define: { |
| 92 | + ...config.define, |
| 93 | + 'process.env.NODE_ENV': '"production"' |
| 94 | + }, |
| 95 | + drop: ['console', 'debugger'], |
| 96 | + treeShaking: true |
| 97 | + }) |
| 98 | + .catch(e => { |
| 99 | + console.error(e); |
| 100 | + process.exit(1); |
| 101 | + }); |
| 102 | + fs.writeFileSync('dist/esbuild-metadata.json', JSON.stringify(result.metafile)); |
| 103 | + } |
| 104 | + |
| 105 | + if (args.includes('--start')) { |
| 106 | + await esbuild |
| 107 | + .context({ |
| 108 | + ...config, |
| 109 | + alias: thirdPartyAliases, |
| 110 | + minify: false, |
| 111 | + sourcemap: true, |
| 112 | + banner: { |
| 113 | + js: `new EventSource('/esbuild').addEventListener('change', () => location.reload());` |
| 114 | + } |
| 115 | + }) |
| 116 | + .then(async ctx => { |
| 117 | + await ctx.watch(); |
| 118 | + await ctx.serve({ |
| 119 | + servedir: 'dist', |
| 120 | + port, |
| 121 | + fallback: 'dist/index.html', |
| 122 | + onRequest: ({remoteAddress, method, path, status, timeInMS}) => { |
| 123 | + console.info(remoteAddress, status, `"${method} ${path}" [${timeInMS}ms]`); |
| 124 | + } |
| 125 | + }); |
| 126 | + console.info(`kepler.gl custom-map-style example running at ${`http://localhost:${port}`}`); |
| 127 | + openURL(`http://localhost:${port}`); |
| 128 | + }) |
| 129 | + .catch(e => { |
| 130 | + console.error(e); |
| 131 | + process.exit(1); |
| 132 | + }); |
| 133 | + } |
| 134 | +})(); |
0 commit comments