in packages/aws-cdk-lib/aws-lambda-nodejs/lib/bundling.ts [215:305]
private createBundlingCommand(options: BundlingCommandOptions): string {
const pathJoin = osPathJoin(options.osPlatform);
let relativeEntryPath = pathJoin(options.inputDir, this.relativeEntryPath);
let tscCommand = '';
if (this.props.preCompilation) {
const tsconfig = this.props.tsconfig ?? findUp('tsconfig.json', path.dirname(this.props.entry));
if (!tsconfig) {
throw new Error('Cannot find a `tsconfig.json` but `preCompilation` is set to `true`, please specify it via `tsconfig`');
}
const compilerOptions = getTsconfigCompilerOptions(tsconfig);
tscCommand = `${options.tscRunner} "${relativeEntryPath}" ${compilerOptions}`;
relativeEntryPath = relativeEntryPath.replace(/\.ts(x?)$/, '.js$1');
}
const loaders = Object.entries(this.props.loader ?? {});
const defines = Object.entries(this.props.define ?? {});
if (this.props.sourceMap === false && this.props.sourceMapMode) {
throw new Error('sourceMapMode cannot be used when sourceMap is false');
}
const sourceMapEnabled = this.props.sourceMapMode ?? this.props.sourceMap;
const sourceMapMode = this.props.sourceMapMode ?? SourceMapMode.DEFAULT;
const sourceMapValue = sourceMapMode === SourceMapMode.DEFAULT ? '' : `=${this.props.sourceMapMode}`;
const sourcesContent = this.props.sourcesContent ?? true;
const outFile = this.props.format === OutputFormat.ESM ? 'index.mjs' : 'index.js';
const esbuildCommand: string[] = [
options.esbuildRunner,
'--bundle', `"${relativeEntryPath}"`,
`--target=${this.props.target ?? toTarget(this.props.runtime)}`,
'--platform=node',
...this.props.format ? [`--format=${this.props.format}`] : [],
`--outfile="${pathJoin(options.outputDir, outFile)}"`,
...this.props.minify ? ['--minify'] : [],
...sourceMapEnabled ? [`--sourcemap${sourceMapValue}`] : [],
...sourcesContent ? [] : [`--sources-content=${sourcesContent}`],
...this.externals.map(external => `--external:${external}`),
...loaders.map(([ext, name]) => `--loader:${ext}=${name}`),
...defines.map(([key, value]) => `--define:${key}=${JSON.stringify(value)}`),
...this.props.logLevel ? [`--log-level=${this.props.logLevel}`] : [],
...this.props.keepNames ? ['--keep-names'] : [],
...this.relativeTsconfigPath ? [`--tsconfig="${pathJoin(options.inputDir, this.relativeTsconfigPath)}"`] : [],
...this.props.metafile ? [`--metafile="${pathJoin(options.outputDir, 'index.meta.json')}"`] : [],
...this.props.banner ? [`--banner:js=${JSON.stringify(this.props.banner)}`] : [],
...this.props.footer ? [`--footer:js=${JSON.stringify(this.props.footer)}`] : [],
...this.props.mainFields ? [`--main-fields=${this.props.mainFields.join(',')}`] : [],
...this.props.inject ? this.props.inject.map(i => `--inject:"${i}"`) : [],
...this.props.esbuildArgs ? [toCliArgs(this.props.esbuildArgs)] : [],
];
let depsCommand = '';
if (this.props.nodeModules) {
// Find 'package.json' closest to entry folder, we are going to extract the
// modules versions from it.
const pkgPath = findUp('package.json', path.dirname(this.props.entry));
if (!pkgPath) {
throw new Error('Cannot find a `package.json` in this project. Using `nodeModules` requires a `package.json`.');
}
// Determine dependencies versions, lock file and installer
const dependencies = extractDependencies(pkgPath, this.props.nodeModules);
const osCommand = new OsCommand(options.osPlatform);
const lockFilePath = pathJoin(options.inputDir, this.relativeDepsLockFilePath ?? this.packageManager.lockFile);
const isPnpm = this.packageManager.lockFile === LockFile.PNPM;
const isBun = this.packageManager.lockFile === LockFile.BUN;
// Create dummy package.json, copy lock file if any and then install
depsCommand = chain([
isPnpm ? osCommand.write(pathJoin(options.outputDir, 'pnpm-workspace.yaml'), ''): '', // Ensure node_modules directory is installed locally by creating local 'pnpm-workspace.yaml' file
osCommand.writeJson(pathJoin(options.outputDir, 'package.json'), { dependencies }),
osCommand.copy(lockFilePath, pathJoin(options.outputDir, this.packageManager.lockFile)),
osCommand.changeDirectory(options.outputDir),
this.packageManager.installCommand.join(' '),
isPnpm ? osCommand.remove(pathJoin(options.outputDir, 'node_modules', '.modules.yaml'), true) : '', // Remove '.modules.yaml' file which changes on each deployment
isBun ? osCommand.removeDir(pathJoin(options.outputDir, 'node_modules', '.cache')) : '', // Remove node_modules/.cache folder since you can't disable its creation
]);
}
return chain([
...this.props.commandHooks?.beforeBundling(options.inputDir, options.outputDir) ?? [],
tscCommand,
esbuildCommand.join(' '),
...(this.props.nodeModules && this.props.commandHooks?.beforeInstall(options.inputDir, options.outputDir)) ?? [],
depsCommand,
...this.props.commandHooks?.afterBundling(options.inputDir, options.outputDir) ?? [],
]);
}