Skip to content

Commit

Permalink
Merge pull request #174 from mrgrain/esbuild-binary-path
Browse files Browse the repository at this point in the history
feat: allow setting of esbuildBinaryPath via Construct interface
  • Loading branch information
mrgrain authored Jun 23, 2022
2 parents 932f793 + f4eeebe commit 72e3571
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
84 changes: 84 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ Instead use only relative paths and avoid `..`.

---

##### `esbuildBinaryPath`<sup>Optional</sup> <a name="@mrgrain/cdk-esbuild.AssetProps.property.esbuildBinaryPath"></a>

```typescript
public readonly esbuildBinaryPath: string;
```

- *Type:* `string`

Path to the binary used by esbuild.

This is the same as setting the ESBUILD_BINARY_PATH environment variable.

---

##### `entryPoints`<sup>Required</sup> <a name="@mrgrain/cdk-esbuild.AssetProps.property.entryPoints"></a>

```typescript
Expand Down Expand Up @@ -885,6 +899,20 @@ Instead use only relative paths and avoid `..`.

---

##### `esbuildBinaryPath`<sup>Optional</sup> <a name="@mrgrain/cdk-esbuild.BundlerProps.property.esbuildBinaryPath"></a>

```typescript
public readonly esbuildBinaryPath: string;
```

- *Type:* `string`

Path to the binary used by esbuild.

This is the same as setting the ESBUILD_BINARY_PATH environment variable.

---

### CodeConfig <a name="@mrgrain/cdk-esbuild.CodeConfig"></a>

#### Initializer <a name="[object Object].Initializer"></a>
Expand Down Expand Up @@ -986,6 +1014,20 @@ Instead use only relative paths and avoid `..`.

---

##### `esbuildBinaryPath`<sup>Optional</sup> <a name="@mrgrain/cdk-esbuild.JavaScriptCodeProps.property.esbuildBinaryPath"></a>

```typescript
public readonly esbuildBinaryPath: string;
```

- *Type:* `string`

Path to the binary used by esbuild.

This is the same as setting the ESBUILD_BINARY_PATH environment variable.

---

##### `assetHash`<sup>Optional</sup> <a name="@mrgrain/cdk-esbuild.JavaScriptCodeProps.property.assetHash"></a>

```typescript
Expand Down Expand Up @@ -1081,6 +1123,20 @@ Instead use only relative paths and avoid `..`.

---

##### `esbuildBinaryPath`<sup>Optional</sup> <a name="@mrgrain/cdk-esbuild.JavaScriptSourceProps.property.esbuildBinaryPath"></a>

```typescript
public readonly esbuildBinaryPath: string;
```

- *Type:* `string`

Path to the binary used by esbuild.

This is the same as setting the ESBUILD_BINARY_PATH environment variable.

---

##### `assetHash`<sup>Optional</sup> <a name="@mrgrain/cdk-esbuild.JavaScriptSourceProps.property.assetHash"></a>

```typescript
Expand Down Expand Up @@ -1638,6 +1694,20 @@ Instead use only relative paths and avoid `..`.

---

##### `esbuildBinaryPath`<sup>Optional</sup> <a name="@mrgrain/cdk-esbuild.TypeScriptCodeProps.property.esbuildBinaryPath"></a>

```typescript
public readonly esbuildBinaryPath: string;
```

- *Type:* `string`

Path to the binary used by esbuild.

This is the same as setting the ESBUILD_BINARY_PATH environment variable.

---

##### `assetHash`<sup>Optional</sup> <a name="@mrgrain/cdk-esbuild.TypeScriptCodeProps.property.assetHash"></a>

```typescript
Expand Down Expand Up @@ -1733,6 +1803,20 @@ Instead use only relative paths and avoid `..`.

---

##### `esbuildBinaryPath`<sup>Optional</sup> <a name="@mrgrain/cdk-esbuild.TypeScriptSourceProps.property.esbuildBinaryPath"></a>

```typescript
public readonly esbuildBinaryPath: string;
```

- *Type:* `string`

Path to the binary used by esbuild.

This is the same as setting the ESBUILD_BINARY_PATH environment variable.

---

##### `assetHash`<sup>Optional</sup> <a name="@mrgrain/cdk-esbuild.TypeScriptSourceProps.property.assetHash"></a>

```typescript
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ It's possible that you want to use an implementation of esbuild that's different

For these situations, this package offers an escape hatch to bypass regular the implementation and provide a custom build and transform function.

#### Esbuild binary path

It is possible to override the binary used by esbuild. The usual way to do this is to set the `ESBUILD_BINARY_PATH` environment variable. For convenience this package allows to set the binary path as a prop:

```ts
new TypeScriptCode("fixtures/handlers/ts-handler.ts", {
esbuildBinaryPath: "path/to/esbuild/binary"
});
```

#### Custom build function

> 💡 See [Using esbuild with plugins](examples/esbuild-with-plugins) for a complete working example of a custom build function using this escape hatch.
Expand Down
16 changes: 16 additions & 0 deletions src/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ export interface BundlerProps {
* @default esbuild.buildSync
*/
readonly buildFn?: any;

/**
* Path to the binary used by esbuild.
*
* This is the same as setting the ESBUILD_BINARY_PATH environment variable.
*
* @stability experimental
*/
readonly esbuildBinaryPath?: string;
}

/**
Expand Down Expand Up @@ -148,6 +157,11 @@ export class EsbuildBundler {
});
}

const originalEsbuildBinaryPath = process.env.ESBUILD_BINARY_PATH;
if (this.props.esbuildBinaryPath) {
process.env.ESBUILD_BINARY_PATH = this.props.esbuildBinaryPath;
}

try {
const buildResult: BuildResult = buildFn({
entryPoints,
Expand All @@ -160,6 +174,8 @@ export class EsbuildBundler {
printBuildMessages(error as BuildFailure, { prefix: 'Build ' });
}

process.env.ESBUILD_BINARY_PATH = originalEsbuildBinaryPath;

return true;
},
};
Expand Down

0 comments on commit 72e3571

Please sign in to comment.