-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
install using "optionalDependencies"
- Loading branch information
Showing
24 changed files
with
238 additions
and
420 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { pkgAndBinForCurrentPlatform } from './node-platform'; | ||
|
||
import fs = require('fs'); | ||
import os = require('os'); | ||
import path = require('path'); | ||
import child_process = require('child_process'); | ||
|
||
declare const ESBUILD_VERSION: string; | ||
const toPath = path.join(__dirname, 'bin', 'esbuild'); | ||
|
||
function validateBinaryVersion(binaryPath: string): void { | ||
const stdout = child_process.execFileSync(binaryPath, ['--version']).toString().trim(); | ||
if (stdout !== ESBUILD_VERSION) { | ||
throw new Error(`Expected ${JSON.stringify(ESBUILD_VERSION)} but got ${JSON.stringify(stdout)}`); | ||
} | ||
} | ||
|
||
function isYarn2OrAbove(): boolean { | ||
const { npm_config_user_agent } = process.env; | ||
if (npm_config_user_agent) { | ||
const match = npm_config_user_agent.match(/yarn\/(\d+)/); | ||
if (match && match[1]) { | ||
return parseInt(match[1], 10) >= 2; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
// This package contains a "bin/esbuild" JavaScript file that finds and runs | ||
// the appropriate binary executable. However, this means that running the | ||
// "esbuild" command runs another instance of "node" which is way slower than | ||
// just running the binary executable directly. | ||
// | ||
// Here we optimize for this by replacing the JavaScript file with the binary | ||
// executable at install time. This optimization does not work on Windows | ||
// because on Windows the binary executable must be called "esbuild.exe" | ||
// instead of "esbuild". This also doesn't work with Yarn 2+ because the Yarn | ||
// developers don't think binary modules should be used. See this thread for | ||
// details: https://github.com/yarnpkg/berry/issues/882. This optimization also | ||
// doesn't apply when npm's "--ignore-scripts" flag is used since in that case | ||
// this install script will not be run. | ||
if (os.platform() !== 'win32' && !isYarn2OrAbove()) { | ||
const { bin } = pkgAndBinForCurrentPlatform(); | ||
try { | ||
fs.unlinkSync(toPath); | ||
fs.linkSync(bin, toPath); | ||
} catch (e) { | ||
// Ignore errors here since this optimization is optional | ||
} | ||
} | ||
|
||
// Verify that the binary is the correct version | ||
validateBinaryVersion(toPath); |
Oops, something went wrong.