1- import * as esbuild from 'esbuild '
1+ import fs from 'node:fs '
22import http from 'node:http'
3+ import * as esbuild from 'esbuild'
34import { globalExternals } from '@fal-works/esbuild-plugin-global-externals'
4- import JbrowseGlobals from '@jbrowse/core/ReExports/list.js '
5+ import JBrowseReExports from '@jbrowse/core/ReExports/list'
56import prettyBytes from 'pretty-bytes'
67
8+ const isWatch = process . argv . includes ( '--watch' )
9+ const PORT = process . env . PORT ? + process . env . PORT : 9000
10+
711function createGlobalMap ( jbrowseGlobals ) {
812 const globalMap = { }
913 for ( const global of jbrowseGlobals ) {
@@ -12,115 +16,84 @@ function createGlobalMap(jbrowseGlobals) {
1216 type : 'cjs' ,
1317 }
1418 }
19+ // Map @jbrowse /mobx-state-tree to mobx-state-tree for backwards compatibility
20+ // In v4.0.0+, JBrowse uses @jbrowse/mobx-state-tree but exports it as 'mobx-state-tree'
21+ // In v3.x, JBrowse used mobx-state-tree directly
22+ globalMap [ '@jbrowse/mobx-state-tree' ] = {
23+ varName : `JBrowseExports["mobx-state-tree"]` ,
24+ type : 'cjs' ,
25+ }
1526 return globalMap
1627}
1728
18- if ( process . env . NODE_ENV === 'production' ) {
19- await esbuild . build ( {
20- entryPoints : [ 'src/index.ts' ] ,
21- bundle : true ,
22- globalName : 'JBrowsePluginMafViewer' ,
23- sourcemap : true ,
24- outfile : 'dist/jbrowse-plugin-mafviewer.umd.production.min.js' ,
25- metafile : true ,
26- minify : true ,
27- plugins : [
28- globalExternals ( createGlobalMap ( JbrowseGlobals . default ) ) ,
29- {
30- name : 'rebuild-log' ,
31- setup ( { onStart, onEnd } ) {
32- let time
33- onStart ( ( ) => {
34- time = Date . now ( )
35- } )
36- onEnd ( ( { metafile, errors, warnings } ) => {
37- console . log (
38- `Built in ${ Date . now ( ) - time } ms with ${
39- errors . length
40- } error(s) and ${ warnings . length } warning(s)`,
41- )
42- if ( ! metafile ) {
43- return
44- }
45- const { outputs } = metafile
46- for ( const [ file , metadata ] of Object . entries ( outputs ) ) {
47- const size = prettyBytes ( metadata . bytes )
48- console . log ( `Wrote ${ size } to ${ file } ` )
49- }
50- } )
51- } ,
52- } ,
53- ] ,
54- } )
55- } else {
56- let ctx = await esbuild . context ( {
57- entryPoints : [ 'src/index.ts' ] ,
58- bundle : true ,
59- globalName : 'JBrowsePluginMafViewer' ,
60- sourcemap : true ,
61- outfile : 'dist/out.js' ,
62- metafile : true ,
63- plugins : [
64- globalExternals ( createGlobalMap ( JbrowseGlobals . default ) ) ,
65- {
66- name : 'rebuild-log' ,
67- setup ( { onStart, onEnd } ) {
68- let time
69- onStart ( ( ) => {
70- time = Date . now ( )
71- } )
72- onEnd ( ( { metafile, errors, warnings } ) => {
73- console . log (
74- `Built in ${ Date . now ( ) - time } ms with ${
75- errors . length
76- } error(s) and ${ warnings . length } warning(s)`,
77- )
78- if ( ! metafile ) {
79- return
80- }
81- const { outputs } = metafile
82- for ( const [ file , metadata ] of Object . entries ( outputs ) ) {
83- const size = prettyBytes ( metadata . bytes )
84- console . log ( `Wrote ${ size } to ${ file } ` )
85- }
86- } )
87- } ,
88- } ,
89- ] ,
90- } )
91- let { hosts, port } = await ctx . serve ( {
92- servedir : '.' ,
93- port : 9001 ,
94- } )
29+ const rebuildLogPlugin = {
30+ name : 'rebuild-log' ,
31+ setup ( { onStart, onEnd } ) {
32+ let time
33+ onStart ( ( ) => {
34+ time = Date . now ( )
35+ } )
36+ onEnd ( ( { metafile, errors, warnings } ) => {
37+ console . log (
38+ `Built in ${ Date . now ( ) - time } ms with ${ errors . length } error(s) and ${ warnings . length } warning(s)` ,
39+ )
40+ if ( metafile ) {
41+ for ( const [ file , metadata ] of Object . entries ( metafile . outputs ) ) {
42+ console . log ( `Wrote ${ prettyBytes ( metadata . bytes ) } to ${ file } ` )
43+ }
44+ }
45+ } )
46+ } ,
47+ }
48+
49+ const globals = JBrowseReExports
50+ const config = {
51+ entryPoints : [ 'src/index.ts' ] ,
52+ bundle : true ,
53+ globalName : 'JBrowsePluginMafViewer' ,
54+ metafile : true ,
55+ plugins : [ globalExternals ( createGlobalMap ( globals ) ) , rebuildLogPlugin ] ,
56+ ...( isWatch
57+ ? { outfile : 'dist/out.js' }
58+ : {
59+ outfile : 'dist/jbrowse-plugin-mafviewer.umd.production.min.js' ,
60+ sourcemap : true ,
61+ minify : true ,
62+ } ) ,
63+ }
64+
65+ if ( isWatch ) {
66+ const ctx = await esbuild . context ( config )
67+ const internalPort = PORT + 400
68+ const { hosts } = await ctx . serve ( { servedir : '.' , port : internalPort } )
9569
9670 http
9771 . createServer ( ( req , res ) => {
9872 const proxyReq = http . request (
9973 {
10074 hostname : hosts [ 0 ] ,
101- port : 9001 ,
75+ port : internalPort ,
10276 path : req . url ,
10377 method : req . method ,
10478 headers : req . headers ,
10579 } ,
10680 proxyRes => {
107- //restore the CORS after
108- //https://github.com/evanw/esbuild/releases/tag/v0.25.0 disabled it
109- //as a potential vuln
81+ // restore CORS after https://github.com/evanw/esbuild/releases/tag/v0.25.0 disabled it
11082 res . writeHead ( proxyRes . statusCode , {
11183 ...proxyRes . headers ,
11284 'Access-Control-Allow-Origin' : '*' ,
11385 } )
11486 proxyRes . pipe ( res , { end : true } )
11587 } ,
11688 )
117-
118- // Forward the body of the request to esbuild
11989 req . pipe ( proxyReq , { end : true } )
12090 } )
121- . listen ( 9000 )
122- console . log ( `Serving at http://${ hosts [ 0 ] } :9000` )
91+ . listen ( PORT )
12392
93+ console . log ( `Serving at http://${ hosts [ 0 ] } :${ PORT } ` )
12494 await ctx . watch ( )
12595 console . log ( 'Watching files...' )
96+ } else {
97+ const result = await esbuild . build ( config )
98+ fs . writeFileSync ( 'meta.json' , JSON . stringify ( result . metafile ) )
12699}
0 commit comments