Skip to content

Commit

Permalink
feat: support outdir as prefix for rendered files
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgrain committed Mar 12, 2021
1 parent 2feeb3d commit 9be0f62
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ The package exports four different types of constructs:
Force the asset to use Docker bundling and skip local bundling. This can be useful in CI environments. The `projectRoot` path will be mounted into the container as context.
- `buildOptions` as per esbuild [(reference)](https://esbuild.github.io/getting-started/#build-scripts) \
Regular defaults apply, with a few changes as noted below. Generally speaking overwriting entrypoint or output options is not supported, as these are inferred from cdk.
Regular defaults apply, with a few changes as noted below. Generally speaking usage of entry and output options are limited, as these are inferred from cdk.
- `buildOptions.outdir` \
The path for the outdir is defined by cdk. However setting this option allows to write files into a subdirectory. For example `{ outdir: 'js' }` will create an asset that has a single directory called `js`, which contains all build files. This approach can be useful for static website deployments, where JavaScript code should go into a sub-directory.
### `TypeScriptCode`, `JavaScriptCode`
Expand Down
9 changes: 7 additions & 2 deletions lib/bundlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ export class LocalBundler implements ILocalBundling {

tryBundle(outputDir: string, _options: BundlingOptions): boolean {
try {
const outdir = join(
...([outputDir, this.options.outdir].filter(Boolean) as string[])
);
buildSync({
...this.options,
outdir: outputDir,
outdir,
});

return true;
Expand Down Expand Up @@ -47,9 +50,11 @@ export class DockerBundler implements BundlingOptions {
public readonly options: BuildOptions;

public constructor(options: BuildOptions) {
const outdir = ["/asset-output", options.outdir].filter(Boolean).join("/");

this.options = {
...options,
outdir: "/asset-output",
outdir,
};
}
}
8 changes: 4 additions & 4 deletions lib/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ import { BuildOptions as EsbuildBuildOptions } from "esbuild";
import { DockerBundler, LocalBundler } from "./bundlers";
import { getAbsolutePath } from "./util";

export type BuildOptions = Omit<EsbuildBuildOptions, "outfile" | "outdir">;
export type BuildOptions = Omit<EsbuildBuildOptions, "outfile">;

export class EsbuildBundling extends DockerBundler implements BundlingOptions {
public readonly local?: LocalBundler;

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

if (tryLocalBundling) {
this.local = new LocalBundler({
...options,
entryPoints: [getAbsolutePath(projectRoot, entry)],
entryPoints: [getAbsolutePath(projectRoot, entryPoint)],
});
}
}
Expand Down
25 changes: 25 additions & 0 deletions test/bundling.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import "@aws-cdk/assert/jest";
import { buildSync } from "esbuild";
import { mocked } from "ts-jest/utils";
import { EsbuildBundling } from "../lib/bundling";

jest.mock("esbuild", () => ({
Expand All @@ -19,4 +21,27 @@ describe("Bundling", () => {
expect(bundler?.options?.entryPoints).toContain("index.ts");
});
});

describe("Given an outdir", () => {
it("should append outdir behind the cdk asset directory", () => {
const bundler = new EsbuildBundling(
"/project",
"index.ts",
{
outdir: "js",
},
true
);
// expect(bundler.local?.options?.outdir).toMatch(/cdk.out\/.*\/js$/);
expect(bundler.options?.outdir).toBe("/asset-output/js");

bundler.local?.tryBundle("cdk.out/123456", bundler);

expect(mocked(buildSync)).toHaveBeenCalledWith(
expect.objectContaining({
outdir: "cdk.out/123456/js",
})
);
});
});
});

0 comments on commit 9be0f62

Please sign in to comment.