Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
bboure committed Jun 29, 2023
1 parent 12904a8 commit eee7f00
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 5 deletions.
19 changes: 19 additions & 0 deletions doc/general-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ appSync:
- `xrayEnabled`: Boolean. Enable or disable X-Ray tracing.
- `visibility`: Optional. `GLOBAL` or `PRIVATE`. Defaults to `GLOBAL`.
- `tags`: A key-value pair for tagging this AppSync API
- `esbuild`: Custom esbuild options, or `false` See [Esbuild](#Esbuild)

## Schema

Expand Down Expand Up @@ -186,3 +187,21 @@ appSync:
- `excludeVerboseContent`: Boolean, Optional. Exclude or not verbose content (headers, response headers, context, and evaluated mapping templates), regardless of field logging level. Defaults to `false`.
- `retentionInDays`: Optional. Number of days to retain the logs. Defaults to [`provider.logRetentionInDays`](https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml#general-function-settings).
- `roleArn`: Optional. The role ARN to use for AppSync to write into CloudWatch. If not specified, a new role is created by default.

## Esbuild

By default, this plugin will use esbuild in order to transpile and bundle Javascript resolvers. This option allows you to pass custom options that must be passed to esbuild.

Use these options carefully. Some options are not compatible with AWS AppSync. For more details about using esbuild with AppSync, see the [official guidelines](https://docs.aws.amazon.com/appsync/latest/devguide/resolver-reference-overview-js.html#additional-utilities)

Set this option to `false` to disable esbuild.

Example:

Override the target and disable sourcemap.

```yml
esbuild:
target: 'es2020',
sourcemap: false
```
3 changes: 2 additions & 1 deletion src/__tests__/validation/__snapshots__/base.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ exports[`Valdiation should validate 1`] = `
: must have required property 'authentication'
/unknownPorp: invalid (unknown) property
/xrayEnabled: must be boolean
/visibility: must be \\"GLOBAL\\" or \\"PRIVATE\\""
/visibility: must be \\"GLOBAL\\" or \\"PRIVATE\\"
/esbuild: must be an esbuild config object or false"
`;
6 changes: 6 additions & 0 deletions src/__tests__/validation/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ describe('Valdiation', () => {
tags: {
foo: 'bar',
},
esbuild: {
target: 'es2020',
sourcemap: false,
treeShaking: false,
},
}),
).toBe(true);

Expand All @@ -20,6 +25,7 @@ describe('Valdiation', () => {
visibility: 'FOO',
xrayEnabled: 'BAR',
unknownPorp: 'foo',
esbuild: 'bad',
});
}).toThrowErrorMatchingSnapshot();
});
Expand Down
23 changes: 19 additions & 4 deletions src/resources/JsResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,33 @@ export class JsResolver {
);
}

return this.processTemplateSubstitutions(this.getResolverContent());
}

getResolverContent(): string {
if (this.api.config.esbuild === false) {
return fs.readFileSync(this.config.path, 'utf8');
}

// process with esbuild
// this will:
// - Bundle the code into one file if necessary
// - Transpile typescript to javascript if necessary

const buildResult = buildSync({
target: 'esnext',
sourcemap: 'inline',
treeShaking: true,
minifyWhitespace: true,
minifyIdentifiers: true,
// custom config overrides
...this.api.config.esbuild,
// These options are required and can't be changed
platform: 'node',
format: 'esm',
entryPoints: [this.config.path],
bundle: true,
write: false,
external: ['@aws-appsync/utils'],
format: 'esm',
target: 'es2020',
});

if (buildResult.errors.length > 0) {
Expand All @@ -45,7 +60,7 @@ export class JsResolver {
);
}

return this.processTemplateSubstitutions(buildResult.outputFiles[0].text);
return buildResult.outputFiles[0].text;
}

processTemplateSubstitutions(template: string): string | IntrinsicFunction {
Expand Down
2 changes: 2 additions & 0 deletions src/types/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CfnWafRuleStatement, IntrinsicFunction } from './cloudFormation';
import { BuildOptions } from 'esbuild';

export type AppSyncConfig = {
name: string;
Expand All @@ -17,6 +18,7 @@ export type AppSyncConfig = {
waf?: WafConfig;
tags?: Record<string, string>;
visibility?: 'GLOBAL' | 'PRIVATE';
esbuild?: BuildOptions | false;
};

export type IamStatement = {
Expand Down
9 changes: 9 additions & 0 deletions src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,15 @@ export const appSyncSchema = {
],
errorMessage: 'contains invalid pipeline function definitions',
},
esbuild: {
oneOf: [
{
type: 'object',
},
{ const: false },
],
errorMessage: 'must be an esbuild config object or false',
},
},
required: ['name', 'authentication'],
additionalProperties: {
Expand Down

0 comments on commit eee7f00

Please sign in to comment.