-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: load binaries in the same process (#97)
- Loading branch information
1 parent
876ce02
commit 5ff6e82
Showing
11 changed files
with
136 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {runMain} from './main'; | ||
|
||
// Used by the generated shims | ||
export {runMain}; | ||
|
||
// Using `eval` to be sure that Webpack doesn't transform it | ||
if (process.mainModule === eval(`module`)) | ||
runMain(process.argv.slice(2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import 'module'; | ||
|
||
declare module 'module' { | ||
const _cache: {[p: string]: NodeModule}; | ||
|
||
function _nodeModulePaths(from: string): Array<string>; | ||
function _resolveFilename(request: string, parent: NodeModule | null | undefined, isMain: boolean): string; | ||
} | ||
|
||
declare global { | ||
namespace NodeJS { | ||
interface Module { | ||
load(path: string): void; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import Module from 'module'; | ||
import path from 'path'; | ||
|
||
declare const __non_webpack_require__: NodeRequire | undefined; | ||
|
||
export const dynamicRequire: NodeRequire = typeof __non_webpack_require__ !== `undefined` | ||
? __non_webpack_require__ | ||
: require; | ||
|
||
function getV8CompileCachePath() { | ||
return typeof __non_webpack_require__ !== `undefined` | ||
? `./vcc.js` | ||
: `corepack/dist/vcc.js`; | ||
} | ||
|
||
export function registerV8CompileCache() { | ||
const vccPath = getV8CompileCachePath(); | ||
dynamicRequire(vccPath); | ||
} | ||
|
||
/** | ||
* Loads a module as a main module, enabling the `require.main === module` pattern. | ||
*/ | ||
export function loadMainModule(id: string): void { | ||
const modulePath = Module._resolveFilename(id, null, true); | ||
|
||
const module = new Module(modulePath, undefined); | ||
|
||
module.filename = modulePath; | ||
module.paths = Module._nodeModulePaths(path.dirname(modulePath)); | ||
|
||
Module._cache[modulePath] = module; | ||
|
||
process.mainModule = module; | ||
module.id = `.`; | ||
|
||
try { | ||
return module.load(modulePath); | ||
} catch (error) { | ||
delete Module._cache[modulePath]; | ||
throw error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,35 @@ | ||
import {PortablePath, npath} from '@yarnpkg/fslib'; | ||
import {PassThrough} from 'stream'; | ||
|
||
import {Engine} from '../sources/Engine'; | ||
import {main} from '../sources/main'; | ||
import {spawn} from 'child_process'; | ||
|
||
export async function runCli(cwd: PortablePath, argv: Array<string>) { | ||
const stdin = new PassThrough(); | ||
const stdout = new PassThrough(); | ||
const stderr = new PassThrough(); | ||
|
||
const out: Array<Buffer> = []; | ||
const err: Array<Buffer> = []; | ||
|
||
stdout.on(`data`, chunk => { | ||
out.push(chunk); | ||
}); | ||
return new Promise((resolve, reject) => { | ||
const child = spawn(process.execPath, [require.resolve(`corepack/dist/corepack.js`), ...argv], { | ||
cwd: npath.fromPortablePath(cwd), | ||
env: process.env, | ||
stdio: `pipe`, | ||
}); | ||
|
||
stderr.on(`data`, chunk => { | ||
err.push(chunk); | ||
}); | ||
child.stdout.on(`data`, chunk => { | ||
out.push(chunk); | ||
}); | ||
|
||
const exitCode = await main(argv, { | ||
cwd: npath.fromPortablePath(cwd), | ||
engine: new Engine(), | ||
stdin, | ||
stdout, | ||
stderr, | ||
}); | ||
child.stderr.on(`data`, chunk => { | ||
err.push(chunk); | ||
}); | ||
|
||
child.on(`error`, error => { | ||
reject(error); | ||
}); | ||
|
||
return { | ||
exitCode, | ||
stdout: Buffer.concat(out).toString(), | ||
stderr: Buffer.concat(err).toString(), | ||
}; | ||
child.on(`exit`, exitCode => { | ||
resolve({ | ||
exitCode, | ||
stdout: Buffer.concat(out).toString(), | ||
stderr: Buffer.concat(err).toString(), | ||
}); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.