1+ import browserify from "browserify"
12import * as cp from "child_process"
2- import Bundler from "parcel-bundler "
3+ import * as fs from "fs "
34import * as path from "path"
45
56async function main ( ) : Promise < void > {
@@ -40,7 +41,6 @@ class Watcher {
4041 const plugin = process . env . PLUGIN_DIR
4142 ? cp . spawn ( "yarn" , [ "build" , "--watch" ] , { cwd : process . env . PLUGIN_DIR } )
4243 : undefined
43- const bundler = this . createBundler ( )
4444
4545 const cleanup = ( code ?: number | null ) : void => {
4646 Watcher . log ( "killing vs code watcher" )
@@ -63,7 +63,7 @@ class Watcher {
6363 server . kill ( )
6464 }
6565
66- Watcher . log ( "killing bundler " )
66+ Watcher . log ( "killing watch " )
6767 process . exit ( code || 0 )
6868 }
6969
@@ -84,23 +84,19 @@ class Watcher {
8484 cleanup ( code )
8585 } )
8686 }
87- const bundle = bundler . bundle ( ) . catch ( ( ) => {
88- Watcher . log ( "parcel watcher terminated unexpectedly" )
89- cleanup ( 1 )
90- } )
91- bundler . on ( "buildEnd" , ( ) => {
92- console . log ( "[parcel] bundled" )
93- } )
94- bundler . on ( "buildError" , ( error ) => {
95- console . error ( "[parcel]" , error )
96- } )
9787
9888 vscode . stderr . on ( "data" , ( d ) => process . stderr . write ( d ) )
9989 tsc . stderr . on ( "data" , ( d ) => process . stderr . write ( d ) )
10090 if ( plugin ) {
10191 plugin . stderr . on ( "data" , ( d ) => process . stderr . write ( d ) )
10292 }
10393
94+ const browserFiles = [
95+ path . join ( this . rootPath , "out/browser/register.js" ) ,
96+ path . join ( this . rootPath , "out/browser/pages/login.js" ) ,
97+ path . join ( this . rootPath , "out/browser/pages/vscode.js" ) ,
98+ ]
99+
104100 // From https://github.com/chalk/ansi-regex
105101 const pattern = [
106102 "[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)" ,
@@ -143,7 +139,7 @@ class Watcher {
143139 startingVscode = true
144140 } else if ( startingVscode && line . includes ( "Finished compilation" ) ) {
145141 if ( startedVscode ) {
146- bundle . then ( restartServer )
142+ restartServer ( )
147143 }
148144 startedVscode = true
149145 }
@@ -155,7 +151,8 @@ class Watcher {
155151 console . log ( "[tsc]" , original )
156152 }
157153 if ( line . includes ( "Watching for file changes" ) ) {
158- bundle . then ( restartServer )
154+ bundleBrowserCode ( browserFiles )
155+ restartServer ( )
159156 }
160157 } )
161158
@@ -166,29 +163,26 @@ class Watcher {
166163 console . log ( "[plugin]" , original )
167164 }
168165 if ( line . includes ( "Watching for file changes" ) ) {
169- bundle . then ( restartServer )
166+ restartServer ( )
170167 }
171168 } )
172169 }
173170 }
171+ }
174172
175- private createBundler ( out = "dist" ) : Bundler {
176- return new Bundler (
177- [
178- path . join ( this . rootPath , "src/browser/register.ts" ) ,
179- path . join ( this . rootPath , "src/browser/serviceWorker.ts" ) ,
180- path . join ( this . rootPath , "src/browser/pages/login.ts" ) ,
181- path . join ( this . rootPath , "src/browser/pages/vscode.ts" ) ,
182- ] ,
183- {
184- outDir : path . join ( this . rootPath , out ) ,
185- cacheDir : path . join ( this . rootPath , ".cache" ) ,
186- minify : ! ! process . env . MINIFY ,
187- logLevel : 1 ,
188- publicUrl : "." ,
189- } ,
190- )
191- }
173+ function bundleBrowserCode ( inputFiles : string [ ] ) {
174+ console . log ( `[browser] bundling...` )
175+ inputFiles . forEach ( async ( path : string ) => {
176+ const outputPath = path . replace ( ".js" , ".browserified.js" )
177+ browserify ( )
178+ . add ( path )
179+ . bundle ( )
180+ . on ( "error" , function ( error : Error ) {
181+ console . error ( error . toString ( ) )
182+ } )
183+ . pipe ( fs . createWriteStream ( outputPath ) )
184+ } )
185+ console . log ( `[browser] done bundling` )
192186}
193187
194188main ( )
0 commit comments