-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: use esbuild for running sources
- Loading branch information
Showing
10 changed files
with
256 additions
and
26 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file added
BIN
+34.2 KB
.yarn/cache/@cspotcode-source-map-support-npm-0.8.1-964f2de99d-4327d8e6e4.zip
Binary file not shown.
Binary file added
BIN
+27.5 KB
.yarn/cache/@jridgewell-trace-mapping-npm-0.3.9-91625cd7fb-542c5f0f0a.zip
Binary file not shown.
Binary file not shown.
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,51 @@ | ||
hyperfine -w 1 \ | ||
-n esbuild-cache \ | ||
--prepare "" \ | ||
"YARNPKG_TRANSPILER=esbuild node ./scripts/run-yarn.js exec yarn -v" \ | ||
-n esbuild-no-cache \ | ||
--prepare "rm -r ./node_modules/.cache/yarn" \ | ||
"YARNPKG_TRANSPILER=esbuild node ./scripts/run-yarn.js exec yarn -v" \ | ||
-n esbuild-partial-cache \ | ||
--prepare "echo '//' | tee -a packages/yarnpkg-core/sources/*.ts" \ | ||
"YARNPKG_TRANSPILER=esbuild node ./scripts/run-yarn.js exec yarn -v" \ | ||
-n babel-cache \ | ||
--prepare "" \ | ||
"YARNPKG_TRANSPILER=babel node ./scripts/run-yarn.js exec yarn -v" \ | ||
-n babel-no-cache \ | ||
--prepare "rm -r /tmp/babel" \ | ||
"YARNPKG_TRANSPILER=babel node ./scripts/run-yarn.js exec yarn -v" \ | ||
-n babel-partial-cache \ | ||
--prepare "echo '//' | tee -a packages/yarnpkg-core/sources/*.ts" \ | ||
"YARNPKG_TRANSPILER=babel node ./scripts/run-yarn.js exec yarn -v" | ||
|
||
# Benchmark 1: esbuild-cache | ||
# Time (mean ± σ): 4.280 s ± 0.053 s [User: 5.142 s, System: 0.432 s] | ||
# Range (min … max): 4.245 s … 4.416 s 10 runs | ||
|
||
# Benchmark 2: esbuild-no-cache | ||
# Time (mean ± σ): 6.374 s ± 0.031 s [User: 5.684 s, System: 0.514 s] | ||
# Range (min … max): 6.337 s … 6.438 s 10 runs | ||
|
||
# Benchmark 3: esbuild-partial-cache | ||
# Time (mean ± σ): 5.482 s ± 0.131 s [User: 5.729 s, System: 0.508 s] | ||
# Range (min … max): 5.331 s … 5.667 s 10 runs | ||
|
||
# Benchmark 4: babel-cache | ||
# Time (mean ± σ): 6.626 s ± 0.145 s [User: 8.551 s, System: 0.621 s] | ||
# Range (min … max): 6.411 s … 6.812 s 10 runs | ||
|
||
# Benchmark 5: babel-no-cache | ||
# Time (mean ± σ): 11.912 s ± 0.501 s [User: 17.095 s, System: 0.816 s] | ||
# Range (min … max): 11.464 s … 12.971 s 10 runs | ||
|
||
# Benchmark 6: babel-partial-cache | ||
# Time (mean ± σ): 7.579 s ± 0.083 s [User: 10.345 s, System: 0.717 s] | ||
# Range (min … max): 7.444 s … 7.710 s 10 runs | ||
|
||
# Summary | ||
# 'esbuild-cache' ran | ||
# 1.28 ± 0.03 times faster than 'esbuild-partial-cache' | ||
# 1.49 ± 0.02 times faster than 'esbuild-no-cache' | ||
# 1.55 ± 0.04 times faster than 'babel-cache' | ||
# 1.77 ± 0.03 times faster than 'babel-partial-cache' | ||
# 2.78 ± 0.12 times faster than 'babel-no-cache' |
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,25 @@ | ||
const path = require(`path`); | ||
const babel = require(`@babel/core`); | ||
const os = require(`os`); | ||
const root = path.dirname(__dirname); | ||
|
||
// The cache in @babel/register never clears itself and will therefore grow | ||
// forever causing massive slowdowns if left unchecked for a while | ||
// this ensures a new cache key is generated every week | ||
const weeksSinceUNIXEpoch = Math.floor(Date.now() / 604800000); | ||
|
||
if (!process.env.BABEL_CACHE_PATH) | ||
process.env.BABEL_CACHE_PATH = path.join(os.tmpdir(), `babel`, `.babel.${babel.version}.${babel.getEnv()}.${weeksSinceUNIXEpoch}.json`); | ||
|
||
require(`@babel/register`)({ | ||
root, | ||
extensions: [`.tsx`, `.ts`, `.js`], | ||
only: [ | ||
p => { | ||
if (p?.endsWith(`.js`)) | ||
return /packages(\\|\/)yarnpkg-pnp(\\|\/)sources(\\|\/)node/.test(p); | ||
|
||
return true; | ||
}, | ||
], | ||
}); |
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,104 @@ | ||
const esbuild = require(`esbuild-wasm`); | ||
const fs = require(`fs`); | ||
const crypto = require(`crypto`); | ||
const v8 = require(`v8`); | ||
const zlib = require(`zlib`); | ||
const path = require(`path`); | ||
const pirates = require(`pirates`); | ||
|
||
// Needed by the worker spawned by Esbuild | ||
if (process.versions.pnp) | ||
process.env.NODE_OPTIONS = `${process.env.NODE_OPTIONS || ``} -r ${JSON.stringify(require.resolve(`pnpapi`))}`; | ||
|
||
const resolveVirtual = process.versions.pnp ? require(`pnpapi`).resolveVirtual : undefined; | ||
|
||
const weeksSinceUNIXEpoch = Math.floor(Date.now() / 604800000); | ||
|
||
const cache = { | ||
version: [6, esbuild.version, weeksSinceUNIXEpoch, process.versions.node, !!process.setSourceMapsEnabled].join(`\0`), | ||
files: new Map(), | ||
isDirty: false, | ||
}; | ||
|
||
const cachePath = path.join(__dirname, `../node_modules/.cache/yarn/esbuild-transpile-cache.bin`); | ||
try { | ||
const cacheData = v8.deserialize(zlib.gunzipSync(fs.readFileSync(cachePath))); | ||
if (cacheData.version === cache.version) { | ||
cache.files = cacheData.files; | ||
} | ||
} catch { } | ||
|
||
function persistCache() { | ||
if (!cache.isDirty) | ||
return; | ||
cache.isDirty = false; | ||
|
||
fs.mkdirSync(path.dirname(cachePath), {recursive: true}); | ||
const tmpPath = cachePath + crypto.randomBytes(8).toString(`hex`); | ||
fs.writeFileSync( | ||
tmpPath, | ||
zlib.gzipSync( | ||
v8.serialize({ | ||
version: cache.version, | ||
files: cache.files, | ||
}), | ||
{level: 1}, | ||
), | ||
); | ||
fs.renameSync(tmpPath, cachePath); | ||
} | ||
|
||
process.once(`exit`, persistCache); | ||
process.nextTick(persistCache); | ||
|
||
process.setSourceMapsEnabled | ||
? process.setSourceMapsEnabled(true) | ||
: require(`@cspotcode/source-map-support`).install({ | ||
environment: `node`, | ||
retrieveSourceMap(filename) { | ||
filename = resolveVirtual?.(filename) || filename; | ||
|
||
const cacheEntry = cache.files.get(filename); | ||
if (cacheEntry) | ||
return {url: filename, map: cacheEntry.map}; | ||
|
||
return null; | ||
}, | ||
}); | ||
|
||
pirates.addHook( | ||
(sourceCode, filename) => { | ||
filename = resolveVirtual?.(filename) || filename; | ||
|
||
const cacheEntry = cache.files.get(filename); | ||
|
||
if (cacheEntry?.source === sourceCode) | ||
return cacheEntry.code; | ||
|
||
const res = esbuild.transformSync(sourceCode, { | ||
target: `node${process.versions.node}`, | ||
loader: path.extname(filename).slice(1), | ||
sourcefile: filename, | ||
sourcemap: process.setSourceMapsEnabled ? `inline` : `both`, | ||
platform: `node`, | ||
format: `cjs`, | ||
}); | ||
|
||
cache.isDirty = true; | ||
cache.files.set(filename, { | ||
source: sourceCode, | ||
code: res.code, | ||
map: res.map, | ||
}); | ||
|
||
return res.code; | ||
}, | ||
{ | ||
extensions: [`.tsx`, `.ts`, `.js`], | ||
matcher(p) { | ||
if (p?.endsWith(`.js`)) return /packages(\\|\/)yarnpkg-pnp(\\|\/)sources(\\|\/)node/.test(p); | ||
|
||
return true; | ||
}, | ||
}, | ||
); |
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,25 +1,9 @@ | ||
const path = require(`path`); | ||
const babel = require(`@babel/core`); | ||
const os = require(`os`); | ||
const root = path.dirname(__dirname); | ||
|
||
// The cache in @babel/register never clears itself and will therefore grow | ||
// forever causing massive slowdowns if left unchecked for a while | ||
// this ensures a new cache key is generated every week | ||
const weeksSinceUNIXEpoch = Math.floor(Date.now() / 604800000); | ||
|
||
if (!process.env.BABEL_CACHE_PATH) | ||
process.env.BABEL_CACHE_PATH = path.join(os.tmpdir(), `babel`, `.babel.${babel.version}.${babel.getEnv()}.${weeksSinceUNIXEpoch}.json`); | ||
|
||
require(`@babel/register`)({ | ||
root, | ||
extensions: [`.tsx`, `.ts`, `.js`], | ||
only: [ | ||
p => { | ||
if (p?.endsWith(`.js`)) | ||
return /packages(\\|\/)yarnpkg-pnp(\\|\/)sources(\\|\/)node/.test(p); | ||
|
||
return true; | ||
}, | ||
], | ||
}); | ||
switch (process.env.YARNPKG_TRANSPILER) { | ||
case `esbuild`: | ||
require(`./setup-ts-execution-esbuild.js`); | ||
break; | ||
default: | ||
case `babel`: | ||
require(`./setup-ts-execution-babel.js`); | ||
break; | ||
} |
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