@@ -19,7 +19,7 @@ import ImportGlobPlugin from 'esbuild-plugin-import-glob'
1919import packagerPlugin from './plugins/packagerPlugin'
2020import inlineWorkerPlugin from './plugins/workerPlugin'
2121import assetOverridePlugin from './plugins/assetOverridePlugin'
22-
22+ import path from 'path'
2323const PACKAGE = JSON . parse ( fs . readFileSync ( './package.json' , 'utf-8' ) )
2424
2525const INFO_PLUGIN : esbuild . Plugin = {
@@ -42,7 +42,35 @@ const INFO_PLUGIN: esbuild.Plugin = {
4242 } )
4343 } ,
4444}
45-
45+ const DEPENDENCY_QUARKS : esbuild . Plugin = {
46+ name : 'dependency-quarks' ,
47+ setup ( build ) {
48+ build . onResolve ( { filter : / ^ t h r e e / } , args => {
49+ if ( args . path === 'three' ) {
50+ return { path : 'three' , external : true }
51+ } else {
52+ return {
53+ path : require . resolve ( args . path ) ,
54+ }
55+ }
56+ } )
57+ build . onResolve ( { filter : / ^ d e e p s l a t e \/ / } , args => {
58+ // esbuild respects the package.json "exports" field
59+ // but the version of typescript we're using doesn't
60+ // so we need to resolve the path manually
61+ const file_path = path . resolve (
62+ process . cwd ( ) ,
63+ path . dirname ( require . resolve ( 'deepslate' ) ) ,
64+ '..' ,
65+ args . path . split ( '/' ) . slice ( 1 ) . join ( '/' ) ,
66+ 'index.js'
67+ )
68+ return {
69+ path : file_path ,
70+ }
71+ } )
72+ } ,
73+ }
4674function createBanner ( ) {
4775 function wrap ( s : string , width : number ) {
4876 return s . replace ( new RegExp ( `(?![^\\n]{1,${ width } }$)([^\\n]{1,${ width } })\\s` , 'g' ) , '$1\n' )
@@ -189,9 +217,11 @@ const devConfig: esbuild.BuildOptions = {
189217 packagerPlugin ( ) ,
190218 inlineWorkerPlugin ( devWorkerConfig ) ,
191219 assetOverridePlugin ( ) ,
220+ DEPENDENCY_QUARKS ,
192221 ] ,
193222 format : 'iife' ,
194223 define : DEFINES ,
224+ treeShaking : true ,
195225}
196226
197227const prodConfig : esbuild . BuildOptions = {
@@ -214,12 +244,14 @@ const prodConfig: esbuild.BuildOptions = {
214244 packagerPlugin ( ) ,
215245 inlineWorkerPlugin ( { } ) ,
216246 assetOverridePlugin ( ) ,
247+ DEPENDENCY_QUARKS ,
217248 ] ,
218249 keepNames : true ,
219250 banner : createBanner ( ) ,
220251 drop : [ 'debugger' ] ,
221252 format : 'iife' ,
222253 define : DEFINES ,
254+ treeShaking : true ,
223255 metafile : true ,
224256}
225257
0 commit comments