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