22// Copyright contributors to the kepler.gl project
33
44import esbuild from 'esbuild' ;
5- import { replace } from 'esbuild-plugin-replace' ;
65import { dotenvRun } from '@dotenv-run/esbuild' ;
76import copyPlugin from 'esbuild-plugin-copy' ;
87
98import process from 'node:process' ;
109import fs from 'node:fs' ;
10+ import path from 'node:path' ;
11+ import { fileURLToPath } from 'node:url' ;
1112import { spawn } from 'node:child_process' ;
12- import { join } from 'node:path' ;
1313
1414const args = process . argv ;
1515
16+ const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
17+
1618const port = 8080 ;
1719
1820const NODE_ENV = JSON . stringify ( process . env . NODE_ENV || 'production' ) ;
1921
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-
3022const config = {
3123 platform : 'browser' ,
3224 format : 'iife' ,
3325 logLevel : 'info' ,
34- loader : {
35- '.js' : 'jsx' ,
36- '.css' : 'css' ,
37- '.ttf' : 'file' ,
38- '.woff' : 'file' ,
39- '.woff2' : 'file'
40- } ,
26+ loader : { '.js' : 'jsx' , '.css' : 'css' } ,
4127 entryPoints : [ 'src/main.js' ] ,
4228 outfile : 'dist/bundle.js' ,
4329 bundle : true ,
4430 define : {
45- NODE_ENV ,
46- 'process.env.MapboxAccessToken' : JSON . stringify ( process . env . MapboxAccessToken || '' )
31+ NODE_ENV
4732 } ,
4833 plugins : [
4934 dotenvRun ( {
5035 verbose : true ,
5136 environment : NODE_ENV ,
5237 root : '../../.env'
5338 } ) ,
54- replace ( {
55- __PACKAGE_VERSION__ : '3.1.10' ,
56- include : / c o n s t a n t s \/ s r c \/ d e f a u l t - s e t t i n g s \. t s /
57- } ) ,
39+ // styled-components: @hubble.gl/react nests its own copy.
40+ // react-palm: several @kepler.gl/* packages nest their own copy.
41+ // Both are singletons that break when loaded more than once.
42+ {
43+ name : 'dedupe-singletons' ,
44+ setup ( build ) {
45+ build . onResolve ( { filter : / ^ ( s t y l e d - c o m p o n e n t s | r e a c t - p a l m ( \/ | $ ) | r e a c t $ | r e a c t - d o m $ ) / } , async args => {
46+ if ( args . pluginData ?. deduped ) return ;
47+ const result = await build . resolve ( args . path , {
48+ resolveDir : __dirname ,
49+ kind : args . kind ,
50+ pluginData : { deduped : true }
51+ } ) ;
52+ return result ;
53+ } ) ;
54+ }
55+ } ,
5856 copyPlugin ( {
5957 resolveFrom : 'cwd' ,
6058 assets : {
@@ -82,16 +80,13 @@ function openURL(url) {
8280 const result = await esbuild
8381 . build ( {
8482 ...config ,
85- alias : thirdPartyAliases ,
8683 minify : true ,
8784 sourcemap : false ,
8885 metafile : true ,
8986 define : {
9087 ...config . define ,
9188 'process.env.NODE_ENV' : '"production"'
92- } ,
93- drop : [ 'console' , 'debugger' ] ,
94- treeShaking : true
89+ }
9590 } )
9691 . catch ( e => {
9792 console . error ( e ) ;
@@ -104,7 +99,6 @@ function openURL(url) {
10499 await esbuild
105100 . context ( {
106101 ...config ,
107- alias : thirdPartyAliases ,
108102 minify : false ,
109103 sourcemap : true ,
110104 banner : {
@@ -121,7 +115,7 @@ function openURL(url) {
121115 console . info ( remoteAddress , status , `"${ method } ${ path } " [${ timeInMS } ms]` ) ;
122116 }
123117 } ) ;
124- console . info ( `kepler.gl custom-theme example running at ${ ` http://localhost:${ port } ` } ` ) ;
118+ console . info ( `kepler.gl custom-theme example running at http://localhost:${ port } ` ) ;
125119 openURL ( `http://localhost:${ port } ` ) ;
126120 } )
127121 . catch ( e => {
0 commit comments