1
1
import * as cp from "child_process"
2
- import Parcel from "@parcel/core"
3
2
import * as path from "path"
3
+ import * as fs from "fs"
4
+ import browserify from "browserify"
4
5
5
6
type FixMeLater = any
6
7
@@ -42,7 +43,6 @@ class Watcher {
42
43
const plugin = process . env . PLUGIN_DIR
43
44
? cp . spawn ( "yarn" , [ "build" , "--watch" ] , { cwd : process . env . PLUGIN_DIR } )
44
45
: undefined
45
- const bundler = this . createBundler ( )
46
46
47
47
const cleanup = ( code ?: number | null ) : void => {
48
48
Watcher . log ( "killing vs code watcher" )
@@ -65,7 +65,7 @@ class Watcher {
65
65
server . kill ( )
66
66
}
67
67
68
- Watcher . log ( "killing bundler " )
68
+ Watcher . log ( "killing watch " )
69
69
process . exit ( code || 0 )
70
70
}
71
71
@@ -86,28 +86,19 @@ class Watcher {
86
86
cleanup ( code )
87
87
} )
88
88
}
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
- } )
104
89
105
90
vscode . stderr . on ( "data" , ( d ) => process . stderr . write ( d ) )
106
91
tsc . stderr . on ( "data" , ( d ) => process . stderr . write ( d ) )
107
92
if ( plugin ) {
108
93
plugin . stderr . on ( "data" , ( d ) => process . stderr . write ( d ) )
109
94
}
110
95
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
+
111
102
// From https://github.com/chalk/ansi-regex
112
103
const pattern = [
113
104
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)" ,
@@ -150,7 +141,7 @@ class Watcher {
150
141
startingVscode = true
151
142
} else if ( startingVscode && line . includes ( "Finished compilation" ) ) {
152
143
if ( startedVscode ) {
153
- bundle . then ( restartServer )
144
+ restartServer ( )
154
145
}
155
146
startedVscode = true
156
147
}
@@ -162,7 +153,8 @@ class Watcher {
162
153
console . log ( "[tsc]" , original )
163
154
}
164
155
if ( line . includes ( "Watching for file changes" ) ) {
165
- bundle . then ( restartServer )
156
+ bundleBrowserCode ( browserFiles )
157
+ restartServer ( )
166
158
}
167
159
} )
168
160
@@ -173,30 +165,26 @@ class Watcher {
173
165
console . log ( "[plugin]" , original )
174
166
}
175
167
if ( line . includes ( "Watching for file changes" ) ) {
176
- bundle . then ( restartServer )
168
+ restartServer ( )
177
169
}
178
170
} )
179
171
}
180
172
}
173
+ }
181
174
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` )
200
188
}
201
189
202
190
main ( )
0 commit comments