1+ import browserify from "browserify"
12import * as cp from "child_process"
2- import Parcel from "@parcel/core "
3+ import * as fs from "fs "
34import * as path from "path"
45
5- type FixMeLater = any
6-
76async function main ( ) : Promise < void > {
87 try {
98 const watcher = new Watcher ( )
@@ -42,7 +41,6 @@ class Watcher {
4241 const plugin = process . env . PLUGIN_DIR
4342 ? cp . spawn ( "yarn" , [ "build" , "--watch" ] , { cwd : process . env . PLUGIN_DIR } )
4443 : undefined
45- const bundler = this . createBundler ( )
4644
4745 const cleanup = ( code ?: number | null ) : void => {
4846 Watcher . log ( "killing vs code watcher" )
@@ -65,7 +63,7 @@ class Watcher {
6563 server . kill ( )
6664 }
6765
68- Watcher . log ( "killing bundler " )
66+ Watcher . log ( "killing watch " )
6967 process . exit ( code || 0 )
7068 }
7169
@@ -86,28 +84,19 @@ class Watcher {
8684 cleanup ( code )
8785 } )
8886 }
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- } )
10487
10588 vscode . stderr . on ( "data" , ( d ) => process . stderr . write ( d ) )
10689 tsc . stderr . on ( "data" , ( d ) => process . stderr . write ( d ) )
10790 if ( plugin ) {
10891 plugin . stderr . on ( "data" , ( d ) => process . stderr . write ( d ) )
10992 }
11093
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+
111100 // From https://github.com/chalk/ansi-regex
112101 const pattern = [
113102 "[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)" ,
@@ -150,7 +139,7 @@ class Watcher {
150139 startingVscode = true
151140 } else if ( startingVscode && line . includes ( "Finished compilation" ) ) {
152141 if ( startedVscode ) {
153- bundle . then ( restartServer )
142+ restartServer ( )
154143 }
155144 startedVscode = true
156145 }
@@ -162,7 +151,8 @@ class Watcher {
162151 console . log ( "[tsc]" , original )
163152 }
164153 if ( line . includes ( "Watching for file changes" ) ) {
165- bundle . then ( restartServer )
154+ bundleBrowserCode ( browserFiles )
155+ restartServer ( )
166156 }
167157 } )
168158
@@ -173,30 +163,26 @@ class Watcher {
173163 console . log ( "[plugin]" , original )
174164 }
175165 if ( line . includes ( "Watching for file changes" ) ) {
176- bundle . then ( restartServer )
166+ restartServer ( )
177167 }
178168 } )
179169 }
180170 }
171+ }
181172
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- }
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` )
200186}
201187
202188main ( )
0 commit comments