Skip to content

Commit

Permalink
chore: remove babel setup
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed Feb 4, 2023
1 parent 1e7b13a commit 031d483
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 241 deletions.
29 changes: 0 additions & 29 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
51 changes: 0 additions & 51 deletions bench.sh

This file was deleted.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"@babel/preset-env": "^7.18.10",
"@babel/preset-react": "^7.18.6",
"@babel/preset-typescript": "^7.18.6",
"@babel/register": "^7.18.9",
"@cspotcode/source-map-support": "^0.8.1",
"@types/jest": "^28.1.6",
"@types/node": "^18.11.11",
Expand Down
27 changes: 0 additions & 27 deletions scripts/setup-ts-execution-babel.js

This file was deleted.

107 changes: 0 additions & 107 deletions scripts/setup-ts-execution-esbuild.js

This file was deleted.

114 changes: 106 additions & 8 deletions scripts/setup-ts-execution.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,107 @@
switch (process.env.YARNPKG_TRANSPILER) {
case `esbuild`:
require(`./setup-ts-execution-esbuild.js`);
break;
default:
case `babel`:
require(`./setup-ts-execution-babel.js`);
break;
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: [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`)) {
const normalizedP = p.replace(/\\/g, `/`);
return normalizedP.includes(`packages/yarnpkg-pnp/sources/node`) || normalizedP.endsWith(`packages/yarnpkg-pnp/sources/loader/node-options.js`);
}

return true;
},
},
);
Loading

0 comments on commit 031d483

Please sign in to comment.