Skip to content

Commit

Permalink
build: migrate to esbuild (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz authored Jan 8, 2023
1 parent 84e6e1d commit 15ceb83
Show file tree
Hide file tree
Showing 14 changed files with 273 additions and 731 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ on:
pull_request:
branches: [main]

env:
YARN_ENABLE_GLOBAL_CACHE: false

jobs:
chore:
name: "Testing chores"
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
push:
branches: [main]

env:
YARN_ENABLE_GLOBAL_CACHE: false

jobs:
release-please:
runs-on: ubuntu-latest
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/update-nock-files.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ on:
type: number
required: true

env:
YARN_ENABLE_GLOBAL_CACHE: false

jobs:
build-and-update-nock-files:
runs-on: ubuntu-latest
Expand Down
3 changes: 2 additions & 1 deletion mkshims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {Engine} from './sources/Engine';
import {SupportedPackageManagerSet} from './sources/types';

function shouldGenerateShim(name: string) {
return name !== `chunks`;
// No filtering needed at the moment
return true;
}

const engine = new Engine();
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"babel-plugin-dynamic-import-node": "^2.3.3",
"clipanion": "^3.0.1",
"debug": "^4.1.1",
"esbuild": "0.16.15",
"eslint": "^8.0.0",
"eslint-plugin-arca": "^0.15.0",
"jest": "^29.0.0",
Expand All @@ -40,16 +41,14 @@
"semver": "^7.1.3",
"supports-color": "^9.0.0",
"tar": "^6.0.1",
"ts-loader": "^9.0.0",
"ts-node": "^10.0.0",
"typescript": "^4.3.2",
"v8-compile-cache": "^2.3.0",
"webpack": "^5.38.1",
"webpack-cli": "^4.0.0",
"which": "^2.0.2"
},
"scripts": {
"build": "rm -rf dist shims && webpack && ts-node ./mkshims.ts",
"build": "rm -rf dist shims && run build:bundle && ts-node ./mkshims.ts",
"build:bundle": "esbuild ./sources/_entryPoint.ts --bundle --platform=node --target=node14.14.0 --external:corepack --outfile='./dist/corepack.js' --resolve-extensions='.ts,.mjs,.js'",
"corepack": "ts-node ./sources/_entryPoint.ts",
"lint": "eslint .",
"prepack": "yarn build",
Expand Down
4 changes: 2 additions & 2 deletions sources/_entryPoint.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node
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`))
if (process.mainModule === module)
runMain(process.argv.slice(2));
4 changes: 1 addition & 3 deletions sources/commands/Enable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import path from 'p
import which from 'which';

import {Context} from '../main';
import * as nodeUtils from '../nodeUtils';
import {isSupportedPackageManager, SupportedPackageManagerSetWithoutNpm} from '../types';

export class EnableCommand extends Command<Context> {
Expand Down Expand Up @@ -51,8 +50,7 @@ export class EnableCommand extends Command<Context> {
// install directory is within a symlink
installDirectory = fs.realpathSync(installDirectory);

// We use `eval` so that Webpack doesn't statically transform it.
const manifestPath = nodeUtils.dynamicRequire.resolve(`corepack/package.json`);
const manifestPath = require.resolve(`corepack/package.json`);

const distFolder = path.join(path.dirname(manifestPath), `dist`);
if (!fs.existsSync(distFolder))
Expand Down
2 changes: 1 addition & 1 deletion sources/commands/Hydrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class HydrateCommand extends Command<Context> {
const archiveEntries = new Map<string, Set<string>>();
let hasShortEntries = false;

const {default: tar} = await import(/* webpackMode: 'eager' */ `tar`);
const {default: tar} = await import(`tar`);

await tar.t({file: fileName, onentry: entry => {
const segments = entry.header.path.split(/\//g);
Expand Down
2 changes: 1 addition & 1 deletion sources/commands/Prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class PrepareCommand extends Command<Context> {
if (!this.json)
this.context.stdout.write(`Packing the selected tools in ${path.basename(outputPath)}...\n`);

const {default: tar} = await import(/* webpackMode: 'eager' */ `tar`);
const {default: tar} = await import(`tar`);
// Recreate the folder in case it was deleted somewhere else:
await mkdir(baseInstallFolder, {recursive: true});
await tar.c({gzip: true, cwd: baseInstallFolder, file: path.resolve(outputPath)}, installLocations.map(location => {
Expand Down
4 changes: 2 additions & 2 deletions sources/corepackUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export async function findInstalledVersion(installTarget: string, descriptor: De
}

export async function installVersion(installTarget: string, locator: Locator, {spec}: {spec: PackageManagerSpec}) {
const {default: tar} = await import(/* webpackMode: 'eager' */ `tar`);
const {default: tar} = await import(`tar`);
const {version, build} = semver.parse(locator.reference)!;

const installFolder = path.join(installTarget, locator.name, version);
Expand Down Expand Up @@ -194,7 +194,7 @@ export async function runVersion(installSpec: { location: string, spec: PackageM
// - Yarn uses process.argv[1] to determine its own path: https://github.com/yarnpkg/berry/blob/0da258120fc266b06f42aed67e4227e81a2a900f/packages/yarnpkg-cli/sources/main.ts#L80
// - pnpm uses `require.main == null` to determine its own version: https://github.com/pnpm/pnpm/blob/e2866dee92991e979b2b0e960ddf5a74f6845d90/packages/cli-meta/src/index.ts#L14

process.env.COREPACK_ROOT = path.dirname(eval(`__dirname`));
process.env.COREPACK_ROOT = path.dirname(require.resolve(`corepack/package.json`));

process.argv = [
process.execPath,
Expand Down
4 changes: 2 additions & 2 deletions sources/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {BaseContext, Builtins, Cli, Command, Option, UsageError} from 'clipanion';

import {version as corepackVersion} from '../package.json';

import {Engine} from './Engine';
import {DisableCommand} from './commands/Disable';
import {EnableCommand} from './commands/Enable';
Expand Down Expand Up @@ -87,8 +89,6 @@ async function executePackageManagerRequest({packageManager, binaryName, binaryV
}

async function main(argv: Array<string>) {
const corepackVersion = require(`../package.json`).version;

// Because we load the binaries in the same process, we don't support custom contexts.
const context = {
...Cli.defaultContext,
Expand Down
6 changes: 0 additions & 6 deletions sources/nodeUtils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
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;

/**
* Loads a module as a main module, enabling the `require.main === module` pattern.
*/
Expand Down
45 changes: 0 additions & 45 deletions webpack.config.js

This file was deleted.

Loading

0 comments on commit 15ceb83

Please sign in to comment.