Skip to content

Commit

Permalink
feat: support multiple entryPoints
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgrain committed Mar 12, 2021
1 parent 9be0f62 commit e41757b
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 36 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,15 @@ The package exports four different types of constructs:
- `EsbuildBundling` implementing `core.BundlingOptions` ([reference](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.BundlingOptions.html)) \
provides a _esbuild_ bundling interface wherever needed

### Parameters

- `entryPoints: string | string[]` \
Single or list of relative paths to the entry points of your code from the root of the project. See `props.projectRoot`.

### Props

- `props.entrypoint` & `entrypoint` \
Relative path to the entrypoint file of your code from the root of the project. See `props.projectRoot`.
- `props.entryPoints` \
Array of relative paths to the entry points of your code from the root of the project. See `props.projectRoot`.

- `projectRoot` \
Absolute path to the root of the project for the asset. If not set, will attempt to guess the root path using a basic algorithm. The combination of `projectRoot + entrypoint` must always be a valid absolute path. \
Expand Down
22 changes: 12 additions & 10 deletions lib/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { BuildOptions, EsbuildBundling } from "./bundling";
import { findProjectRoot } from "./util";
export interface EsbuildAssetProps extends Partial<IAsset> {
/**
* Path to the entrypoint of your code, e.g. `src/index.ts`
* Relative paths to the entrypoints of your code, e.g. `src/index.ts`
*/
entrypoint: string;
entryPoints: string[];

/**
* The root path for the code asset. If not set, will attempt to guess the root path.
Expand Down Expand Up @@ -44,17 +44,19 @@ abstract class Asset<Props extends EsbuildAssetProps> extends S3Asset {
public constructor(scope: Construct, id: string, props: Props) {
const name = scope.node.path + ConstructNode.PATH_SEP + id;

if (isAbsolute(props.entrypoint)) {
throw new Error(
`${name}: Entrypoint must be a relative path. If you need to define an absolute path, please use \`props.projectRoot\` accordingly.`
);
}
props.entryPoints.forEach((entryPoint: string) => {
if (isAbsolute(entryPoint)) {
throw new Error(
`${name}: Entrypoints must be a relative path. If you need to define an absolute path, please use \`props.projectRoot\` accordingly.`
);
}
});

const {
entrypoint,
entryPoints,
assetHash,
forceDockerBundling = false,
projectRoot = findProjectRoot(entrypoint),
projectRoot = findProjectRoot(),
buildOptions: options = {},
} = props;

Expand All @@ -75,7 +77,7 @@ abstract class Asset<Props extends EsbuildAssetProps> extends S3Asset {
assetHashType: assetHash ? AssetHashType.CUSTOM : AssetHashType.OUTPUT,
bundling: new EsbuildBundling(
projectRoot,
entrypoint,
entryPoints,
buildOptions,
!forceDockerBundling
),
Expand Down
8 changes: 5 additions & 3 deletions lib/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@ export class EsbuildBundling extends DockerBundler implements BundlingOptions {

public constructor(
projectRoot: string,
entryPoint: string,
entryPoints: string[],
options: BuildOptions,
tryLocalBundling = true
) {
super({
...options,
entryPoints: [entryPoint],
entryPoints,
});

if (tryLocalBundling) {
this.local = new LocalBundler({
...options,
entryPoints: [getAbsolutePath(projectRoot, entryPoint)],
entryPoints: entryPoints.map((entryPoint) =>
getAbsolutePath(projectRoot, entryPoint)
),
});
}
}
Expand Down
16 changes: 8 additions & 8 deletions lib/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
TypeScriptAsset as TSAsset,
} from "./asset";

type CodeProps = Omit<EsbuildAssetProps, "entrypoint">;
type CodeProps = Omit<EsbuildAssetProps, "entryPoints">;

type JavaScriptCodeProps = CodeProps;
type TypeScriptCodeProps = CodeProps;
Expand All @@ -35,10 +35,10 @@ abstract class Code<

/**
*
* @param entrypoint - Relative path to the asset code from `props.projectRoot`.
* @param entryPoints - Relative path to the asset code from `props.projectRoot`.
* @param props - Asset properties.
*/
constructor(entrypoint: string, props: Props) {
constructor(entryPoints: string | string[], props: Props) {
super();

const defaultOptions: BuildOptions = {
Expand All @@ -49,7 +49,7 @@ abstract class Code<
};

this.props = {
entrypoint,
entryPoints: Array.isArray(entryPoints) ? entryPoints : [entryPoints],
...props,
buildOptions: {
...defaultOptions,
Expand Down Expand Up @@ -95,15 +95,15 @@ abstract class Code<
export class JavaScriptCode extends Code<JavaScriptCodeProps, JSAsset> {
protected AssetClass = JSAsset;

constructor(entrypoint: string, props: JavaScriptCodeProps = {}) {
super(entrypoint, props);
constructor(entryPoints: string | string[], props: JavaScriptCodeProps = {}) {
super(entryPoints, props);
}
}
export class TypeScriptCode extends Code<TypeScriptCodeProps, TSAsset> {
protected AssetClass = TSAsset;

constructor(entrypoint: string, props: TypeScriptCodeProps = {}) {
super(entrypoint, props);
constructor(entryPoints: string | string[], props: TypeScriptCodeProps = {}) {
super(entryPoints, props);
}
}

Expand Down
22 changes: 14 additions & 8 deletions lib/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Construct, Stack } from "@aws-cdk/core";
import { EsbuildAssetProps, JavaScriptAsset, TypeScriptAsset } from "./asset";
import { BuildOptions } from "./bundling";

type SourceProps = Omit<EsbuildAssetProps, "entrypoint">;
type SourceProps = Omit<EsbuildAssetProps, "entryPoints">;

type JavaScriptSourceProps = SourceProps;
type TypeScriptSourceProps = SourceProps;
Expand All @@ -28,10 +28,10 @@ abstract class Source<

/**
*
* @param entrypoint - Relative path to the source code from `props.projectRoot`.
* @param entryPoints - Relative path to the source code from `props.projectRoot`.
* @param props - Source properties.
*/
constructor(entrypoint: string, props: Props) {
constructor(entryPoints: string | string[], props: Props) {
const defaultOptions: BuildOptions = {
platform: "browser",
...(!props.buildOptions?.define
Expand All @@ -46,7 +46,7 @@ abstract class Source<
};

this.props = {
entrypoint,
entryPoints: Array.isArray(entryPoints) ? entryPoints : [entryPoints],
...props,
buildOptions: {
...defaultOptions,
Expand Down Expand Up @@ -95,8 +95,11 @@ export class JavaScriptSource extends Source<
> {
protected AssetClass = JavaScriptAsset;

constructor(entrypoint: string, props: JavaScriptSourceProps = {}) {
super(entrypoint, props);
constructor(
entryPoints: string | string[],
props: JavaScriptSourceProps = {}
) {
super(entryPoints, props);
}
}

Expand All @@ -106,7 +109,10 @@ export class TypeScriptSource extends Source<
> {
protected AssetClass = TypeScriptAsset;

constructor(entrypoint: string, props: TypeScriptSourceProps = {}) {
super(entrypoint, props);
constructor(
entryPoints: string | string[],
props: TypeScriptSourceProps = {}
) {
super(entryPoints, props);
}
}
6 changes: 3 additions & 3 deletions test/bundling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ jest.mock("esbuild", () => ({
describe("Bundling", () => {
describe("Given a project root path", () => {
it("should append the entry path to the project root for the local bundler", () => {
const bundler = new EsbuildBundling("/project", "index.ts", {}, true);
const bundler = new EsbuildBundling("/project", ["index.ts"], {}, true);
expect(bundler.local?.options?.entryPoints).toContain(
"/project/index.ts"
);
});

it("should keep the relative entry path for the docker bundler", () => {
const bundler = new EsbuildBundling("/project", "index.ts", {}, true);
const bundler = new EsbuildBundling("/project", ["index.ts"], {}, true);
expect(bundler?.options?.entryPoints).toContain("index.ts");
});
});
Expand All @@ -26,7 +26,7 @@ describe("Bundling", () => {
it("should append outdir behind the cdk asset directory", () => {
const bundler = new EsbuildBundling(
"/project",
"index.ts",
["index.ts"],
{
outdir: "js",
},
Expand Down
2 changes: 1 addition & 1 deletion test/code.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe("asset", () => {
code,
});
}).toThrow(
/MyFunction\/TypeScriptCode: Entrypoint must be a relative path/
/MyFunction\/TypeScriptCode: Entrypoints must be a relative path/
);
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/source.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe("source", () => {
sources: [website],
});
}).toThrow(
/DeployWebsite\/TypeScriptSource: Entrypoint must be a relative path/
/DeployWebsite\/TypeScriptSource: Entrypoints must be a relative path/
);
});
});
Expand Down

0 comments on commit e41757b

Please sign in to comment.