Skip to content

Commit

Permalink
fix #979: call "initialize" in node to warm up
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Mar 19, 2021
1 parent db2b84b commit 4281d80
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions lib/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export let initialize: typeof types.initialize = options => {
if (options.wasmURL) throw new Error(`The "wasmURL" option only works in the browser`)
if (options.worker) throw new Error(`The "worker" option only works in the browser`)
if (initializeWasCalled) throw new Error('Cannot call "initialize" more than once')
ensureServiceIsRunning()
initializeWasCalled = true
return Promise.resolve();
}
Expand All @@ -130,11 +131,7 @@ let defaultWD = process.cwd();
let longLivedService: Service | undefined;

let ensureServiceIsRunning = (): Service => {
if (!longLivedService) longLivedService = startRunningService();
return longLivedService;
}

let startRunningService = (): Service => {
if (longLivedService) return longLivedService;
let [command, args] = esbuildCommandAndArgs();
let child = child_process.spawn(command, args.concat(`--service=${ESBUILD_VERSION}`, '--ping'), {
windowsHide: true,
Expand Down Expand Up @@ -170,7 +167,7 @@ let startRunningService = (): Service => {
unref() { if (--refCount === 0) child.unref(); },
}

return {
longLivedService = {
build: (options: types.BuildOptions): Promise<any> => {
return new Promise<types.BuildResult>((resolve, reject) => {
service.buildOrServe('build', refs, null, options, isTTY(), defaultWD, (err, res) => {
Expand Down Expand Up @@ -222,6 +219,7 @@ let startRunningService = (): Service => {
}, (err, res) => err ? reject(err) : resolve(res!)));
},
};
return longLivedService;
}

let runServiceSync = (callback: (service: common.StreamService) => void): void => {
Expand Down

0 comments on commit 4281d80

Please sign in to comment.