Skip to content

Commit 15ceb83

Browse files
authored
build: migrate to esbuild (#229)
1 parent 84e6e1d commit 15ceb83

14 files changed

+273
-731
lines changed

.github/workflows/ci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ on:
66
pull_request:
77
branches: [main]
88

9+
env:
10+
YARN_ENABLE_GLOBAL_CACHE: false
11+
912
jobs:
1013
chore:
1114
name: "Testing chores"

.github/workflows/publish.yml

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ on:
44
push:
55
branches: [main]
66

7+
env:
8+
YARN_ENABLE_GLOBAL_CACHE: false
9+
710
jobs:
811
release-please:
912
runs-on: ubuntu-latest

.github/workflows/update-nock-files.yml

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ on:
88
type: number
99
required: true
1010

11+
env:
12+
YARN_ENABLE_GLOBAL_CACHE: false
13+
1114
jobs:
1215
build-and-update-nock-files:
1316
runs-on: ubuntu-latest

mkshims.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {Engine} from './sources/Engine';
66
import {SupportedPackageManagerSet} from './sources/types';
77

88
function shouldGenerateShim(name: string) {
9-
return name !== `chunks`;
9+
// No filtering needed at the moment
10+
return true;
1011
}
1112

1213
const engine = new Engine();

package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"babel-plugin-dynamic-import-node": "^2.3.3",
3333
"clipanion": "^3.0.1",
3434
"debug": "^4.1.1",
35+
"esbuild": "0.16.15",
3536
"eslint": "^8.0.0",
3637
"eslint-plugin-arca": "^0.15.0",
3738
"jest": "^29.0.0",
@@ -40,16 +41,14 @@
4041
"semver": "^7.1.3",
4142
"supports-color": "^9.0.0",
4243
"tar": "^6.0.1",
43-
"ts-loader": "^9.0.0",
4444
"ts-node": "^10.0.0",
4545
"typescript": "^4.3.2",
4646
"v8-compile-cache": "^2.3.0",
47-
"webpack": "^5.38.1",
48-
"webpack-cli": "^4.0.0",
4947
"which": "^2.0.2"
5048
},
5149
"scripts": {
52-
"build": "rm -rf dist shims && webpack && ts-node ./mkshims.ts",
50+
"build": "rm -rf dist shims && run build:bundle && ts-node ./mkshims.ts",
51+
"build:bundle": "esbuild ./sources/_entryPoint.ts --bundle --platform=node --target=node14.14.0 --external:corepack --outfile='./dist/corepack.js' --resolve-extensions='.ts,.mjs,.js'",
5352
"corepack": "ts-node ./sources/_entryPoint.ts",
5453
"lint": "eslint .",
5554
"prepack": "yarn build",

sources/_entryPoint.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
#!/usr/bin/env node
12
import {runMain} from './main';
23

34
// Used by the generated shims
45
export {runMain};
56

6-
// Using `eval` to be sure that Webpack doesn't transform it
7-
if (process.mainModule === eval(`module`))
7+
if (process.mainModule === module)
88
runMain(process.argv.slice(2));

sources/commands/Enable.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import path from 'p
55
import which from 'which';
66

77
import {Context} from '../main';
8-
import * as nodeUtils from '../nodeUtils';
98
import {isSupportedPackageManager, SupportedPackageManagerSetWithoutNpm} from '../types';
109

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

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

5755
const distFolder = path.join(path.dirname(manifestPath), `dist`);
5856
if (!fs.existsSync(distFolder))

sources/commands/Hydrate.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class HydrateCommand extends Command<Context> {
3535
const archiveEntries = new Map<string, Set<string>>();
3636
let hasShortEntries = false;
3737

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

4040
await tar.t({file: fileName, onentry: entry => {
4141
const segments = entry.header.path.split(/\//g);

sources/commands/Prepare.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export class PrepareCommand extends Command<Context> {
117117
if (!this.json)
118118
this.context.stdout.write(`Packing the selected tools in ${path.basename(outputPath)}...\n`);
119119

120-
const {default: tar} = await import(/* webpackMode: 'eager' */ `tar`);
120+
const {default: tar} = await import(`tar`);
121121
// Recreate the folder in case it was deleted somewhere else:
122122
await mkdir(baseInstallFolder, {recursive: true});
123123
await tar.c({gzip: true, cwd: baseInstallFolder, file: path.resolve(outputPath)}, installLocations.map(location => {

sources/corepackUtils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export async function findInstalledVersion(installTarget: string, descriptor: De
9595
}
9696

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

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

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

199199
process.argv = [
200200
process.execPath,

sources/main.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {BaseContext, Builtins, Cli, Command, Option, UsageError} from 'clipanion';
22

3+
import {version as corepackVersion} from '../package.json';
4+
35
import {Engine} from './Engine';
46
import {DisableCommand} from './commands/Disable';
57
import {EnableCommand} from './commands/Enable';
@@ -87,8 +89,6 @@ async function executePackageManagerRequest({packageManager, binaryName, binaryV
8789
}
8890

8991
async function main(argv: Array<string>) {
90-
const corepackVersion = require(`../package.json`).version;
91-
9292
// Because we load the binaries in the same process, we don't support custom contexts.
9393
const context = {
9494
...Cli.defaultContext,

sources/nodeUtils.ts

-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import Module from 'module';
22
import path from 'path';
33

4-
declare const __non_webpack_require__: NodeRequire | undefined;
5-
6-
export const dynamicRequire: NodeRequire = typeof __non_webpack_require__ !== `undefined`
7-
? __non_webpack_require__
8-
: require;
9-
104
/**
115
* Loads a module as a main module, enabling the `require.main === module` pattern.
126
*/

webpack.config.js

-45
This file was deleted.

0 commit comments

Comments
 (0)