11import * as cp from "child_process"
2- import Parcel from "@parcel/core"
32import * as path from "path"
4-
5- type FixMeLater = any
3+ import * as fs from "fs"
4+ import browserify from "browserify"
65
76async function main ( ) : Promise < void > {
87 try {
@@ -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,21 @@ 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+
100+ bundleBrowserCode ( browserFiles )
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,27 @@ class Watcher {
173165 console . log ( "[plugin]" , original )
174166 }
175167 if ( line . includes ( "Watching for file changes" ) ) {
176- bundle . then ( restartServer )
168+ bundleBrowserCode ( browserFiles )
169+ restartServer ( )
177170 }
178171 } )
179172 }
180173 }
174+ }
181175
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- }
176+ function bundleBrowserCode ( inputFiles : string [ ] ) {
177+ console . log ( `[browser] bundling...` )
178+ inputFiles . forEach ( async ( path : string ) => {
179+ const outputPath = path . replace ( ".js" , ".browserified.js" )
180+ browserify ( )
181+ . add ( path )
182+ . bundle ( )
183+ . on ( "error" , function ( error : Error ) {
184+ console . error ( error . toString ( ) )
185+ } )
186+ . pipe ( fs . createWriteStream ( outputPath ) )
187+ } )
188+ console . log ( `[browser] done bundling` )
200189}
201190
202191main ( )
0 commit comments