-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
bundling.ts
465 lines (397 loc) · 17.7 KB
/
bundling.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
import * as os from 'os';
import * as path from 'path';
import { IConstruct } from 'constructs';
import { PackageInstallation } from './package-installation';
import { LockFile, PackageManager } from './package-manager';
import { BundlingOptions, OutputFormat, SourceMapMode } from './types';
import { exec, extractDependencies, findUp, getTsconfigCompilerOptions } from './util';
import { Architecture, AssetCode, Code, Runtime } from '../../aws-lambda';
import * as cdk from '../../core';
const ESBUILD_MAJOR_VERSION = '0';
/**
* Bundling properties
*/
export interface BundlingProps extends BundlingOptions {
/**
* Path to lock file
*/
readonly depsLockFilePath: string;
/**
* Entry file
*/
readonly entry: string;
/**
* The runtime of the lambda function
*/
readonly runtime: Runtime;
/**
* The system architecture of the lambda function
*/
readonly architecture: Architecture;
/**
* Path to project root
*/
readonly projectRoot: string;
/**
* Run compilation using `tsc` before bundling
*/
readonly preCompilation?: boolean
/**
* Which option to use to copy the source files to the docker container and output files back
* @default - BundlingFileAccess.BIND_MOUNT
*/
readonly bundlingFileAccess?: cdk.BundlingFileAccess;
}
/**
* Bundling with esbuild
*/
export class Bundling implements cdk.BundlingOptions {
/**
* esbuild bundled Lambda asset code
*/
public static bundle(scope: IConstruct, options: BundlingProps): AssetCode {
return Code.fromAsset(options.projectRoot, {
assetHash: options.assetHash,
assetHashType: options.assetHash ? cdk.AssetHashType.CUSTOM : cdk.AssetHashType.OUTPUT,
bundling: new Bundling(scope, options),
});
}
public static clearEsbuildInstallationCache(): void {
this.esbuildInstallation = undefined;
}
public static clearTscInstallationCache(): void {
this.tscInstallation = undefined;
}
private static esbuildInstallation?: PackageInstallation;
private static tscInstallation?: PackageInstallation;
// Core bundling options
public readonly image: cdk.DockerImage;
public readonly entrypoint?: string[]
public readonly command: string[];
public readonly volumes?: cdk.DockerVolume[];
public readonly volumesFrom?: string[];
public readonly environment?: { [key: string]: string };
public readonly workingDirectory: string;
public readonly user?: string;
public readonly securityOpt?: string;
public readonly network?: string;
public readonly local?: cdk.ILocalBundling;
public readonly bundlingFileAccess?: cdk.BundlingFileAccess;
private readonly projectRoot: string;
private readonly relativeEntryPath: string;
private readonly relativeTsconfigPath?: string;
private readonly relativeDepsLockFilePath: string;
private readonly externals: string[];
private readonly packageManager: PackageManager;
constructor(scope: IConstruct, private readonly props: BundlingProps) {
this.packageManager = PackageManager.fromLockFile(props.depsLockFilePath, props.logLevel);
Bundling.esbuildInstallation = Bundling.esbuildInstallation ?? PackageInstallation.detect('esbuild');
Bundling.tscInstallation = Bundling.tscInstallation ?? PackageInstallation.detect('typescript');
this.projectRoot = props.projectRoot;
this.relativeEntryPath = path.relative(this.projectRoot, path.resolve(props.entry));
this.relativeDepsLockFilePath = path.relative(this.projectRoot, path.resolve(props.depsLockFilePath));
if (this.relativeDepsLockFilePath.includes('..')) {
throw new Error(`Expected depsLockFilePath: ${props.depsLockFilePath} to be under projectRoot: ${this.projectRoot} (${this.relativeDepsLockFilePath})`);
}
if (props.tsconfig) {
this.relativeTsconfigPath = path.relative(this.projectRoot, path.resolve(props.tsconfig));
}
if (props.preCompilation && !/\.tsx?$/.test(props.entry)) {
throw new Error('preCompilation can only be used with typescript files');
}
if (props.format === OutputFormat.ESM && !isEsmRuntime(props.runtime)) {
throw new Error(`ECMAScript module output format is not supported by the ${props.runtime.name} runtime`);
}
// Modules to externalize when using a constant known version of the runtime.
// Mark aws-sdk as external by default (available in the runtime)
const isV2Runtime = isSdkV2Runtime(props.runtime);
const versionedExternals = isV2Runtime ? ['aws-sdk'] : ['@aws-sdk/*'];
// Don't automatically externalize any dependencies when using a `latest` runtime which may
// update versions in the future.
const defaultExternals = props.runtime?.isVariable ? [] : versionedExternals;
const externals = props.externalModules ?? defaultExternals;
// Warn users if they are trying to rely on global versions of the SDK that aren't available in
// their environment.
if (isV2Runtime && externals.some((pkgName) => pkgName.startsWith('@aws-sdk/'))) {
cdk.Annotations.of(scope).addWarningV2('@aws-cdk/aws-lambda-nodejs:sdkV3NotInRuntime', 'If you are relying on AWS SDK v3 to be present in the Lambda environment already, please explicitly configure a NodeJS runtime of Node 18 or higher.');
} else if (!isV2Runtime && externals.includes('aws-sdk')) {
cdk.Annotations.of(scope).addWarningV2('@aws-cdk/aws-lambda-nodejs:sdkV2NotInRuntime', 'If you are relying on AWS SDK v2 to be present in the Lambda environment already, please explicitly configure a NodeJS runtime of Node 16 or lower.');
}
// Warn users if they are using a runtime that may change and are excluding any dependencies from
// bundling.
if (externals.length && props.runtime?.isVariable) {
cdk.Annotations.of(scope).addWarningV2('@aws-cdk/aws-lambda-nodejs:variableRuntimeExternals', 'When using NODEJS_LATEST the runtime version may change as new runtimes are released, this may affect the availability of packages shipped with the environment. Ensure that any external dependencies are available through layers or specify a specific runtime version.');
}
this.externals = [
...externals,
...props.nodeModules ?? [], // Mark the modules that we are going to install as externals also
];
// Docker bundling
const shouldBuildImage = props.forceDockerBundling || !Bundling.esbuildInstallation;
this.image = shouldBuildImage ? props.dockerImage ?? cdk.DockerImage.fromBuild(path.join(__dirname, '../lib'),
{
buildArgs: {
...props.buildArgs ?? {},
// If runtime isn't passed use regional default, lowest common denominator is node18
IMAGE: props.runtime.bundlingImage.image,
ESBUILD_VERSION: props.esbuildVersion ?? ESBUILD_MAJOR_VERSION,
},
platform: props.architecture.dockerPlatform,
})
: cdk.DockerImage.fromRegistry('dummy'); // Do not build if we don't need to
const bundlingCommand = this.createBundlingCommand({
inputDir: cdk.AssetStaging.BUNDLING_INPUT_DIR,
outputDir: cdk.AssetStaging.BUNDLING_OUTPUT_DIR,
esbuildRunner: 'esbuild', // esbuild is installed globally in the docker image
tscRunner: 'tsc', // tsc is installed globally in the docker image
osPlatform: 'linux', // linux docker image
});
this.command = props.command ?? ['bash', '-c', bundlingCommand];
this.environment = props.environment;
// Bundling sets the working directory to cdk.AssetStaging.BUNDLING_INPUT_DIR
// and we want to force npx to use the globally installed esbuild.
this.workingDirectory = props.workingDirectory ?? '/';
this.entrypoint = props.entrypoint;
this.volumes = props.volumes;
this.volumesFrom = props.volumesFrom;
this.user = props.user;
this.securityOpt = props.securityOpt;
this.network = props.network;
this.bundlingFileAccess = props.bundlingFileAccess;
// Local bundling
if (!props.forceDockerBundling) { // only if Docker is not forced
this.local = this.getLocalBundlingProvider();
}
}
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;
// 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
]);
}
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) ?? [],
]);
}
private getLocalBundlingProvider(): cdk.ILocalBundling {
const osPlatform = os.platform();
const createLocalCommand = (outputDir: string, esbuild: PackageInstallation, tsc?: PackageInstallation) => this.createBundlingCommand({
inputDir: this.projectRoot,
outputDir,
esbuildRunner: esbuild.isLocal ? this.packageManager.runBinCommand('esbuild') : 'esbuild',
tscRunner: tsc && (tsc.isLocal ? this.packageManager.runBinCommand('tsc') : 'tsc'),
osPlatform,
});
const environment = this.props.environment ?? {};
const cwd = this.projectRoot;
return {
tryBundle(outputDir: string) {
if (!Bundling.esbuildInstallation) {
process.stderr.write('esbuild cannot run locally. Switching to Docker bundling.\n');
return false;
}
if (!Bundling.esbuildInstallation.version.startsWith(`${ESBUILD_MAJOR_VERSION}.`)) {
throw new Error(`Expected esbuild version ${ESBUILD_MAJOR_VERSION}.x but got ${Bundling.esbuildInstallation.version}`);
}
const localCommand = createLocalCommand(outputDir, Bundling.esbuildInstallation, Bundling.tscInstallation);
exec(
osPlatform === 'win32' ? 'cmd' : 'bash',
[
osPlatform === 'win32' ? '/c' : '-c',
localCommand,
],
{
env: { ...process.env, ...environment },
stdio: [ // show output
'ignore', // ignore stdio
process.stderr, // redirect stdout to stderr
'inherit', // inherit stderr
],
cwd,
windowsVerbatimArguments: osPlatform === 'win32',
});
return true;
},
};
}
}
interface BundlingCommandOptions {
readonly inputDir: string;
readonly outputDir: string;
readonly esbuildRunner: string;
readonly tscRunner?: string;
readonly osPlatform: NodeJS.Platform;
}
/**
* OS agnostic command
*/
class OsCommand {
constructor(private readonly osPlatform: NodeJS.Platform) {}
public write(filePath: string, data: string): string {
if (this.osPlatform === 'win32') {
if (!data) { // if `data` is empty, echo a blank line, otherwise the file will contain a `^` character
return `echo. > "${filePath}"`;
}
return `echo ^${data}^ > "${filePath}"`;
}
return `echo '${data}' > "${filePath}"`;
}
public writeJson(filePath: string, data: any): string {
const stringifiedData = JSON.stringify(data);
return this.write(filePath, stringifiedData);
}
public copy(src: string, dest: string): string {
if (this.osPlatform === 'win32') {
return `copy "${src}" "${dest}"`;
}
return `cp "${src}" "${dest}"`;
}
public changeDirectory(dir: string): string {
return `cd "${dir}"`;
}
public remove(filePath: string, force: boolean = false): string {
if (this.osPlatform === 'win32') {
return `del "${filePath}"`;
}
const opts = force ? ['-f'] : [];
return `rm ${opts.join(' ')} "${filePath}"`;
}
}
/**
* Chain commands
*/
function chain(commands: string[]): string {
return commands.filter(c => !!c).join(' && ');
}
/**
* Platform specific path join
*/
function osPathJoin(platform: NodeJS.Platform) {
return function(...paths: string[]): string {
const joined = path.join(...paths);
// If we are on win32 but need posix style paths
if (os.platform() === 'win32' && platform !== 'win32') {
return joined.replace(/\\/g, '/');
}
return joined;
};
}
/**
* Converts a runtime to an esbuild node target
*/
function toTarget(runtime: Runtime): string {
const match = runtime.name.match(/nodejs(\d+)/);
if (!match) {
throw new Error('Cannot extract version from runtime.');
}
return `node${match[1]}`;
}
function toCliArgs(esbuildArgs: { [key: string]: string | boolean }): string {
const args = new Array<string>();
for (const [key, value] of Object.entries(esbuildArgs)) {
if (value === true || value === '') {
args.push(key);
} else if (value) {
args.push(`${key}="${value}"`);
}
}
return args.join(' ');
}
/**
* Detect if a given Node.js runtime uses SDKv2
*/
function isSdkV2Runtime(runtime: Runtime): boolean {
const sdkV2RuntimeList = [
Runtime.NODEJS,
Runtime.NODEJS_4_3,
Runtime.NODEJS_6_10,
Runtime.NODEJS_8_10,
Runtime.NODEJS_10_X,
Runtime.NODEJS_12_X,
Runtime.NODEJS_14_X,
Runtime.NODEJS_16_X,
];
return sdkV2RuntimeList.some((r) => {return r.family === runtime.family && r.name === runtime.name;});
}
/**
* Detect if a given Node.js runtime supports ESM (ECMAScript modules)
*/
function isEsmRuntime(runtime: Runtime): boolean {
const unsupportedRuntimes = [
Runtime.NODEJS,
Runtime.NODEJS_4_3,
Runtime.NODEJS_6_10,
Runtime.NODEJS_8_10,
Runtime.NODEJS_10_X,
Runtime.NODEJS_12_X,
];
return !unsupportedRuntimes.some((r) => {return r.family === runtime.family && r.name === runtime.name;});
}