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- '.ts' : 'ts' ,
37- '.tsx' : 'tsx' ,
38- '.css' : 'css' ,
39- '.ttf' : 'file' ,
40- '.woff' : 'file' ,
41- '.woff2' : 'file'
42- } ,
26+ loader : { '.js' : 'jsx' , '.ts' : 'ts' , '.tsx' : 'tsx' , '.css' : 'css' } ,
4327 entryPoints : [ 'src/main.tsx' ] ,
4428 outfile : 'dist/bundle.js' ,
4529 bundle : true ,
@@ -53,10 +37,23 @@ const config = {
5337 environment : NODE_ENV ,
5438 root : '../../.env'
5539 } ) ,
56- replace ( {
57- __PACKAGE_VERSION__ : '3.1.10' ,
58- 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 /
59- } ) ,
40+ // styled-components: @hubble.gl/react nests its own copy.
41+ // react-palm: several @kepler.gl/* packages nest their own copy.
42+ // Both are singletons that break when loaded more than once.
43+ {
44+ name : 'dedupe-singletons' ,
45+ setup ( build ) {
46+ 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 => {
47+ if ( args . pluginData ?. deduped ) return ;
48+ const result = await build . resolve ( args . path , {
49+ resolveDir : __dirname ,
50+ kind : args . kind ,
51+ pluginData : { deduped : true }
52+ } ) ;
53+ return result ;
54+ } ) ;
55+ }
56+ } ,
6057 copyPlugin ( {
6158 resolveFrom : 'cwd' ,
6259 assets : {
@@ -84,16 +81,13 @@ function openURL(url) {
8481 const result = await esbuild
8582 . build ( {
8683 ...config ,
87- alias : thirdPartyAliases ,
8884 minify : true ,
8985 sourcemap : false ,
9086 metafile : true ,
9187 define : {
9288 ...config . define ,
9389 'process.env.NODE_ENV' : '"production"'
94- } ,
95- drop : [ 'console' , 'debugger' ] ,
96- treeShaking : true
90+ }
9791 } )
9892 . catch ( e => {
9993 console . error ( e ) ;
@@ -106,7 +100,6 @@ function openURL(url) {
106100 await esbuild
107101 . context ( {
108102 ...config ,
109- alias : thirdPartyAliases ,
110103 minify : false ,
111104 sourcemap : true ,
112105 banner : {
@@ -123,7 +116,7 @@ function openURL(url) {
123116 console . info ( remoteAddress , status , `"${ method } ${ path } " [${ timeInMS } ms]` ) ;
124117 }
125118 } ) ;
126- console . info ( `kepler.gl custom-map-style example running at ${ ` http://localhost:${ port } ` } ` ) ;
119+ console . info ( `kepler.gl custom-map-style example running at http://localhost:${ port } ` ) ;
127120 openURL ( `http://localhost:${ port } ` ) ;
128121 } )
129122 . catch ( e => {
0 commit comments