-
-
Notifications
You must be signed in to change notification settings - Fork 936
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: improve spnpm to use esbuild (#4574)
- Loading branch information
Showing
4 changed files
with
64 additions
and
4 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
bin/nodes | ||
dist-spnpm |
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,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') | ||
})() |
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