Skip to content

Commit

Permalink
feat: support absolute entry points, as long as they are within the w…
Browse files Browse the repository at this point in the history
…orking directory
  • Loading branch information
mrgrain committed Jul 12, 2022
1 parent b212bab commit 0e56b44
Show file tree
Hide file tree
Showing 7 changed files with 402 additions and 71 deletions.
129 changes: 115 additions & 14 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,18 @@ public readonly entryPoints: string | string[] | {[ key: string ]: string};

- *Type:* `string` | `string`[] | {[ key: string ]: `string`}

A relative path or list or map of relative paths to the entry points of your code from the root of the project.
A path or list or map of paths to the entry points of your code.

E.g. `src/index.ts`.
Relative paths are by default resolved from the current working directory.
To change the working directory, see `buildOptions.absWorkingDir`.

Absolute paths can be used if files are part of the working directory.

Examples:
- `'src/index.ts'`
- `require.resolve('./lambda')`
- `['src/index.ts', 'src/util.ts']`
- `{one: 'src/two.ts', two: 'src/one.ts'}`

---

Expand Down Expand Up @@ -1991,9 +2000,18 @@ new EsbuildBundler(entryPoints: string | string[] | {[ key: string ]: string}, p

- *Type:* `string` | `string`[] | {[ key: string ]: `string`}

A relative path or list or map of relative paths to the entry points of your code from the root of the project.
A path or list or map of paths to the entry points of your code.

Relative paths are by default resolved from the current working directory.
To change the working directory, see `buildOptions.absWorkingDir`.

Absolute paths can be used if files are part of the working directory.

E.g. `src/index.ts`.
Examples:
- `'src/index.ts'`
- `require.resolve('./lambda')`
- `['src/index.ts', 'src/util.ts']`
- `{one: 'src/two.ts', two: 'src/one.ts'}`

---

Expand All @@ -2017,9 +2035,18 @@ public readonly entryPoints: string | string[] | {[ key: string ]: string};

- *Type:* `string` | `string`[] | {[ key: string ]: `string`}

A relative path or list or map of relative paths to the entry points of your code from the root of the project.
A path or list or map of paths to the entry points of your code.

E.g. `src/index.ts`.
Relative paths are by default resolved from the current working directory.
To change the working directory, see `buildOptions.absWorkingDir`.

Absolute paths can be used if files are part of the working directory.

Examples:
- `'src/index.ts'`
- `require.resolve('./lambda')`
- `['src/index.ts', 'src/util.ts']`
- `{one: 'src/two.ts', two: 'src/one.ts'}`

---

Expand Down Expand Up @@ -2078,9 +2105,18 @@ new EsbuildCode(entryPoints: string | string[] | {[ key: string ]: string}, prop

- *Type:* `string` | `string`[] | {[ key: string ]: `string`}

A relative path or list or map of relative paths to the entry points of your code from the root of the project.
A path or list or map of paths to the entry points of your code.

Relative paths are by default resolved from the current working directory.
To change the working directory, see `buildOptions.absWorkingDir`.

Absolute paths can be used if files are part of the working directory.

E.g. `src/index.ts`.
Examples:
- `'src/index.ts'`
- `require.resolve('./lambda')`
- `['src/index.ts', 'src/util.ts']`
- `{one: 'src/two.ts', two: 'src/one.ts'}`

---

Expand Down Expand Up @@ -2140,9 +2176,18 @@ public readonly entryPoints: string | string[] | {[ key: string ]: string};

- *Type:* `string` | `string`[] | {[ key: string ]: `string`}

A relative path or list or map of relative paths to the entry points of your code from the root of the project.
A path or list or map of paths to the entry points of your code.

E.g. `src/index.ts`.
Relative paths are by default resolved from the current working directory.
To change the working directory, see `buildOptions.absWorkingDir`.

Absolute paths can be used if files are part of the working directory.

Examples:
- `'src/index.ts'`
- `require.resolve('./lambda')`
- `['src/index.ts', 'src/util.ts']`
- `{one: 'src/two.ts', two: 'src/one.ts'}`

---

Expand Down Expand Up @@ -2333,9 +2378,18 @@ new JavaScriptCode(entryPoints: string | string[] | {[ key: string ]: string}, p

- *Type:* `string` | `string`[] | {[ key: string ]: `string`}

A relative path or list or map of relative paths to the entry points of your code from the root of the project.
A path or list or map of paths to the entry points of your code.

Relative paths are by default resolved from the current working directory.
To change the working directory, see `buildOptions.absWorkingDir`.

Absolute paths can be used if files are part of the working directory.

E.g. `src/index.ts`.
Examples:
- `'src/index.ts'`
- `require.resolve('./lambda')`
- `['src/index.ts', 'src/util.ts']`
- `{one: 'src/two.ts', two: 'src/one.ts'}`

---

Expand Down Expand Up @@ -2372,12 +2426,31 @@ new JavaScriptSource(entryPoints: string | string[] | {[ key: string ]: string},

- *Type:* `string` | `string`[] | {[ key: string ]: `string`}

A path or list or map of paths to the entry points of your code.

Relative paths are by default resolved from the current working directory.
To change the working directory, see `buildOptions.absWorkingDir`.

Absolute paths can be used if files are part of the working directory.

Examples:
- `'src/index.ts'`
- `require.resolve('./lambda')`
- `['src/index.ts', 'src/util.ts']`
- `{one: 'src/two.ts', two: 'src/one.ts'}`

---

##### `props`<sup>Optional</sup> <a name="@mrgrain/cdk-esbuild.JavaScriptSource.parameter.props"></a>

- *Type:* [`@mrgrain/cdk-esbuild.JavaScriptSourceProps`](#@mrgrain/cdk-esbuild.JavaScriptSourceProps)

Props to change the behavior of the bundler.

Default values for `props.buildOptions`:
- `bundle=true`
- `platform=browser`

---

#### Methods <a name="Methods"></a>
Expand Down Expand Up @@ -2430,9 +2503,18 @@ new TypeScriptCode(entryPoints: string | string[] | {[ key: string ]: string}, p

- *Type:* `string` | `string`[] | {[ key: string ]: `string`}

A relative path or list or map of relative paths to the entry points of your code from the root of the project.
A path or list or map of paths to the entry points of your code.

Relative paths are by default resolved from the current working directory.
To change the working directory, see `buildOptions.absWorkingDir`.

E.g. `src/index.ts`.
Absolute paths can be used if files are part of the working directory.

Examples:
- `'src/index.ts'`
- `require.resolve('./lambda')`
- `['src/index.ts', 'src/util.ts']`
- `{one: 'src/two.ts', two: 'src/one.ts'}`

---

Expand Down Expand Up @@ -2469,12 +2551,31 @@ new TypeScriptSource(entryPoints: string | string[] | {[ key: string ]: string},

- *Type:* `string` | `string`[] | {[ key: string ]: `string`}

A path or list or map of paths to the entry points of your code.

Relative paths are by default resolved from the current working directory.
To change the working directory, see `buildOptions.absWorkingDir`.

Absolute paths can be used if files are part of the working directory.

Examples:
- `'src/index.ts'`
- `require.resolve('./lambda')`
- `['src/index.ts', 'src/util.ts']`
- `{one: 'src/two.ts', two: 'src/one.ts'}`

---

##### `props`<sup>Optional</sup> <a name="@mrgrain/cdk-esbuild.TypeScriptSource.parameter.props"></a>

- *Type:* [`@mrgrain/cdk-esbuild.TypeScriptSourceProps`](#@mrgrain/cdk-esbuild.TypeScriptSourceProps)

Props to change the behavior of the bundler.

Default values for `props.buildOptions`:
- `bundle=true`
- `platform=browser`

---

#### Methods <a name="Methods"></a>
Expand Down
44 changes: 36 additions & 8 deletions src/asset.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isAbsolute } from 'path';
import { isAbsolute, relative } from 'path';
import { AssetHashType } from 'aws-cdk-lib';
import { Asset as S3Asset } from 'aws-cdk-lib/aws-s3-assets';
import { Construct, Node } from 'constructs';
Expand All @@ -22,7 +22,18 @@ export interface AssetBaseProps extends BundlerProps {

export interface AssetProps extends AssetBaseProps {
/**
* A relative path or list or map of relative paths to the entry points of your code from the root of the project. E.g. `src/index.ts`.
* A path or list or map of paths to the entry points of your code.
*
* Relative paths are by default resolved from the current working directory.
* To change the working directory, see `buildOptions.absWorkingDir`.
*
* Absolute paths can be used if files are part of the working directory.
*
* Examples:
* - `'src/index.ts'`
* - `require.resolve('./lambda')`
* - `['src/index.ts', 'src/util.ts']`
* - `{one: 'src/two.ts', two: 'src/one.ts'}`
*
* @stability stable
*/
Expand Down Expand Up @@ -59,15 +70,32 @@ export class EsbuildAsset<Props extends AssetProps> extends S3Asset {

const name = scope.node.path + Node.PATH_SEP + id;

Object.values(entryPoints).forEach((entryPoint: string) => {
if (isAbsolute(entryPoint)) {
const absWorkingDir = options.absWorkingDir ?? process.cwd();

const forceRelativeEntrypointPath = (entryPoint: string): string => {
if (!isAbsolute(entryPoint)) {
return entryPoint;
}

const relativeEntryPoint = relative(absWorkingDir, entryPoint);
if (relativeEntryPoint.startsWith('..') || isAbsolute(relativeEntryPoint)) {
throw new Error(
`${name}: Entry points must be a relative path. If you need to define an absolute path, please use \`buildOptions.absWorkingDir\` accordingly.`,
`${name}: Entry points must be part of the working directory. See \`buildOptions.absWorkingDir\` to set a working directory different to the current one.`,
);
}
});

const absWorkingDir = options.absWorkingDir ?? process.cwd();
return relativeEntryPoint;
};

const relativeEntryPoints =
Array.isArray(entryPoints) ?
entryPoints.map(forceRelativeEntrypointPath) :
Object.fromEntries(
Object.entries(entryPoints)
.map(([out, entryPoint]) => ([out, forceRelativeEntrypointPath(entryPoint)]),
),
);


const buildOptions = {
bundle: true,
Expand All @@ -80,7 +108,7 @@ export class EsbuildAsset<Props extends AssetProps> extends S3Asset {
assetHash,
assetHashType: assetHash ? AssetHashType.CUSTOM : AssetHashType.OUTPUT,
bundling: new EsbuildBundler(
entryPoints,
relativeEntryPoints,
{
buildOptions,
copyDir,
Expand Down
27 changes: 24 additions & 3 deletions src/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,18 @@ import { buildSync } from './esbuild-wrapper';
import { printBuildMessages } from './formatMessages';

/**
* A relative path or list or map of relative paths to the entry points of your code from the root of the project. E.g. `src/index.ts`.
* A path or list or map of paths to the entry points of your code.
*
* Relative paths are by default resolved from the current working directory.
* To change the working directory, see `buildOptions.absWorkingDir`.
*
* Absolute paths can be used if files are part of the working directory.
*
* Examples:
* - `'src/index.ts'`
* - `require.resolve('./lambda')`
* - `['src/index.ts', 'src/util.ts']`
* - `{one: 'src/two.ts', two: 'src/one.ts'}`
*
* @stability stable
*/
Expand Down Expand Up @@ -114,8 +125,18 @@ export class EsbuildBundler {
*/
public constructor(
/**
* A relative path or list or map of relative paths to the entry points of your code from the root of the project.
* E.g. `src/index.ts`.
* A path or list or map of paths to the entry points of your code.
*
* Relative paths are by default resolved from the current working directory.
* To change the working directory, see `buildOptions.absWorkingDir`.
*
* Absolute paths can be used if files are part of the working directory.
*
* Examples:
* - `'src/index.ts'`
* - `require.resolve('./lambda')`
* - `['src/index.ts', 'src/util.ts']`
* - `{one: 'src/two.ts', two: 'src/one.ts'}`
*
* @stability experimental
*/
Expand Down
44 changes: 38 additions & 6 deletions src/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,18 @@ export class EsbuildCode<

constructor(
/**
* A relative path or list or map of relative paths to the entry points of your code from the root of the project.
* E.g. `src/index.ts`.
* A path or list or map of paths to the entry points of your code.
*
* Relative paths are by default resolved from the current working directory.
* To change the working directory, see `buildOptions.absWorkingDir`.
*
* Absolute paths can be used if files are part of the working directory.
*
* Examples:
* - `'src/index.ts'`
* - `require.resolve('./lambda')`
* - `['src/index.ts', 'src/util.ts']`
* - `{one: 'src/two.ts', two: 'src/one.ts'}`
*
* @stability stable
*/
Expand Down Expand Up @@ -142,12 +152,23 @@ export class JavaScriptCode extends EsbuildCode<JavaScriptCodeProps> {

constructor(
/**
* A relative path or list or map of relative paths to the entry points of your code from the root of the project.
* E.g. `src/index.ts`.
* A path or list or map of paths to the entry points of your code.
*
* Relative paths are by default resolved from the current working directory.
* To change the working directory, see `buildOptions.absWorkingDir`.
*
* Absolute paths can be used if files are part of the working directory.
*
* Examples:
* - `'src/index.ts'`
* - `require.resolve('./lambda')`
* - `['src/index.ts', 'src/util.ts']`
* - `{one: 'src/two.ts', two: 'src/one.ts'}`
*
* @stability stable
*/
entryPoints: EntryPoints,

/**
* Props to change the behavior of the bundler.
*
Expand Down Expand Up @@ -180,12 +201,23 @@ export class TypeScriptCode extends EsbuildCode<TypeScriptCodeProps> {

constructor(
/**
* A relative path or list or map of relative paths to the entry points of your code from the root of the project.
* E.g. `src/index.ts`.
* A path or list or map of paths to the entry points of your code.
*
* Relative paths are by default resolved from the current working directory.
* To change the working directory, see `buildOptions.absWorkingDir`.
*
* Absolute paths can be used if files are part of the working directory.
*
* Examples:
* - `'src/index.ts'`
* - `require.resolve('./lambda')`
* - `['src/index.ts', 'src/util.ts']`
* - `{one: 'src/two.ts', two: 'src/one.ts'}`
*
* @stability stable
*/
entryPoints: EntryPoints,

/**
* Props to change the behavior of the bundler.
*
Expand Down
Loading

0 comments on commit 0e56b44

Please sign in to comment.