Skip to content

Commit

Permalink
chore: improve spnpm to use esbuild (#4574)
Browse files Browse the repository at this point in the history
  • Loading branch information
jondlm authored and zkochan committed Apr 16, 2022
1 parent 4d6ebfd commit 509ac6d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/fetch/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { requestRetryLogger } from '@pnpm/core-loggers'
import { operation, RetryTimeoutOptions } from '@zkochan/retry'
import fetch, { Request, RequestInit as NodeRequestInit, Response } from 'node-fetch'

export { isRedirect } from 'node-fetch'

export { Response, RetryTimeoutOptions }

interface URLLike {
Expand All @@ -15,8 +17,6 @@ export interface RequestInit extends NodeRequestInit {
timeout?: number
}

export const isRedirect = fetch.isRedirect

export default async function fetchRetry (url: RequestInfo, opts: RequestInit = {}): Promise<Response> {
const retryOpts = opts.retry ?? {}
const maxRetries = retryOpts.retries ?? 2
Expand Down
1 change: 1 addition & 0 deletions packages/pnpm/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
bin/nodes
dist-spnpm
62 changes: 60 additions & 2 deletions packages/pnpm/spnpm.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,61 @@
require('@pnpm/ts-execution-runtime')
const fs = require('fs')
const esbuild = require('esbuild')
const pathLib = require('path')
const packagesDir = pathLib.resolve(__dirname, '..')

require('./src/pnpm.ts')
const localPackages = fs.readdirSync(packagesDir)

const pnpmPackageJson = JSON.parse(fs.readFileSync(pathLib.join(__dirname, 'package.json'), 'utf8'))

// This plugin rewrites imports to reference the `src` dir instead of `lib` so
// esbuild can compile the original TypeScript
const spnpmImportsPlugin = {
name: 'spnpmImports',
setup: (build) => {
// This is an exception to the rule that all local packages start with `@pnpm`
build.onResolve({ filter: /^dependency-path$/ }, ({path, resolveDir}) => ({
path: pathLib.resolve(packagesDir, 'dependency-path', 'src', 'index.ts')
}))

// E.g. @pnpm/config -> /<some_dir>/pnpm/packages/config/src/index.ts
build.onResolve({ filter: /@pnpm\// }, ({path, resolveDir}) => {
const pathParts = path.split('/')
const packageName = pathParts[1]

// Bail if the package isn't present locally
if (!localPackages.includes(packageName)) {
return
}

const newPath = pathLib.resolve(packagesDir, packageName, 'src', 'index.ts')

return {
path: newPath
}
})
}
}

;(async () => {
await esbuild.build({
bundle: true,
platform: 'node',
target: 'node14',
entryPoints: [pathLib.resolve(__dirname, 'lib/pnpm.js')],
outfile: pathLib.resolve(__dirname, 'dist-spnpm/spnpm.cjs'),
external: [
'node-gyp',
'./get-uid-gid.js', // traces back to: https://github.com/npm/uid-number/blob/6e9bdb302ae4799d05abf12e922ccdb4bd9ea023/uid-number.js#L31
],
define: {
'process.env.npm_package_name': JSON.stringify(pnpmPackageJson.name),
'process.env.npm_package_version': JSON.stringify(pnpmPackageJson.version),
},
sourcemap: true, // nice for local debugging
logLevel: 'warning', // keeps esbuild quiet unless there's a problem
plugins: [spnpmImportsPlugin],
})

// Require the file just built by esbuild
require('./dist-spnpm/spnpm.cjs')
})()
1 change: 1 addition & 0 deletions packages/resolve-dependencies/src/resolvePeers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ function resolvePeersOfNode<T extends PartialResolvedPackage> (
return { name, version }
})
)
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
depPath = `${resolvedPackage.depPath}${peersFolderSuffix}`
}
const localLocation = path.join(ctx.virtualStoreDir, depPathToFilename(depPath, ctx.lockfileDir))
Expand Down

0 comments on commit 509ac6d

Please sign in to comment.