Skip to content

Commit

Permalink
feat: InlineJavaScriptCode and InlineTypeScriptCode transform to …
Browse files Browse the repository at this point in the history
…CommonJS and Node by default (#282)

BREAKING CHANGE: The default format and platform InlineXCode classes has been changed to CommonJS and Node. If you are using these classes to create browser or ESM compatible code, please update `format` and `platform` on `props.transformOptions` to your desired values.
  • Loading branch information
mrgrain authored Dec 18, 2022
1 parent be31a04 commit 37736a7
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 232 deletions.
4 changes: 4 additions & 0 deletions API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 2 additions & 10 deletions src/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import {
} from './asset';
import { EntryPoints } from './bundler';
import { BuildOptions } from './esbuild-types';

function nodeMajorVersion(): number {
return parseInt(process.versions.node.split('.')[0], 10);
}
import { defaultPlatformProps } from './utils';

export { CodeConfig } from 'aws-cdk-lib/aws-lambda';
export interface JavaScriptCodeProps extends AssetBaseProps {};
Expand Down Expand Up @@ -81,12 +78,7 @@ export class EsbuildCode<
) {
super();

const defaultOptions: Partial<BuildOptions> = {
...(!props.buildOptions?.platform ||
props.buildOptions?.platform === 'node'
? { platform: 'node', target: 'node' + nodeMajorVersion() }
: {}),
};
const defaultOptions: Partial<BuildOptions> = defaultPlatformProps(props.buildOptions);

this.props = {
...props,
Expand Down
9 changes: 7 additions & 2 deletions src/inline-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CodeConfig, InlineCode } from 'aws-cdk-lib/aws-lambda';
import { Construct, Node } from 'constructs';
import { EsbuildProvider } from './esbuild-provider';
import { TransformOptions, Loader } from './esbuild-types';
import { isEsbuildError } from './utils';
import { defaultPlatformProps, isEsbuildError } from './utils';

/**
* @stability stable
Expand Down Expand Up @@ -107,6 +107,8 @@ function transformerProps(loader: Loader, props: TransformerProps = {}): Transfo
...props,
transformOptions: {
loader,
format: 'cjs',
...defaultPlatformProps(props.transformOptions),
...props.transformOptions,
},
};
Expand All @@ -130,13 +132,14 @@ export class InlineJavaScriptCode extends BaseInlineCode {
*
* Default values for `props.transformOptions`:
* - `loader='js'`
* - `platform=node`
* - `target=nodeX` with X being the major node version running locally
*
* @see https://esbuild.github.io/api/#transform-api
* @stability stable
*/
props?: TransformerProps,
) {

super(code, transformerProps('js', props));
}
}
Expand All @@ -160,6 +163,8 @@ export class InlineTypeScriptCode extends BaseInlineCode {
*
* Default values for `transformOptions`:
* - `loader='ts'`
* - `platform=node`
* - `target=nodeX` with X being the major node version running locally
*
* @see https://esbuild.github.io/api/#transform-api
* @stability stable
Expand Down
17 changes: 17 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import { BuildOptions, Platform, TransformOptions } from './esbuild-types';

export function isEsbuildError(error: unknown): boolean {
return !!error
&& typeof error == 'object'
&& error != null
&& 'errors' in error
&& 'warnings' in error;
}

export function nodeMajorVersion(): number {
return parseInt(process.versions.node.split('.')[0], 10);
}

export function defaultPlatformProps(options?: BuildOptions | TransformOptions): {
platform?: Platform;
target?: string | string[];
} {
if (!options?.platform || options?.platform === 'node') {
return { platform: 'node', target: 'node' + nodeMajorVersion() };
}

return {};
}
Loading

0 comments on commit 37736a7

Please sign in to comment.