11import * as cp from "child_process"
2- import Parcel from "@parcel/core"
32import * as path from "path"
3+ import * as fs from "fs"
4+ import browserify from "browserify"
45
56type FixMeLater = any
67
@@ -42,7 +43,6 @@ class Watcher {
4243 const plugin = process . env . PLUGIN_DIR
4344 ? cp . spawn ( "yarn" , [ "build" , "--watch" ] , { cwd : process . env . PLUGIN_DIR } )
4445 : undefined
45- const bundler = this . createBundler ( )
4646
4747 const cleanup = ( code ?: number | null ) : void => {
4848 Watcher . log ( "killing vs code watcher" )
@@ -65,7 +65,7 @@ class Watcher {
6565 server . kill ( )
6666 }
6767
68- Watcher . log ( "killing bundler " )
68+ Watcher . log ( "killing watch " )
6969 process . exit ( code || 0 )
7070 }
7171
@@ -86,28 +86,19 @@ class Watcher {
8686 cleanup ( code )
8787 } )
8888 }
89- const bundle = bundler . watch ( ( err : FixMeLater , buildEvent : FixMeLater ) => {
90- if ( err ) {
91- console . error ( err )
92- Watcher . log ( "parcel watcher terminated unexpectedly" )
93- cleanup ( 1 )
94- }
95-
96- if ( buildEvent . type === "buildEnd" ) {
97- console . log ( "[parcel] bundled" )
98- }
99-
100- if ( buildEvent . type === "buildError" ) {
101- console . error ( "[parcel]" , err )
102- }
103- } )
10489
10590 vscode . stderr . on ( "data" , ( d ) => process . stderr . write ( d ) )
10691 tsc . stderr . on ( "data" , ( d ) => process . stderr . write ( d ) )
10792 if ( plugin ) {
10893 plugin . stderr . on ( "data" , ( d ) => process . stderr . write ( d ) )
10994 }
11095
96+ const browserFiles = [
97+ path . join ( this . rootPath , "out/browser/register.js" ) ,
98+ path . join ( this . rootPath , "out/browser/pages/login.js" ) ,
99+ path . join ( this . rootPath , "out/browser/pages/vscode.js" ) ,
100+ ]
101+
111102 // From https://github.com/chalk/ansi-regex
112103 const pattern = [
113104 "[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)" ,
@@ -150,7 +141,7 @@ class Watcher {
150141 startingVscode = true
151142 } else if ( startingVscode && line . includes ( "Finished compilation" ) ) {
152143 if ( startedVscode ) {
153- bundle . then ( restartServer )
144+ restartServer ( )
154145 }
155146 startedVscode = true
156147 }
@@ -162,7 +153,8 @@ class Watcher {
162153 console . log ( "[tsc]" , original )
163154 }
164155 if ( line . includes ( "Watching for file changes" ) ) {
165- bundle . then ( restartServer )
156+ bundleBrowserCode ( browserFiles )
157+ restartServer ( )
166158 }
167159 } )
168160
@@ -173,30 +165,26 @@ class Watcher {
173165 console . log ( "[plugin]" , original )
174166 }
175167 if ( line . includes ( "Watching for file changes" ) ) {
176- bundle . then ( restartServer )
168+ restartServer ( )
177169 }
178170 } )
179171 }
180172 }
173+ }
181174
182- private createBundler ( out = "dist" ) : FixMeLater {
183- return new ( Parcel as FixMeLater ) ( {
184- entries : [
185- path . join ( this . rootPath , "src/browser/register.ts" ) ,
186- path . join ( this . rootPath , "src/browser/serviceWorker.ts" ) ,
187- path . join ( this . rootPath , "src/browser/pages/login.ts" ) ,
188- path . join ( this . rootPath , "src/browser/pages/vscode.ts" ) ,
189- ] ,
190- cacheDir : path . join ( this . rootPath , ".cache" ) ,
191- logLevel : 1 ,
192- defaultConfig : require . resolve ( "@parcel/config-default" ) ,
193- defaultTargetOptions : {
194- publicUrl : "." ,
195- shouldOptimize : ! ! process . env . MINIFY ,
196- distDir : path . join ( this . rootPath , out ) ,
197- } ,
198- } )
199- }
175+ function bundleBrowserCode ( inputFiles : string [ ] ) {
176+ console . log ( `[browser] bundling...` )
177+ inputFiles . forEach ( async ( path : string ) => {
178+ const outputPath = path . replace ( ".js" , ".browserified.js" )
179+ browserify ( )
180+ . add ( path )
181+ . bundle ( )
182+ . on ( "error" , function ( error : Error ) {
183+ console . error ( error . toString ( ) )
184+ } )
185+ . pipe ( fs . createWriteStream ( outputPath ) )
186+ } )
187+ console . log ( `[browser] done bundling` )
200188}
201189
202190main ( )
0 commit comments