Skip to content

Commit

Permalink
feat: auto install esbuild on jsii platforms (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgrain authored Aug 20, 2022
1 parent 1a28221 commit d97688a
Show file tree
Hide file tree
Showing 14 changed files with 499 additions and 128 deletions.
5 changes: 3 additions & 2 deletions .projenrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { InternalConsoleOptions } from 'projen/lib/vscode';
import { SourceFile } from 'ts-morph';
import { tagOnNpm } from './projenrc/release';
import { TypeScriptSourceFile } from './projenrc/TypeScriptSourceFile';
import { Esbuild } from './src/esbuild-source';

const project = new awscdk.AwsCdkConstructLibrary({
projenrcTs: true,
Expand Down Expand Up @@ -89,7 +90,7 @@ const project = new awscdk.AwsCdkConstructLibrary({
devDeps: [
'@aws-cdk/[email protected]',
'@types/eslint',
'esbuild@^0.15.0',
Esbuild.spec,
'jest-mock',
'ts-morph',
],
Expand Down Expand Up @@ -252,7 +253,7 @@ launchConfig?.addOverride('configurations.0.cwd', '${workspaceFolder}');

// esbuild
project.tryFindObjectFile('package.json')?.addOverride('optionalDependencies', {
esbuild: '^0.15.0',
[Esbuild.name]: Esbuild.version,
});

new TypeScriptSourceFile(project, 'src/esbuild-types.ts', {
Expand Down
127 changes: 113 additions & 14 deletions API.md

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

21 changes: 16 additions & 5 deletions examples/python-app/python_app/python_app_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@
InlineTypeScriptCode,
TypeScriptCode,
TypeScriptSource,
TransformerProps,
EsbuildSource,
)


class PythonAppStack(Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)

EsbuildSource.default = EsbuildSource.global_paths

s3_deployment.BucketDeployment(
self,
"Website",
sources=[
TypeScriptSource(
"lambda-handler/index.ts",
copy_dir="lambda-handler",
esbuild_module_path=None, # Use default
)
],
destination_bucket=s3.Bucket(
Expand All @@ -38,11 +43,13 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
code=TypeScriptCode(
"lambda-handler/index.ts",
build_options=BuildOptions(
format="esm", outfile="index.mjs", external=["aws-sdk"]
format="esm",
outfile="index.mjs",
external=["aws-sdk"],
log_level="verbose",
),
# Override the global setting with a specific path per Construct
# This can be useful if a Construct requires a different version of esbuild
esbuild_module_path="/project/node_modules/esbuild@13",
# Override the default setting with a specific path per Construct
esbuild_module_path=EsbuildSource.install,
),
)

Expand All @@ -57,6 +64,10 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
export function handler() {
console.log(hello);
}
"""
""",
props=TransformerProps(
# Try to find the package anywhere, but don't install it
esbuild_module_path=EsbuildSource.anywhere
),
),
)
12 changes: 7 additions & 5 deletions src/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
FileSystem,
ILocalBundling,
} from 'aws-cdk-lib';
import { EsbuildProvider } from './esbuild-provider';
import { BuildOptions } from './esbuild-types';
import { detectEsbuildModulePath, esbuild, wrapWithEsbuildBinaryPath } from './esbuild-wrapper';
import { errorHasCode } from './utils';

/**
Expand Down Expand Up @@ -99,9 +99,9 @@ export interface BundlerProps {
readonly esbuildBinaryPath?: string;

/**
* Path used to import the esbuild module.
* Absolute path to the esbuild module JS file.
*
* Python, Go, .NET and Java should use an absolute path, because the jsii execution environment uses a temporary working directory.
* E.g. "/home/user/.npm/node_modules/esbuild/lib/main.js"
*
* If not set, the module path will be determined in the following order:
*
Expand Down Expand Up @@ -195,8 +195,10 @@ export class EsbuildBundler {
}

try {
const { buildFn = esbuild(detectEsbuildModulePath(props.esbuildModulePath)).buildSync } = this.props;
wrapWithEsbuildBinaryPath(buildFn, this.props.esbuildBinaryPath)({
const buildFn = this.props.buildFn ?? EsbuildProvider.require(props.esbuildModulePath).buildSync;
const buildSync = EsbuildProvider.withEsbuildBinaryPath(buildFn, this.props.esbuildBinaryPath);

buildSync({
entryPoints,
color: process.env.NO_COLOR ? Boolean(process.env.NO_COLOR) : undefined,
...(this.props?.buildOptions || {}),
Expand Down
Loading

0 comments on commit d97688a

Please sign in to comment.